From 5b717db63a2425a3570710d37db4112c32e45a8b Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 2 Feb 2015 20:40:31 +0000 Subject: [PATCH] Make Zefania import guess book from number if name is unavailable. Fixes bug 1417033. Fixes: https://launchpad.net/bugs/1417033 --- openlp/plugins/bibles/lib/osis.py | 2 +- openlp/plugins/bibles/lib/zefania.py | 20 ++++++-- .../bibles/test_zefaniaimport.py | 32 ++++++++++++- tests/resources/bibles/rst.json | 16 +++++++ tests/resources/bibles/zefania-rst.xml | 48 +++++++++++++++++++ 5 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 tests/resources/bibles/rst.json create mode 100644 tests/resources/bibles/zefania-rst.xml diff --git a/openlp/plugins/bibles/lib/osis.py b/openlp/plugins/bibles/lib/osis.py index 52e232ac9..07ec4ab23 100644 --- a/openlp/plugins/bibles/lib/osis.py +++ b/openlp/plugins/bibles/lib/osis.py @@ -58,7 +58,7 @@ class OSISBible(BibleDB): # NOTE: We don't need to do any of the normal encoding detection here, because lxml does it's own encoding # detection, and the two mechanisms together interfere with each other. import_file = open(self.filename, 'rb') - osis_bible_tree = etree.parse(import_file) + osis_bible_tree = etree.parse(import_file, parser=etree.XMLParser(recover=True)) namespace = {'ns': 'http://www.bibletechnologies.net/2003/OSIS/namespace'} # Find bible language language_id = None diff --git a/openlp/plugins/bibles/lib/zefania.py b/openlp/plugins/bibles/lib/zefania.py index 355fb82cf..ad8f96648 100644 --- a/openlp/plugins/bibles/lib/zefania.py +++ b/openlp/plugins/bibles/lib/zefania.py @@ -57,7 +57,7 @@ class ZefaniaBible(BibleDB): # NOTE: We don't need to do any of the normal encoding detection here, because lxml does it's own encoding # detection, and the two mechanisms together interfere with each other. import_file = open(self.filename, 'rb') - zefania_bible_tree = etree.parse(import_file) + zefania_bible_tree = etree.parse(import_file, parser=etree.XMLParser(recover=True)) # Find bible language language_id = None language = zefania_bible_tree.xpath("/XMLBIBLE/INFORMATION/language/text()") @@ -76,9 +76,19 @@ class ZefaniaBible(BibleDB): etree.strip_elements(zefania_bible_tree, ('PROLOG', 'REMARK', 'CAPTION', 'MEDIA'), with_tail=False) xmlbible = zefania_bible_tree.getroot() for BIBLEBOOK in xmlbible: - book_ref_id = self.get_book_ref_id_by_name(str(BIBLEBOOK.get('bname')), num_books) - if not book_ref_id: - book_ref_id = self.get_book_ref_id_by_localised_name(str(BIBLEBOOK.get('bname'))) + if self.stop_import_flag: + break + bname = BIBLEBOOK.get('bname') + bnumber = BIBLEBOOK.get('bnumber') + if not bname and not bnumber: + continue + if bname: + book_ref_id = self.get_book_ref_id_by_name(bname, num_books) + if not book_ref_id: + book_ref_id = self.get_book_ref_id_by_localised_name(bname) + else: + log.debug('Could not find a name, will use number, basically a guess.') + book_ref_id = int(bnumber) if not book_ref_id: log.error('Importing books from "%s" failed' % self.filename) return False @@ -94,7 +104,7 @@ class ZefaniaBible(BibleDB): self.wizard.increment_progress_bar( translate('BiblesPlugin.Zefnia', 'Importing %(bookname)s %(chapter)s...' % {'bookname': db_book.name, 'chapter': chapter_number})) - self.session.commit() + self.session.commit() self.application.process_events() except Exception as e: critical_error_message_box( diff --git a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py index 0a234903c..63545a00c 100644 --- a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py +++ b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py @@ -77,7 +77,6 @@ class TestZefaniaImport(TestCase): mocked_import_wizard = MagicMock() importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') importer.wizard = mocked_import_wizard - importer.get_book_ref_id_by_name = MagicMock() importer.create_verse = MagicMock() importer.create_book = MagicMock() importer.session = MagicMock() @@ -92,3 +91,34 @@ class TestZefaniaImport(TestCase): self.assertTrue(importer.create_verse.called) for verse_tag, verse_text in test_data['verses']: importer.create_verse.assert_any_call(importer.create_book().id, '1', verse_tag, verse_text) + importer.create_book.assert_any_call('Genesis', 1, 1) + + def file_import_no_book_name_test(self): + """ + Test the import of Zefania Bible file without book names + """ + # GIVEN: Test files with a mocked out "manager", "import_wizard", and mocked functions + # get_book_ref_id_by_name, create_verse, create_book, session and get_language. + result_file = open(os.path.join(TEST_PATH, 'rst.json'), 'rb') + test_data = json.loads(result_file.read().decode()) + bible_file = 'zefania-rst.xml' + with patch('openlp.plugins.bibles.lib.zefania.ZefaniaBible.application'): + mocked_manager = MagicMock() + mocked_import_wizard = MagicMock() + importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') + importer.wizard = mocked_import_wizard + importer.create_verse = MagicMock() + importer.create_book = MagicMock() + importer.session = MagicMock() + importer.get_language = MagicMock() + importer.get_language.return_value = 'Russian' + + # WHEN: Importing bible file + importer.filename = os.path.join(TEST_PATH, bible_file) + importer.do_import() + + # THEN: The create_verse() method should have been called with each verse in the file. + self.assertTrue(importer.create_verse.called) + for verse_tag, verse_text in test_data['verses']: + importer.create_verse.assert_any_call(importer.create_book().id, '1', verse_tag, verse_text) + importer.create_book.assert_any_call('Exodus', 2, 1) diff --git a/tests/resources/bibles/rst.json b/tests/resources/bibles/rst.json new file mode 100644 index 000000000..d8aca09ac --- /dev/null +++ b/tests/resources/bibles/rst.json @@ -0,0 +1,16 @@ +{ + "book": "Exodus", + "chapter": 1, + "verses": [ + [ "1", "Вот имена сынов Израилевых, которые вошли в Египет с Иаковом, вошли каждый с домом своим:" ], + [ "2", "Рувим, Симеон, Левий и Иуда," ], + [ "3", "Иссахар, Завулон и Вениамин," ], + [ "4", "Дан и Неффалим, Гад и Асир." ], + [ "5", "Всех же душ, происшедших от чресл Иакова, было семьдесят, а Иосиф был [уже] в Египте." ], + [ "6", "И умер Иосиф и все братья его и весь род их;" ], + [ "7", "а сыны Израилевы расплодились и размножились, и возросли и усилились чрезвычайно, и наполнилась ими земля та." ], + [ "8", "И восстал в Египте новый царь, который не знал Иосифа," ], + [ "9", "и сказал народу своему: вот, народ сынов Израилевых многочислен и сильнее нас;" ], + [ "10", "перехитрим же его, чтобы он не размножался; иначе, когда случится война, соединится и он с нашими неприятелями, и вооружится против нас, и выйдет из земли [нашей]." ] + ] +} diff --git a/tests/resources/bibles/zefania-rst.xml b/tests/resources/bibles/zefania-rst.xml new file mode 100644 index 000000000..72d1230a4 --- /dev/null +++ b/tests/resources/bibles/zefania-rst.xml @@ -0,0 +1,48 @@ + + + + + + + + Russian Synodal Translation + Zefania XML Bible Markup Language + 2009-01-20 + Jens Grabner + http://www.agape-biblia.org + http://www.crosswire.org/sword/modules/ + RUS + + RST + + 1876 Russian Synodal Translation, 1956 Edition + The text was supplied by "Light in East Germany". + + + + "Light in East Germany" Tel +49 711 83 30 57 + Postfach 1340 Fax +49 711 83 13 51 + 7015 Korntal + Munchingen 1 + Germany + + + + + + + + Вторая книга Моисеева. Исход + Вот имена сынов Израилевых, которые вошли в Египет с Иаковом, вошли каждый с домом своим: + Рувим, Симеон, Левий и Иуда, + Иссахар, Завулон и Вениамин, + Дан и Неффалим, Гад и Асир. + Всех же душ, происшедших от чресл Иакова, было семьдесят, а Иосиф был [уже] в Египте. + И умер Иосиф и все братья его и весь род их; + а сыны Израилевы расплодились и размножились, и возросли и усилились чрезвычайно, и наполнилась ими земля та. + И восстал в Египте новый царь, который не знал Иосифа, + и сказал народу своему: вот, народ сынов Израилевых многочислен и сильнее нас; + перехитрим же его, чтобы он не размножался; иначе, когда случится война, соединится и он с нашими неприятелями, и вооружится против нас, и выйдет из земли [нашей]. + + +