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

View File

@ -153,15 +153,7 @@ class TestEasyWorshipSongImport(TestCase):
"""
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):
"""
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
"""
# GIVEN: Test files with a mocked out SongImport class, a mocked out "manager", a mocked out "import_wizard",
# and mocked out "author", "add_copyright", "add_verse", "finish" methods.
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'
# GIVEN: A mocked out SongImport class, a mocked out "manager" and mocked out "author" method.
with patch('openlp.plugins.songs.lib.ewimport.SongImport'):
mocked_manager = MagicMock()
mocked_import_wizard = 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.import_wizard = mocked_import_wizard
importer.stop_import_flag = False
importer.add_author = mocked_add_author
importer.add_verse = mocked_add_verse
importer.title = mocked_title
importer.finish = mocked_finish
importer.topics = []
importer.encoding = 'cp1252'
# 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')
# THEN: The escaping shoud be added
mocked_strip_rtf.assert_called_with("Det som var fr\'86n begynnelsen", 'cp1252')
# THEN: The import should fail
self.assertEquals(importer.entry_error_log, 'Unexpected data formatting.', 'Import should fail')