diff --git a/openlp/plugins/songs/lib/openlyricsimport.py b/openlp/plugins/songs/lib/openlyricsimport.py index 5346b5803..27c2308f4 100644 --- a/openlp/plugins/songs/lib/openlyricsimport.py +++ b/openlp/plugins/songs/lib/openlyricsimport.py @@ -28,12 +28,17 @@ The :mod:`openlyricsimport` module provides the functionality for importing songs which are saved as OpenLyrics files. """ +import logging import os +from lxml import etree + from openlp.core.lib import translate from openlp.plugins.songs.lib.songimport import SongImport from openlp.plugins.songs.lib import OpenLyricsParser +log = logging.getLogger(__name__) + class OpenLyricsImport(SongImport): """ This provides the Openlyrics import. @@ -42,6 +47,7 @@ class OpenLyricsImport(SongImport): """ Initialise the import. """ + log.debug(u'initialise OpenLyricsImport') SongImport.__init__(self, master_manager) self.master_manager = master_manager self.openLyricsParser = OpenLyricsParser(master_manager) @@ -58,15 +64,14 @@ class OpenLyricsImport(SongImport): for file_path in self.import_source: if self.stop_import_flag: return False - file = open(file_path) - lines = file.readlines() - file.close() - lines = [line.strip() for line in lines] - xml = u''.join(lines) self.import_wizard.incrementProgressBar(unicode(translate( 'SongsPlugin.OpenLyricsImport', 'Importing %s...')) % os.path.basename(file_path)) + parser = etree.XMLParser(remove_blank_text=True) + file = etree.parse(file_path, parser) + xml = etree.tostring(file) if self.openLyricsParser.xml_to_song(xml) == 0: + log.debug(u'File could not be imported: %s' % file_path) # Importing this song failed! For now we stop import. return False return True diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index 326f97537..4045a412a 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -248,7 +248,8 @@ class OpenLyricsParser(object): This class represents the converter for Song to/from `OpenLyrics `_ XML. """ - # TODO: complete OpenLyrics standard implementation as fare as possible! + # TODO: Complete OpenLyrics standard implementation and document what is + # supported and what not! def __init__(self, manager): self.manager = manager @@ -316,8 +317,6 @@ class OpenLyricsParser(object): song = Song() if xml[:5] == u'').sub(u'', xml) song_xml = objectify.fromstring(xml) properties = song_xml.properties # Process Copyright @@ -349,17 +348,17 @@ class OpenLyricsParser(object): for verse in lyrics.verse: text = u'' for line in verse.lines: - for line in line.line: - line = unicode(line) - if not text: - text = line - else: - text += u'\n' + line - type = VerseType.expand_string(verse.attrib[u'name'][0]) - sxml.add_verse_to_lyrics(type, verse.attrib[u'name'][1], text) - # TODO: test this verse_order thing! + text = u'\n'.join([unicode(line) for line in line.line]) + # Remove chords + text = re.compile(u'').sub(u'', text) + # OpenLyrics allows e. g. "c", but we need "c1". + if self._get(verse, u'name').isalpha(): + verse.set(u'name', self._get(verse, u'name') + u'1') + type = VerseType.expand_string(self._get(verse, u'name')[0]) + sxml.add_verse_to_lyrics( + type, self._get(verse, u'name')[1], text) song.verse_order += u'%s%s ' % (type[0], - verse.attrib[u'name'][1]) + self._get(verse, u'name')[1]) search_text = search_text + text song.search_lyrics = search_text.lower() song.lyrics = unicode(sxml.extract_xml(), u'utf-8') @@ -399,7 +398,7 @@ class OpenLyricsParser(object): try: for songbook in properties.songbooks.songbook: self._process_songbook(self._get(songbook, u'name'), song) - if songbook.get(u'entry'): + if self._get(songbook, u'entry'): song.song_number = self._get(songbook, u'entry') # OpenLp does only support one song book, so take the first one. break @@ -427,7 +426,7 @@ class OpenLyricsParser(object): The element's attribute (unicode). """ if element.get(attribute) is not None: - return element.get(attribute) + return unicode(element.get(attribute)) return u'' def _text(self, element): @@ -483,8 +482,8 @@ class OpenLyricsParser(object): Author.display_name == name) if author is None: # We need to create a new author, as the author does not exist. - author = Author.populate(first_name=name.rsplit(u' ', 1)[0], - last_name=name.rsplit(u' ', 1)[1], display_name=name) + author = Author.populate(last_name=name.split(u' ')[-1], + first_name=u' '.join(name.split(u' ')[:-1]), display_name=name) self.manager.save_object(author) song.authors.append(author)