From ec598253d722068c540f7afaf0b8886e805018c5 Mon Sep 17 00:00:00 2001 From: STEPHANVS Date: Fri, 23 Jul 2021 00:14:53 +0200 Subject: [PATCH] Invalid chord throws KeyError, not ValueError --- openlp/plugins/songs/forms/editverseform.py | 12 +++---- openlp/plugins/songs/lib/__init__.py | 40 ++++++++++----------- tests/openlp_plugins/songs/test_lib.py | 6 ++-- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/openlp/plugins/songs/forms/editverseform.py b/openlp/plugins/songs/forms/editverseform.py index 710ae3785..044d9df7b 100644 --- a/openlp/plugins/songs/forms/editverseform.py +++ b/openlp/plugins/songs/forms/editverseform.py @@ -124,12 +124,12 @@ class EditVerseForm(QtWidgets.QDialog, Ui_EditVerseDialog): try: transposed_lyrics = transpose_lyrics(self.verse_text_edit.toPlainText(), 1) self.verse_text_edit.setPlainText(transposed_lyrics) - except ValueError as ve: + except KeyError as ke: # Transposing failed critical_error_message_box(title=translate('SongsPlugin.EditVerseForm', 'Transposing failed'), message=translate('SongsPlugin.EditVerseForm', 'Transposing failed because of invalid chord:\n{err_msg}' - .format(err_msg=ve))) + .format(err_msg=ke))) return self.verse_text_edit.setFocus() self.verse_text_edit.moveCursor(QtGui.QTextCursor.End) @@ -141,12 +141,12 @@ class EditVerseForm(QtWidgets.QDialog, Ui_EditVerseDialog): try: transposed_lyrics = transpose_lyrics(self.verse_text_edit.toPlainText(), -1) self.verse_text_edit.setPlainText(transposed_lyrics) - except ValueError as ve: + except KeyError as ke: # Transposing failed critical_error_message_box(title=translate('SongsPlugin.EditVerseForm', 'Transposing failed'), message=translate('SongsPlugin.EditVerseForm', 'Transposing failed because of invalid chord:\n{err_msg}' - .format(err_msg=ve))) + .format(err_msg=ke))) return self.verse_text_edit.setPlainText(transposed_lyrics) self.verse_text_edit.setFocus() @@ -235,11 +235,11 @@ class EditVerseForm(QtWidgets.QDialog, Ui_EditVerseDialog): try: transpose_lyrics(self.verse_text_edit.toPlainText(), 0) super(EditVerseForm, self).accept() - except ValueError as ve: + except KeyError as ke: # Transposing failed critical_error_message_box(title=translate('SongsPlugin.EditVerseForm', 'Invalid Chord'), message=translate('SongsPlugin.EditVerseForm', 'An invalid chord was detected:\n{err_msg}' - .format(err_msg=ve))) + .format(err_msg=ke))) else: super(EditVerseForm, self).accept() diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index 3274575c9..ef37efb39 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -791,28 +791,28 @@ def transpose_chord(chord, transpose_value, notation, key, lastchord, isbass): chord = chord[1:] if len(chord) > 1 else '' else: minor = '' - if note in notenumbers[notation]: - note_number = notenumbers[notation][note] - note_number += transpose_value - while note_number > 11: - note_number -= 12 - while note_number < 0: - note_number += 12 - if isbass: - if lastchord: - note = scales[notation][lastchord][note_number] - elif key: - note = scales[notation][key][note_number] - else: - note = notes_sharp[note_number] if notes_preferred[note_number] == '#' else notes_flat[note_number] + #if note in notenumbers[notation]: + note_number = notenumbers[notation][note] + note_number += transpose_value + while note_number > 11: + note_number -= 12 + while note_number < 0: + note_number += 12 + if isbass: + if lastchord: + note = scales[notation][lastchord][note_number] + elif key: + note = scales[notation][key][note_number] else: - if not key or thischordchangeskey: - note = notes_sharp[note_number] if notes_preferred[note_number] == '#' else notes_flat[note_number] - else: - note = scales[notation][key][note_number] - transposed_chord += note + minor + chord + note = notes_sharp[note_number] if notes_preferred[note_number] == '#' else notes_flat[note_number] else: - transposed_chord += note + chord + if not key or thischordchangeskey: + note = notes_sharp[note_number] if notes_preferred[note_number] == '#' else notes_flat[note_number] + else: + note = scales[notation][key][note_number] + transposed_chord += note + minor + chord + #else: + # transposed_chord += note + chord if thischordchangeskey: key = note + minor else: diff --git a/tests/openlp_plugins/songs/test_lib.py b/tests/openlp_plugins/songs/test_lib.py index ee4cc5c54..ca64f8d68 100644 --- a/tests/openlp_plugins/songs/test_lib.py +++ b/tests/openlp_plugins/songs/test_lib.py @@ -339,10 +339,10 @@ def test_transpose_chord_error(): # WHEN: Transposing it 1 down # THEN: An exception should be raised - with pytest.raises(ValueError) as err: + with pytest.raises(KeyError) as err: transpose_chord(chord, -1, 'english', None, None, False) - assert err.value != ValueError('\'T\' is not in list'), \ - 'ValueError exception should have been thrown for invalid chord' + assert err.value != KeyError('\'T\' is not in list'), \ + 'KeyError exception should have been thrown for invalid chord' @patch('openlp.plugins.songs.lib.transpose_verse')