Added support for import of Zefania XML bibles.

This commit is contained in:
Tomas Groth 2014-05-06 21:53:08 +02:00
parent fe52d50619
commit 0d48c09180
5 changed files with 166 additions and 2 deletions

View File

@ -51,6 +51,7 @@ class WizardStrings(object):
CSV = 'CSV'
OS = 'OpenSong'
OSIS = 'OSIS'
ZEF = 'Zefania'
# These strings should need a good reason to be retranslated elsewhere.
FinishedImport = translate('OpenLP.Ui', 'Finished import.')
FormatLabel = translate('OpenLP.Ui', 'Format:')

View File

@ -110,6 +110,7 @@ class BibleImportForm(OpenLPWizard):
self.csv_books_button.clicked.connect(self.on_csv_books_browse_button_clicked)
self.csv_verses_button.clicked.connect(self.on_csv_verses_browse_button_clicked)
self.open_song_browse_button.clicked.connect(self.on_open_song_browse_button_clicked)
self.zefania_browse_button.clicked.connect(self.on_zefania_browse_button_clicked)
def add_custom_pages(self):
"""
@ -125,7 +126,7 @@ class BibleImportForm(OpenLPWizard):
self.format_label = QtGui.QLabel(self.select_page)
self.format_label.setObjectName('FormatLabel')
self.format_combo_box = QtGui.QComboBox(self.select_page)
self.format_combo_box.addItems(['', '', '', ''])
self.format_combo_box.addItems(['', '', '', '', ''])
self.format_combo_box.setObjectName('FormatComboBox')
self.format_layout.addRow(self.format_label, self.format_combo_box)
self.spacer = QtGui.QSpacerItem(10, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
@ -247,6 +248,25 @@ class BibleImportForm(OpenLPWizard):
self.web_proxy_layout.setWidget(2, QtGui.QFormLayout.FieldRole, self.web_password_edit)
self.web_tab_widget.addTab(self.web_proxy_tab, '')
self.select_stack.addWidget(self.web_tab_widget)
self.zefania_widget = QtGui.QWidget(self.select_page)
self.zefania_widget.setObjectName('ZefaniaWidget')
self.zefania_layout = QtGui.QFormLayout(self.zefania_widget)
self.zefania_layout.setMargin(0)
self.zefania_layout.setObjectName('ZefaniaLayout')
self.zefania_file_label = QtGui.QLabel(self.zefania_widget)
self.zefania_file_label.setObjectName('ZefaniaFileLabel')
self.zefania_file_layout = QtGui.QHBoxLayout()
self.zefania_file_layout.setObjectName('ZefaniaFileLayout')
self.zefania_file_edit = QtGui.QLineEdit(self.zefania_widget)
self.zefania_file_edit.setObjectName('ZefaniaFileEdit')
self.zefania_file_layout.addWidget(self.zefania_file_edit)
self.zefania_browse_button = QtGui.QToolButton(self.zefania_widget)
self.zefania_browse_button.setIcon(self.open_icon)
self.zefania_browse_button.setObjectName('ZefaniaBrowseButton')
self.zefania_file_layout.addWidget(self.zefania_browse_button)
self.zefania_layout.addRow(self.zefania_file_label, self.zefania_file_layout)
self.zefania_layout.setItem(5, QtGui.QFormLayout.LabelRole, self.spacer)
self.select_stack.addWidget(self.zefania_widget)
self.select_page_layout.addLayout(self.select_stack)
self.addPage(self.select_page)
# License Page
@ -294,11 +314,13 @@ class BibleImportForm(OpenLPWizard):
self.format_combo_box.setItemText(BibleFormat.OpenSong, WizardStrings.OS)
self.format_combo_box.setItemText(BibleFormat.WebDownload, translate('BiblesPlugin.ImportWizardForm',
'Web Download'))
self.format_combo_box.setItemText(BibleFormat.Zefania, WizardStrings.ZEF)
self.osis_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:'))
self.csv_books_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Books file:'))
self.csv_verses_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Verses file:'))
self.open_song_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:'))
self.web_source_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Location:'))
self.zefania_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:'))
self.web_source_combo_box.setItemText(WebDownload.Crosswalk, translate('BiblesPlugin.ImportWizardForm',
'Crosswalk'))
self.web_source_combo_box.setItemText(WebDownload.BibleGateway, translate('BiblesPlugin.ImportWizardForm',
@ -331,7 +353,8 @@ class BibleImportForm(OpenLPWizard):
self.osis_file_label.minimumSizeHint().width(),
self.csv_books_label.minimumSizeHint().width(),
self.csv_verses_label.minimumSizeHint().width(),
self.open_song_file_label.minimumSizeHint().width())
self.open_song_file_label.minimumSizeHint().width(),
self.zefania_file_label.minimumSizeHint().width())
self.spacer.changeSize(label_width, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
def validateCurrentPage(self):
@ -366,6 +389,11 @@ class BibleImportForm(OpenLPWizard):
critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.OS)
self.open_song_file_edit.setFocus()
return False
elif self.field('source_format') == BibleFormat.Zefania:
if not self.field('zefania_file'):
critical_error_message_box(UiStrings().NFSs, WizardStrings.YouSpecifyFile % WizardStrings.ZEF)
self.zefania_file_edit.setFocus()
return False
elif self.field('source_format') == BibleFormat.WebDownload:
self.version_name_edit.setText(self.web_translation_combo_box.currentText())
return True
@ -447,6 +475,13 @@ class BibleImportForm(OpenLPWizard):
self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.OS, self.open_song_file_edit,
'last directory import')
def on_zefania_browse_button_clicked(self):
"""
Show the file open dialog for the Zefania file.
"""
self.get_file_name(WizardStrings.OpenTypeFile % WizardStrings.ZEF, self.zefania_file_edit,
'last directory import')
def register_fields(self):
"""
Register the bible import wizard fields.
@ -456,6 +491,7 @@ class BibleImportForm(OpenLPWizard):
self.select_page.registerField('csv_booksfile', self.csv_books_edit)
self.select_page.registerField('csv_versefile', self.csv_verses_edit)
self.select_page.registerField('opensong_file', self.open_song_file_edit)
self.select_page.registerField('zefania_file', self.zefania_file_edit)
self.select_page.registerField('web_location', self.web_source_combo_box)
self.select_page.registerField('web_biblename', self.web_translation_combo_box)
self.select_page.registerField('proxy_server', self.web_server_edit)
@ -479,6 +515,7 @@ class BibleImportForm(OpenLPWizard):
self.setField('csv_booksfile', '')
self.setField('csv_versefile', '')
self.setField('opensong_file', '')
self.setField('zefania_file', '')
self.setField('web_location', WebDownload.Crosswalk)
self.setField('web_biblename', self.web_translation_combo_box.currentIndex())
self.setField('proxy_server', settings.value('proxy address'))
@ -562,6 +599,10 @@ class BibleImportForm(OpenLPWizard):
proxy_username=self.field('proxy_username'),
proxy_password=self.field('proxy_password')
)
elif bible_type == BibleFormat.Zefania:
# Import an Zefania bible.
importer = self.manager.import_bible(BibleFormat.Zefania, name=license_version,
filename=self.field('zefania_file'))
if importer.do_import(license_version):
self.manager.save_meta_data(license_version, license_version, license_copyright, license_permissions)
self.manager.reload_bibles()

