From 037d3f4f7dd68fca2a3e7f777855f211ce73d83b Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Fri, 18 Dec 2015 23:24:20 +0100 Subject: [PATCH] Added support for author types in tests + some VideoPsalm fixes --- openlp/plugins/songs/lib/importer.py | 4 +- openlp/plugins/songs/lib/importers/lyrix.py | 4 +- .../plugins/songs/lib/importers/songimport.py | 10 +-- .../plugins/songs/lib/importers/videopsalm.py | 63 ++++++++----------- tests/helpers/songfileimport.py | 5 +- .../videopsalmsongs/as-safe-a-stronghold.json | 3 +- 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/openlp/plugins/songs/lib/importer.py b/openlp/plugins/songs/lib/importer.py index 1d08e2c36..2be9beb40 100644 --- a/openlp/plugins/songs/lib/importer.py +++ b/openlp/plugins/songs/lib/importer.py @@ -342,8 +342,8 @@ class SongFormat(object): 'selectMode': SongFormatSelect.SingleFile, 'filter': '%s (*.json)' % translate('SongsPlugin.ImportWizardForm', 'VideoPsalm Files'), 'comboBoxText': translate('SongsPlugin.ImportWizardForm', 'VideoPsalm'), - 'descriptionText': translate('SongsPlugin.ImportWizardForm','The VideoPsalm songbooks are normally located ' - 'in %s') % 'C:\\Users\\Public\\Documents\\VideoPsalm\\SongBooks\\' + 'descriptionText': translate('SongsPlugin.ImportWizardForm', 'The VideoPsalm songbooks are normally located' + ' in %s') % 'C:\\Users\\Public\\Documents\\VideoPsalm\\SongBooks\\' }, WordsOfWorship: { 'class': WordsOfWorshipImport, diff --git a/openlp/plugins/songs/lib/importers/lyrix.py b/openlp/plugins/songs/lib/importers/lyrix.py index f6e5bb3a9..5caa46258 100644 --- a/openlp/plugins/songs/lib/importers/lyrix.py +++ b/openlp/plugins/songs/lib/importers/lyrix.py @@ -85,7 +85,7 @@ class LyrixImport(SongImport): # If the CCLI was found, we are near the end # Find author line = next(file) - author = line[line.find(':')+2:].strip() + author = line[line.find(':') + 2:].strip() # Find copyright copyright = next(file) except StopIteration: @@ -111,4 +111,4 @@ class LyrixImport(SongImport): for verse in verses: self.add_verse(verse, 'v') if not self.finish(): - self.log_error(file.name) \ No newline at end of file + self.log_error(file.name) diff --git a/openlp/plugins/songs/lib/importers/songimport.py b/openlp/plugins/songs/lib/importers/songimport.py index 9f6caa901..d3b725f85 100644 --- a/openlp/plugins/songs/lib/importers/songimport.py +++ b/openlp/plugins/songs/lib/importers/songimport.py @@ -255,13 +255,13 @@ class SongImport(QtCore.QObject): if author2: self.add_author(author2) - def add_author(self, author): + def add_author(self, author, type=None): """ Add an author to the list """ - if author in self.authors: + if (author, type) in self.authors: return - self.authors.append(author) + self.authors.append((author, type)) def add_media_file(self, filename, weight=0): """ @@ -360,13 +360,13 @@ class SongImport(QtCore.QObject): song.comments = self.comments song.theme_name = self.theme_name song.ccli_number = self.ccli_number - for author_text in self.authors: + for author_text, author_type in self.authors: author = self.manager.get_object_filtered(Author, Author.display_name == author_text) if not author: author = Author.populate(display_name=author_text, last_name=author_text.split(' ')[-1], first_name=' '.join(author_text.split(' ')[:-1])) - song.add_author(author) + song.add_author(author, author_type) if self.song_book_name: song_book = self.manager.get_object_filtered(Book, Book.name == self.song_book_name) if song_book is None: diff --git a/openlp/plugins/songs/lib/importers/videopsalm.py b/openlp/plugins/songs/lib/importers/videopsalm.py index 134649997..871f4b4a7 100644 --- a/openlp/plugins/songs/lib/importers/videopsalm.py +++ b/openlp/plugins/songs/lib/importers/videopsalm.py @@ -30,7 +30,7 @@ import os from openlp.core.common import translate from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib.importers.songimport import SongImport -from openlp.plugins.songs.lib.ui import SongStrings +from openlp.plugins.songs.lib.db import AuthorType log = logging.getLogger(__name__) @@ -57,7 +57,7 @@ class VideoPsalmImport(SongImport): file_content = song_file.read() processed_content = '' inside_quotes = False - # The VideoPsalm format is not valid json, it uses illegal line breaks and unquoted keys, this must be fixed. + # The VideoPsalm format is not valid json, it uses illegal line breaks and unquoted keys, this must be fixed file_content_it = iter(file_content) for c in file_content_it: if c == '"': @@ -83,47 +83,36 @@ class VideoPsalmImport(SongImport): songbook_name = songbook['Text'] media_folder = os.path.normpath(os.path.join(os.path.dirname(song_file.name), '..', 'Audio')) for song in songs: - #song['Composer'] - try: + self.song_book_name = songbook_name + if 'Text' in song: self.title = song['Text'] - except KeyError: - pass - try: - self.add_author(song['Author']) - except KeyError: - pass - try: + composer = None + author = None + if 'Composer' in song: + composer = song['Composer'] + if 'Author' in song: + author = song['Author'] + if author and composer == author: + self.add_author(author, AuthorType.WordsAndMusic) + else: + if author: + self.add_author(author, AuthorType.Words) + if composer: + self.add_author(composer, AuthorType.Music) + if 'Copyright' in song: self.add_copyright(song['Copyright'].replace('\n', ' ').strip()) - except KeyError: - pass - try: + if 'CCLI' in song: self.ccli_number = song['CCLI'] - except KeyError: - pass - try: - self.song_book_name = songbook_name - except KeyError: - pass - try: + if 'Theme' in song: self.topics = song['Theme'].splitlines() - except KeyError: - pass - #try: - # self.add_media_file(os.path.join(media_folder, song['AudioFile'])) - #except KeyError: - # pass - try: + if 'AudioFile' in song: + self.add_media_file(os.path.join(media_folder, song['AudioFile'])) + if 'Memo1' in song: self.add_comment(song['Memo1']) - except KeyError: - pass - try: + if 'Memo2' in song: self.add_comment(song['Memo2']) - except KeyError: - pass - try: + if 'Memo3' in song: self.add_comment(song['Memo3']) - except KeyError: - pass for verse in song['Verses']: self.add_verse(verse['Text'], 'v') if not self.finish(): @@ -131,4 +120,4 @@ class VideoPsalmImport(SongImport): except Exception as e: self.log_error(translate('SongsPlugin.VideoPsalmImport', 'File %s' % file.name), translate('SongsPlugin.VideoPsalmImport', 'Error: %s') % e) - song_file.close() \ No newline at end of file + song_file.close() diff --git a/tests/helpers/songfileimport.py b/tests/helpers/songfileimport.py index 470c319d2..7cf6d115a 100644 --- a/tests/helpers/songfileimport.py +++ b/tests/helpers/songfileimport.py @@ -124,7 +124,10 @@ class SongImportTestHelper(TestCase): self.assertEqual(importer.title, title, 'title for %s should be "%s"' % (source_file_name, title)) for author in author_calls: - self.mocked_add_author.assert_any_call(author) + if isinstance(author, str): + self.mocked_add_author.assert_any_call(author) + else: + self.mocked_add_author.assert_any_call(author[0], author[1]) if song_copyright: self.mocked_add_copyright.assert_called_with(song_copyright) if ccli_number: diff --git a/tests/resources/videopsalmsongs/as-safe-a-stronghold.json b/tests/resources/videopsalmsongs/as-safe-a-stronghold.json index a48ff1f9d..4755e5027 100644 --- a/tests/resources/videopsalmsongs/as-safe-a-stronghold.json +++ b/tests/resources/videopsalmsongs/as-safe-a-stronghold.json @@ -1,6 +1,7 @@ { "authors": [ - "Martin Luther" + ["Martin Luther", "words"], + ["Unknown", "music"] ], "ccli_number": "12345", "comments": "This is\nthe first comment\nThis is\nthe second comment\nThis is\nthe third comment\n",