This commit is contained in:
Raoul Snyman 2017-11-15 20:52:11 -07:00
commit 0bef29758c
19 changed files with 310 additions and 395 deletions

View File

@ -23,7 +23,6 @@
The bible import functions for OpenLP The bible import functions for OpenLP
""" """
import logging import logging
import os
import urllib.error import urllib.error
from lxml import etree from lxml import etree
@ -41,6 +40,7 @@ from openlp.core.common.settings import Settings
from openlp.core.lib.db import delete_database from openlp.core.lib.db import delete_database
from openlp.core.lib.exceptions import ValidationError from openlp.core.lib.exceptions import ValidationError
from openlp.core.lib.ui import critical_error_message_box from openlp.core.lib.ui import critical_error_message_box
from openlp.core.widgets.edits import PathEdit
from openlp.core.widgets.wizard import OpenLPWizard, WizardStrings from openlp.core.widgets.wizard import OpenLPWizard, WizardStrings
from openlp.plugins.bibles.lib.db import clean_filename from openlp.plugins.bibles.lib.db import clean_filename
from openlp.plugins.bibles.lib.importers.http import CWExtract, BGExtract, BSExtract from openlp.plugins.bibles.lib.importers.http import CWExtract, BGExtract, BSExtract
@ -122,15 +122,9 @@ class BibleImportForm(OpenLPWizard):
Set up the signals used in the bible importer. Set up the signals used in the bible importer.
""" """
self.web_source_combo_box.currentIndexChanged.connect(self.on_web_source_combo_box_index_changed) self.web_source_combo_box.currentIndexChanged.connect(self.on_web_source_combo_box_index_changed)
self.osis_browse_button.clicked.connect(self.on_osis_browse_button_clicked)
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)
self.wordproject_browse_button.clicked.connect(self.on_wordproject_browse_button_clicked)
self.web_update_button.clicked.connect(self.on_web_update_button_clicked) self.web_update_button.clicked.connect(self.on_web_update_button_clicked)
self.sword_browse_button.clicked.connect(self.on_sword_browse_button_clicked) self.sword_folder_path_edit.pathChanged.connect(self.on_sword_folder_path_edit_path_changed)
self.sword_zipbrowse_button.clicked.connect(self.on_sword_zipbrowse_button_clicked) self.sword_zipfile_path_edit.pathChanged.connect(self.on_sword_zipfile_path_edit_path_changed)
def add_custom_pages(self): def add_custom_pages(self):
""" """
@ -161,17 +155,12 @@ class BibleImportForm(OpenLPWizard):
self.osis_layout.setObjectName('OsisLayout') self.osis_layout.setObjectName('OsisLayout')
self.osis_file_label = QtWidgets.QLabel(self.osis_widget) self.osis_file_label = QtWidgets.QLabel(self.osis_widget)
self.osis_file_label.setObjectName('OsisFileLabel') self.osis_file_label.setObjectName('OsisFileLabel')
self.osis_file_layout = QtWidgets.QHBoxLayout() self.osis_path_edit = PathEdit(
self.osis_file_layout.setObjectName('OsisFileLayout') self.osis_widget,
self.osis_file_edit = QtWidgets.QLineEdit(self.osis_widget) default_path=Settings().value('bibles/last directory import'),
self.osis_file_edit.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred) dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.OSIS),
self.osis_file_edit.setObjectName('OsisFileEdit') show_revert=False)
self.osis_file_layout.addWidget(self.osis_file_edit) self.osis_layout.addRow(self.osis_file_label, self.osis_path_edit)
self.osis_browse_button = QtWidgets.QToolButton(self.osis_widget)
self.osis_browse_button.setIcon(self.open_icon)
self.osis_browse_button.setObjectName('OsisBrowseButton')
self.osis_file_layout.addWidget(self.osis_browse_button)
self.osis_layout.addRow(self.osis_file_label, self.osis_file_layout)
self.osis_layout.setItem(1, QtWidgets.QFormLayout.LabelRole, self.spacer) self.osis_layout.setItem(1, QtWidgets.QFormLayout.LabelRole, self.spacer)
self.select_stack.addWidget(self.osis_widget) self.select_stack.addWidget(self.osis_widget)
self.csv_widget = QtWidgets.QWidget(self.select_page) self.csv_widget = QtWidgets.QWidget(self.select_page)
@ -181,30 +170,27 @@ class BibleImportForm(OpenLPWizard):
self.csv_layout.setObjectName('CsvLayout') self.csv_layout.setObjectName('CsvLayout')
self.csv_books_label = QtWidgets.QLabel(self.csv_widget) self.csv_books_label = QtWidgets.QLabel(self.csv_widget)
self.csv_books_label.setObjectName('CsvBooksLabel') self.csv_books_label.setObjectName('CsvBooksLabel')
self.csv_books_layout = QtWidgets.QHBoxLayout() self.csv_books_path_edit = PathEdit(
self.csv_books_layout.setObjectName('CsvBooksLayout') self.csv_widget,
self.csv_books_edit = QtWidgets.QLineEdit(self.csv_widget) default_path=Settings().value('bibles/last directory import'),
self.csv_books_edit.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred) dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.CSV),
self.csv_books_edit.setObjectName('CsvBooksEdit') show_revert=False,
self.csv_books_layout.addWidget(self.csv_books_edit) )
self.csv_books_button = QtWidgets.QToolButton(self.csv_widget) self.csv_books_path_edit.filters = \
self.csv_books_button.setIcon(self.open_icon) '{name} (*.csv)'.format(name=translate('BiblesPlugin.ImportWizardForm', 'CSV File'))
self.csv_books_button.setObjectName('CsvBooksButton') self.csv_layout.addRow(self.csv_books_label, self.csv_books_path_edit)
self.csv_books_layout.addWidget(self.csv_books_button)
self.csv_layout.addRow(self.csv_books_label, self.csv_books_layout)
self.csv_verses_label = QtWidgets.QLabel(self.csv_widget) self.csv_verses_label = QtWidgets.QLabel(self.csv_widget)
self.csv_verses_label.setObjectName('CsvVersesLabel') self.csv_verses_label.setObjectName('CsvVersesLabel')
self.csv_verses_layout = QtWidgets.QHBoxLayout() self.csv_verses_path_edit = PathEdit(
self.csv_verses_layout.setObjectName('CsvVersesLayout') self.csv_widget,
self.csv_verses_edit = QtWidgets.QLineEdit(self.csv_widget) default_path=Settings().value('bibles/last directory import'),
self.csv_verses_edit.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred) dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.CSV),
self.csv_verses_edit.setObjectName('CsvVersesEdit') show_revert=False,
self.csv_verses_layout.addWidget(self.csv_verses_edit) )
self.csv_verses_button = QtWidgets.QToolButton(self.csv_widget) self.csv_verses_path_edit.filters = \
self.csv_verses_button.setIcon(self.open_icon) '{name} (*.csv)'.format(name=translate('BiblesPlugin.ImportWizardForm', 'CSV File'))
self.csv_verses_button.setObjectName('CsvVersesButton') self.csv_layout.addRow(self.csv_books_label, self.csv_books_path_edit)
self.csv_verses_layout.addWidget(self.csv_verses_button) self.csv_layout.addRow(self.csv_verses_label, self.csv_verses_path_edit)
self.csv_layout.addRow(self.csv_verses_label, self.csv_verses_layout)
self.csv_layout.setItem(3, QtWidgets.QFormLayout.LabelRole, self.spacer) self.csv_layout.setItem(3, QtWidgets.QFormLayout.LabelRole, self.spacer)
self.select_stack.addWidget(self.csv_widget) self.select_stack.addWidget(self.csv_widget)
self.open_song_widget = QtWidgets.QWidget(self.select_page) self.open_song_widget = QtWidgets.QWidget(self.select_page)
@ -214,17 +200,13 @@ class BibleImportForm(OpenLPWizard):
self.open_song_layout.setObjectName('OpenSongLayout') self.open_song_layout.setObjectName('OpenSongLayout')
self.open_song_file_label = QtWidgets.QLabel(self.open_song_widget) self.open_song_file_label = QtWidgets.QLabel(self.open_song_widget)
self.open_song_file_label.setObjectName('OpenSongFileLabel') self.open_song_file_label.setObjectName('OpenSongFileLabel')
self.open_song_file_layout = QtWidgets.QHBoxLayout() self.open_song_path_edit = PathEdit(
self.open_song_file_layout.setObjectName('OpenSongFileLayout') self.open_song_widget,
self.open_song_file_edit = QtWidgets.QLineEdit(self.open_song_widget) default_path=Settings().value('bibles/last directory import'),
self.open_song_file_edit.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred) dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.OS),
self.open_song_file_edit.setObjectName('OpenSongFileEdit') show_revert=False,
self.open_song_file_layout.addWidget(self.open_song_file_edit) )
self.open_song_browse_button = QtWidgets.QToolButton(self.open_song_widget) self.open_song_layout.addRow(self.open_song_file_label, self.open_song_path_edit)
self.open_song_browse_button.setIcon(self.open_icon)
self.open_song_browse_button.setObjectName('OpenSongBrowseButton')
self.open_song_file_layout.addWidget(self.open_song_browse_button)
self.open_song_layout.addRow(self.open_song_file_label, self.open_song_file_layout)
self.open_song_layout.setItem(1, QtWidgets.QFormLayout.LabelRole, self.spacer) self.open_song_layout.setItem(1, QtWidgets.QFormLayout.LabelRole, self.spacer)
self.select_stack.addWidget(self.open_song_widget) self.select_stack.addWidget(self.open_song_widget)
self.web_tab_widget = QtWidgets.QTabWidget(self.select_page) self.web_tab_widget = QtWidgets.QTabWidget(self.select_page)
@ -292,17 +274,14 @@ class BibleImportForm(OpenLPWizard):
self.zefania_layout.setObjectName('ZefaniaLayout') self.zefania_layout.setObjectName('ZefaniaLayout')
self.zefania_file_label = QtWidgets.QLabel(self.zefania_widget) self.zefania_file_label = QtWidgets.QLabel(self.zefania_widget)
self.zefania_file_label.setObjectName('ZefaniaFileLabel') self.zefania_file_label.setObjectName('ZefaniaFileLabel')
self.zefania_file_layout = QtWidgets.QHBoxLayout() self.zefania_path_edit = PathEdit(
self.zefania_file_layout.setObjectName('ZefaniaFileLayout') self.zefania_widget,
self.zefania_file_edit = QtWidgets.QLineEdit(self.zefania_widget) default_path=Settings().value('bibles/last directory import'),
self.zefania_file_edit.setObjectName('ZefaniaFileEdit') dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.ZEF),
self.zefania_file_layout.addWidget(self.zefania_file_edit) show_revert=False,
self.zefania_browse_button = QtWidgets.QToolButton(self.zefania_widget) )
self.zefania_browse_button.setIcon(self.open_icon) self.zefania_layout.addRow(self.zefania_file_label, self.zefania_path_edit)
self.zefania_browse_button.setObjectName('ZefaniaBrowseButton') self.zefania_layout.setItem(2, QtWidgets.QFormLayout.LabelRole, self.spacer)
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, QtWidgets.QFormLayout.LabelRole, self.spacer)
self.select_stack.addWidget(self.zefania_widget) self.select_stack.addWidget(self.zefania_widget)
self.sword_widget = QtWidgets.QWidget(self.select_page) self.sword_widget = QtWidgets.QWidget(self.select_page)
self.sword_widget.setObjectName('SwordWidget') self.sword_widget.setObjectName('SwordWidget')
@ -318,16 +297,13 @@ class BibleImportForm(OpenLPWizard):
self.sword_folder_label = QtWidgets.QLabel(self.sword_folder_tab) self.sword_folder_label = QtWidgets.QLabel(self.sword_folder_tab)
self.sword_folder_label.setObjectName('SwordSourceLabel') self.sword_folder_label.setObjectName('SwordSourceLabel')
self.sword_folder_label.setObjectName('SwordFolderLabel') self.sword_folder_label.setObjectName('SwordFolderLabel')
self.sword_folder_edit = QtWidgets.QLineEdit(self.sword_folder_tab) self.sword_folder_path_edit = PathEdit(
self.sword_folder_edit.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred) self.sword_folder_tab,
self.sword_folder_edit.setObjectName('SwordFolderEdit') default_path=Settings().value('bibles/last directory import'),
self.sword_browse_button = QtWidgets.QToolButton(self.sword_folder_tab) dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.SWORD),
self.sword_browse_button.setIcon(self.open_icon) show_revert=False,
self.sword_browse_button.setObjectName('SwordBrowseButton') )
self.sword_folder_layout = QtWidgets.QHBoxLayout() self.sword_folder_tab_layout.addRow(self.sword_folder_label, self.sword_folder_path_edit)
self.sword_folder_layout.addWidget(self.sword_folder_edit)
self.sword_folder_layout.addWidget(self.sword_browse_button)
self.sword_folder_tab_layout.addRow(self.sword_folder_label, self.sword_folder_layout)
self.sword_bible_label = QtWidgets.QLabel(self.sword_folder_tab) self.sword_bible_label = QtWidgets.QLabel(self.sword_folder_tab)
self.sword_bible_label.setObjectName('SwordBibleLabel') self.sword_bible_label.setObjectName('SwordBibleLabel')
self.sword_bible_combo_box = QtWidgets.QComboBox(self.sword_folder_tab) self.sword_bible_combo_box = QtWidgets.QComboBox(self.sword_folder_tab)
@ -342,16 +318,13 @@ class BibleImportForm(OpenLPWizard):
self.sword_zip_layout.setObjectName('SwordZipLayout') self.sword_zip_layout.setObjectName('SwordZipLayout')
self.sword_zipfile_label = QtWidgets.QLabel(self.sword_zip_tab) self.sword_zipfile_label = QtWidgets.QLabel(self.sword_zip_tab)
self.sword_zipfile_label.setObjectName('SwordZipFileLabel') self.sword_zipfile_label.setObjectName('SwordZipFileLabel')
self.sword_zipfile_edit = QtWidgets.QLineEdit(self.sword_zip_tab) self.sword_zipfile_path_edit = PathEdit(
self.sword_zipfile_edit.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred) self.sword_zip_tab,
self.sword_zipfile_edit.setObjectName('SwordZipFileEdit') default_path=Settings().value('bibles/last directory import'),
self.sword_zipbrowse_button = QtWidgets.QToolButton(self.sword_zip_tab) dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.SWORD),
self.sword_zipbrowse_button.setIcon(self.open_icon) show_revert=False,
self.sword_zipbrowse_button.setObjectName('SwordZipBrowseButton') )
self.sword_zipfile_layout = QtWidgets.QHBoxLayout() self.sword_zip_layout.addRow(self.sword_zipfile_label, self.sword_zipfile_path_edit)
self.sword_zipfile_layout.addWidget(self.sword_zipfile_edit)
self.sword_zipfile_layout.addWidget(self.sword_zipbrowse_button)
self.sword_zip_layout.addRow(self.sword_zipfile_label, self.sword_zipfile_layout)
self.sword_zipbible_label = QtWidgets.QLabel(self.sword_folder_tab) self.sword_zipbible_label = QtWidgets.QLabel(self.sword_folder_tab)
self.sword_zipbible_label.setObjectName('SwordZipBibleLabel') self.sword_zipbible_label.setObjectName('SwordZipBibleLabel')
self.sword_zipbible_combo_box = QtWidgets.QComboBox(self.sword_zip_tab) self.sword_zipbible_combo_box = QtWidgets.QComboBox(self.sword_zip_tab)
@ -372,18 +345,13 @@ class BibleImportForm(OpenLPWizard):
self.wordproject_layout.setObjectName('WordProjectLayout') self.wordproject_layout.setObjectName('WordProjectLayout')
self.wordproject_file_label = QtWidgets.QLabel(self.wordproject_widget) self.wordproject_file_label = QtWidgets.QLabel(self.wordproject_widget)
self.wordproject_file_label.setObjectName('WordProjectFileLabel') self.wordproject_file_label.setObjectName('WordProjectFileLabel')
self.wordproject_file_layout = QtWidgets.QHBoxLayout() self.wordproject_path_edit = PathEdit(
self.wordproject_file_layout.setObjectName('WordProjectFileLayout') self.wordproject_widget,
self.wordproject_file_edit = QtWidgets.QLineEdit(self.wordproject_widget) default_path=Settings().value('bibles/last directory import'),
self.wordproject_file_edit.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred) dialog_caption=WizardStrings.OpenTypeFile.format(file_type=WizardStrings.WordProject),
self.wordproject_file_edit.setObjectName('WordProjectFileEdit') show_revert=False)
self.wordproject_file_layout.addWidget(self.wordproject_file_edit) self.wordproject_layout.addRow(self.wordproject_file_label, self.wordproject_path_edit)
self.wordproject_browse_button = QtWidgets.QToolButton(self.wordproject_widget) self.wordproject_layout.setItem(1, QtWidgets.QFormLayout.LabelRole, self.spacer)
self.wordproject_browse_button.setIcon(self.open_icon)
self.wordproject_browse_button.setObjectName('WordProjectBrowseButton')
self.wordproject_file_layout.addWidget(self.wordproject_browse_button)
self.wordproject_layout.addRow(self.wordproject_file_label, self.wordproject_file_layout)
self.wordproject_layout.setItem(5, QtWidgets.QFormLayout.LabelRole, self.spacer)
self.select_stack.addWidget(self.wordproject_widget) self.select_stack.addWidget(self.wordproject_widget)
self.select_page_layout.addLayout(self.select_stack) self.select_page_layout.addLayout(self.select_stack)
self.addPage(self.select_page) self.addPage(self.select_page)
@ -468,8 +436,6 @@ class BibleImportForm(OpenLPWizard):
self.sword_bible_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bibles:')) self.sword_bible_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bibles:'))
self.sword_folder_label.setText(translate('BiblesPlugin.ImportWizardForm', 'SWORD data folder:')) self.sword_folder_label.setText(translate('BiblesPlugin.ImportWizardForm', 'SWORD data folder:'))
self.sword_zipfile_label.setText(translate('BiblesPlugin.ImportWizardForm', 'SWORD zip-file:')) self.sword_zipfile_label.setText(translate('BiblesPlugin.ImportWizardForm', 'SWORD zip-file:'))
self.sword_folder_edit.setPlaceholderText(translate('BiblesPlugin.ImportWizardForm',
'Defaults to the standard SWORD data folder'))
self.sword_zipbible_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bibles:')) self.sword_zipbible_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bibles:'))
self.sword_tab_widget.setTabText(self.sword_tab_widget.indexOf(self.sword_folder_tab), self.sword_tab_widget.setTabText(self.sword_tab_widget.indexOf(self.sword_folder_tab),
translate('BiblesPlugin.ImportWizardForm', 'Import from folder')) translate('BiblesPlugin.ImportWizardForm', 'Import from folder'))
@ -518,7 +484,7 @@ class BibleImportForm(OpenLPWizard):
if self.field('source_format') == BibleFormat.OSIS: if self.field('source_format') == BibleFormat.OSIS:
if not self.field('osis_location'): if not self.field('osis_location'):
critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.OSIS) critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.OSIS)
self.osis_file_edit.setFocus() self.osis_path_edit.setFocus()
return False return False
elif self.field('source_format') == BibleFormat.CSV: elif self.field('source_format') == BibleFormat.CSV:
if not self.field('csv_booksfile'): if not self.field('csv_booksfile'):
@ -538,18 +504,18 @@ class BibleImportForm(OpenLPWizard):
elif self.field('source_format') == BibleFormat.OpenSong: elif self.field('source_format') == BibleFormat.OpenSong:
if not self.field('opensong_file'): if not self.field('opensong_file'):
critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.OS) critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.OS)
self.open_song_file_edit.setFocus() self.open_song_path_edit.setFocus()
return False return False
elif self.field('source_format') == BibleFormat.Zefania: elif self.field('source_format') == BibleFormat.Zefania:
if not self.field('zefania_file'): if not self.field('zefania_file'):
critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.ZEF) critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.ZEF)
self.zefania_file_edit.setFocus() self.zefania_path_edit.setFocus()
return False return False
elif self.field('source_format') == BibleFormat.WordProject: elif self.field('source_format') == BibleFormat.WordProject:
if not self.field('wordproject_file'): if not self.field('wordproject_file'):
critical_error_message_box(UiStrings().NFSs, critical_error_message_box(UiStrings().NFSs,
WizardStrings.YouSpecifyFile % WizardStrings.WordProject) WizardStrings.YouSpecifyFile % WizardStrings.WordProject)
self.wordproject_file_edit.setFocus() self.wordproject_path_edit.setFocus()
return False return False
elif self.field('source_format') == BibleFormat.WebDownload: elif self.field('source_format') == BibleFormat.WebDownload:
# If count is 0 the bible list has not yet been downloaded # If count is 0 the bible list has not yet been downloaded
@ -563,7 +529,7 @@ class BibleImportForm(OpenLPWizard):
if not self.field('sword_folder_path') and self.sword_bible_combo_box.count() == 0: if not self.field('sword_folder_path') and self.sword_bible_combo_box.count() == 0:
critical_error_message_box(UiStrings().NFSs, critical_error_message_box(UiStrings().NFSs,
WizardStrings.YouSpecifyFolder % WizardStrings.SWORD) WizardStrings.YouSpecifyFolder % WizardStrings.SWORD)
self.sword_folder_edit.setFocus() self.sword_folder_path_edit.setFocus()
return False return False
key = self.sword_bible_combo_box.itemData(self.sword_bible_combo_box.currentIndex()) key = self.sword_bible_combo_box.itemData(self.sword_bible_combo_box.currentIndex())
if 'description' in self.pysword_folder_modules_json[key]: if 'description' in self.pysword_folder_modules_json[key]:
@ -575,7 +541,7 @@ class BibleImportForm(OpenLPWizard):
elif self.sword_tab_widget.currentIndex() == self.sword_tab_widget.indexOf(self.sword_zip_tab): elif self.sword_tab_widget.currentIndex() == self.sword_tab_widget.indexOf(self.sword_zip_tab):
if not self.field('sword_zip_path'): if not self.field('sword_zip_path'):
critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.SWORD) critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.SWORD)
self.sword_zipfile_edit.setFocus() self.sword_zipfile_path_edit.setFocus()
return False return False
key = self.sword_zipbible_combo_box.itemData(self.sword_zipbible_combo_box.currentIndex()) key = self.sword_zipbible_combo_box.itemData(self.sword_zipbible_combo_box.currentIndex())
if 'description' in self.pysword_zip_modules_json[key]: if 'description' in self.pysword_zip_modules_json[key]:
@ -586,7 +552,6 @@ class BibleImportForm(OpenLPWizard):
elif self.currentPage() == self.license_details_page: elif self.currentPage() == self.license_details_page:
license_version = self.field('license_version') license_version = self.field('license_version')
license_copyright = self.field('license_copyright') license_copyright = self.field('license_copyright')
path = str(AppLocation.get_section_data_path('bibles'))
if not license_version: if not license_version:
critical_error_message_box( critical_error_message_box(
UiStrings().EmptyField, UiStrings().EmptyField,
@ -608,7 +573,7 @@ class BibleImportForm(OpenLPWizard):
'existing one.')) 'existing one.'))
self.version_name_edit.setFocus() self.version_name_edit.setFocus()
return False return False
elif os.path.exists(os.path.join(path, clean_filename(license_version))): elif (AppLocation.get_section_data_path('bibles') / clean_filename(license_version)).exists():
critical_error_message_box( critical_error_message_box(
translate('BiblesPlugin.ImportWizardForm', 'Bible Exists'), translate('BiblesPlugin.ImportWizardForm', 'Bible Exists'),
translate('BiblesPlugin.ImportWizardForm', 'This Bible already exists. Please import ' translate('BiblesPlugin.ImportWizardForm', 'This Bible already exists. Please import '
@ -631,57 +596,6 @@ class BibleImportForm(OpenLPWizard):
bibles.sort(key=get_locale_key) bibles.sort(key=get_locale_key)
self.web_translation_combo_box.addItems(bibles) self.web_translation_combo_box.addItems(bibles)
def on_osis_browse_button_clicked(self):
"""
Show the file open dialog for the OSIS file.
"""
self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.OSIS, self.osis_file_edit,
'last directory import')
def on_csv_books_browse_button_clicked(self):
"""
Show the file open dialog for the books CSV file.
"""
# TODO: Verify format() with varible template
self.get_file_name(
WizardStrings.OpenTypeFile % WizardStrings.CSV,
self.csv_books_edit,
'last directory import',
'{name} (*.csv)'.format(name=translate('BiblesPlugin.ImportWizardForm', 'CSV File')))
def on_csv_verses_browse_button_clicked(self):
"""
Show the file open dialog for the verses CSV file.
"""
# TODO: Verify format() with variable template
self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.CSV, self.csv_verses_edit,
'last directory import',
'{name} (*.csv)'.format(name=translate('BiblesPlugin.ImportWizardForm', 'CSV File')))
def on_open_song_browse_button_clicked(self):
"""
Show the file open dialog for the OpenSong file.
"""
# TODO: Verify format() with variable template
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.
"""
# TODO: Verify format() with variable template
self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.ZEF, self.zefania_file_edit,
'last directory import')
def on_wordproject_browse_button_clicked(self):
"""
Show the file open dialog for the WordProject file.
"""
# TODO: Verify format() with variable template
self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.WordProject, self.wordproject_file_edit,
'last directory import')
def on_web_update_button_clicked(self): def on_web_update_button_clicked(self):
""" """
Download list of bibles from Crosswalk, BibleServer and BibleGateway. Download list of bibles from Crosswalk, BibleServer and BibleGateway.
@ -718,15 +632,13 @@ class BibleImportForm(OpenLPWizard):
self.web_update_button.setEnabled(True) self.web_update_button.setEnabled(True)
self.web_progress_bar.setVisible(False) self.web_progress_bar.setVisible(False)
def on_sword_browse_button_clicked(self): def on_sword_folder_path_edit_path_changed(self, new_path):
""" """
Show the file open dialog for the SWORD folder. Show the file open dialog for the SWORD folder.
""" """
self.get_folder(WizardStrings.OpenTypeFolder % WizardStrings.SWORD, self.sword_folder_edit, if new_path:
'last directory import')
if self.sword_folder_edit.text():
try: try:
self.pysword_folder_modules = modules.SwordModules(self.sword_folder_edit.text()) self.pysword_folder_modules = modules.SwordModules(str(new_path))
self.pysword_folder_modules_json = self.pysword_folder_modules.parse_modules() self.pysword_folder_modules_json = self.pysword_folder_modules.parse_modules()
bible_keys = self.pysword_folder_modules_json.keys() bible_keys = self.pysword_folder_modules_json.keys()
self.sword_bible_combo_box.clear() self.sword_bible_combo_box.clear()
@ -735,15 +647,13 @@ class BibleImportForm(OpenLPWizard):
except: except:
self.sword_bible_combo_box.clear() self.sword_bible_combo_box.clear()
def on_sword_zipbrowse_button_clicked(self): def on_sword_zipfile_path_edit_path_changed(self, new_path):
""" """
Show the file open dialog for a SWORD zip-file. Show the file open dialog for a SWORD zip-file.
""" """
self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.SWORD, self.sword_zipfile_edit, if new_path:
'last directory import')
if self.sword_zipfile_edit.text():
try: try:
self.pysword_zip_modules = modules.SwordModules(self.sword_zipfile_edit.text()) self.pysword_zip_modules = modules.SwordModules(str(new_path))
self.pysword_zip_modules_json = self.pysword_zip_modules.parse_modules() self.pysword_zip_modules_json = self.pysword_zip_modules.parse_modules()
bible_keys = self.pysword_zip_modules_json.keys() bible_keys = self.pysword_zip_modules_json.keys()
self.sword_zipbible_combo_box.clear() self.sword_zipbible_combo_box.clear()
@ -757,16 +667,16 @@ class BibleImportForm(OpenLPWizard):
Register the bible import wizard fields. Register the bible import wizard fields.
""" """
self.select_page.registerField('source_format', self.format_combo_box) self.select_page.registerField('source_format', self.format_combo_box)
self.select_page.registerField('osis_location', self.osis_file_edit) self.select_page.registerField('osis_location', self.osis_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('csv_booksfile', self.csv_books_edit) self.select_page.registerField('csv_booksfile', self.csv_books_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('csv_versefile', self.csv_verses_edit) self.select_page.registerField('csv_versefile', self.csv_verses_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('opensong_file', self.open_song_file_edit) self.select_page.registerField('opensong_file', self.open_song_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('zefania_file', self.zefania_file_edit) self.select_page.registerField('zefania_file', self.zefania_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('wordproject_file', self.wordproject_file_edit) self.select_page.registerField('wordproject_file', self.wordproject_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('web_location', self.web_source_combo_box) 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('web_biblename', self.web_translation_combo_box)
self.select_page.registerField('sword_folder_path', self.sword_folder_edit) self.select_page.registerField('sword_folder_path', self.sword_folder_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('sword_zip_path', self.sword_zipfile_edit) self.select_page.registerField('sword_zip_path', self.sword_zipfile_path_edit, 'path', PathEdit.pathChanged)
self.select_page.registerField('proxy_server', self.web_server_edit) self.select_page.registerField('proxy_server', self.web_server_edit)
self.select_page.registerField('proxy_username', self.web_user_edit) self.select_page.registerField('proxy_username', self.web_user_edit)
self.select_page.registerField('proxy_password', self.web_password_edit) self.select_page.registerField('proxy_password', self.web_password_edit)
@ -785,13 +695,13 @@ class BibleImportForm(OpenLPWizard):
self.finish_button.setVisible(False) self.finish_button.setVisible(False)
self.cancel_button.setVisible(True) self.cancel_button.setVisible(True)
self.setField('source_format', 0) self.setField('source_format', 0)
self.setField('osis_location', '') self.setField('osis_location', None)
self.setField('csv_booksfile', '') self.setField('csv_booksfile', None)
self.setField('csv_versefile', '') self.setField('csv_versefile', None)
self.setField('opensong_file', '') self.setField('opensong_file', None)
self.setField('zefania_file', '') self.setField('zefania_file', None)
self.setField('sword_folder_path', '') self.setField('sword_folder_path', None)
self.setField('sword_zip_path', '') self.setField('sword_zip_path', None)
self.setField('web_location', WebDownload.Crosswalk) self.setField('web_location', WebDownload.Crosswalk)
self.setField('web_biblename', self.web_translation_combo_box.currentIndex()) self.setField('web_biblename', self.web_translation_combo_box.currentIndex())
self.setField('proxy_server', settings.value('proxy address')) self.setField('proxy_server', settings.value('proxy address'))
@ -833,16 +743,16 @@ class BibleImportForm(OpenLPWizard):
if bible_type == BibleFormat.OSIS: if bible_type == BibleFormat.OSIS:
# Import an OSIS bible. # Import an OSIS bible.
importer = self.manager.import_bible(BibleFormat.OSIS, name=license_version, importer = self.manager.import_bible(BibleFormat.OSIS, name=license_version,
filename=self.field('osis_location')) file_path=self.field('osis_location'))
elif bible_type == BibleFormat.CSV: elif bible_type == BibleFormat.CSV:
# Import a CSV bible. # Import a CSV bible.
importer = self.manager.import_bible(BibleFormat.CSV, name=license_version, importer = self.manager.import_bible(BibleFormat.CSV, name=license_version,
booksfile=self.field('csv_booksfile'), books_path=self.field('csv_booksfile'),
versefile=self.field('csv_versefile')) verse_path=self.field('csv_versefile'))
elif bible_type == BibleFormat.OpenSong: elif bible_type == BibleFormat.OpenSong:
# Import an OpenSong bible. # Import an OpenSong bible.
importer = self.manager.import_bible(BibleFormat.OpenSong, name=license_version, importer = self.manager.import_bible(BibleFormat.OpenSong, name=license_version,
filename=self.field('opensong_file')) file_path=self.field('opensong_file'))
elif bible_type == BibleFormat.WebDownload: elif bible_type == BibleFormat.WebDownload:
# Import a bible from the web. # Import a bible from the web.
self.progress_bar.setMaximum(1) self.progress_bar.setMaximum(1)
@ -861,11 +771,11 @@ class BibleImportForm(OpenLPWizard):
elif bible_type == BibleFormat.Zefania: elif bible_type == BibleFormat.Zefania:
# Import a Zefania bible. # Import a Zefania bible.
importer = self.manager.import_bible(BibleFormat.Zefania, name=license_version, importer = self.manager.import_bible(BibleFormat.Zefania, name=license_version,
filename=self.field('zefania_file')) file_path=self.field('zefania_file'))
elif bible_type == BibleFormat.WordProject: elif bible_type == BibleFormat.WordProject:
# Import a WordProject bible. # Import a WordProject bible.
importer = self.manager.import_bible(BibleFormat.WordProject, name=license_version, importer = self.manager.import_bible(BibleFormat.WordProject, name=license_version,
filename=self.field('wordproject_file')) file_path=self.field('wordproject_file'))
elif bible_type == BibleFormat.SWORD: elif bible_type == BibleFormat.SWORD:
# Import a SWORD bible. # Import a SWORD bible.
if self.sword_tab_widget.currentIndex() == self.sword_tab_widget.indexOf(self.sword_folder_tab): if self.sword_tab_widget.currentIndex() == self.sword_tab_widget.indexOf(self.sword_folder_tab):

View File

@ -37,23 +37,23 @@ class BibleImport(BibleDB, LogMixin, RegistryProperties):
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.filename = kwargs['filename'] if 'filename' in kwargs else None self.file_path = kwargs.get('file_path')
self.wizard = None self.wizard = None
self.stop_import_flag = False self.stop_import_flag = False
Registry().register_function('openlp_stop_wizard', self.stop_import) Registry().register_function('openlp_stop_wizard', self.stop_import)
@staticmethod @staticmethod
def is_compressed(file): def is_compressed(file_path):
""" """
Check if the supplied file is compressed Check if the supplied file is compressed
:param file: A path to the file to check :param file_path: A path to the file to check
""" """
if is_zipfile(file): if is_zipfile(str(file_path)):
critical_error_message_box( critical_error_message_box(
message=translate('BiblesPlugin.BibleImport', message=translate('BiblesPlugin.BibleImport',
'The file "{file}" you supplied is compressed. You must decompress it before import.' 'The file "{file}" you supplied is compressed. You must decompress it before import.'
).format(file=file)) ).format(file=file_path))
return True return True
return False return False
@ -143,24 +143,24 @@ class BibleImport(BibleDB, LogMixin, RegistryProperties):
self.log_debug('No book name supplied. Falling back to guess_id') self.log_debug('No book name supplied. Falling back to guess_id')
book_ref_id = guess_id book_ref_id = guess_id
if not book_ref_id: if not book_ref_id:
raise ValidationError(msg='Could not resolve book_ref_id in "{}"'.format(self.filename)) raise ValidationError(msg='Could not resolve book_ref_id in "{}"'.format(self.file_path))
book_details = BiblesResourcesDB.get_book_by_id(book_ref_id) book_details = BiblesResourcesDB.get_book_by_id(book_ref_id)
if book_details is None: if book_details is None:
raise ValidationError(msg='book_ref_id: {book_ref} Could not be found in the BibleResourcesDB while ' raise ValidationError(msg='book_ref_id: {book_ref} Could not be found in the BibleResourcesDB while '
'importing {file}'.format(book_ref=book_ref_id, file=self.filename)) 'importing {file}'.format(book_ref=book_ref_id, file=self.file_path))
return self.create_book(name, book_ref_id, book_details['testament_id']) return self.create_book(name, book_ref_id, book_details['testament_id'])
def parse_xml(self, filename, use_objectify=False, elements=None, tags=None): def parse_xml(self, file_path, use_objectify=False, elements=None, tags=None):
""" """
Parse and clean the supplied file by removing any elements or tags we don't use. Parse and clean the supplied file by removing any elements or tags we don't use.
:param filename: The filename of the xml file to parse. Str :param file_path: The filename of the xml file to parse. Str
:param use_objectify: Use the objectify parser rather than the etree parser. (Bool) :param use_objectify: Use the objectify parser rather than the etree parser. (Bool)
:param elements: A tuple of element names (Str) to remove along with their content. :param elements: A tuple of element names (Str) to remove along with their content.
:param tags: A tuple of element names (Str) to remove, preserving their content. :param tags: A tuple of element names (Str) to remove, preserving their content.
:return: The root element of the xml document :return: The root element of the xml document
""" """
try: try:
with open(filename, 'rb') as import_file: with file_path.open('rb') as import_file:
# NOTE: We don't need to do any of the normal encoding detection here, because lxml does it's own # 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. # encoding detection, and the two mechanisms together interfere with each other.
if not use_objectify: if not use_objectify:
@ -207,17 +207,17 @@ class BibleImport(BibleDB, LogMixin, RegistryProperties):
self.log_debug('Stopping import') self.log_debug('Stopping import')
self.stop_import_flag = True self.stop_import_flag = True
def validate_xml_file(self, filename, tag): def validate_xml_file(self, file_path, tag):
""" """
Validate the supplied file Validate the supplied file
:param filename: The supplied file :param file_path: The supplied file
:param tag: The expected root tag type :param tag: The expected root tag type
:return: True if valid. ValidationError is raised otherwise. :return: True if valid. ValidationError is raised otherwise.
""" """
if BibleImport.is_compressed(filename): if BibleImport.is_compressed(file_path):
raise ValidationError(msg='Compressed file') raise ValidationError(msg='Compressed file')
bible = self.parse_xml(filename, use_objectify=True) bible = self.parse_xml(file_path, use_objectify=True)
if bible is None: if bible is None:
raise ValidationError(msg='Error when opening file') raise ValidationError(msg='Error when opening file')
root_tag = bible.tag.lower() root_tag = bible.tag.lower()

View File

@ -41,11 +41,11 @@ class BiblesTab(SettingsTab):
""" """
log.info('Bible Tab loaded') log.info('Bible Tab loaded')
def _init_(self, parent, title, visible_title, icon_path): def _init_(self, *args, **kwargs):
self.paragraph_style = True self.paragraph_style = True
self.show_new_chapters = False self.show_new_chapters = False
self.display_style = 0 self.display_style = 0
super(BiblesTab, self).__init__(parent, title, visible_title, icon_path) super().__init__(*args, **kwargs)
def setupUi(self): def setupUi(self):
self.setObjectName('BiblesTab') self.setObjectName('BiblesTab')

View File

@ -35,6 +35,7 @@ from sqlalchemy.orm.exc import UnmappedClassError
from openlp.core.common import clean_filename from openlp.core.common import clean_filename
from openlp.core.common.applocation import AppLocation from openlp.core.common.applocation import AppLocation
from openlp.core.common.i18n import translate from openlp.core.common.i18n import translate
from openlp.core.common.path import Path
from openlp.core.lib.db import BaseModel, init_db, Manager from openlp.core.lib.db import BaseModel, init_db, Manager
from openlp.core.lib.ui import critical_error_message_box from openlp.core.lib.ui import critical_error_message_box
from openlp.plugins.bibles.lib import BibleStrings, LanguageSelection, upgrade from openlp.plugins.bibles.lib import BibleStrings, LanguageSelection, upgrade
@ -129,10 +130,15 @@ class BibleDB(Manager):
:param parent: :param parent:
:param kwargs: :param kwargs:
``path`` ``path``
The path to the bible database file. The path to the bible database file. Type: openlp.core.common.path.Path
``name`` ``name``
The name of the database. This is also used as the file name for SQLite databases. The name of the database. This is also used as the file name for SQLite databases.
``file``
Type: openlp.core.common.path.Path
:rtype: None
""" """
log.info('BibleDB loaded') log.info('BibleDB loaded')
self._setup(parent, **kwargs) self._setup(parent, **kwargs)
@ -145,20 +151,20 @@ class BibleDB(Manager):
self.session = None self.session = None
if 'path' not in kwargs: if 'path' not in kwargs:
raise KeyError('Missing keyword argument "path".') raise KeyError('Missing keyword argument "path".')
self.path = kwargs['path']
if 'name' not in kwargs and 'file' not in kwargs: if 'name' not in kwargs and 'file' not in kwargs:
raise KeyError('Missing keyword argument "name" or "file".') raise KeyError('Missing keyword argument "name" or "file".')
if 'name' in kwargs: if 'name' in kwargs:
self.name = kwargs['name'] self.name = kwargs['name']
if not isinstance(self.name, str): if not isinstance(self.name, str):
self.name = str(self.name, 'utf-8') self.name = str(self.name, 'utf-8')
self.file = clean_filename(self.name) + '.sqlite' # TODO: To path object
file_path = Path(clean_filename(self.name) + '.sqlite')
if 'file' in kwargs: if 'file' in kwargs:
self.file = kwargs['file'] file_path = kwargs['file']
Manager.__init__(self, 'bibles', init_schema, self.file, upgrade) Manager.__init__(self, 'bibles', init_schema, file_path, upgrade)
if self.session and 'file' in kwargs: if self.session and 'file' in kwargs:
self.get_name() self.get_name()
if 'path' in kwargs:
self.path = kwargs['path']
self._is_web_bible = None self._is_web_bible = None
def get_name(self): def get_name(self):
@ -470,9 +476,9 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
Return the cursor object. Instantiate one if it doesn't exist yet. Return the cursor object. Instantiate one if it doesn't exist yet.
""" """
if BiblesResourcesDB.cursor is None: if BiblesResourcesDB.cursor is None:
file_path = os.path.join(str(AppLocation.get_directory(AppLocation.PluginsDir)), file_path = \
'bibles', 'resources', 'bibles_resources.sqlite') AppLocation.get_directory(AppLocation.PluginsDir) / 'bibles' / 'resources' / 'bibles_resources.sqlite'
conn = sqlite3.connect(file_path) conn = sqlite3.connect(str(file_path))
BiblesResourcesDB.cursor = conn.cursor() BiblesResourcesDB.cursor = conn.cursor()
return BiblesResourcesDB.cursor return BiblesResourcesDB.cursor
@ -758,17 +764,13 @@ class AlternativeBookNamesDB(QtCore.QObject, Manager):
If necessary loads up the database and creates the tables if the database doesn't exist. If necessary loads up the database and creates the tables if the database doesn't exist.
""" """
if AlternativeBookNamesDB.cursor is None: if AlternativeBookNamesDB.cursor is None:
file_path = os.path.join( file_path = AppLocation.get_directory(AppLocation.DataDir) / 'bibles' / 'alternative_book_names.sqlite'
str(AppLocation.get_directory(AppLocation.DataDir)), 'bibles', 'alternative_book_names.sqlite') AlternativeBookNamesDB.conn = sqlite3.connect(str(file_path))
if not os.path.exists(file_path): if not file_path.exists():
# create new DB, create table alternative_book_names # create new DB, create table alternative_book_names
AlternativeBookNamesDB.conn = sqlite3.connect(file_path)
AlternativeBookNamesDB.conn.execute( AlternativeBookNamesDB.conn.execute(
'CREATE TABLE alternative_book_names(id INTEGER NOT NULL, ' 'CREATE TABLE alternative_book_names(id INTEGER NOT NULL, '
'book_reference_id INTEGER, language_id INTEGER, name VARCHAR(50), PRIMARY KEY (id))') 'book_reference_id INTEGER, language_id INTEGER, name VARCHAR(50), PRIMARY KEY (id))')
else:
# use existing DB
AlternativeBookNamesDB.conn = sqlite3.connect(file_path)
AlternativeBookNamesDB.cursor = AlternativeBookNamesDB.conn.cursor() AlternativeBookNamesDB.cursor = AlternativeBookNamesDB.conn.cursor()
return AlternativeBookNamesDB.cursor return AlternativeBookNamesDB.cursor

View File

@ -19,3 +19,6 @@
# with this program; if not, write to the Free Software Foundation, Inc., 59 # # with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Temple Place, Suite 330, Boston, MA 02111-1307 USA #
############################################################################### ###############################################################################
"""
The :mod:`~openlp.plugins.bibles.lib.importers` module contains importers for the Bibles plugin.
"""

View File

@ -73,8 +73,8 @@ class CSVBible(BibleImport):
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.log_info(self.__class__.__name__) self.log_info(self.__class__.__name__)
self.books_file = kwargs['booksfile'] self.books_path = kwargs['books_path']
self.verses_file = kwargs['versefile'] self.verses_path = kwargs['verse_path']
@staticmethod @staticmethod
def get_book_name(name, books): def get_book_name(name, books):
@ -92,21 +92,22 @@ class CSVBible(BibleImport):
return book_name return book_name
@staticmethod @staticmethod
def parse_csv_file(filename, results_tuple): def parse_csv_file(file_path, results_tuple):
""" """
Parse the supplied CSV file. Parse the supplied CSV file.
:param filename: The name of the file to parse. Str :param openlp.core.common.path.Path file_path: The name of the file to parse.
:param results_tuple: The namedtuple to use to store the results. namedtuple :param namedtuple results_tuple: The namedtuple to use to store the results.
:return: An iterable yielding namedtuples of type results_tuple :return: An list of namedtuples of type results_tuple
:rtype: list[namedtuple]
""" """
try: try:
encoding = get_file_encoding(Path(filename))['encoding'] encoding = get_file_encoding(file_path)['encoding']
with open(filename, 'r', encoding=encoding, newline='') as csv_file: with file_path.open('r', encoding=encoding, newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"') csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
return [results_tuple(*line) for line in csv_reader] return [results_tuple(*line) for line in csv_reader]
except (OSError, csv.Error): except (OSError, csv.Error):
raise ValidationError(msg='Parsing "{file}" failed'.format(file=filename)) raise ValidationError(msg='Parsing "{file}" failed'.format(file=file_path))
def process_books(self, books): def process_books(self, books):
""" """
@ -159,12 +160,12 @@ class CSVBible(BibleImport):
self.language_id = self.get_language(bible_name) self.language_id = self.get_language(bible_name)
if not self.language_id: if not self.language_id:
return False return False
books = self.parse_csv_file(self.books_file, Book) books = self.parse_csv_file(self.books_path, Book)
self.wizard.progress_bar.setValue(0) self.wizard.progress_bar.setValue(0)
self.wizard.progress_bar.setMinimum(0) self.wizard.progress_bar.setMinimum(0)
self.wizard.progress_bar.setMaximum(len(books)) self.wizard.progress_bar.setMaximum(len(books))
book_list = self.process_books(books) book_list = self.process_books(books)
verses = self.parse_csv_file(self.verses_file, Verse) verses = self.parse_csv_file(self.verses_path, Verse)
self.wizard.progress_bar.setValue(0) self.wizard.progress_bar.setValue(0)
self.wizard.progress_bar.setMaximum(len(books) + 1) self.wizard.progress_bar.setMaximum(len(books) + 1)
self.process_verses(verses, book_list) self.process_verses(verses, book_list)

View File

@ -46,7 +46,8 @@ def parse_chapter_number(number, previous_number):
:param number: The raw data from the xml :param number: The raw data from the xml
:param previous_number: The previous chapter number :param previous_number: The previous chapter number
:return: Number of current chapter. (Int) :return: Number of current chapter.
:rtype: int
""" """
if number: if number:
return int(number.split()[-1]) return int(number.split()[-1])
@ -132,13 +133,13 @@ class OpenSongBible(BibleImport):
:param bible_name: The name of the bible being imported :param bible_name: The name of the bible being imported
:return: True if import completed, False if import was unsuccessful :return: True if import completed, False if import was unsuccessful
""" """
self.log_debug('Starting OpenSong import from "{name}"'.format(name=self.filename)) self.log_debug('Starting OpenSong import from "{name}"'.format(name=self.file_path))
self.validate_xml_file(self.filename, 'bible') self.validate_xml_file(self.file_path, 'bible')
bible = self.parse_xml(self.filename, use_objectify=True) bible = self.parse_xml(self.file_path, use_objectify=True)
if bible is None: if bible is None:
return False return False
# No language info in the opensong format, so ask the user # No language info in the opensong format, so ask the user
self.language_id = self.get_language_id(bible_name=self.filename) self.language_id = self.get_language_id(bible_name=str(self.file_path))
if not self.language_id: if not self.language_id:
return False return False
self.process_books(bible.b) self.process_books(bible.b)

View File

@ -159,14 +159,14 @@ class OSISBible(BibleImport):
""" """
Loads a Bible from file. Loads a Bible from file.
""" """
self.log_debug('Starting OSIS import from "{name}"'.format(name=self.filename)) self.log_debug('Starting OSIS import from "{name}"'.format(name=self.file_path))
self.validate_xml_file(self.filename, '{http://www.bibletechnologies.net/2003/osis/namespace}osis') self.validate_xml_file(self.file_path, '{http://www.bibletechnologies.net/2003/osis/namespace}osis')
bible = self.parse_xml(self.filename, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS) bible = self.parse_xml(self.file_path, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS)
if bible is None: if bible is None:
return False return False
# Find bible language # Find bible language
language = bible.xpath("//ns:osisText/@xml:lang", namespaces=NS) language = bible.xpath("//ns:osisText/@xml:lang", namespaces=NS)
self.language_id = self.get_language_id(language[0] if language else None, bible_name=self.filename) self.language_id = self.get_language_id(language[0] if language else None, bible_name=str(self.file_path))
if not self.language_id: if not self.language_id:
return False return False
self.process_books(bible) self.process_books(bible)

View File

@ -60,7 +60,7 @@ class SwordBible(BibleImport):
bible = pysword_modules.get_bible_from_module(self.sword_key) bible = pysword_modules.get_bible_from_module(self.sword_key)
language = pysword_module_json['lang'] language = pysword_module_json['lang']
language = language[language.find('.') + 1:] language = language[language.find('.') + 1:]
language_id = self.get_language_id(language, bible_name=self.filename) language_id = self.get_language_id(language, bible_name=str(self.file_path))
if not language_id: if not language_id:
return False return False
books = bible.get_structure().get_books() books = bible.get_structure().get_books()

View File

@ -19,15 +19,14 @@
# with this program; if not, write to the Free Software Foundation, Inc., 59 # # with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Temple Place, Suite 330, Boston, MA 02111-1307 USA #
############################################################################### ###############################################################################
import os
import re
import logging import logging
from codecs import open as copen import re
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from zipfile import ZipFile from zipfile import ZipFile
from bs4 import BeautifulSoup, Tag, NavigableString from bs4 import BeautifulSoup, Tag, NavigableString
from openlp.core.common.path import Path
from openlp.plugins.bibles.lib.bibleimport import BibleImport from openlp.plugins.bibles.lib.bibleimport import BibleImport
BOOK_NUMBER_PATTERN = re.compile(r'\[(\d+)\]') BOOK_NUMBER_PATTERN = re.compile(r'\[(\d+)\]')
@ -51,9 +50,9 @@ class WordProjectBible(BibleImport):
Unzip the file to a temporary directory Unzip the file to a temporary directory
""" """
self.tmp = TemporaryDirectory() self.tmp = TemporaryDirectory()
zip_file = ZipFile(os.path.abspath(self.filename)) with ZipFile(str(self.file_path)) as zip_file:
zip_file.extractall(self.tmp.name) zip_file.extractall(self.tmp.name)
self.base_dir = os.path.join(self.tmp.name, os.path.splitext(os.path.basename(self.filename))[0]) self.base_path = Path(self.tmp.name, self.file_path.stem)
def process_books(self): def process_books(self):
""" """
@ -62,8 +61,7 @@ class WordProjectBible(BibleImport):
:param bible_data: parsed xml :param bible_data: parsed xml
:return: None :return: None
""" """
with copen(os.path.join(self.base_dir, 'index.htm'), encoding='utf-8', errors='ignore') as index_file: page = (self.base_path / 'index.htm').read_text(encoding='utf-8', errors='ignore')
page = index_file.read()
soup = BeautifulSoup(page, 'lxml') soup = BeautifulSoup(page, 'lxml')
bible_books = soup.find('div', 'textOptions').find_all('li') bible_books = soup.find('div', 'textOptions').find_all('li')
book_count = len(bible_books) book_count = len(bible_books)
@ -93,9 +91,7 @@ class WordProjectBible(BibleImport):
:return: None :return: None
""" """
log.debug(book_link) log.debug(book_link)
book_file = os.path.join(self.base_dir, os.path.normpath(book_link)) page = (self.base_path / book_link).read_text(encoding='utf-8', errors='ignore')
with copen(book_file, encoding='utf-8', errors='ignore') as f:
page = f.read()
soup = BeautifulSoup(page, 'lxml') soup = BeautifulSoup(page, 'lxml')
header_div = soup.find('div', 'textHeader') header_div = soup.find('div', 'textHeader')
chapters_p = header_div.find('p') chapters_p = header_div.find('p')
@ -114,9 +110,8 @@ class WordProjectBible(BibleImport):
""" """
Get the verses for a particular book Get the verses for a particular book
""" """
chapter_file_name = os.path.join(self.base_dir, '{:02d}'.format(book_number), '{}.htm'.format(chapter_number)) chapter_file_path = self.base_path / '{:02d}'.format(book_number) / '{}.htm'.format(chapter_number)
with copen(chapter_file_name, encoding='utf-8', errors='ignore') as chapter_file: page = chapter_file_path.read_text(encoding='utf-8', errors='ignore')
page = chapter_file.read()
soup = BeautifulSoup(page, 'lxml') soup = BeautifulSoup(page, 'lxml')
text_body = soup.find('div', 'textBody') text_body = soup.find('div', 'textBody')
if text_body: if text_body:
@ -158,9 +153,9 @@ class WordProjectBible(BibleImport):
""" """
Loads a Bible from file. Loads a Bible from file.
""" """
self.log_debug('Starting WordProject import from "{name}"'.format(name=self.filename)) self.log_debug('Starting WordProject import from "{name}"'.format(name=self.file_path))
self._unzip_file() self._unzip_file()
self.language_id = self.get_language_id(None, bible_name=self.filename) self.language_id = self.get_language_id(None, bible_name=str(self.file_path))
result = False result = False
if self.language_id: if self.language_id:
self.process_books() self.process_books()

View File

@ -45,13 +45,13 @@ class ZefaniaBible(BibleImport):
""" """
Loads a Bible from file. Loads a Bible from file.
""" """
log.debug('Starting Zefania import from "{name}"'.format(name=self.filename)) log.debug('Starting Zefania import from "{name}"'.format(name=self.file_path))
success = True success = True
try: try:
xmlbible = self.parse_xml(self.filename, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS) xmlbible = self.parse_xml(self.file_path, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS)
# Find bible language # Find bible language
language = xmlbible.xpath("/XMLBIBLE/INFORMATION/language/text()") language = xmlbible.xpath("/XMLBIBLE/INFORMATION/language/text()")
language_id = self.get_language_id(language[0] if language else None, bible_name=self.filename) language_id = self.get_language_id(language[0] if language else None, bible_name=str(self.file_path))
if not language_id: if not language_id:
return False return False
no_of_books = int(xmlbible.xpath('count(//BIBLEBOOK)')) no_of_books = int(xmlbible.xpath('count(//BIBLEBOOK)'))
@ -69,7 +69,7 @@ class ZefaniaBible(BibleImport):
log.debug('Could not find a name, will use number, basically a guess.') log.debug('Could not find a name, will use number, basically a guess.')
book_ref_id = int(bnumber) book_ref_id = int(bnumber)
if not book_ref_id: if not book_ref_id:
log.error('Importing books from "{name}" failed'.format(name=self.filename)) log.error('Importing books from "{name}" failed'.format(name=self.file_path))
return False return False
book_details = BiblesResourcesDB.get_book_by_id(book_ref_id) 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']) db_book = self.create_book(book_details['name'], book_ref_id, book_details['testament_id'])

View File

@ -19,7 +19,6 @@
# with this program; if not, write to the Free Software Foundation, Inc., 59 # # with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Temple Place, Suite 330, Boston, MA 02111-1307 USA #
############################################################################### ###############################################################################
import logging import logging
from openlp.core.common import delete_file from openlp.core.common import delete_file
@ -115,7 +114,7 @@ class BibleManager(LogMixin, RegistryProperties):
self.settings_section = 'bibles' self.settings_section = 'bibles'
self.web = 'Web' self.web = 'Web'
self.db_cache = None self.db_cache = None
self.path = str(AppLocation.get_section_data_path(self.settings_section)) self.path = AppLocation.get_section_data_path(self.settings_section)
self.proxy_name = Settings().value(self.settings_section + '/proxy name') self.proxy_name = Settings().value(self.settings_section + '/proxy name')
self.suffix = '.sqlite' self.suffix = '.sqlite'
self.import_wizard = None self.import_wizard = None
@ -128,20 +127,20 @@ class BibleManager(LogMixin, RegistryProperties):
of HTTPBible is loaded instead of the BibleDB class. of HTTPBible is loaded instead of the BibleDB class.
""" """
log.debug('Reload bibles') log.debug('Reload bibles')
files = [str(file) for file in AppLocation.get_files(self.settings_section, self.suffix)] file_paths = AppLocation.get_files(self.settings_section, self.suffix)
if 'alternative_book_names.sqlite' in files: if Path('alternative_book_names.sqlite') in file_paths:
files.remove('alternative_book_names.sqlite') file_paths.remove(Path('alternative_book_names.sqlite'))
log.debug('Bible Files {text}'.format(text=files)) log.debug('Bible Files {text}'.format(text=file_paths))
self.db_cache = {} self.db_cache = {}
for filename in files: for file_path in file_paths:
bible = BibleDB(self.parent, path=self.path, file=filename) bible = BibleDB(self.parent, path=self.path, file=file_path)
if not bible.session: if not bible.session:
continue continue
name = bible.get_name() name = bible.get_name()
# Remove corrupted files. # Remove corrupted files.
if name is None: if name is None:
bible.session.close_all() bible.session.close_all()
delete_file(Path(self.path, filename)) delete_file(self.path / file_path)
continue continue
log.debug('Bible Name: "{name}"'.format(name=name)) log.debug('Bible Name: "{name}"'.format(name=name))
self.db_cache[name] = bible self.db_cache[name] = bible
@ -150,7 +149,7 @@ class BibleManager(LogMixin, RegistryProperties):
source = self.db_cache[name].get_object(BibleMeta, 'download_source') source = self.db_cache[name].get_object(BibleMeta, 'download_source')
download_name = self.db_cache[name].get_object(BibleMeta, 'download_name').value download_name = self.db_cache[name].get_object(BibleMeta, 'download_name').value
meta_proxy = self.db_cache[name].get_object(BibleMeta, 'proxy_server') meta_proxy = self.db_cache[name].get_object(BibleMeta, 'proxy_server')
web_bible = HTTPBible(self.parent, path=self.path, file=filename, download_source=source.value, web_bible = HTTPBible(self.parent, path=self.path, file=file_path, download_source=source.value,
download_name=download_name) download_name=download_name)
if meta_proxy: if meta_proxy:
web_bible.proxy_server = meta_proxy.value web_bible.proxy_server = meta_proxy.value

View File

@ -30,6 +30,7 @@ from lxml import etree, objectify
from PyQt5.QtWidgets import QDialog from PyQt5.QtWidgets import QDialog
from openlp.core.common.i18n import Language from openlp.core.common.i18n import Language
from openlp.core.common.path import Path
from openlp.core.lib.exceptions import ValidationError from openlp.core.lib.exceptions import ValidationError
from openlp.plugins.bibles.lib.bibleimport import BibleImport from openlp.plugins.bibles.lib.bibleimport import BibleImport
from openlp.plugins.bibles.lib.db import BibleDB from openlp.plugins.bibles.lib.db import BibleDB
@ -48,7 +49,7 @@ class TestBibleImport(TestCase):
b' <data><unsupported>Test<x>data</x><y>to</y>discard</unsupported></data>\n' b' <data><unsupported>Test<x>data</x><y>to</y>discard</unsupported></data>\n'
b'</root>' b'</root>'
) )
self.open_patcher = patch('builtins.open') self.open_patcher = patch.object(Path, 'open')
self.addCleanup(self.open_patcher.stop) self.addCleanup(self.open_patcher.stop)
self.mocked_open = self.open_patcher.start() self.mocked_open = self.open_patcher.start()
self.critical_error_message_box_patcher = \ self.critical_error_message_box_patcher = \
@ -74,8 +75,8 @@ class TestBibleImport(TestCase):
# WHEN: Creating an instance of BibleImport with no key word arguments # WHEN: Creating an instance of BibleImport with no key word arguments
instance = BibleImport(MagicMock()) instance = BibleImport(MagicMock())
# THEN: The filename attribute should be None # THEN: The file_path attribute should be None
assert instance.filename is None assert instance.file_path is None
assert isinstance(instance, BibleDB) assert isinstance(instance, BibleDB)
def test_init_kwargs_set(self): def test_init_kwargs_set(self):
@ -84,11 +85,11 @@ class TestBibleImport(TestCase):
""" """
# GIVEN: A patched BibleDB._setup, BibleImport class and mocked parent # GIVEN: A patched BibleDB._setup, BibleImport class and mocked parent
# WHEN: Creating an instance of BibleImport with selected key word arguments # WHEN: Creating an instance of BibleImport with selected key word arguments
kwargs = {'filename': 'bible.xml'} kwargs = {'file_path': 'bible.xml'}
instance = BibleImport(MagicMock(), **kwargs) instance = BibleImport(MagicMock(), **kwargs)
# THEN: The filename keyword should be set to bible.xml # THEN: The file_path keyword should be set to bible.xml
assert instance.filename == 'bible.xml' assert instance.file_path == 'bible.xml'
assert isinstance(instance, BibleDB) assert isinstance(instance, BibleDB)
@patch.object(BibleDB, '_setup') @patch.object(BibleDB, '_setup')
@ -361,7 +362,7 @@ class TestBibleImport(TestCase):
instance.wizard = MagicMock() instance.wizard = MagicMock()
# WHEN: Calling parse_xml # WHEN: Calling parse_xml
result = instance.parse_xml('file.tst') result = instance.parse_xml(Path('file.tst'))
# THEN: The result returned should contain the correct data, and should be an instance of eetree_Element # THEN: The result returned should contain the correct data, and should be an instance of eetree_Element
assert etree.tostring(result) == b'<root>\n <data><div>Test<p>data</p><a>to</a>keep</div></data>\n' \ assert etree.tostring(result) == b'<root>\n <data><div>Test<p>data</p><a>to</a>keep</div></data>\n' \
@ -378,7 +379,7 @@ class TestBibleImport(TestCase):
instance.wizard = MagicMock() instance.wizard = MagicMock()
# WHEN: Calling parse_xml # WHEN: Calling parse_xml
result = instance.parse_xml('file.tst', use_objectify=True) result = instance.parse_xml(Path('file.tst'), use_objectify=True)
# THEN: The result returned should contain the correct data, and should be an instance of ObjectifiedElement # THEN: The result returned should contain the correct data, and should be an instance of ObjectifiedElement
assert etree.tostring(result) == b'<root><data><div>Test<p>data</p><a>to</a>keep</div></data>' \ assert etree.tostring(result) == b'<root><data><div>Test<p>data</p><a>to</a>keep</div></data>' \
@ -396,7 +397,7 @@ class TestBibleImport(TestCase):
instance.wizard = MagicMock() instance.wizard = MagicMock()
# WHEN: Calling parse_xml, with a test file # WHEN: Calling parse_xml, with a test file
result = instance.parse_xml('file.tst', elements=elements) result = instance.parse_xml(Path('file.tst'), elements=elements)
# THEN: The result returned should contain the correct data # THEN: The result returned should contain the correct data
assert etree.tostring(result) == \ assert etree.tostring(result) == \
@ -413,7 +414,7 @@ class TestBibleImport(TestCase):
instance.wizard = MagicMock() instance.wizard = MagicMock()
# WHEN: Calling parse_xml, with a test file # WHEN: Calling parse_xml, with a test file
result = instance.parse_xml('file.tst', tags=tags) result = instance.parse_xml(Path('file.tst'), tags=tags)
# THEN: The result returned should contain the correct data # THEN: The result returned should contain the correct data
assert etree.tostring(result) == b'<root>\n <data>Testdatatokeep</data>\n <data><unsupported>Test' \ assert etree.tostring(result) == b'<root>\n <data>Testdatatokeep</data>\n <data><unsupported>Test' \
@ -431,7 +432,7 @@ class TestBibleImport(TestCase):
instance.wizard = MagicMock() instance.wizard = MagicMock()
# WHEN: Calling parse_xml, with a test file # WHEN: Calling parse_xml, with a test file
result = instance.parse_xml('file.tst', elements=elements, tags=tags) result = instance.parse_xml(Path('file.tst'), elements=elements, tags=tags)
# THEN: The result returned should contain the correct data # THEN: The result returned should contain the correct data
assert etree.tostring(result) == b'<root>\n <data>Testdatatokeep</data>\n <data/>\n</root>' assert etree.tostring(result) == b'<root>\n <data>Testdatatokeep</data>\n <data/>\n</root>'
@ -446,10 +447,10 @@ class TestBibleImport(TestCase):
exception.filename = 'file.tst' exception.filename = 'file.tst'
exception.strerror = 'No such file or directory' exception.strerror = 'No such file or directory'
self.mocked_open.side_effect = exception self.mocked_open.side_effect = exception
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling parse_xml # WHEN: Calling parse_xml
result = importer.parse_xml('file.tst') result = importer.parse_xml(Path('file.tst'))
# THEN: parse_xml should have caught the error, informed the user and returned None # THEN: parse_xml should have caught the error, informed the user and returned None
mocked_log_exception.assert_called_once_with('Opening file.tst failed.') mocked_log_exception.assert_called_once_with('Opening file.tst failed.')
@ -468,10 +469,10 @@ class TestBibleImport(TestCase):
exception.filename = 'file.tst' exception.filename = 'file.tst'
exception.strerror = 'Permission denied' exception.strerror = 'Permission denied'
self.mocked_open.side_effect = exception self.mocked_open.side_effect = exception
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling parse_xml # WHEN: Calling parse_xml
result = importer.parse_xml('file.tst') result = importer.parse_xml(Path('file.tst'))
# THEN: parse_xml should have caught the error, informed the user and returned None # THEN: parse_xml should have caught the error, informed the user and returned None
mocked_log_exception.assert_called_once_with('Opening file.tst failed.') mocked_log_exception.assert_called_once_with('Opening file.tst failed.')
@ -485,7 +486,7 @@ class TestBibleImport(TestCase):
Test set_current_chapter Test set_current_chapter
""" """
# GIVEN: An instance of BibleImport and a mocked wizard # GIVEN: An instance of BibleImport and a mocked wizard
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
importer.wizard = MagicMock() importer.wizard = MagicMock()
# WHEN: Calling set_current_chapter # WHEN: Calling set_current_chapter
@ -500,7 +501,7 @@ class TestBibleImport(TestCase):
Test that validate_xml_file raises a ValidationError when is_compressed returns True Test that validate_xml_file raises a ValidationError when is_compressed returns True
""" """
# GIVEN: A mocked parse_xml which returns None # GIVEN: A mocked parse_xml which returns None
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling is_compressed # WHEN: Calling is_compressed
# THEN: ValidationError should be raised, with the message 'Compressed file' # THEN: ValidationError should be raised, with the message 'Compressed file'
@ -515,7 +516,7 @@ class TestBibleImport(TestCase):
Test that validate_xml_file raises a ValidationError when parse_xml returns None Test that validate_xml_file raises a ValidationError when parse_xml returns None
""" """
# GIVEN: A mocked parse_xml which returns None # GIVEN: A mocked parse_xml which returns None
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling validate_xml_file # WHEN: Calling validate_xml_file
# THEN: ValidationError should be raised, with the message 'Error when opening file' # THEN: ValidationError should be raised, with the message 'Error when opening file'
@ -531,7 +532,7 @@ class TestBibleImport(TestCase):
Test that validate_xml_file returns True with valid XML Test that validate_xml_file returns True with valid XML
""" """
# GIVEN: Some test data with an OpenSong Bible "bible" root tag # GIVEN: Some test data with an OpenSong Bible "bible" root tag
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling validate_xml_file # WHEN: Calling validate_xml_file
result = importer.validate_xml_file('file.name', 'bible') result = importer.validate_xml_file('file.name', 'bible')
@ -546,7 +547,7 @@ class TestBibleImport(TestCase):
Test that validate_xml_file raises a ValidationError with an OpenSong root tag Test that validate_xml_file raises a ValidationError with an OpenSong root tag
""" """
# GIVEN: Some test data with an Zefania root tag and an instance of BibleImport # GIVEN: Some test data with an Zefania root tag and an instance of BibleImport
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling validate_xml_file # WHEN: Calling validate_xml_file
# THEN: ValidationError should be raised, and the critical error message box should was called informing # THEN: ValidationError should be raised, and the critical error message box should was called informing
@ -566,7 +567,7 @@ class TestBibleImport(TestCase):
# GIVEN: Some test data with an Zefania root tag and an instance of BibleImport # GIVEN: Some test data with an Zefania root tag and an instance of BibleImport
mocked_parse_xml.return_value = objectify.fromstring( mocked_parse_xml.return_value = objectify.fromstring(
'<osis xmlns=\'http://www.bibletechnologies.net/2003/OSIS/namespace\'></osis>') '<osis xmlns=\'http://www.bibletechnologies.net/2003/OSIS/namespace\'></osis>')
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling validate_xml_file # WHEN: Calling validate_xml_file
# THEN: ValidationError should be raised, and the critical error message box should was called informing # THEN: ValidationError should be raised, and the critical error message box should was called informing
@ -584,7 +585,7 @@ class TestBibleImport(TestCase):
Test that validate_xml_file raises a ValidationError with an Zefania root tag Test that validate_xml_file raises a ValidationError with an Zefania root tag
""" """
# GIVEN: Some test data with an Zefania root tag and an instance of BibleImport # GIVEN: Some test data with an Zefania root tag and an instance of BibleImport
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling validate_xml_file # WHEN: Calling validate_xml_file
# THEN: ValidationError should be raised, and the critical error message box should was called informing # THEN: ValidationError should be raised, and the critical error message box should was called informing
@ -602,7 +603,7 @@ class TestBibleImport(TestCase):
Test that validate_xml_file raises a ValidationError with an unknown root tag Test that validate_xml_file raises a ValidationError with an unknown root tag
""" """
# GIVEN: Some test data with an unknown root tag and an instance of BibleImport # GIVEN: Some test data with an unknown root tag and an instance of BibleImport
importer = BibleImport(MagicMock(), path='.', name='.', filename='') importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling validate_xml_file # WHEN: Calling validate_xml_file
# THEN: ValidationError should be raised, and the critical error message box should was called informing # THEN: ValidationError should be raised, and the critical error message box should was called informing

View File

@ -29,6 +29,7 @@ from collections import namedtuple
from unittest import TestCase from unittest import TestCase
from unittest.mock import ANY, MagicMock, PropertyMock, call, patch from unittest.mock import ANY, MagicMock, PropertyMock, call, patch
from openlp.core.common.path import Path
from openlp.core.lib.exceptions import ValidationError from openlp.core.lib.exceptions import ValidationError
from openlp.plugins.bibles.lib.bibleimport import BibleImport from openlp.plugins.bibles.lib.bibleimport import BibleImport
from openlp.plugins.bibles.lib.importers.csvbible import Book, CSVBible, Verse from openlp.plugins.bibles.lib.importers.csvbible import Book, CSVBible, Verse
@ -59,12 +60,13 @@ class TestCSVImport(TestCase):
mocked_manager = MagicMock() mocked_manager = MagicMock()
# WHEN: An importer object is created # WHEN: An importer object is created
importer = CSVBible(mocked_manager, path='.', name='.', booksfile='books.csv', versefile='verse.csv') importer = \
CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'), verse_path=Path('verse.csv'))
# THEN: The importer should be an instance of BibleImport # THEN: The importer should be an instance of BibleImport
self.assertIsInstance(importer, BibleImport) self.assertIsInstance(importer, BibleImport)
self.assertEqual(importer.books_file, 'books.csv') self.assertEqual(importer.books_path, Path('books.csv'))
self.assertEqual(importer.verses_file, 'verse.csv') self.assertEqual(importer.verses_path, Path('verse.csv'))
def test_book_namedtuple(self): def test_book_namedtuple(self):
""" """
@ -134,17 +136,17 @@ class TestCSVImport(TestCase):
with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding', with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
return_value={'encoding': 'utf-8', 'confidence': 0.99}),\ return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
patch('openlp.plugins.bibles.lib.importers.csvbible.open', create=True) as mocked_open,\ patch('openlp.plugins.bibles.lib.importers.csvbible.Path.open', create=True) as mocked_open,\
patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader', patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader',
return_value=iter(test_data)) as mocked_reader: return_value=iter(test_data)) as mocked_reader:
# WHEN: Calling the CSVBible parse_csv_file method with a file name and TestTuple # WHEN: Calling the CSVBible parse_csv_file method with a file name and TestTuple
result = CSVBible.parse_csv_file('file.csv', TestTuple) result = CSVBible.parse_csv_file(Path('file.csv'), TestTuple)
# THEN: A list of TestTuple instances with the parsed data should be returned # THEN: A list of TestTuple instances with the parsed data should be returned
self.assertEqual(result, [TestTuple('1', 'Line 1', 'Data 1'), TestTuple('2', 'Line 2', 'Data 2'), self.assertEqual(result, [TestTuple('1', 'Line 1', 'Data 1'), TestTuple('2', 'Line 2', 'Data 2'),
TestTuple('3', 'Line 3', 'Data 3')]) TestTuple('3', 'Line 3', 'Data 3')])
mocked_open.assert_called_once_with('file.csv', 'r', encoding='utf-8', newline='') mocked_open.assert_called_once_with('r', encoding='utf-8', newline='')
mocked_reader.assert_called_once_with(ANY, delimiter=',', quotechar='"') mocked_reader.assert_called_once_with(ANY, delimiter=',', quotechar='"')
def test_parse_csv_file_oserror(self): def test_parse_csv_file_oserror(self):
@ -154,12 +156,12 @@ class TestCSVImport(TestCase):
# GIVEN: Mocked a mocked open object which raises an OSError # GIVEN: Mocked a mocked open object which raises an OSError
with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding', with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
return_value={'encoding': 'utf-8', 'confidence': 0.99}),\ return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
patch('openlp.plugins.bibles.lib.importers.csvbible.open', side_effect=OSError, create=True): patch('openlp.plugins.bibles.lib.importers.csvbible.Path.open', side_effect=OSError, create=True):
# WHEN: Calling CSVBible.parse_csv_file # WHEN: Calling CSVBible.parse_csv_file
# THEN: A ValidationError should be raised # THEN: A ValidationError should be raised
with self.assertRaises(ValidationError) as context: with self.assertRaises(ValidationError) as context:
CSVBible.parse_csv_file('file.csv', None) CSVBible.parse_csv_file(Path('file.csv'), None)
self.assertEqual(context.exception.msg, 'Parsing "file.csv" failed') self.assertEqual(context.exception.msg, 'Parsing "file.csv" failed')
def test_parse_csv_file_csverror(self): def test_parse_csv_file_csverror(self):
@ -169,13 +171,13 @@ class TestCSVImport(TestCase):
# GIVEN: Mocked a csv.reader which raises an csv.Error # GIVEN: Mocked a csv.reader which raises an csv.Error
with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding', with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
return_value={'encoding': 'utf-8', 'confidence': 0.99}),\ return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
patch('openlp.plugins.bibles.lib.importers.csvbible.open', create=True),\ patch('openlp.plugins.bibles.lib.importers.csvbible.Path.open', create=True),\
patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader', side_effect=csv.Error): patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader', side_effect=csv.Error):
# WHEN: Calling CSVBible.parse_csv_file # WHEN: Calling CSVBible.parse_csv_file
# THEN: A ValidationError should be raised # THEN: A ValidationError should be raised
with self.assertRaises(ValidationError) as context: with self.assertRaises(ValidationError) as context:
CSVBible.parse_csv_file('file.csv', None) CSVBible.parse_csv_file(Path('file.csv'), None)
self.assertEqual(context.exception.msg, 'Parsing "file.csv" failed') self.assertEqual(context.exception.msg, 'Parsing "file.csv" failed')
def test_process_books_stopped_import(self): def test_process_books_stopped_import(self):
@ -185,7 +187,8 @@ class TestCSVImport(TestCase):
# GIVEN: An instance of CSVBible with the stop_import_flag set to True # GIVEN: An instance of CSVBible with the stop_import_flag set to True
mocked_manager = MagicMock() mocked_manager = MagicMock()
with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'): with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'):
importer = CSVBible(mocked_manager, path='.', name='.', booksfile='books.csv', versefile='verse.csv') importer = CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'),
verse_path=Path('verse.csv'))
type(importer).application = PropertyMock() type(importer).application = PropertyMock()
importer.stop_import_flag = True importer.stop_import_flag = True
importer.wizard = MagicMock() importer.wizard = MagicMock()
@ -205,7 +208,8 @@ class TestCSVImport(TestCase):
mocked_manager = MagicMock() mocked_manager = MagicMock()
with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'),\ with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'),\
patch('openlp.plugins.bibles.lib.importers.csvbible.translate'): patch('openlp.plugins.bibles.lib.importers.csvbible.translate'):
importer = CSVBible(mocked_manager, path='.', name='.', booksfile='books.csv', versefile='verse.csv') importer = CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'),
verse_path=Path('verse.csv'))
importer.find_and_create_book = MagicMock() importer.find_and_create_book = MagicMock()
importer.language_id = 10 importer.language_id = 10
importer.stop_import_flag = False importer.stop_import_flag = False
@ -229,7 +233,8 @@ class TestCSVImport(TestCase):
# GIVEN: An instance of CSVBible with the stop_import_flag set to True # GIVEN: An instance of CSVBible with the stop_import_flag set to True
mocked_manager = MagicMock() mocked_manager = MagicMock()
with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'): with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'):
importer = CSVBible(mocked_manager, path='.', name='.', booksfile='books.csv', versefile='verse.csv') importer = CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'),
verse_path=Path('verse.csv'))
importer.get_book_name = MagicMock() importer.get_book_name = MagicMock()
importer.session = MagicMock() importer.session = MagicMock()
importer.stop_import_flag = True importer.stop_import_flag = True
@ -250,7 +255,8 @@ class TestCSVImport(TestCase):
mocked_manager = MagicMock() mocked_manager = MagicMock()
with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'),\ with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'),\
patch('openlp.plugins.bibles.lib.importers.csvbible.translate'): patch('openlp.plugins.bibles.lib.importers.csvbible.translate'):
importer = CSVBible(mocked_manager, path='.', name='.', booksfile='books.csv', versefile='verse.csv') importer = CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'),
verse_path=Path('verse.csv'))
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
importer.get_book = MagicMock(return_value=Book('1', '1', '1. Mosebog', '1Mos')) importer.get_book = MagicMock(return_value=Book('1', '1', '1. Mosebog', '1Mos'))
importer.get_book_name = MagicMock(return_value='1. Mosebog') importer.get_book_name = MagicMock(return_value='1. Mosebog')
@ -281,7 +287,8 @@ class TestCSVImport(TestCase):
# GIVEN: An instance of CSVBible and a mocked get_language which simulates the user cancelling the language box # GIVEN: An instance of CSVBible and a mocked get_language which simulates the user cancelling the language box
mocked_manager = MagicMock() mocked_manager = MagicMock()
with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'): with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'):
importer = CSVBible(mocked_manager, path='.', name='.', booksfile='books.csv', versefile='verse.csv') importer = CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'),
verse_path=Path('verse.csv'))
importer.get_language = MagicMock(return_value=None) importer.get_language = MagicMock(return_value=None)
# WHEN: Calling do_import # WHEN: Calling do_import
@ -298,7 +305,8 @@ class TestCSVImport(TestCase):
# GIVEN: An instance of CSVBible # GIVEN: An instance of CSVBible
mocked_manager = MagicMock() mocked_manager = MagicMock()
with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'): with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'):
importer = CSVBible(mocked_manager, path='.', name='.', booksfile='books.csv', versefile='verses.csv') importer = CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'),
verse_path=Path('verses.csv'))
importer.get_language = MagicMock(return_value=10) importer.get_language = MagicMock(return_value=10)
importer.parse_csv_file = MagicMock(side_effect=[['Book 1'], ['Verse 1']]) importer.parse_csv_file = MagicMock(side_effect=[['Book 1'], ['Verse 1']])
importer.process_books = MagicMock(return_value=['Book 1']) importer.process_books = MagicMock(return_value=['Book 1'])
@ -312,7 +320,8 @@ class TestCSVImport(TestCase):
# THEN: parse_csv_file should be called twice, # THEN: parse_csv_file should be called twice,
# and True should be returned. # and True should be returned.
self.assertEqual(importer.parse_csv_file.mock_calls, [call('books.csv', Book), call('verses.csv', Verse)]) self.assertEqual(importer.parse_csv_file.mock_calls,
[call(Path('books.csv'), Book), call(Path('verses.csv'), Verse)])
importer.process_books.assert_called_once_with(['Book 1']) importer.process_books.assert_called_once_with(['Book 1'])
importer.process_verses.assert_called_once_with(['Verse 1'], ['Book 1']) importer.process_verses.assert_called_once_with(['Verse 1'], ['Book 1'])
self.assertTrue(result) self.assertTrue(result)
@ -325,12 +334,12 @@ class TestCSVImport(TestCase):
# get_book_ref_id_by_name, create_verse, create_book, session and get_language. # get_book_ref_id_by_name, create_verse, create_book, session and get_language.
result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb') result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb')
test_data = json.loads(result_file.read().decode()) test_data = json.loads(result_file.read().decode())
books_file = os.path.join(TEST_PATH, 'dk1933-books.csv') books_file = Path(TEST_PATH, 'dk1933-books.csv')
verses_file = os.path.join(TEST_PATH, 'dk1933-verses.csv') verses_file = Path(TEST_PATH, 'dk1933-verses.csv')
with patch('openlp.plugins.bibles.lib.importers.csvbible.CSVBible.application'): with patch('openlp.plugins.bibles.lib.importers.csvbible.CSVBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = CSVBible(mocked_manager, path='.', name='.', booksfile=books_file, versefile=verses_file) importer = CSVBible(mocked_manager, path='.', name='.', books_path=books_file, verse_path=verses_file)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock() importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock() importer.create_verse = MagicMock()

View File

@ -29,6 +29,7 @@ from unittest.mock import MagicMock, patch, call
from lxml import objectify from lxml import objectify
from openlp.core.common.path import Path
from openlp.core.common.registry import Registry from openlp.core.common.registry import Registry
from openlp.plugins.bibles.lib.importers.opensong import OpenSongBible, get_text, parse_chapter_number from openlp.plugins.bibles.lib.importers.opensong import OpenSongBible, get_text, parse_chapter_number
from openlp.plugins.bibles.lib.bibleimport import BibleImport from openlp.plugins.bibles.lib.bibleimport import BibleImport
@ -64,7 +65,7 @@ class TestOpenSongImport(TestCase, TestMixin):
mocked_manager = MagicMock() mocked_manager = MagicMock()
# WHEN: An importer object is created # WHEN: An importer object is created
importer = OpenSongBible(mocked_manager, path='.', name='.', filename='') importer = OpenSongBible(mocked_manager, path='.', name='.', file_path=None)
# THEN: The importer should be an instance of BibleDB # THEN: The importer should be an instance of BibleDB
self.assertIsInstance(importer, BibleImport) self.assertIsInstance(importer, BibleImport)
@ -126,7 +127,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test parse_verse_number when supplied with a valid verse number Test parse_verse_number when supplied with a valid verse number
""" """
# GIVEN: An instance of OpenSongBible, the number 15 represented as a string and an instance of OpenSongBible # GIVEN: An instance of OpenSongBible, the number 15 represented as a string and an instance of OpenSongBible
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling parse_verse_number # WHEN: Calling parse_verse_number
result = importer.parse_verse_number('15', 0) result = importer.parse_verse_number('15', 0)
@ -139,7 +140,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test parse_verse_number when supplied with a verse range Test parse_verse_number when supplied with a verse range
""" """
# GIVEN: An instance of OpenSongBible, and the range 24-26 represented as a string # GIVEN: An instance of OpenSongBible, and the range 24-26 represented as a string
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling parse_verse_number # WHEN: Calling parse_verse_number
result = importer.parse_verse_number('24-26', 0) result = importer.parse_verse_number('24-26', 0)
@ -152,7 +153,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test parse_verse_number when supplied with a invalid verse number Test parse_verse_number when supplied with a invalid verse number
""" """
# GIVEN: An instance of OpenSongBible, a non numeric string represented as a string # GIVEN: An instance of OpenSongBible, a non numeric string represented as a string
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling parse_verse_number # WHEN: Calling parse_verse_number
result = importer.parse_verse_number('invalid', 41) result = importer.parse_verse_number('invalid', 41)
@ -165,7 +166,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test parse_verse_number when the verse number is an empty string. (Bug #1074727) Test parse_verse_number when the verse number is an empty string. (Bug #1074727)
""" """
# GIVEN: An instance of OpenSongBible, an empty string, and the previous verse number set as 14 # GIVEN: An instance of OpenSongBible, an empty string, and the previous verse number set as 14
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling parse_verse_number # WHEN: Calling parse_verse_number
result = importer.parse_verse_number('', 14) result = importer.parse_verse_number('', 14)
@ -178,7 +179,7 @@ class TestOpenSongImport(TestCase, TestMixin):
""" """
with patch.object(OpenSongBible, 'log_warning')as mocked_log_warning: with patch.object(OpenSongBible, 'log_warning')as mocked_log_warning:
# GIVEN: An instanceofOpenSongBible, a Tuple, and the previous verse number set as 12 # GIVEN: An instanceofOpenSongBible, a Tuple, and the previous verse number set as 12
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling parse_verse_number # WHEN: Calling parse_verse_number
result = importer.parse_verse_number((1, 2, 3), 12) result = importer.parse_verse_number((1, 2, 3), 12)
@ -193,7 +194,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test process_books when stop_import is set to True Test process_books when stop_import is set to True
""" """
# GIVEN: An instance of OpenSongBible # GIVEN: An instance of OpenSongBible
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: stop_import_flag is set to True # WHEN: stop_import_flag is set to True
importer.stop_import_flag = True importer.stop_import_flag = True
@ -209,7 +210,7 @@ class TestOpenSongImport(TestCase, TestMixin):
# GIVEN: An instance of OpenSongBible Importer and two mocked books # GIVEN: An instance of OpenSongBible Importer and two mocked books
self.mocked_find_and_create_book.side_effect = ['db_book1', 'db_book2'] self.mocked_find_and_create_book.side_effect = ['db_book1', 'db_book2']
with patch.object(OpenSongBible, 'process_chapters') as mocked_process_chapters: with patch.object(OpenSongBible, 'process_chapters') as mocked_process_chapters:
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
book1 = MagicMock() book1 = MagicMock()
book1.attrib = {'n': 'Name1'} book1.attrib = {'n': 'Name1'}
@ -236,7 +237,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test process_chapters when stop_import is set to True Test process_chapters when stop_import is set to True
""" """
# GIVEN: An isntance of OpenSongBible # GIVEN: An isntance of OpenSongBible
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
importer.parse_chapter_number = MagicMock() importer.parse_chapter_number = MagicMock()
# WHEN: stop_import_flag is set to True # WHEN: stop_import_flag is set to True
@ -252,7 +253,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test process_chapters when it completes Test process_chapters when it completes
""" """
# GIVEN: An instance of OpenSongBible # GIVEN: An instance of OpenSongBible
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
importer.wizard = MagicMock() importer.wizard = MagicMock()
# WHEN: called with some valid data # WHEN: called with some valid data
@ -284,7 +285,7 @@ class TestOpenSongImport(TestCase, TestMixin):
Test process_verses when stop_import is set to True Test process_verses when stop_import is set to True
""" """
# GIVEN: An isntance of OpenSongBible # GIVEN: An isntance of OpenSongBible
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
importer.parse_verse_number = MagicMock() importer.parse_verse_number = MagicMock()
# WHEN: stop_import_flag is set to True # WHEN: stop_import_flag is set to True
@ -303,7 +304,7 @@ class TestOpenSongImport(TestCase, TestMixin):
patch.object(OpenSongBible, 'parse_verse_number', patch.object(OpenSongBible, 'parse_verse_number',
**{'side_effect': [1, 2]}) as mocked_parse_verse_number: **{'side_effect': [1, 2]}) as mocked_parse_verse_number:
# GIVEN: An instance of OpenSongBible # GIVEN: An instance of OpenSongBible
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
importer.wizard = MagicMock() importer.wizard = MagicMock()
# WHEN: called with some valid data # WHEN: called with some valid data
@ -338,7 +339,7 @@ class TestOpenSongImport(TestCase, TestMixin):
patch.object(OpenSongBible, 'validate_xml_file'), \ patch.object(OpenSongBible, 'validate_xml_file'), \
patch.object(OpenSongBible, 'parse_xml', return_value=None), \ patch.object(OpenSongBible, 'parse_xml', return_value=None), \
patch.object(OpenSongBible, 'get_language_id') as mocked_language_id: patch.object(OpenSongBible, 'get_language_id') as mocked_language_id:
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling do_import # WHEN: Calling do_import
result = importer.do_import() result = importer.do_import()
@ -357,7 +358,7 @@ class TestOpenSongImport(TestCase, TestMixin):
patch.object(OpenSongBible, 'parse_xml'), \ patch.object(OpenSongBible, 'parse_xml'), \
patch.object(OpenSongBible, 'get_language_id', return_value=False), \ patch.object(OpenSongBible, 'get_language_id', return_value=False), \
patch.object(OpenSongBible, 'process_books') as mocked_process_books: patch.object(OpenSongBible, 'process_books') as mocked_process_books:
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling do_import # WHEN: Calling do_import
result = importer.do_import() result = importer.do_import()
@ -376,7 +377,7 @@ class TestOpenSongImport(TestCase, TestMixin):
patch.object(OpenSongBible, 'parse_xml'), \ patch.object(OpenSongBible, 'parse_xml'), \
patch.object(OpenSongBible, 'get_language_id', return_value=10), \ patch.object(OpenSongBible, 'get_language_id', return_value=10), \
patch.object(OpenSongBible, 'process_books'): patch.object(OpenSongBible, 'process_books'):
importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling do_import # WHEN: Calling do_import
result = importer.do_import() result = importer.do_import()
@ -406,7 +407,7 @@ class TestOpenSongImportFileImports(TestCase, TestMixin):
with patch('openlp.plugins.bibles.lib.importers.opensong.OpenSongBible.application'): with patch('openlp.plugins.bibles.lib.importers.opensong.OpenSongBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = OpenSongBible(mocked_manager, path='.', name='.', filename='') importer = OpenSongBible(mocked_manager, path='.', name='.', file_path=None)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock() importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
@ -416,7 +417,7 @@ class TestOpenSongImportFileImports(TestCase, TestMixin):
importer.get_language.return_value = 'Danish' importer.get_language.return_value = 'Danish'
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.file_path = Path(TEST_PATH, bible_file)
importer.do_import() importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.

View File

@ -27,6 +27,7 @@ import json
from unittest import TestCase from unittest import TestCase
from unittest.mock import MagicMock, call, patch from unittest.mock import MagicMock, call, patch
from openlp.core.common.path import Path
from openlp.plugins.bibles.lib.bibleimport import BibleImport from openlp.plugins.bibles.lib.bibleimport import BibleImport
from openlp.plugins.bibles.lib.db import BibleDB from openlp.plugins.bibles.lib.db import BibleDB
from openlp.plugins.bibles.lib.importers.osis import OSISBible from openlp.plugins.bibles.lib.importers.osis import OSISBible
@ -63,7 +64,7 @@ class TestOsisImport(TestCase):
mocked_manager = MagicMock() mocked_manager = MagicMock()
# WHEN: An importer object is created # WHEN: An importer object is created
importer = OSISBible(mocked_manager, path='.', name='.', filename='') importer = OSISBible(mocked_manager, path='.', name='.', file_path=None)
# THEN: The importer should be an instance of BibleDB # THEN: The importer should be an instance of BibleDB
self.assertIsInstance(importer, BibleDB) self.assertIsInstance(importer, BibleDB)
@ -73,7 +74,7 @@ class TestOsisImport(TestCase):
Test process_books when stop_import is set to True Test process_books when stop_import is set to True
""" """
# GIVEN: An instance of OSISBible adn some mocked data # GIVEN: An instance of OSISBible adn some mocked data
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
mocked_data = MagicMock(**{'xpath.return_value': ['Book']}) mocked_data = MagicMock(**{'xpath.return_value': ['Book']})
# WHEN: stop_import_flag is set to True and process_books is called # WHEN: stop_import_flag is set to True and process_books is called
@ -90,7 +91,7 @@ class TestOsisImport(TestCase):
# GIVEN: An instance of OSISBible Importer and two mocked books # GIVEN: An instance of OSISBible Importer and two mocked books
self.mocked_find_and_create_book.side_effect = ['db_book1', 'db_book2'] self.mocked_find_and_create_book.side_effect = ['db_book1', 'db_book2']
with patch.object(OSISBible, 'process_chapters') as mocked_process_chapters: with patch.object(OSISBible, 'process_chapters') as mocked_process_chapters:
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
book1 = MagicMock() book1 = MagicMock()
book1.get.return_value = 'Name1' book1.get.return_value = 'Name1'
@ -128,7 +129,7 @@ class TestOsisImport(TestCase):
test_chapter = MagicMock() test_chapter = MagicMock()
test_chapter.__iter__.return_value = [test_verse] test_chapter.__iter__.return_value = [test_verse]
test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x) test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x)
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_chapters # WHEN: Calling process_chapters
importer.process_chapters(test_book, [test_chapter]) importer.process_chapters(test_book, [test_chapter])
@ -155,7 +156,7 @@ class TestOsisImport(TestCase):
test_chapter = MagicMock() test_chapter = MagicMock()
test_chapter.__iter__.return_value = [test_verse] test_chapter.__iter__.return_value = [test_verse]
test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x) test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x)
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_chapters # WHEN: Calling process_chapters
importer.process_chapters(test_book, [test_chapter]) importer.process_chapters(test_book, [test_chapter])
@ -180,7 +181,7 @@ class TestOsisImport(TestCase):
test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4'}.get(x) test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4'}.get(x)
# WHEN: Calling process_chapters # WHEN: Calling process_chapters
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
importer.process_chapters(test_book, [test_chapter]) importer.process_chapters(test_book, [test_chapter])
# THEN: neither set_current_chapter or process_verse should have been called # THEN: neither set_current_chapter or process_verse should have been called
@ -201,7 +202,7 @@ class TestOsisImport(TestCase):
test_chapter = MagicMock() test_chapter = MagicMock()
test_chapter.tag = '{http://www.bibletechnologies.net/2003/OSIS/namespace}chapter' test_chapter.tag = '{http://www.bibletechnologies.net/2003/OSIS/namespace}chapter'
test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x) test_chapter.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x)
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_chapters # WHEN: Calling process_chapters
importer.process_chapters(test_book, [test_chapter]) importer.process_chapters(test_book, [test_chapter])
@ -228,7 +229,7 @@ class TestOsisImport(TestCase):
test_verse.text = 'Verse Text' test_verse.text = 'Verse Text'
# WHEN: Calling process_chapters # WHEN: Calling process_chapters
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
importer.process_chapters(test_book, [test_verse]) importer.process_chapters(test_book, [test_verse])
# THEN: process_verse should have been called with the test data # THEN: process_verse should have been called with the test data
@ -245,7 +246,7 @@ class TestOsisImport(TestCase):
test_verse.get.side_effect = lambda x: {}.get(x) test_verse.get.side_effect = lambda x: {}.get(x)
test_verse.tail = 'Verse Text' test_verse.tail = 'Verse Text'
test_verse.text = None test_verse.text = None
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_verse with the test data # WHEN: Calling process_verse with the test data
importer.process_verse(test_book, 2, test_verse) importer.process_verse(test_book, 2, test_verse)
@ -264,7 +265,7 @@ class TestOsisImport(TestCase):
test_verse.get.side_effect = lambda x: {}.get(x) test_verse.get.side_effect = lambda x: {}.get(x)
test_verse.tail = 'Verse Text' test_verse.tail = 'Verse Text'
test_verse.text = None test_verse.text = None
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_verse with the test data # WHEN: Calling process_verse with the test data
importer.process_verse(test_book, 2, test_verse) importer.process_verse(test_book, 2, test_verse)
@ -282,7 +283,7 @@ class TestOsisImport(TestCase):
test_verse.tail = None test_verse.tail = None
test_verse.text = None test_verse.text = None
test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x) test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x)
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_verse with the test data # WHEN: Calling process_verse with the test data
importer.process_verse(test_book, 2, test_verse, use_milestones=True) importer.process_verse(test_book, 2, test_verse, use_milestones=True)
@ -301,7 +302,7 @@ class TestOsisImport(TestCase):
test_verse.tail = 'Verse Text' test_verse.tail = 'Verse Text'
test_verse.text = None test_verse.text = None
test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x) test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x)
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_verse with the test data # WHEN: Calling process_verse with the test data
importer.process_verse(test_book, 2, test_verse, use_milestones=True) importer.process_verse(test_book, 2, test_verse, use_milestones=True)
@ -320,7 +321,7 @@ class TestOsisImport(TestCase):
test_verse.tail = '\n ' # Whitespace test_verse.tail = '\n ' # Whitespace
test_verse.text = None test_verse.text = None
test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x) test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x)
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_verse with the test data # WHEN: Calling process_verse with the test data
importer.process_verse(test_book, 2, test_verse) importer.process_verse(test_book, 2, test_verse)
@ -339,7 +340,7 @@ class TestOsisImport(TestCase):
test_verse.tail = '\n ' # Whitespace test_verse.tail = '\n ' # Whitespace
test_verse.text = 'Verse Text' test_verse.text = 'Verse Text'
test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x) test_verse.get.side_effect = lambda x: {'osisID': '1.2.4', 'sID': '999'}.get(x)
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling process_verse with the test data # WHEN: Calling process_verse with the test data
importer.process_verse(test_book, 2, test_verse) importer.process_verse(test_book, 2, test_verse)
@ -356,7 +357,7 @@ class TestOsisImport(TestCase):
patch.object(OSISBible, 'validate_xml_file'), \ patch.object(OSISBible, 'validate_xml_file'), \
patch.object(OSISBible, 'parse_xml', return_value=None), \ patch.object(OSISBible, 'parse_xml', return_value=None), \
patch.object(OSISBible, 'get_language_id') as mocked_language_id: patch.object(OSISBible, 'get_language_id') as mocked_language_id:
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling do_import # WHEN: Calling do_import
result = importer.do_import() result = importer.do_import()
@ -375,7 +376,7 @@ class TestOsisImport(TestCase):
patch.object(OSISBible, 'parse_xml'), \ patch.object(OSISBible, 'parse_xml'), \
patch.object(OSISBible, 'get_language_id', **{'return_value': False}), \ patch.object(OSISBible, 'get_language_id', **{'return_value': False}), \
patch.object(OSISBible, 'process_books') as mocked_process_books: patch.object(OSISBible, 'process_books') as mocked_process_books:
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling do_import # WHEN: Calling do_import
result = importer.do_import() result = importer.do_import()
@ -394,7 +395,7 @@ class TestOsisImport(TestCase):
patch.object(OSISBible, 'parse_xml'), \ patch.object(OSISBible, 'parse_xml'), \
patch.object(OSISBible, 'get_language_id', **{'return_value': 10}), \ patch.object(OSISBible, 'get_language_id', **{'return_value': 10}), \
patch.object(OSISBible, 'process_books'): patch.object(OSISBible, 'process_books'):
importer = OSISBible(MagicMock(), path='.', name='.', filename='') importer = OSISBible(MagicMock(), path='.', name='.', file_path=None)
# WHEN: Calling do_import # WHEN: Calling do_import
result = importer.do_import() result = importer.do_import()
@ -427,7 +428,7 @@ class TestOsisImportFileImports(TestCase):
with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'): with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = OSISBible(mocked_manager, path='.', name='.', filename='') importer = OSISBible(mocked_manager, path='.', name='.', file_path=None)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock() importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
@ -437,7 +438,7 @@ class TestOsisImportFileImports(TestCase):
importer.get_language.return_value = 'Danish' importer.get_language.return_value = 'Danish'
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.file_path = Path(TEST_PATH, bible_file)
importer.do_import() importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.
@ -457,7 +458,7 @@ class TestOsisImportFileImports(TestCase):
with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'): with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = OSISBible(mocked_manager, path='.', name='.', filename='') importer = OSISBible(mocked_manager, path='.', name='.', file_path=None)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock() importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
@ -467,7 +468,7 @@ class TestOsisImportFileImports(TestCase):
importer.get_language.return_value = 'English' importer.get_language.return_value = 'English'
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.file_path = Path(TEST_PATH, bible_file)
importer.do_import() importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.
@ -487,7 +488,7 @@ class TestOsisImportFileImports(TestCase):
with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'): with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = OSISBible(mocked_manager, path='.', name='.', filename='') importer = OSISBible(mocked_manager, path='.', name='.', file_path=None)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock() importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
@ -497,7 +498,7 @@ class TestOsisImportFileImports(TestCase):
importer.get_language.return_value = 'English' importer.get_language.return_value = 'English'
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.file_path = Path(TEST_PATH, bible_file)
importer.do_import() importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.
@ -517,7 +518,7 @@ class TestOsisImportFileImports(TestCase):
with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'): with patch('openlp.plugins.bibles.lib.importers.osis.OSISBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = OSISBible(mocked_manager, path='.', name='.', filename='') importer = OSISBible(mocked_manager, path='.', name='.', file_path=None)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.get_book_ref_id_by_name = MagicMock() importer.get_book_ref_id_by_name = MagicMock()
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
@ -527,7 +528,7 @@ class TestOsisImportFileImports(TestCase):
importer.get_language.return_value = 'Danish' importer.get_language.return_value = 'Danish'
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.file_path = Path(TEST_PATH, bible_file)
importer.do_import() importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.

View File

@ -64,7 +64,7 @@ class TestSwordImport(TestCase):
mocked_manager = MagicMock() mocked_manager = MagicMock()
# WHEN: An importer object is created # WHEN: An importer object is created
importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='') importer = SwordBible(mocked_manager, path='.', name='.', file_path=None, sword_key='', sword_path='')
# THEN: The importer should be an instance of BibleDB # THEN: The importer should be an instance of BibleDB
self.assertIsInstance(importer, BibleDB) self.assertIsInstance(importer, BibleDB)
@ -80,7 +80,7 @@ class TestSwordImport(TestCase):
# Also mocked pysword structures # Also mocked pysword structures
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='') importer = SwordBible(mocked_manager, path='.', name='.', file_path=None, sword_key='', sword_path='')
result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb') result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb')
test_data = json.loads(result_file.read().decode()) test_data = json.loads(result_file.read().decode())
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard

View File

@ -26,6 +26,7 @@ import os
from unittest import TestCase from unittest import TestCase
from unittest.mock import MagicMock, patch, call from unittest.mock import MagicMock, patch, call
from openlp.core.common.path import Path
from openlp.plugins.bibles.lib.importers.wordproject import WordProjectBible from openlp.plugins.bibles.lib.importers.wordproject import WordProjectBible
@ -48,19 +49,17 @@ class TestWordProjectImport(TestCase):
self.addCleanup(self.manager_patcher.stop) self.addCleanup(self.manager_patcher.stop)
self.manager_patcher.start() self.manager_patcher.start()
@patch('openlp.plugins.bibles.lib.importers.wordproject.os') @patch.object(Path, 'read_text')
@patch('openlp.plugins.bibles.lib.importers.wordproject.copen') def test_process_books(self, mocked_read_text):
def test_process_books(self, mocked_open, mocked_os):
""" """
Test the process_books() method Test the process_books() method
""" """
# GIVEN: A WordProject importer and a bunch of mocked things # GIVEN: A WordProject importer and a bunch of mocked things
importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer = WordProjectBible(MagicMock(), path='.', name='.', file_path=Path('kj.zip'))
importer.base_dir = '' importer.base_path = Path()
importer.stop_import_flag = False importer.stop_import_flag = False
importer.language_id = 'en' importer.language_id = 'en'
mocked_open.return_value.__enter__.return_value.read.return_value = INDEX_PAGE mocked_read_text.return_value = INDEX_PAGE
mocked_os.path.join.side_effect = lambda *x: ''.join(x)
# WHEN: process_books() is called # WHEN: process_books() is called
with patch.object(importer, 'find_and_create_book') as mocked_find_and_create_book, \ with patch.object(importer, 'find_and_create_book') as mocked_find_and_create_book, \
@ -69,26 +68,22 @@ class TestWordProjectImport(TestCase):
importer.process_books() importer.process_books()
# THEN: The right methods should have been called # THEN: The right methods should have been called
mocked_os.path.join.assert_called_once_with('', 'index.htm') mocked_read_text.assert_called_once_with(encoding='utf-8', errors='ignore')
mocked_open.assert_called_once_with('index.htm', encoding='utf-8', errors='ignore')
assert mocked_find_and_create_book.call_count == 66, 'There should be 66 books' assert mocked_find_and_create_book.call_count == 66, 'There should be 66 books'
assert mocked_process_chapters.call_count == 66, 'There should be 66 books' assert mocked_process_chapters.call_count == 66, 'There should be 66 books'
assert mocked_session.commit.call_count == 66, 'There should be 66 books' assert mocked_session.commit.call_count == 66, 'There should be 66 books'
@patch('openlp.plugins.bibles.lib.importers.wordproject.os') @patch.object(Path, 'read_text')
@patch('openlp.plugins.bibles.lib.importers.wordproject.copen') def test_process_chapters(self, mocked_read_text):
def test_process_chapters(self, mocked_open, mocked_os):
""" """
Test the process_chapters() method Test the process_chapters() method
""" """
# GIVEN: A WordProject importer and a bunch of mocked things # GIVEN: A WordProject importer and a bunch of mocked things
importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer = WordProjectBible(MagicMock(), path='.', name='.', file_path=Path('kj.zip'))
importer.base_dir = '' importer.base_path = Path()
importer.stop_import_flag = False importer.stop_import_flag = False
importer.language_id = 'en' importer.language_id = 'en'
mocked_open.return_value.__enter__.return_value.read.return_value = CHAPTER_PAGE mocked_read_text.return_value = CHAPTER_PAGE
mocked_os.path.join.side_effect = lambda *x: ''.join(x)
mocked_os.path.normpath.side_effect = lambda x: x
mocked_db_book = MagicMock() mocked_db_book = MagicMock()
mocked_db_book.name = 'Genesis' mocked_db_book.name = 'Genesis'
book_id = 1 book_id = 1
@ -102,24 +97,21 @@ class TestWordProjectImport(TestCase):
# THEN: The right methods should have been called # THEN: The right methods should have been called
expected_set_current_chapter_calls = [call('Genesis', ch) for ch in range(1, 51)] expected_set_current_chapter_calls = [call('Genesis', ch) for ch in range(1, 51)]
expected_process_verses_calls = [call(mocked_db_book, 1, ch) for ch in range(1, 51)] expected_process_verses_calls = [call(mocked_db_book, 1, ch) for ch in range(1, 51)]
mocked_os.path.join.assert_called_once_with('', '01/1.htm') mocked_read_text.assert_called_once_with(encoding='utf-8', errors='ignore')
mocked_open.assert_called_once_with('01/1.htm', encoding='utf-8', errors='ignore')
assert mocked_set_current_chapter.call_args_list == expected_set_current_chapter_calls assert mocked_set_current_chapter.call_args_list == expected_set_current_chapter_calls
assert mocked_process_verses.call_args_list == expected_process_verses_calls assert mocked_process_verses.call_args_list == expected_process_verses_calls
@patch('openlp.plugins.bibles.lib.importers.wordproject.os') @patch.object(Path, 'read_text')
@patch('openlp.plugins.bibles.lib.importers.wordproject.copen') def test_process_verses(self, mocked_read_text):
def test_process_verses(self, mocked_open, mocked_os):
""" """
Test the process_verses() method Test the process_verses() method
""" """
# GIVEN: A WordProject importer and a bunch of mocked things # GIVEN: A WordProject importer and a bunch of mocked things
importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer = WordProjectBible(MagicMock(), path='.', name='.', file_path=Path('kj.zip'))
importer.base_dir = '' importer.base_path = Path()
importer.stop_import_flag = False importer.stop_import_flag = False
importer.language_id = 'en' importer.language_id = 'en'
mocked_open.return_value.__enter__.return_value.read.return_value = CHAPTER_PAGE mocked_read_text.return_value = CHAPTER_PAGE
mocked_os.path.join.side_effect = lambda *x: '/'.join(x)
mocked_db_book = MagicMock() mocked_db_book = MagicMock()
mocked_db_book.name = 'Genesis' mocked_db_book.name = 'Genesis'
book_number = 1 book_number = 1
@ -130,8 +122,7 @@ class TestWordProjectImport(TestCase):
importer.process_verses(mocked_db_book, book_number, chapter_number) importer.process_verses(mocked_db_book, book_number, chapter_number)
# THEN: All the right methods should have been called # THEN: All the right methods should have been called
mocked_os.path.join.assert_called_once_with('', '01', '1.htm') mocked_read_text.assert_called_once_with(encoding='utf-8', errors='ignore')
mocked_open.assert_called_once_with('/01/1.htm', encoding='utf-8', errors='ignore')
assert mocked_process_verse.call_count == 31 assert mocked_process_verse.call_count == 31
def test_process_verse(self): def test_process_verse(self):
@ -139,7 +130,7 @@ class TestWordProjectImport(TestCase):
Test the process_verse() method Test the process_verse() method
""" """
# GIVEN: An importer and a mocked method # GIVEN: An importer and a mocked method
importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer = WordProjectBible(MagicMock(), path='.', name='.', file_path=Path('kj.zip'))
mocked_db_book = MagicMock() mocked_db_book = MagicMock()
mocked_db_book.id = 1 mocked_db_book.id = 1
chapter_number = 1 chapter_number = 1
@ -158,7 +149,7 @@ class TestWordProjectImport(TestCase):
Test the process_verse() method when there's no text Test the process_verse() method when there's no text
""" """
# GIVEN: An importer and a mocked method # GIVEN: An importer and a mocked method
importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer = WordProjectBible(MagicMock(), path='.', name='.', file_path=Path('kj.zip'))
mocked_db_book = MagicMock() mocked_db_book = MagicMock()
mocked_db_book.id = 1 mocked_db_book.id = 1
chapter_number = 1 chapter_number = 1
@ -177,7 +168,7 @@ class TestWordProjectImport(TestCase):
Test the do_import() method Test the do_import() method
""" """
# GIVEN: An importer and mocked methods # GIVEN: An importer and mocked methods
importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer = WordProjectBible(MagicMock(), path='.', name='.', file_path='kj.zip')
# WHEN: do_import() is called # WHEN: do_import() is called
with patch.object(importer, '_unzip_file') as mocked_unzip_file, \ with patch.object(importer, '_unzip_file') as mocked_unzip_file, \
@ -199,7 +190,7 @@ class TestWordProjectImport(TestCase):
Test the do_import() method when the language is not available Test the do_import() method when the language is not available
""" """
# GIVEN: An importer and mocked methods # GIVEN: An importer and mocked methods
importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer = WordProjectBible(MagicMock(), path='.', name='.', file_path='kj.zip')
# WHEN: do_import() is called # WHEN: do_import() is called
with patch.object(importer, '_unzip_file') as mocked_unzip_file, \ with patch.object(importer, '_unzip_file') as mocked_unzip_file, \

View File

@ -27,6 +27,7 @@ import json
from unittest import TestCase from unittest import TestCase
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
from openlp.core.common.path import Path
from openlp.plugins.bibles.lib.importers.zefania import ZefaniaBible from openlp.plugins.bibles.lib.importers.zefania import ZefaniaBible
from openlp.plugins.bibles.lib.db import BibleDB from openlp.plugins.bibles.lib.db import BibleDB
@ -55,7 +56,7 @@ class TestZefaniaImport(TestCase):
mocked_manager = MagicMock() mocked_manager = MagicMock()
# WHEN: An importer object is created # WHEN: An importer object is created
importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') importer = ZefaniaBible(mocked_manager, path='.', name='.', file_path=None)
# THEN: The importer should be an instance of BibleDB # THEN: The importer should be an instance of BibleDB
self.assertIsInstance(importer, BibleDB) self.assertIsInstance(importer, BibleDB)
@ -72,7 +73,7 @@ class TestZefaniaImport(TestCase):
with patch('openlp.plugins.bibles.lib.importers.zefania.ZefaniaBible.application'): with patch('openlp.plugins.bibles.lib.importers.zefania.ZefaniaBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') importer = ZefaniaBible(mocked_manager, path='.', name='.', file_path=None)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
importer.create_book = MagicMock() importer.create_book = MagicMock()
@ -81,7 +82,7 @@ class TestZefaniaImport(TestCase):
importer.get_language.return_value = 'Danish' importer.get_language.return_value = 'Danish'
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.file_path = Path(TEST_PATH, bible_file)
importer.do_import() importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.
@ -102,7 +103,7 @@ class TestZefaniaImport(TestCase):
with patch('openlp.plugins.bibles.lib.importers.zefania.ZefaniaBible.application'): with patch('openlp.plugins.bibles.lib.importers.zefania.ZefaniaBible.application'):
mocked_manager = MagicMock() mocked_manager = MagicMock()
mocked_import_wizard = MagicMock() mocked_import_wizard = MagicMock()
importer = ZefaniaBible(mocked_manager, path='.', name='.', filename='') importer = ZefaniaBible(mocked_manager, path='.', name='.', file_path=None)
importer.wizard = mocked_import_wizard importer.wizard = mocked_import_wizard
importer.create_verse = MagicMock() importer.create_verse = MagicMock()
importer.create_book = MagicMock() importer.create_book = MagicMock()
@ -111,7 +112,7 @@ class TestZefaniaImport(TestCase):
importer.get_language.return_value = 'Russian' importer.get_language.return_value = 'Russian'
# WHEN: Importing bible file # WHEN: Importing bible file
importer.filename = os.path.join(TEST_PATH, bible_file) importer.file_path = Path(TEST_PATH, bible_file)
importer.do_import() importer.do_import()
# THEN: The create_verse() method should have been called with each verse in the file. # THEN: The create_verse() method should have been called with each verse in the file.