Inform user when openlyric errors occur

bzr-revno: 1896
This commit is contained in:
Mattias Põldaru 2012-03-10 22:59:00 +00:00 committed by Jonathan Corwin
commit 4129887f93
2 changed files with 33 additions and 3 deletions

View File

@ -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)

View File

@ -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 = []
try:
lyrics = song_xml.lyrics
except AttributeError:
raise OpenLyricsError(OpenLyricsError.LyricsError,
'<lyrics> tag is missing.',
unicode(translate('OpenLP.OpenLyricsImportError',
'<lyrics> tag is missing.')))
try:
verses = lyrics.verse
except AttributeError:
raise OpenLyricsError(OpenLyricsError.VerseError,
'<verse> tag is missing.',
unicode(translate('OpenLP.OpenLyricsImportError',
'<verse> 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