openlp/openlp/plugins/bibles/lib/bibleOSISimpl.py

177 lines
7.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2009 Raoul Snyman #
# Portions copyright (c) 2008-2009 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard #
# --------------------------------------------------------------------------- #
# 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 os
import os.path
import logging
import chardet
2009-07-10 15:41:08 +00:00
import codecs
import re
from PyQt4 import QtCore
2009-10-24 16:40:36 +00:00
from openlp.core.lib import Receiver
class BibleOSISImpl():
"""
OSIS Bible format importer class.
"""
global log
log = logging.getLogger(u'BibleOSISImpl')
log.info(u'BibleOSISImpl loaded')
def __init__(self, biblepath, bibledb):
"""
Constructor to create and set up an instance of the
BibleOSISImpl class.
``biblepath``
This does not seem to be used.
``bibledb``
A reference to a Bible database object.
"""
2009-09-26 14:36:08 +00:00
log.info(u'BibleOSISImpl Initialising')
2009-12-24 18:03:49 +00:00
self.verse_regex = re.compile(
r'<verse osisID="([a-zA-Z0-9 ]*).([0-9]*).([0-9]*)">(.*?)</verse>')
2009-12-23 22:31:14 +00:00
self.note_regex = re.compile(r'<note(.*?)>(.*?)</note>')
self.title_regex = re.compile(r'<title(.*?)>(.*?)</title>')
self.milestone_regex = re.compile(r'<milestone(.*?)/>')
2009-12-24 15:12:09 +00:00
self.fi_regex = re.compile(r'<FI>(.*?)<Fi>')
self.rf_regex = re.compile(r'<RF>(.*?)<Rf>')
2009-12-23 22:31:14 +00:00
self.lb_regex = re.compile(r'<lb(.*?)>')
self.l_regex = re.compile(r'<l (.*?)>')
self.w_regex = re.compile(r'<w (.*?)>')
self.q_regex = re.compile(r'<q (.*?)>')
self.spaces_regex = re.compile(r'([ ]{2,})')
self.bibledb = bibledb
2009-12-23 22:31:14 +00:00
self.books = {}
filepath = os.path.split(os.path.abspath(__file__))[0]
filepath = os.path.abspath(os.path.join(
filepath, u'..', u'resources', u'osisbooks.csv'))
2009-11-07 00:00:36 +00:00
fbibles = None
2009-12-11 17:40:18 +00:00
self.loadbible = True
2009-11-07 00:00:36 +00:00
try:
fbibles = open(filepath, u'r')
for line in fbibles:
2009-12-24 15:12:09 +00:00
book = line.split(u',')
self.books[book[0]] = (book[1].lstrip().rstrip(),
book[2].lstrip().rstrip())
2009-11-07 00:00:36 +00:00
except:
log.exception(u'OSIS bible import failed')
finally:
if fbibles:
fbibles.close()
QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'openlpstopimport'), self.stop_import)
2009-02-11 19:08:18 +00:00
def stop_import(self):
"""
Stops the import of the Bible.
"""
self.loadbible = False
def load_data(self, osisfile_record, dialogobject=None):
"""
Loads a Bible from file.
``osisfile_record``
The file to import from.
``dialogobject``
The Import dialog, so that we can increase the counter on
the progress bar.
"""
2009-12-11 17:40:18 +00:00
log.info(u'Load data for %s' % osisfile_record)
2009-11-07 00:00:36 +00:00
detect_file = None
try:
detect_file = open(osisfile_record, u'r')
details = chardet.detect(detect_file.read(3000))
2009-11-07 00:00:36 +00:00
except:
log.exception(u'Failed to detect OSIS file encoding')
return
finally:
if detect_file:
detect_file.close()
osis = None
try:
osis = codecs.open(osisfile_record, u'r', details['encoding'])
2009-12-23 22:31:14 +00:00
last_chapter = 0
testament = 1
2009-12-24 15:12:09 +00:00
db_book = None
for file_record in osis:
match = self.verse_regex.search(file_record)
if match:
2009-12-23 22:31:14 +00:00
book = match.group(1)
chapter = int(match.group(2))
verse = int(match.group(3))
verse_text = match.group(4)
2009-12-24 15:12:09 +00:00
if not db_book or db_book.name != book:
if book == u'Matt':
testament += 1
db_book = self.bibledb.create_book(
unicode(self.books[book][0]),
unicode(self.books[book][1]),
testament)
2009-12-23 22:31:14 +00:00
if last_chapter == 0:
if book == u'Gen':
dialogobject.ImportProgressBar.setMaximum(1188)
else:
dialogobject.ImportProgressBar.setMaximum(260)
if last_chapter != chapter:
if last_chapter != 0:
self.bibledb.save_verses()
dialogobject.incrementProgressBar(
u'Importing %s %s...' % \
(self.books[match.group(1)][0], chapter))
last_chapter = chapter
2009-12-24 15:12:09 +00:00
# All of this rigmarol below is because the mod2osis
# tool from the Sword library embeds XML in the OSIS
# but neglects to enclose the verse text (with XML) in
# <[CDATA[ ]]> tags.
2009-12-23 22:31:14 +00:00
verse_text = self.note_regex.sub(u'', verse_text)
verse_text = self.title_regex.sub(u'', verse_text)
verse_text = self.milestone_regex.sub(u'', verse_text)
2009-12-24 15:12:09 +00:00
verse_text = self.fi_regex.sub(u'', verse_text)
verse_text = self.rf_regex.sub(u'', verse_text)
2009-12-23 22:31:14 +00:00
verse_text = self.lb_regex.sub(u'', verse_text)
verse_text = self.l_regex.sub(u'', verse_text)
verse_text = self.w_regex.sub(u'', verse_text)
verse_text = self.q_regex.sub(u'', verse_text)
verse_text = verse_text.replace(u'</lb>', u'')\
.replace(u'</l>', u'').replace(u'<lg>', u'')\
.replace(u'</lg>', u'').replace(u'</q>', u'')\
.replace(u'</div>', u'')
verse_text = self.spaces_regex.sub(u' ', verse_text)
self.bibledb.add_verse(db_book.id, chapter, verse, verse_text)
self.bibledb.save_verses()
dialogobject.incrementProgressBar(u'Finishing import...')
2009-11-07 00:00:36 +00:00
except:
log.exception(u'Loading bible from OSIS file failed')
finally:
if osis:
osis.close()