2008-11-19 18:13:22 +00:00
|
|
|
"""
|
|
|
|
OpenLP - Open Source Lyrics Projection
|
|
|
|
Copyright (c) 2008 Raoul Snyman
|
2009-02-22 07:44:08 +00:00
|
|
|
Portions copyright (c) 2008 - 2009 Martin Thompson, Tim Bentley
|
2008-11-19 18:13:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2009-02-15 19:11:27 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2009-02-02 19:54:38 +00:00
|
|
|
import logging
|
2008-12-06 09:33:46 +00:00
|
|
|
from openlp.plugins.bibles.lib.bibleDBimpl import BibleDBImpl
|
2009-02-08 15:25:00 +00:00
|
|
|
from openlp.core.lib import Receiver
|
2009-02-11 19:08:18 +00:00
|
|
|
from PyQt4 import QtCore
|
2008-11-19 18:13:22 +00:00
|
|
|
|
|
|
|
class BibleOSISImpl():
|
|
|
|
global log
|
|
|
|
log=logging.getLogger("BibleOSISImpl")
|
|
|
|
log.info("BibleOSISImpl loaded")
|
2008-12-13 19:42:25 +00:00
|
|
|
def __init__(self, biblepath, bibledb):
|
2008-11-19 18:13:22 +00:00
|
|
|
self.bibledb = bibledb
|
2008-11-24 21:27:49 +00:00
|
|
|
self.booksOfBible = {} # books of the bible linked to bibleid {osis , name}
|
2009-02-15 19:11:27 +00:00
|
|
|
self.abbrevOfBible = {} # books of the bible linked to bibleid {osis ,Abbrev }
|
|
|
|
|
|
|
|
filepath = os.path.split(os.path.abspath(__file__))[0]
|
|
|
|
filepath = os.path.abspath(os.path.join(filepath, '..', 'resources','osisbooks.csv'))
|
|
|
|
fbibles=open(filepath, 'r')
|
2008-11-24 21:27:49 +00:00
|
|
|
for line in fbibles:
|
|
|
|
p = line.split(",")
|
|
|
|
self.booksOfBible[p[0]] = p[1].replace('\n', '')
|
2009-02-11 19:08:18 +00:00
|
|
|
self.abbrevOfBible[p[0]] = p[2].replace('\n', '')
|
|
|
|
self.loadbible = True
|
|
|
|
QtCore.QObject.connect(Receiver().get_receiver(),QtCore.SIGNAL("openlpstopimport"),self.stop_import)
|
2008-11-24 21:27:49 +00:00
|
|
|
|
2009-02-11 19:08:18 +00:00
|
|
|
def stop_import(self):
|
|
|
|
self.loadbible= False
|
2008-11-19 18:13:22 +00:00
|
|
|
|
2008-12-16 17:29:39 +00:00
|
|
|
def load_data(self, osisfile, dialogobject=None):
|
2008-11-19 18:13:22 +00:00
|
|
|
osis=open(osisfile, 'r')
|
|
|
|
|
2009-02-02 19:54:38 +00:00
|
|
|
book_ptr = None
|
2008-11-19 18:13:22 +00:00
|
|
|
id = 0
|
2009-02-08 15:25:00 +00:00
|
|
|
count = 0
|
2008-11-19 18:13:22 +00:00
|
|
|
verseText = "<verse osisID="
|
2009-02-02 19:54:38 +00:00
|
|
|
testament = 1
|
2009-03-07 06:59:08 +00:00
|
|
|
for file in osis.readlines():
|
2009-03-12 19:52:37 +00:00
|
|
|
# cancel pressed on UI
|
|
|
|
if self.loadbible == False:
|
2009-02-11 19:08:18 +00:00
|
|
|
break
|
2009-03-07 06:59:08 +00:00
|
|
|
# print file
|
|
|
|
pos = file.find(verseText)
|
|
|
|
if pos > -1: # we have a verse
|
|
|
|
epos= file.find(">", pos)
|
|
|
|
ref = file[pos+15:epos-1] # Book Reference
|
2009-03-12 19:52:37 +00:00
|
|
|
|
|
|
|
#lets find the bible text only
|
2009-03-07 06:59:08 +00:00
|
|
|
pos = epos + 1 # find start of text
|
|
|
|
epos = file.find("</verse>", pos) # end of text
|
|
|
|
text = file[pos : epos]
|
|
|
|
#print pos, e, f[pos:e] # Found Basic Text
|
2008-11-19 18:13:22 +00:00
|
|
|
|
2009-03-12 19:52:37 +00:00
|
|
|
#remove tags of extra information
|
|
|
|
text = self.remove_block('<title','</title>', text)
|
|
|
|
text = self.remove_block('<note','</note>', text)
|
|
|
|
text = self.remove_block('<divineName','</divineName>', text)
|
2009-03-07 06:59:08 +00:00
|
|
|
|
2009-03-12 19:52:37 +00:00
|
|
|
text = self.remove_tag('<lb', text)
|
|
|
|
text = self.remove_tag('<q', text)
|
|
|
|
text = self.remove_tag('<l', text)
|
|
|
|
text = self.remove_tag('<lg', text)
|
2008-11-19 18:13:22 +00:00
|
|
|
|
2009-03-12 19:52:37 +00:00
|
|
|
# Strange tags where the end is not the same as the start
|
|
|
|
# The must be in this order as at least one bible has them
|
|
|
|
# crossing and the removal does not work.
|
2009-03-07 06:59:08 +00:00
|
|
|
pos = text.find("<FI>")
|
|
|
|
while pos > -1:
|
|
|
|
epos = text.find("<Fi>", pos)
|
|
|
|
if epos == -1: # TODO
|
|
|
|
#print "Y", search_text, e
|
|
|
|
pos = -1
|
|
|
|
else:
|
|
|
|
text = text[:pos] + text[epos + 4: ]
|
|
|
|
pos = text.find("<FI>")
|
|
|
|
|
|
|
|
pos = text.find("<RF>")
|
|
|
|
while pos > -1:
|
|
|
|
epos = text.find("<Rf>", pos)
|
|
|
|
text = text[:pos] + text[epos + 4: ]
|
|
|
|
#print "X", pos, epos, text
|
|
|
|
pos = text.find("<RF>")
|
2008-11-19 18:13:22 +00:00
|
|
|
|
2009-02-02 19:54:38 +00:00
|
|
|
p = ref.split(".", 3) # split up the reference
|
2009-03-07 06:59:08 +00:00
|
|
|
#print p, ">>>", text
|
|
|
|
|
2008-11-19 18:13:22 +00:00
|
|
|
if book_ptr != p[0]:
|
2009-02-02 19:54:38 +00:00
|
|
|
if book_ptr == None: # first time through
|
|
|
|
if p[0] == "Gen": # set the max book size depending on the first book read
|
2009-02-08 15:25:00 +00:00
|
|
|
dialogobject.setMax(65)
|
2009-02-02 19:54:38 +00:00
|
|
|
else:
|
|
|
|
dialogobject.setMax(27)
|
2009-02-08 15:25:00 +00:00
|
|
|
if p[0] == "Matt": # First book of NT
|
2009-02-02 19:54:38 +00:00
|
|
|
testament += 1
|
2008-11-19 18:13:22 +00:00
|
|
|
book_ptr = p[0]
|
2009-02-02 19:54:38 +00:00
|
|
|
book = self.bibledb.create_book(self.booksOfBible[p[0]] , self.abbrevOfBible[p[0]], testament)
|
2009-03-07 06:59:08 +00:00
|
|
|
dialogobject.incrementProgressBar(self.booksOfBible[p[0]] )
|
2009-02-11 19:08:18 +00:00
|
|
|
Receiver().send_message("openlpprocessevents")
|
2009-02-08 15:25:00 +00:00
|
|
|
count = 0
|
2009-03-07 06:59:08 +00:00
|
|
|
self.bibledb.add_verse(book.id, p[1], p[2], text)
|
2009-02-08 15:25:00 +00:00
|
|
|
count += 1
|
2009-03-12 19:52:37 +00:00
|
|
|
if count % 3 == 0: #Every 3 verses repaint the screen
|
2009-02-11 19:08:18 +00:00
|
|
|
Receiver().send_message("openlpprocessevents")
|
2009-02-08 15:25:00 +00:00
|
|
|
count = 0
|
2009-03-12 19:52:37 +00:00
|
|
|
|
|
|
|
def remove_block(self, start_tag, end_tag, text):
|
|
|
|
"""
|
|
|
|
removes a block of text between two tags
|
|
|
|
<tag attrib=xvf > Some not wanted text </tag>
|
|
|
|
"""
|
|
|
|
pos = text.find(start_tag)
|
|
|
|
while pos > -1:
|
|
|
|
epos = text.find(end_tag, pos)
|
|
|
|
if epos == -1: # TODO
|
|
|
|
#print "Y", pos, epos
|
|
|
|
pos = -1
|
|
|
|
else:
|
|
|
|
text = text[:pos] + text[epos + len(end_tag): ]
|
|
|
|
pos = text.find(start_tag)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def remove_tag(self, start_tag, text):
|
|
|
|
"""
|
|
|
|
removes a single tag
|
|
|
|
<tag attrib1=fajkdf attrib2=fajkdf attrib2=fajkdf />
|
|
|
|
"""
|
|
|
|
pos = text.find(start_tag)
|
|
|
|
while pos > -1:
|
|
|
|
epos = text.find('/>', pos)
|
|
|
|
text = text[:pos] + text[epos + 2: ]
|
|
|
|
pos = text.find(start_tag)
|
|
|
|
return text
|
2008-11-19 18:13:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|