Changed to ignore wrongly formatted songs.

This commit is contained in:
Tomas Groth 2014-05-05 19:29:57 +02:00
parent 689700dd01
commit c7ce62bd43
2 changed files with 26 additions and 40 deletions

View File

@ -74,6 +74,7 @@ class EasyWorshipSongImport(SongImport):
""" """
def __init__(self, manager, **kwargs): def __init__(self, manager, **kwargs):
super(EasyWorshipSongImport, self).__init__(manager, **kwargs) super(EasyWorshipSongImport, self).__init__(manager, **kwargs)
self.entry_error_log = ''
def do_import(self): def do_import(self):
""" """
@ -183,7 +184,12 @@ class EasyWorshipSongImport(SongImport):
self.set_song_import_object(authors, inflated_content) self.set_song_import_object(authors, inflated_content)
if self.stop_import_flag: if self.stop_import_flag:
break break
if not self.finish(): if self.entry_error_log:
self.log_error(self.import_source,
translate('SongsPlugin.EasyWorshipSongImport', '"%s" could not be imported. %s')
% (self.title, self.entry_error_log))
self.entry_error_log = ''
elif not self.finish():
self.log_error(self.import_source) self.log_error(self.import_source)
# Set file_pos for next entry # Set file_pos for next entry
file_pos += entry_length file_pos += entry_length
@ -305,7 +311,12 @@ class EasyWorshipSongImport(SongImport):
self.set_song_import_object(authors, words) self.set_song_import_object(authors, words)
if self.stop_import_flag: if self.stop_import_flag:
break break
if not self.finish(): if self.entry_error_log:
self.log_error(self.import_source,
translate('SongsPlugin.EasyWorshipSongImport', '"%s" could not be imported. %s')
% (self.title, self.entry_error_log))
self.entry_error_log = ''
elif not self.finish():
self.log_error(self.import_source) self.log_error(self.import_source)
db_file.close() db_file.close()
self.memo_file.close() self.memo_file.close()
@ -333,16 +344,14 @@ class EasyWorshipSongImport(SongImport):
try: try:
decoded_words = words.decode() decoded_words = words.decode()
except UnicodeDecodeError: except UnicodeDecodeError:
# The unicode chars in the rtf was not escaped in the expected manor, doing it manually. # The unicode chars in the rtf was not escaped in the expected manor
newbytes = bytearray() self.entry_error_log = translate('SongsPlugin.EasyWorshipSongImport',
for b in words: 'Unexpected data formatting.')
if b > 127: return
newbytes += bytearray(b'\\\'') + bytearray(str(hex(b))[-2:].encode())
else:
newbytes.append(b)
decoded_words = newbytes.decode()
result = strip_rtf(decoded_words, self.encoding) result = strip_rtf(decoded_words, self.encoding)
if result is None: if result is None:
self.entry_error_log = translate('SongsPlugin.EasyWorshipSongImport',
'No song text found.')
return return
words, self.encoding = result words, self.encoding = result
verse_type = VerseType.tags[VerseType.Verse] verse_type = VerseType.tags[VerseType.Verse]

View File

@ -153,15 +153,7 @@ class TestEasyWorshipSongImport(TestCase):
""" """
Test the functions in the :mod:`ewimport` module. Test the functions in the :mod:`ewimport` module.
""" """
"""def setUp(self):
self.songimport_patcher = patch('openlp.plugins.songs.lib.ewimport.EasyWorshipSongImport.__init__')
self.mocked_songimport = self.songimport_patcher.start()
self.mocked_songimport.return_value = None
def tearDown(self):
self.songimport_patcher.stop()
"""
def create_field_desc_entry_test(self): def create_field_desc_entry_test(self):
""" """
Test creating an instance of the :class`FieldDescEntry` class. Test creating an instance of the :class`FieldDescEntry` class.
@ -495,32 +487,17 @@ class TestEasyWorshipSongImport(TestCase):
""" """
Test import of rtf without the expected escaping of unicode Test import of rtf without the expected escaping of unicode
""" """
# GIVEN: Test files with a mocked out SongImport class, a mocked out "manager", a mocked out "import_wizard", # GIVEN: A mocked out SongImport class, a mocked out "manager" and mocked out "author" method.
# and mocked out "author", "add_copyright", "add_verse", "finish" methods. with patch('openlp.plugins.songs.lib.ewimport.SongImport'):
with patch('openlp.plugins.songs.lib.ewimport.SongImport'), \
patch('openlp.plugins.songs.lib.ewimport.strip_rtf') as mocked_strip_rtf, \
patch('openlp.plugins.songs.lib.ewimport.retrieve_windows_encoding') \
as mocked_retrieve_windows_encoding:
mocked_retrieve_windows_encoding.return_value = 'cp1252'
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
mocked_add_author = MagicMock() mocked_add_author = MagicMock()
mocked_add_verse = MagicMock()
mocked_finish = MagicMock()
mocked_title = MagicMock()
mocked_finish.return_value = True
importer = EasyWorshipSongImportLogger(mocked_manager) importer = EasyWorshipSongImportLogger(mocked_manager)
importer.import_wizard = mocked_import_wizard
importer.stop_import_flag = False
importer.add_author = mocked_add_author importer.add_author = mocked_add_author
importer.add_verse = mocked_add_verse importer.encoding = 'cp1252'
importer.title = mocked_title
importer.finish = mocked_finish
importer.topics = []
# WHEN: running set_song_import_object on a verse string without the needed escaping # WHEN: running set_song_import_object on a verse string without the needed escaping
importer.set_song_import_object('Test Author', b'Det som var fr\x86n begynnelsen') importer.set_song_import_object('Test Author', b'Det som var fr\x86n begynnelsen')
# THEN: The escaping shoud be added # THEN: The import should fail
mocked_strip_rtf.assert_called_with("Det som var fr\'86n begynnelsen", 'cp1252') self.assertEquals(importer.entry_error_log, 'Unexpected data formatting.', 'Import should fail')