View File

@ -38,6 +38,7 @@ from .csvbible import CSVBible
from .http import HTTPBible
from .opensong import OpenSongBible
from .osis import OSISBible
from .zefania import ZefaniaBible
log = logging.getLogger(__name__)
@ -52,6 +53,7 @@ class BibleFormat(object):
CSV = 1
OpenSong = 2
WebDownload = 3
Zefania = 4
@staticmethod
def get_class(bible_format):
@ -68,6 +70,8 @@ class BibleFormat(object):
return OpenSongBible
elif bible_format == BibleFormat.WebDownload:
return HTTPBible
elif bible_format == BibleFormat.Zefania:
return ZefaniaBible
else:
return None
@ -81,6 +85,7 @@ class BibleFormat(object):
BibleFormat.CSV,
BibleFormat.OpenSong,
BibleFormat.WebDownload,
BibleFormar.Zefania,
]

View File

@ -81,6 +81,13 @@ class OpenSongBible(BibleDB):
import_file = open(self.filename, 'rb')
opensong = objectify.parse(import_file)
bible = opensong.getroot()
# Check that we're not trying to import a Zefania XML bible, it is sometimes refered to as 'OpenSong'
if bible.tag.upper() == 'XMLBIBLE':
critical_error_message_box(
message=translate('BiblesPlugin.OpenSongImport',
'Incorrect Bible file type supplied. This looks like a Zefania XML bible, '
'please use the Zefania import option.'))
return False
language_id = self.get_language(bible_name)
if not language_id:
log.error('Importing books from "%s" failed' % self.filename)

