From 3ae188c287f0e1825b652948beba74a9fb7ac8ec Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Thu, 6 Mar 2014 20:56:31 +0000 Subject: [PATCH] FoilPresenter --- openlp/plugins/songs/lib/ewimport.py | 16 ++++ .../plugins/songs/lib/foilpresenterimport.py | 85 ++++++------------- .../songs/test_foilpresenterimport.py | 6 +- 3 files changed, 47 insertions(+), 60 deletions(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 169099fe0..9e3ae566e 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -76,6 +76,11 @@ class EasyWorshipSongImport(SongImport): SongImport.__init__(self, manager, **kwargs) def do_import(self): + """ + Import the songs + + :return: + """ # Open the DB and MB files if they exist import_source_mb = self.import_source.replace('.DB', '.MB') if not os.path.isfile(self.import_source) or not os.path.isfile(import_source_mb): @@ -239,9 +244,20 @@ class EasyWorshipSongImport(SongImport): self.memo_file.close() def find_field(self, field_name): + """ + Find a field in the descriptions + + :param field_name: field to find + :return: + """ return [i for i, x in enumerate(self.field_descriptions) if x.name == field_name][0] def set_record_struct(self, field_descriptions): + """ + Save the record structure + + :param field_descriptions: An array of field descriptions + """ # Begin with empty field struct list fsl = ['>'] for field_desc in field_descriptions: diff --git a/openlp/plugins/songs/lib/foilpresenterimport.py b/openlp/plugins/songs/lib/foilpresenterimport.py index 3ce9e7a59..5c88cc6a3 100644 --- a/openlp/plugins/songs/lib/foilpresenterimport.py +++ b/openlp/plugins/songs/lib/foilpresenterimport.py @@ -106,6 +106,7 @@ from openlp.plugins.songs.lib.xml import SongXML log = logging.getLogger(__name__) + class FoilPresenterImport(SongImport): """ This provides the Foilpresenter import. @@ -116,7 +117,7 @@ class FoilPresenterImport(SongImport): """ log.debug('initialise FoilPresenterImport') SongImport.__init__(self, manager, **kwargs) - self.FoilPresenter = FoilPresenter(self.manager, self) + self.foil_presenter = FoilPresenter(self.manager, self) def do_import(self): """ @@ -131,7 +132,7 @@ class FoilPresenterImport(SongImport): try: parsed_file = etree.parse(file_path, parser) xml = etree.tostring(parsed_file).decode() - self.FoilPresenter.xml_to_song(xml) + self.foil_presenter.xml_to_song(xml) except etree.XMLSyntaxError: self.log_error(file_path, SongStrings.XMLSyntaxError) log.exception('XML syntax error in file %s' % file_path) @@ -245,8 +246,7 @@ class FoilPresenter(object): """ This returns the text of an element as unicode string. - ``element`` - The element. + :param element: The element """ if element is not None: return str(element) @@ -256,11 +256,8 @@ class FoilPresenter(object): """ Adds the authors specified in the XML to the song. - ``foilpresenterfolie`` - The property object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The property object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ authors = [] try: @@ -324,8 +321,7 @@ class FoilPresenter(object): break author_temp = [] for author in strings: - temp = re.split(',(?=\D{2})|(?<=\D),|\/(?=\D{3,})|(?<=\D);', - author) + temp = re.split(',(?=\D{2})|(?<=\D),|\/(?=\D{3,})|(?<=\D);', author) for tempx in temp: author_temp.append(tempx) for author in author_temp: @@ -349,7 +345,7 @@ class FoilPresenter(object): if author is None: # We need to create a new author, as the author does not exist. author = Author.populate(display_name=display_name, last_name=display_name.split(' ')[-1], - first_name=' '.join(display_name.split(' ')[:-1])) + first_name=' '.join(display_name.split(' ')[:-1])) self.manager.save_object(author) song.authors.append(author) @@ -357,11 +353,8 @@ class FoilPresenter(object): """ Adds the CCLI number to the song. - ``foilpresenterfolie`` - The property object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The property object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ try: song.ccli_number = self._child(foilpresenterfolie.ccliid) @@ -372,11 +365,8 @@ class FoilPresenter(object): """ Joins the comments specified in the XML and add it to the song. - ``foilpresenterfolie`` - The property object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The property object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ try: song.comments = self._child(foilpresenterfolie.notiz) @@ -387,11 +377,8 @@ class FoilPresenter(object): """ Adds the copyright to the song. - ``foilpresenterfolie`` - The property object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The property object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ try: song.copyright = self._child(foilpresenterfolie.copyright.text_) @@ -402,11 +389,8 @@ class FoilPresenter(object): """ Processes the verses and search_lyrics for the song. - ``foilpresenterfolie`` - The foilpresenterfolie object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The foilpresenterfolie object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ sxml = SongXML() temp_verse_order = {} @@ -424,8 +408,8 @@ class FoilPresenter(object): } if not hasattr(foilpresenterfolie.strophen, 'strophe'): self.importer.log_error(self._child(foilpresenterfolie.titel), - str(translate('SongsPlugin.FoilPresenterSongImport', - 'Invalid Foilpresenter song file. No verses found.'))) + str(translate('SongsPlugin.FoilPresenterSongImport', + 'Invalid Foilpresenter song file. No verses found.'))) self.save_song = False return for strophe in foilpresenterfolie.strophen.strophe: @@ -478,10 +462,8 @@ class FoilPresenter(object): verse_number = str(int(verse_number) + 1) verse_type_index = VerseType.from_tag(verse_type[0]) verse_type = VerseType.tags[verse_type_index] - temp_verse_order[verse_sortnr] = ''.join((verse_type[0], - verse_number)) - temp_verse_order_backup.append(''.join((verse_type[0], - verse_number))) + temp_verse_order[verse_sortnr] = ''.join((verse_type[0], verse_number)) + temp_verse_order_backup.append(''.join((verse_type[0], verse_number))) sxml.add_verse_to_lyrics(verse_type, verse_number, text) song.lyrics = str(sxml.extract_xml(), 'utf-8') # Process verse order @@ -506,11 +488,8 @@ class FoilPresenter(object): """ Adds the song book and song number specified in the XML to the song. - ``foilpresenterfolie`` - The property object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The property object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ song.song_book_id = 0 song.song_number = '' @@ -518,8 +497,7 @@ class FoilPresenter(object): for bucheintrag in foilpresenterfolie.buch.bucheintrag: book_name = self._child(bucheintrag.name) if book_name: - book = self.manager.get_object_filtered(Book, - Book.name == book_name) + book = self.manager.get_object_filtered(Book, Book.name == book_name) if book is None: # We need to create a book, because it does not exist. book = Book.populate(name=book_name, publisher='') @@ -539,11 +517,8 @@ class FoilPresenter(object): """ Processes the titles specified in the song's XML. - ``foilpresenterfolie`` - The property object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The property object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ try: for title_string in foilpresenterfolie.titel.titelstring: @@ -561,18 +536,14 @@ class FoilPresenter(object): """ Adds the topics to the song. - ``foilpresenterfolie`` - The property object (lxml.objectify.ObjectifiedElement). - - ``song`` - The song object. + :param foilpresenterfolie: The property object (lxml.objectify.ObjectifiedElement). + :param song: The song object. """ try: for name in foilpresenterfolie.kategorien.name: topic_text = self._child(name) if topic_text: - topic = self.manager.get_object_filtered(Topic, - Topic.name == topic_text) + topic = self.manager.get_object_filtered(Topic, Topic.name == topic_text) if topic is None: # We need to create a topic, because it does not exist. topic = Topic.populate(name=topic_text) diff --git a/tests/functional/openlp_plugins/songs/test_foilpresenterimport.py b/tests/functional/openlp_plugins/songs/test_foilpresenterimport.py index f1c707aad..fbd339cf3 100644 --- a/tests/functional/openlp_plugins/songs/test_foilpresenterimport.py +++ b/tests/functional/openlp_plugins/songs/test_foilpresenterimport.py @@ -116,17 +116,17 @@ class TestFoilPresenter(TestCase): def create_foil_presenter_test(self): """ - Test creating an instance of the FoilPresenter class + Test creating an instance of the foil_presenter class """ # GIVEN: A mocked out "manager" and "SongImport" instance mocked_manager = MagicMock() mocked_song_import = MagicMock() - # WHEN: An FoilPresenter instance is created + # WHEN: An foil_presenter instance is created foil_presenter_instance = FoilPresenter(mocked_manager, mocked_song_import) # THEN: The instance should not be None - self.assertIsNotNone(foil_presenter_instance, 'FoilPresenter instance should not be none') + self.assertIsNotNone(foil_presenter_instance, 'foil_presenter instance should not be none') def no_xml_test(self): """