diff --git a/openlp/plugins/songs/lib/openlyricsimport.py b/openlp/plugins/songs/lib/openlyricsimport.py index 6aafdce16..43a6bc51b 100644 --- a/openlp/plugins/songs/lib/openlyricsimport.py +++ b/openlp/plugins/songs/lib/openlyricsimport.py @@ -38,6 +38,7 @@ from openlp.core.ui.wizard import WizardStrings from openlp.plugins.songs.lib.songimport import SongImport from openlp.plugins.songs.lib.ui import SongStrings from openlp.plugins.songs.lib import OpenLyrics +from openlp.plugins.songs.lib.xml import OpenLyricsError log = logging.getLogger(__name__) @@ -73,3 +74,7 @@ class OpenLyricsImport(SongImport): except etree.XMLSyntaxError: log.exception(u'XML syntax error in file %s' % file_path) self.logError(file_path, SongStrings.XMLSyntaxError) + except OpenLyricsError as exception: + log.exception(u'OpenLyricsException %d in file %s: %s' + % (exception.type, file_path, exception.log_message)) + self.logError(file_path, exception.display_message) diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index 9eb867856..4ed3f97d0 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -66,7 +66,7 @@ import re from lxml import etree, objectify -from openlp.core.lib import FormattingTags +from openlp.core.lib import FormattingTags, translate from openlp.plugins.songs.lib import clean_song, VerseType from openlp.plugins.songs.lib.db import Author, Book, Song, Topic from openlp.core.utils import get_application_version @@ -673,9 +673,22 @@ class OpenLyrics(object): sxml = SongXML() verses = {} verse_def_list = [] - lyrics = song_xml.lyrics + try: + lyrics = song_xml.lyrics + except AttributeError: + raise OpenLyricsError(OpenLyricsError.LyricsError, + ' tag is missing.', + unicode(translate('OpenLP.OpenLyricsImportError', + ' tag is missing.'))) + try: + verses = lyrics.verse + except AttributeError: + raise OpenLyricsError(OpenLyricsError.VerseError, + ' tag is missing.', + unicode(translate('OpenLP.OpenLyricsImportError', + ' tag is missing.'))) # Loop over the "verse" elements. - for verse in lyrics.verse: + for verse in verses: text = u'' # Loop over the "lines" elements. for lines in verse.lines: @@ -791,3 +804,15 @@ class OpenLyrics(object): """ return etree.tostring(xml, encoding=u'UTF-8', xml_declaration=True, pretty_print=True) + + +class OpenLyricsError(Exception): + # XML tree is missing the lyrics tag + LyricsError = 1 + # XML tree has no verse tags + VerseError = 2 + + def __init__(self, type, log_message, display_message): + self.type = type + self.log_message = log_message + self.display_message = display_message