From 0d48c09180b0648ca476c9977ef3392a48b4211d Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Tue, 6 May 2014 21:53:08 +0200 Subject: [PATCH 1/6] Added support for import of Zefania XML bibles. --- openlp/core/ui/wizard.py | 1 + .../plugins/bibles/forms/bibleimportform.py | 45 ++++++- openlp/plugins/bibles/lib/manager.py | 5 + openlp/plugins/bibles/lib/opensong.py | 7 ++ openlp/plugins/bibles/lib/zefania.py | 110 ++++++++++++++++++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 openlp/plugins/bibles/lib/zefania.py diff --git a/openlp/core/ui/wizard.py b/openlp/core/ui/wizard.py index 5996296b5..37070092b 100644 --- a/openlp/core/ui/wizard.py +++ b/openlp/core/ui/wizard.py @@ -51,6 +51,7 @@ class WizardStrings(object): CSV = 'CSV' OS = 'OpenSong' OSIS = 'OSIS' + ZEF = 'Zefania' # These strings should need a good reason to be retranslated elsewhere. FinishedImport = translate('OpenLP.Ui', 'Finished import.') FormatLabel = translate('OpenLP.Ui', 'Format:') diff --git a/openlp/plugins/bibles/forms/bibleimportform.py b/openlp/plugins/bibles/forms/bibleimportform.py index 79b0bc699..171994d18 100644 --- a/openlp/plugins/bibles/forms/bibleimportform.py +++ b/openlp/plugins/bibles/forms/bibleimportform.py @@ -110,6 +110,7 @@ class BibleImportForm(OpenLPWizard): self.csv_books_button.clicked.connect(self.on_csv_books_browse_button_clicked) self.csv_verses_button.clicked.connect(self.on_csv_verses_browse_button_clicked) self.open_song_browse_button.clicked.connect(self.on_open_song_browse_button_clicked) + self.zefania_browse_button.clicked.connect(self.on_zefania_browse_button_clicked) def add_custom_pages(self): """ @@ -125,7 +126,7 @@ class BibleImportForm(OpenLPWizard): self.format_label = QtGui.QLabel(self.select_page) self.format_label.setObjectName('FormatLabel') self.format_combo_box = QtGui.QComboBox(self.select_page) - self.format_combo_box.addItems(['', '', '', '']) + self.format_combo_box.addItems(['', '', '', '', '']) self.format_combo_box.setObjectName('FormatComboBox') self.format_layout.addRow(self.format_label, self.format_combo_box) self.spacer = QtGui.QSpacerItem(10, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum) @@ -247,6 +248,25 @@ class BibleImportForm(OpenLPWizard): self.web_proxy_layout.setWidget(2, QtGui.QFormLayout.FieldRole, self.web_password_edit) self.web_tab_widget.addTab(self.web_proxy_tab, '') self.select_stack.addWidget(self.web_tab_widget) + self.zefania_widget = QtGui.QWidget(self.select_page) + self.zefania_widget.setObjectName('ZefaniaWidget') + self.zefania_layout = QtGui.QFormLayout(self.zefania_widget) + self.zefania_layout.setMargin(0) + self.zefania_layout.setObjectName('ZefaniaLayout') + self.zefania_file_label = QtGui.QLabel(self.zefania_widget) + self.zefania_file_label.setObjectName('ZefaniaFileLabel') + self.zefania_file_layout = QtGui.QHBoxLayout() + self.zefania_file_layout.setObjectName('ZefaniaFileLayout') + self.zefania_file_edit = QtGui.QLineEdit(self.zefania_widget) + self.zefania_file_edit.setObjectName('ZefaniaFileEdit') + self.zefania_file_layout.addWidget(self.zefania_file_edit) + self.zefania_browse_button = QtGui.QToolButton(self.zefania_widget) + self.zefania_browse_button.setIcon(self.open_icon) + self.zefania_browse_button.setObjectName('ZefaniaBrowseButton') + self.zefania_file_layout.addWidget(self.zefania_browse_button) + self.zefania_layout.addRow(self.zefania_file_label, self.zefania_file_layout) + self.zefania_layout.setItem(5, QtGui.QFormLayout.LabelRole, self.spacer) + self.select_stack.addWidget(self.zefania_widget) self.select_page_layout.addLayout(self.select_stack) self.addPage(self.select_page) # License Page @@ -294,11 +314,13 @@ class BibleImportForm(OpenLPWizard): self.format_combo_box.setItemText(BibleFormat.OpenSong, WizardStrings.OS) self.format_combo_box.setItemText(BibleFormat.WebDownload, translate('BiblesPlugin.ImportWizardForm', 'Web Download')) + self.format_combo_box.setItemText(BibleFormat.Zefania, WizardStrings.ZEF) self.osis_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:')) self.csv_books_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Books file:')) self.csv_verses_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Verses file:')) self.open_song_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:')) self.web_source_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Location:')) + self.zefania_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:')) self.web_source_combo_box.setItemText(WebDownload.Crosswalk, translate('BiblesPlugin.ImportWizardForm', 'Crosswalk')) self.web_source_combo_box.setItemText(WebDownload.BibleGateway, translate('BiblesPlugin.ImportWizardForm', @@ -331,7 +353,8 @@ class BibleImportForm(OpenLPWizard): self.osis_file_label.minimumSizeHint().width(), self.csv_books_label.minimumSizeHint().width(), self.csv_verses_label.minimumSizeHint().width(), - self.open_song_file_label.minimumSizeHint().width()) + self.open_song_file_label.minimumSizeHint().width(), + self.zefania_file_label.minimumSizeHint().width()) self.spacer.changeSize(label_width, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) def validateCurrentPage(self): @@ -366,6 +389,11 @@ class BibleImportForm(OpenLPWizard): critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.OS) self.open_song_file_edit.setFocus() return False + elif self.field('source_format') == BibleFormat.Zefania: + if not self.field('zefania_file'): + critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.ZEF) + self.zefania_file_edit.setFocus() + return False elif self.field('source_format') == BibleFormat.WebDownload: self.version_name_edit.setText(self.web_translation_combo_box.currentText()) return True @@ -447,6 +475,13 @@ class BibleImportForm(OpenLPWizard): self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.OS, self.open_song_file_edit, 'last directory import') + def on_zefania_browse_button_clicked(self): + """ + Show the file open dialog for the Zefania file. + """ + self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.ZEF, self.zefania_file_edit, + 'last directory import') + def register_fields(self): """ Register the bible import wizard fields. @@ -456,6 +491,7 @@ class BibleImportForm(OpenLPWizard): self.select_page.registerField('csv_booksfile', self.csv_books_edit) self.select_page.registerField('csv_versefile', self.csv_verses_edit) self.select_page.registerField('opensong_file', self.open_song_file_edit) + self.select_page.registerField('zefania_file', self.zefania_file_edit) self.select_page.registerField('web_location', self.web_source_combo_box) self.select_page.registerField('web_biblename', self.web_translation_combo_box) self.select_page.registerField('proxy_server', self.web_server_edit) @@ -479,6 +515,7 @@ class BibleImportForm(OpenLPWizard): self.setField('csv_booksfile', '') self.setField('csv_versefile', '') self.setField('opensong_file', '') + self.setField('zefania_file', '') self.setField('web_location', WebDownload.Crosswalk) self.setField('web_biblename', self.web_translation_combo_box.currentIndex()) self.setField('proxy_server', settings.value('proxy address')) @@ -562,6 +599,10 @@ class BibleImportForm(OpenLPWizard): proxy_username=self.field('proxy_username'), proxy_password=self.field('proxy_password') ) + elif bible_type == BibleFormat.Zefania: + # Import an Zefania bible. + importer = self.manager.import_bible(BibleFormat.Zefania, name=license_version, + filename=self.field('zefania_file')) if importer.do_import(license_version): self.manager.save_meta_data(license_version, license_version, license_copyright, license_permissions) self.manager.reload_bibles() diff --git a/openlp/plugins/bibles/lib/manager.py b/openlp/plugins/bibles/lib/manager.py index c57fc117e..7dab8b3d7 100644 --- a/openlp/plugins/bibles/lib/manager.py +++ b/openlp/plugins/bibles/lib/manager.py @@ -38,6 +38,7 @@ from .csvbible import CSVBible from .http import HTTPBible from .opensong import OpenSongBible from .osis import OSISBible +from .zefania import ZefaniaBible log = logging.getLogger(__name__) @@ -52,6 +53,7 @@ class BibleFormat(object): CSV = 1 OpenSong = 2 WebDownload = 3 + Zefania = 4 @staticmethod def get_class(bible_format): @@ -68,6 +70,8 @@ class BibleFormat(object): return OpenSongBible elif bible_format == BibleFormat.WebDownload: return HTTPBible + elif bible_format == BibleFormat.Zefania: + return ZefaniaBible else: return None @@ -81,6 +85,7 @@ class BibleFormat(object): BibleFormat.CSV, BibleFormat.OpenSong, BibleFormat.WebDownload, + BibleFormar.Zefania, ] diff --git a/openlp/plugins/bibles/lib/opensong.py b/openlp/plugins/bibles/lib/opensong.py index c7bfa01a2..fa8323d7f 100644 --- a/openlp/plugins/bibles/lib/opensong.py +++ b/openlp/plugins/bibles/lib/opensong.py @@ -81,6 +81,13 @@ class OpenSongBible(BibleDB): import_file = open(self.filename, 'rb') opensong = objectify.parse(import_file) bible = opensong.getroot() + # Check that we're not trying to import a Zefania XML bible, it is sometimes refered to as 'OpenSong' + if bible.tag.upper() == 'XMLBIBLE': + critical_error_message_box( + message=translate('BiblesPlugin.OpenSongImport', + 'Incorrect Bible file type supplied. This looks like a Zefania XML bible, ' + 'please use the Zefania import option.')) + return False language_id = self.get_language(bible_name) if not language_id: log.error('Importing books from "%s" failed' % self.filename) diff --git a/openlp/plugins/bibles/lib/zefania.py b/openlp/plugins/bibles/lib/zefania.py new file mode 100644 index 000000000..16680efa3 --- /dev/null +++ b/openlp/plugins/bibles/lib/zefania.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2014 Raoul Snyman # +# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan # +# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, # +# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. # +# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, # +# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, # +# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, # +# Frode Woldsund, Martin Zibricky, Patrick Zimmermann # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program is distributed in the hope that it will be useful, but WITHOUT # +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # +# more details. # +# # +# You should have received a copy of the GNU General Public License along # +# with this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### + +import logging +from lxml import etree, objectify + +from openlp.core.common import translate +from openlp.core.lib.ui import critical_error_message_box +from openlp.plugins.bibles.lib.db import BibleDB, BiblesResourcesDB + + +log = logging.getLogger(__name__) + + +class ZefaniaBible(BibleDB): + """ + Zefania Bible format importer class. + """ + def __init__(self, parent, **kwargs): + """ + Constructor to create and set up an instance of the ZefaniaBible class. This class is used to import Bibles + from ZefaniaBible's XML format. + """ + log.debug(self.__class__.__name__) + BibleDB.__init__(self, parent, **kwargs) + self.filename = kwargs['filename'] + + def do_import(self, bible_name=None): + """ + Loads a Bible from file. + """ + log.debug('Starting Zefania import from "%s"' % self.filename) + if not isinstance(self.filename, str): + self.filename = str(self.filename, 'utf8') + import_file = None + success = True + try: + # 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') + language_id = self.get_language(bible_name) + if not language_id: + log.error('Importing books from "%s" failed' % self.filename) + return False + zefania_bible_tree = etree.parse(import_file) + num_books = int(zefania_bible_tree.xpath("count(//BIBLEBOOK)")) + # Strip tags we don't use + etree.strip_tags(zefania_bible_tree, ('STYLE', 'GRAM', 'NOTE', 'SUP', 'XREF')) + 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 not book_ref_id: + log.error('Importing books from "%s" failed' % self.filename) + return False + book_details = BiblesResourcesDB.get_book_by_id(book_ref_id) + db_book = self.create_book(book_details['name'], book_ref_id, book_details['testament_id']) + for CHAPTER in BIBLEBOOK: + if self.stop_import_flag: + break + chapter_number = CHAPTER.get("cnumber") + for VERS in CHAPTER: + verse_number = VERS.get("vnumber") + self.create_verse(db_book.id, chapter_number, verse_number, VERS.text.replace('
', '\n')) + self.wizard.increment_progress_bar( + translate('BiblesPlugin.Zefnia', 'Importing %(bookname)s %(chapter)s...' % + {'bookname': db_book.name, 'chapter': chapter_number})) + self.session.commit() + self.application.process_events() + except Exception as e: + critical_error_message_box( + message=translate('BiblesPlugin.ZefaniaImport', + 'Incorrect Bible file type supplied. Zefania Bibles may be ' + 'compressed. You must decompress them before import.')) + log.exception(str(e)) + success = False + finally: + if import_file: + import_file.close() + if self.stop_import_flag: + return False + else: + return success From 37f04c51996b7e8d58b3bb0f8816581bf33bf5b5 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Wed, 14 May 2014 22:53:43 +0200 Subject: [PATCH 2/6] Added simple tests. --- openlp/plugins/bibles/lib/zefania.py | 4 +- .../bibles/test_zefaniaimport.py | 116 ++++++++++++++++++ tests/resources/bibles/zefania-dk1933.xml | 45 +++++++ 3 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 tests/functional/openlp_plugins/bibles/test_zefaniaimport.py create mode 100644 tests/resources/bibles/zefania-dk1933.xml diff --git a/openlp/plugins/bibles/lib/zefania.py b/openlp/plugins/bibles/lib/zefania.py index 16680efa3..c52b58eae 100644 --- a/openlp/plugins/bibles/lib/zefania.py +++ b/openlp/plugins/bibles/lib/zefania.py @@ -70,8 +70,10 @@ class ZefaniaBible(BibleDB): return False zefania_bible_tree = etree.parse(import_file) num_books = int(zefania_bible_tree.xpath("count(//BIBLEBOOK)")) - # Strip tags we don't use + # Strip tags we don't use - keep content etree.strip_tags(zefania_bible_tree, ('STYLE', 'GRAM', 'NOTE', 'SUP', 'XREF')) + # Strip tags we don't use - remove content + 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) diff --git a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py new file mode 100644 index 000000000..64fa3251e --- /dev/null +++ b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py @@ -0,0 +1,116 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2014 Raoul Snyman # +# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan # +# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, # +# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. # +# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, # +# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, # +# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, # +# Frode Woldsund, Martin Zibricky, Patrick Zimmermann # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program is distributed in the hope that it will be useful, but WITHOUT # +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # +# more details. # +# # +# You should have received a copy of the GNU General Public License along # +# with this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### +""" +This module contains tests for the Zefania Bible importer. +""" + +import os +from unittest import TestCase + +from tests.functional import MagicMock, patch +from openlp.plugins.bibles.lib.zefania import ZefaniaBible +from openlp.plugins.bibles.lib.db import BibleDB + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), + '..', '..', '..', 'resources', 'bibles')) +ZEFANIA_TEST_DATA = { + 'zefania-dk1933.xml': { + 'book': 'Genesis', + 'chapter' : 1, + 'verses': [ + (1, 'I Begyndelsen skabte Gud Himmelen og Jorden.\n'), + (2, 'Og Jorden var øde og tom, og der var Mørke over Verdensdybet. ' + 'Men Guds Ånd svævede over Vandene.'), + (3, 'Og Gud sagde: "Der blive Lys!" Og der blev Lys.'), + (4, 'Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket,'), + (5, 'og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, ' + 'og det blev Morgen, første Dag.'), + (6, 'Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!"'), + (7, 'Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen ' + 'fra Vandet over Hvælvingen;'), + (8, 'og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag.'), + (9, 'Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" ' + 'Og således skete det;'), + (10, 'og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, ' + 'at det var godt.') + ] + } +} + + +class TestZefaniaImport(TestCase): + """ + Test the functions in the :mod:`zefaniaimport` module. + """ + + def setUp(self): + self.construtor_patcher = patch('openlp.plugins.bibles.lib.zefania.BibleDB') + self.construtor_patcher.start() + + def tearDown(self): + self.construtor_patcher.stop() + + def create_importer_test(self): + """ + Test creating an instance of the Zefania file importer + """ + # GIVEN: A mocked out BibleDB class, and a mocked out "manager" + #with patch('openlp.plugins.bibles.lib.zefania.BibleDB'): + if True: + mocked_manager = MagicMock() + + # WHEN: An importer object is created + importer = ZefaniaBible(mocked_manager, filename='') + + # THEN: The importer should be an instance of SongImport + self.assertIsInstance(importer, BibleDB) + + def file_import_test(self): + """ + Test the actual import of real song files + """ + # GIVEN: Test files with a mocked out "manager" and a mocked out "import_wizard" + #with patch('openlp.plugins.bibles.lib.zefania.BibleDB'): + if True: + for bible_file in ZEFANIA_TEST_DATA: + mocked_manager = MagicMock() + mocked_import_wizard = MagicMock() + importer = ZefaniaBible(mocked_manager, filename='') + importer.import_wizard = mocked_import_wizard + importer.get_book_ref_id_by_name = MagicMock() + importer.get_book_ref_id_by_name.return_value = { 'id': 1, 'testament_id': 1, 'name': 'Genesis', + 'abbreviation': 'Gen', 'chapters': 1 } + importer.create_verse = MagicMock() + + # WHEN: Importing each file + importer.filename = os.path.join(TEST_PATH, bible_file) + importer.do_import() + + # THEN: The create_verse() method should have been called + self.assertTrue(importer.create_verse.called) diff --git a/tests/resources/bibles/zefania-dk1933.xml b/tests/resources/bibles/zefania-dk1933.xml new file mode 100644 index 000000000..f538edb99 --- /dev/null +++ b/tests/resources/bibles/zefania-dk1933.xml @@ -0,0 +1,45 @@ + + + + + + + + Danish Version + + + The Holy Bible + The electronic edition of this Bible comes from the Danish 1933 edition. The Old Testament is an update from the 1931 edition, and the New Testament is an update from the 1907 edition. + Free Bible Software Group + + The Unbound Bible + Biola University: Administrative Computing + 13800 Biola Ave. + La Mirada, CA 90639 + United States of America + 562-903-4722 + + 2009-01-20 + Zefania XML Bible Markup Language + DAN + ftp://unboundftp.biola.edu/pub/danish1933_utf8.zip + DAN + provide the bible to the world + + + + + + I skabte Gud Himmelen og Jorden. + Og Jorden var øde og tom, og der var Mørke over Verdensdybet. Men Guds Ånd svævede over Vandene. + Og Gud sagde: "Der blive Lys!" Og der blev Lys. + Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket, + og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, og det blev Morgen, første Dag. + Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!" + Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen fra Vandet over Hvælvingen; + og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag. + Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" Og således skete det; + og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, at det var godt. + + + \ No newline at end of file From 58179f8d832ec661fbf55243db4db78654715220 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Tue, 20 May 2014 20:41:07 +0200 Subject: [PATCH 3/6] Tests --- openlp/plugins/bibles/lib/zefania.py | 1 + .../bibles/test_zefaniaimport.py | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/openlp/plugins/bibles/lib/zefania.py b/openlp/plugins/bibles/lib/zefania.py index c52b58eae..4e8373a93 100644 --- a/openlp/plugins/bibles/lib/zefania.py +++ b/openlp/plugins/bibles/lib/zefania.py @@ -97,6 +97,7 @@ class ZefaniaBible(BibleDB): self.session.commit() self.application.process_events() except Exception as e: + print(str(e)) critical_error_message_box( message=translate('BiblesPlugin.ZefaniaImport', 'Incorrect Bible file type supplied. Zefania Bibles may be ' diff --git a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py index 64fa3251e..942834d7a 100644 --- a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py +++ b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py @@ -70,11 +70,20 @@ class TestZefaniaImport(TestCase): """ def setUp(self): - self.construtor_patcher = patch('openlp.plugins.bibles.lib.zefania.BibleDB') + self.construtor_patcher = patch('openlp.plugins.bibles.lib.db.Registry') self.construtor_patcher.start() + self.get_lang_patcher = patch('openlp.plugins.bibles.lib.db.Manager') + self.get_lang_patcher.start() + self.brdb = patch('openlp.plugins.bibles.lib.zefania.BiblesResourcesDB') + self.brdb.start() + self.qt_core = patch('openlp.plugins.bibles.lib.db.QtCore') + self.qt_core.start() def tearDown(self): self.construtor_patcher.stop() + self.get_lang_patcher.stop() + self.brdb.stop() + self.qt_core.stop() def create_importer_test(self): """ @@ -86,7 +95,7 @@ class TestZefaniaImport(TestCase): mocked_manager = MagicMock() # WHEN: An importer object is created - importer = ZefaniaBible(mocked_manager, filename='') + importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') # THEN: The importer should be an instance of SongImport self.assertIsInstance(importer, BibleDB) @@ -96,21 +105,27 @@ class TestZefaniaImport(TestCase): Test the actual import of real song files """ # GIVEN: Test files with a mocked out "manager" and a mocked out "import_wizard" - #with patch('openlp.plugins.bibles.lib.zefania.BibleDB'): + #with patch('openlp.plugins.bibles.lib.zefania.BiblesResourcesDB') as brdb: if True: for bible_file in ZEFANIA_TEST_DATA: mocked_manager = MagicMock() mocked_import_wizard = MagicMock() - importer = ZefaniaBible(mocked_manager, filename='') - importer.import_wizard = mocked_import_wizard + importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') + importer.wizard = mocked_import_wizard + #importer.wizard.increment_progress_bar = MagicMock() importer.get_book_ref_id_by_name = MagicMock() importer.get_book_ref_id_by_name.return_value = { 'id': 1, 'testament_id': 1, 'name': 'Genesis', 'abbreviation': 'Gen', 'chapters': 1 } importer.create_verse = MagicMock() + importer.create_book = MagicMock() + importer.session = MagicMock() + importer.get_language = MagicMock() + importer.get_language.return_value = 'Danish' + importer.application.process_events = MagicMock() # WHEN: Importing each file importer.filename = os.path.join(TEST_PATH, bible_file) - importer.do_import() + importer.do_import('Dansk Version') # THEN: The create_verse() method should have been called self.assertTrue(importer.create_verse.called) From 4039cf1103230fec05d8035153f6f2fbbccefb32 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Tue, 20 May 2014 23:54:18 +0200 Subject: [PATCH 4/6] More tests --- .../bibles/test_zefaniaimport.py | 64 ++++++++----------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py index 942834d7a..63e6ae40d 100644 --- a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py +++ b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py @@ -44,20 +44,20 @@ ZEFANIA_TEST_DATA = { 'book': 'Genesis', 'chapter' : 1, 'verses': [ - (1, 'I Begyndelsen skabte Gud Himmelen og Jorden.\n'), - (2, 'Og Jorden var øde og tom, og der var Mørke over Verdensdybet. ' + ('1', 'I Begyndelsen skabte Gud Himmelen og Jorden.'), + ('2', 'Og Jorden var øde og tom, og der var Mørke over Verdensdybet. ' 'Men Guds Ånd svævede over Vandene.'), - (3, 'Og Gud sagde: "Der blive Lys!" Og der blev Lys.'), - (4, 'Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket,'), - (5, 'og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, ' + ('3', 'Og Gud sagde: "Der blive Lys!" Og der blev Lys.'), + ('4', 'Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket,'), + ('5', 'og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, ' 'og det blev Morgen, første Dag.'), - (6, 'Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!"'), - (7, 'Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen ' + ('6', 'Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!"'), + ('7', 'Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen ' 'fra Vandet over Hvælvingen;'), - (8, 'og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag.'), - (9, 'Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" ' + ('8', 'og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag.'), + ('9', 'Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" ' 'Og således skete det;'), - (10, 'og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, ' + ('10', 'og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, ' 'at det var godt.') ] } @@ -70,62 +70,52 @@ class TestZefaniaImport(TestCase): """ def setUp(self): - self.construtor_patcher = patch('openlp.plugins.bibles.lib.db.Registry') - self.construtor_patcher.start() - self.get_lang_patcher = patch('openlp.plugins.bibles.lib.db.Manager') - self.get_lang_patcher.start() - self.brdb = patch('openlp.plugins.bibles.lib.zefania.BiblesResourcesDB') - self.brdb.start() - self.qt_core = patch('openlp.plugins.bibles.lib.db.QtCore') - self.qt_core.start() + self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry') + self.registry_patcher.start() + self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager') + self.manager_patcher.start() def tearDown(self): - self.construtor_patcher.stop() - self.get_lang_patcher.stop() - self.brdb.stop() - self.qt_core.stop() + self.registry_patcher.stop() + self.manager_patcher.stop() def create_importer_test(self): """ Test creating an instance of the Zefania file importer """ # GIVEN: A mocked out BibleDB class, and a mocked out "manager" - #with patch('openlp.plugins.bibles.lib.zefania.BibleDB'): - if True: - mocked_manager = MagicMock() + mocked_manager = MagicMock() - # WHEN: An importer object is created - importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') + # WHEN: An importer object is created + importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') - # THEN: The importer should be an instance of SongImport - self.assertIsInstance(importer, BibleDB) + # THEN: The importer should be an instance of SongImport + self.assertIsInstance(importer, BibleDB) def file_import_test(self): """ Test the actual import of real song files """ # GIVEN: Test files with a mocked out "manager" and a mocked out "import_wizard" - #with patch('openlp.plugins.bibles.lib.zefania.BiblesResourcesDB') as brdb: - if True: + with patch('openlp.plugins.bibles.lib.zefania.ZefaniaBible.application') as brdb: for bible_file in ZEFANIA_TEST_DATA: mocked_manager = MagicMock() mocked_import_wizard = MagicMock() importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') importer.wizard = mocked_import_wizard - #importer.wizard.increment_progress_bar = MagicMock() importer.get_book_ref_id_by_name = MagicMock() - importer.get_book_ref_id_by_name.return_value = { 'id': 1, 'testament_id': 1, 'name': 'Genesis', - 'abbreviation': 'Gen', 'chapters': 1 } importer.create_verse = MagicMock() importer.create_book = MagicMock() importer.session = MagicMock() importer.get_language = MagicMock() importer.get_language.return_value = 'Danish' - importer.application.process_events = MagicMock() - # WHEN: Importing each file + # WHEN: Importing bible file importer.filename = os.path.join(TEST_PATH, bible_file) importer.do_import('Dansk Version') - # THEN: The create_verse() method should have been called + # 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 ZEFANIA_TEST_DATA['zefania-dk1933.xml']['verses']: + importer.create_verse.assert_any_call(importer.create_book().id, '1', verse_tag, verse_text) + From c0b167ca781cd2a024ff3953573c5acc26ed7580 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Wed, 21 May 2014 11:30:36 +0200 Subject: [PATCH 5/6] Added tests for import of OpenSong bible format --- .../bibles/test_opensongimport.py | 140 ++++++++++++++++++ .../bibles/test_zefaniaimport.py | 16 +- tests/resources/bibles/opensong-dk1933.xml | 46 ++++++ 3 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 tests/functional/openlp_plugins/bibles/test_opensongimport.py create mode 100644 tests/resources/bibles/opensong-dk1933.xml diff --git a/tests/functional/openlp_plugins/bibles/test_opensongimport.py b/tests/functional/openlp_plugins/bibles/test_opensongimport.py new file mode 100644 index 000000000..542cf289d --- /dev/null +++ b/tests/functional/openlp_plugins/bibles/test_opensongimport.py @@ -0,0 +1,140 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2014 Raoul Snyman # +# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan # +# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, # +# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. # +# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, # +# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, # +# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, # +# Frode Woldsund, Martin Zibricky, Patrick Zimmermann # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program is distributed in the hope that it will be useful, but WITHOUT # +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # +# more details. # +# # +# You should have received a copy of the GNU General Public License along # +# with this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### +""" +This module contains tests for the OpenSong Bible importer. +""" + +import os +from unittest import TestCase + +from tests.functional import MagicMock, patch +from openlp.plugins.bibles.lib.opensong import OpenSongBible +from openlp.plugins.bibles.lib.db import BibleDB + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), + '..', '..', '..', 'resources', 'bibles')) +OPENSONG_TEST_DATA = { + 'opensong-dk1933.xml': { + 'book': 'Genesis', + 'chapter': 1, + 'verses': [ + (1, 'I Begyndelsen skabte Gud Himmelen og Jorden.'), + (2, 'Og Jorden var øde og tom, og der var Mørke over Verdensdybet. ' + 'Men Guds Ånd svævede over Vandene.'), + (3, 'Og Gud sagde: "Der blive Lys!" Og der blev Lys.'), + (4, 'Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket,'), + (5, 'og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, ' + 'og det blev Morgen, første Dag.'), + (6, 'Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!"'), + (7, 'Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen ' + 'fra Vandet over Hvælvingen;'), + (8, 'og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag.'), + (9, 'Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" ' + 'Og således skete det;'), + (10, 'og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, ' + 'at det var godt.') + ] + } +} + + +class TestOpenSongImport(TestCase): + """ + Test the functions in the :mod:`opensongimport` module. + """ + + def setUp(self): + self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry') + self.registry_patcher.start() + self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager') + self.manager_patcher.start() + + def tearDown(self): + self.registry_patcher.stop() + self.manager_patcher.stop() + + def create_importer_test(self): + """ + Test creating an instance of the OpenSong file importer + """ + # GIVEN: A mocked out "manager" + mocked_manager = MagicMock() + + # WHEN: An importer object is created + importer = OpenSongBible(mocked_manager, path='.', name='.', filename='') + + # THEN: The importer should be an instance of BibleDB + self.assertIsInstance(importer, BibleDB) + + def file_import_test(self): + """ + Test the actual import of real song files + """ + # 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. + with patch('openlp.plugins.bibles.lib.opensong.OpenSongBible.application'): + for bible_file in OPENSONG_TEST_DATA: + mocked_manager = MagicMock() + mocked_import_wizard = MagicMock() + importer = OpenSongBible(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() + importer.get_language = MagicMock() + importer.get_language.return_value = 'Danish' + + # 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 OPENSONG_TEST_DATA[bible_file]['verses']: + importer.create_verse.assert_any_call(importer.create_book().id, 1, verse_tag, verse_text) + + def zefania_import_error_test(self): + """ + Test that we give an error message if trying to import a zefania bible + """ + # GIVEN: A mocked out "manager" and mocked out critical_error_message_box and an import + with patch('openlp.plugins.bibles.lib.opensong.critical_error_message_box') as \ + mocked_critical_error_message_box: + mocked_manager = MagicMock() + importer = OpenSongBible(mocked_manager, path='.', name='.', filename='') + + # WHEN: An trying to import a zefania bible + importer.filename = os.path.join(TEST_PATH, 'zefania-dk1933.xml') + importer.do_import() + + # THEN: The importer should have "shown" an error message + mocked_critical_error_message_box.assert_called_with(message='Incorrect Bible file type supplied. ' + 'This looks like a Zefania XML bible, ' + 'please use the Zefania import option.') diff --git a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py index 63e6ae40d..50307b401 100644 --- a/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py +++ b/tests/functional/openlp_plugins/bibles/test_zefaniaimport.py @@ -42,7 +42,7 @@ TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), ZEFANIA_TEST_DATA = { 'zefania-dk1933.xml': { 'book': 'Genesis', - 'chapter' : 1, + 'chapter': 1, 'verses': [ ('1', 'I Begyndelsen skabte Gud Himmelen og Jorden.'), ('2', 'Og Jorden var øde og tom, og der var Mørke over Verdensdybet. ' @@ -83,21 +83,22 @@ class TestZefaniaImport(TestCase): """ Test creating an instance of the Zefania file importer """ - # GIVEN: A mocked out BibleDB class, and a mocked out "manager" + # GIVEN: A mocked out "manager" mocked_manager = MagicMock() # WHEN: An importer object is created importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') - # THEN: The importer should be an instance of SongImport + # THEN: The importer should be an instance of BibleDB self.assertIsInstance(importer, BibleDB) def file_import_test(self): """ Test the actual import of real song files """ - # GIVEN: Test files with a mocked out "manager" and a mocked out "import_wizard" - with patch('openlp.plugins.bibles.lib.zefania.ZefaniaBible.application') as brdb: + # 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. + with patch('openlp.plugins.bibles.lib.zefania.ZefaniaBible.application'): for bible_file in ZEFANIA_TEST_DATA: mocked_manager = MagicMock() mocked_import_wizard = MagicMock() @@ -112,10 +113,9 @@ class TestZefaniaImport(TestCase): # WHEN: Importing bible file importer.filename = os.path.join(TEST_PATH, bible_file) - importer.do_import('Dansk Version') + 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 ZEFANIA_TEST_DATA['zefania-dk1933.xml']['verses']: + for verse_tag, verse_text in ZEFANIA_TEST_DATA[bible_file]['verses']: importer.create_verse.assert_any_call(importer.create_book().id, '1', verse_tag, verse_text) - diff --git a/tests/resources/bibles/opensong-dk1933.xml b/tests/resources/bibles/opensong-dk1933.xml new file mode 100644 index 000000000..1aa1140f7 --- /dev/null +++ b/tests/resources/bibles/opensong-dk1933.xml @@ -0,0 +1,46 @@ + + + + + + + + + Danish Version + + + The Holy Bible + The electronic edition of this Bible comes from the Danish 1933 edition. The Old Testament is an update from the 1931 edition, and the New Testament is an update from the 1907 edition. + Free Bible Software Group + + The Unbound Bible + Biola University: Administrative Computing + 13800 Biola Ave. + La Mirada, CA 90639 + United States of America + 562-903-4722 + + 2009-01-20 + Zefania XML Bible Markup Language + DAN + ftp://unboundftp.biola.edu/pub/danish1933_utf8.zip + DAN + provide the bible to the world + + + + + + I Begyndelsen skabte Gud Himmelen og Jorden. + Og Jorden var øde og tom, og der var Mørke over Verdensdybet. Men Guds Ånd svævede over Vandene. + Og Gud sagde: "Der blive Lys!" Og der blev Lys. + Og Gud så, at Lyset var godt, og Gud satte Skel mellem Lyset og Mørket, + og Gud kaldte Lyset Dag, og Mørket kaldte han Nat. Og det blev Aften, og det blev Morgen, første Dag. + Derpå sagde Gud: "Der blive en Hvælving midt i Vandene til at skille Vandene ad!" + Og således skete det: Gud gjorde Hvælvingen og skilte Vandet under Hvælvingen fra Vandet over Hvælvingen; + og Gud kaldte Hvælvingen Himmel. Og det blev Aften, og det blev Morgen, anden Dag. + Derpå sagde Gud: "Vandet under Himmelen samle sig på eet Sted, så det faste Land kommer til Syne!" Og således skete det; + og Gud kaldte det faste Land Jord, og Stedet, hvor Vandet samlede sig, kaldte han Hav. Og Gud så, at det var godt. + + + From b352c05ff065aa3844fb7207d67d7c20e7e07e60 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Wed, 21 May 2014 11:33:23 +0200 Subject: [PATCH 6/6] Fix spelling in a comment --- openlp/plugins/songs/lib/ewimport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index b08193672..42f375e4b 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -344,7 +344,7 @@ class EasyWorshipSongImport(SongImport): try: decoded_words = words.decode() except UnicodeDecodeError: - # The unicode chars in the rtf was not escaped in the expected manor + # The unicode chars in the rtf was not escaped in the expected manner self.entry_error_log = translate('SongsPlugin.EasyWorshipSongImport', 'Unexpected data formatting.') return