View File

@ -0,0 +1,110 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2014 Raoul Snyman #
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
import logging
from lxml import etree, objectify
from openlp.core.common import translate
from openlp.core.lib.ui import critical_error_message_box
from openlp.plugins.bibles.lib.db import BibleDB, BiblesResourcesDB
log = logging.getLogger(__name__)
class ZefaniaBible(BibleDB):
"""
Zefania Bible format importer class.
"""
def __init__(self, parent, **kwargs):
"""
Constructor to create and set up an instance of the ZefaniaBible class. This class is used to import Bibles
from ZefaniaBible's XML format.
"""
log.debug(self.__class__.__name__)
BibleDB.__init__(self, parent, **kwargs)
self.filename = kwargs['filename']
def do_import(self, bible_name=None):
"""
Loads a Bible from file.
"""
log.debug('Starting Zefania import from "%s"' % self.filename)
if not isinstance(self.filename, str):
self.filename = str(self.filename, 'utf8')
import_file = None
success = True
try:
# NOTE: We don't need to do any of the normal encoding detection here, because lxml does it's own encoding
# detection, and the two mechanisms together interfere with each other.
import_file = open(self.filename, 'rb')
language_id = self.get_language(bible_name)
if not language_id:
log.error('Importing books from "%s" failed' % self.filename)
return False
zefania_bible_tree = etree.parse(import_file)
num_books = int(zefania_bible_tree.xpath("count(//BIBLEBOOK)"))
# Strip tags we don't use
etree.strip_tags(zefania_bible_tree, ('STYLE', 'GRAM', 'NOTE', 'SUP', 'XREF'))
xmlbible = zefania_bible_tree.getroot()
for BIBLEBOOK in xmlbible:
book_ref_id = self.get_book_ref_id_by_name(str(BIBLEBOOK.get('bname')), num_books)
if not book_ref_id:
book_ref_id = self.get_book_ref_id_by_localised_name(str(BIBLEBOOK.get('bname')))
if not book_ref_id:
log.error('Importing books from "%s" failed' % self.filename)
return False
book_details = BiblesResourcesDB.get_book_by_id(book_ref_id)
db_book = self.create_book(book_details['name'], book_ref_id, book_details['testament_id'])
for CHAPTER in BIBLEBOOK:
if self.stop_import_flag:
break
chapter_number = CHAPTER.get("cnumber")
for VERS in CHAPTER:
verse_number = VERS.get("vnumber")
self.create_verse(db_book.id, chapter_number, verse_number, VERS.text.replace('<BR/>', '\n'))
self.wizard.increment_progress_bar(
translate('BiblesPlugin.Zefnia', 'Importing %(bookname)s %(chapter)s...' %
{'bookname': db_book.name, 'chapter': chapter_number}))
self.session.commit()
self.application.process_events()
except Exception as e:
critical_error_message_box(
message=translate('BiblesPlugin.ZefaniaImport',
'Incorrect Bible file type supplied. Zefania Bibles may be '
'compressed. You must decompress them before import.'))
log.exception(str(e))
success = False
finally:
if import_file:
import_file.close()
if self.stop_import_flag:
return False
else:
return success