forked from openlp/openlp
Add lazy load of bible
bzr-revno: 64
This commit is contained in:
parent
85e3022672
commit
e714b1e5ce
@ -116,7 +116,6 @@ class BibleDBImpl:
|
|||||||
metadata.bind.echo = False
|
metadata.bind.echo = False
|
||||||
self.Session = sessionmaker()
|
self.Session = sessionmaker()
|
||||||
self.Session.configure(bind=self.db)
|
self.Session.configure(bind=self.db)
|
||||||
self.book_ptr = ""
|
|
||||||
|
|
||||||
def createTables(self):
|
def createTables(self):
|
||||||
if os.path.exists(self.biblefile): # delete bible file and set it up again
|
if os.path.exists(self.biblefile): # delete bible file and set it up again
|
||||||
@ -127,37 +126,57 @@ class BibleDBImpl:
|
|||||||
verse_table.create()
|
verse_table.create()
|
||||||
self.loadMeta("dbversion", "0.1")
|
self.loadMeta("dbversion", "0.1")
|
||||||
|
|
||||||
def loadVerse(self, book, chapter, verse, text):
|
def createChapter(self, bk, chap, textlist):
|
||||||
metadata.bind.echo = True
|
print "createChapter ", bk, chap, textlist
|
||||||
|
metadata.bind.echo = False
|
||||||
session = self.Session()
|
session = self.Session()
|
||||||
if self.book_ptr is not book:
|
s = text (""" select id FROM book where book.name == :b """)
|
||||||
query = session.query(Book).filter(Book.name==book)
|
data = self.db.execute(s, b=bk).fetchone()
|
||||||
#print query.first().id
|
id = data[0] # id is first record in list.
|
||||||
self.book_ptr = book
|
#print "id = " , id
|
||||||
#print text
|
for v , t in textlist.iteritems():
|
||||||
versemeta = Verse(book_id=query.first().id, chapter=int(chapter), verse=int(verse), text=(text))
|
versemeta = Verse(book_id=id, chapter=int(chap), verse=int(v), text=(t))
|
||||||
session.add(versemeta)
|
session.add(versemeta)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
def createBook(self, bk):
|
||||||
|
print "createBook ", bk
|
||||||
|
metadata.bind.echo = False
|
||||||
|
session = self.Session()
|
||||||
|
bookmeta = Book(int(5), bk, bk)
|
||||||
|
session.add(bookmeta)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
def loadMeta(self, key, value):
|
def loadMeta(self, key, value):
|
||||||
|
metadata.bind.echo = False
|
||||||
session = self.Session()
|
session = self.Session()
|
||||||
bmeta= BibleMeta(key, value)
|
bmeta= BibleMeta(key, value)
|
||||||
session.add(bmeta)
|
session.add(bmeta)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
def loadData(self, booksfile, versesfile):
|
def getMeta(self, key):
|
||||||
self.loadMeta("version", "Bible Version")
|
s = text (""" select value FROM meta where key == :k """)
|
||||||
self.loadMeta("Copyright", "(c) Some Bible company")
|
return self.db.execute(s, k=key).fetchone()
|
||||||
self.loadMeta("Permission", "You Have Some")
|
|
||||||
|
|
||||||
|
def _loadTestaments(self):
|
||||||
|
metadata.bind.echo = False
|
||||||
session = self.Session()
|
session = self.Session()
|
||||||
#Populate the Tables
|
|
||||||
testmeta = ONTestament(name="Old Testament")
|
testmeta = ONTestament(name="Old Testament")
|
||||||
session.add(testmeta)
|
session.add(testmeta)
|
||||||
testmeta = ONTestament(name="New Testament")
|
testmeta = ONTestament(name="New Testament")
|
||||||
session.add(testmeta)
|
session.add(testmeta)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def loadData(self, booksfile, versesfile):
|
||||||
|
self.loadMeta("version", "Bible Version")
|
||||||
|
self.loadMeta("Copyright", "(c) Some Bible company")
|
||||||
|
self.loadMeta("Permission", "You Have Some")
|
||||||
|
|
||||||
|
self._loadTestaments()
|
||||||
|
session = self.Session()
|
||||||
|
|
||||||
|
#Populate the Tables
|
||||||
fbooks=open(booksfile, 'r')
|
fbooks=open(booksfile, 'r')
|
||||||
fverse=open(versesfile, 'r')
|
fverse=open(versesfile, 'r')
|
||||||
|
|
||||||
@ -170,48 +189,54 @@ class BibleDBImpl:
|
|||||||
session.add(bookmeta)
|
session.add(bookmeta)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
book_ptr = ""
|
||||||
for line in fverse:
|
for line in fverse:
|
||||||
#print line
|
#print line
|
||||||
p = line.split(",", 3) # split into 3 units and leave the rest as a single field
|
p = line.split(",", 3) # split into 3 units and leave the rest as a single field
|
||||||
p[0] = self._cleanText(p[0])
|
p[0] = self._cleanText(p[0])
|
||||||
p[3] = self._cleanText(p[3])
|
p[3] = self._cleanText(p[3])
|
||||||
self.loadVerse(p[0], p[1], p[2], p[3])
|
if book_ptr is not p[0]:
|
||||||
|
query = session.query(Book).filter(Book.name==p[0])
|
||||||
|
#print query
|
||||||
|
#print query.first()
|
||||||
|
#print query.first().id
|
||||||
|
book_ptr = p[0]
|
||||||
|
#print text
|
||||||
|
versemeta = Verse(book_id=query.first().id, chapter=int(p[1]), verse=int(p[2]), text=(p[3]))
|
||||||
|
session.add(versemeta)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
def getBibleBook(self, bookname):
|
||||||
|
print "getBibleBook ", bookname
|
||||||
|
metadata.bind.echo = False
|
||||||
|
s = text (""" select name FROM book where book.name == :b """)
|
||||||
|
return self.db.execute(s, b=bookname).fetchone()
|
||||||
|
|
||||||
def getBibleChapter(self, bookname, chapter):
|
def getBibleChapter(self, bookname, chapter):
|
||||||
s = text (""" select text FROM verse,book where verse.book_id == book.id AND verse.chapter == :c and book.name == :b """)
|
print "getBibleChapter ", bookname, chapter
|
||||||
|
metadata.bind.echo = False
|
||||||
|
s = text (""" select book.name FROM verse,book where verse.book_id == book.id AND verse.chapter == :c and book.name == :b """)
|
||||||
|
print s
|
||||||
|
print self.db.execute(s, c=chapter, b=bookname).fetchone()
|
||||||
return self.db.execute(s, c=chapter, b=bookname).fetchone()
|
return self.db.execute(s, c=chapter, b=bookname).fetchone()
|
||||||
|
|
||||||
def getBibleText(self, bookname, chapter, sverse, everse):
|
def getBibleText(self, bookname, chapter, sverse, everse):
|
||||||
metadata.bind.echo = True
|
print "getBibleText ", bookname, chapter, sverse, everse
|
||||||
s = text (""" select text FROM verse,book where verse.book_id == book.id AND verse.chapter == :c AND (verse.verse between :v1 and :v2) and book.name == :b """)
|
metadata.bind.echo = False
|
||||||
|
s = text (""" select verse.verse, verse.text FROM verse,book where verse.book_id == book.id AND verse.chapter == :c AND (verse.verse between :v1 and :v2) and book.name == :b """)
|
||||||
return self.db.execute(s, c=chapter, v1=sverse , v2=everse, b=bookname).fetchall()
|
return self.db.execute(s, c=chapter, v1=sverse , v2=everse, b=bookname).fetchall()
|
||||||
|
|
||||||
|
def dumpBible(self):
|
||||||
|
print ".........Dumping Bible Database"
|
||||||
|
print "...............................Books "
|
||||||
|
s = text (""" select * FROM book """)
|
||||||
|
print self.db.execute(s).fetchall()
|
||||||
|
print "...............................Verses "
|
||||||
|
s = text (""" select * FROM verse """)
|
||||||
|
print self.db.execute(s).fetchall()
|
||||||
|
|
||||||
def _cleanText(self, text):
|
def _cleanText(self, text):
|
||||||
text = text.replace('\n', '')
|
text = text.replace('\n', '')
|
||||||
text = text.replace('\r', '')
|
text = text.replace('\r', '')
|
||||||
text = text.replace('"', '')
|
text = text.replace('"', '')
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def Run_Tests(self):
|
|
||||||
metadata.bind.echo = True
|
|
||||||
print "test print"
|
|
||||||
session = self.Session()
|
|
||||||
print session.query(Book).filter(Book.name=='John').all()
|
|
||||||
q = session.query(Verse).filter(Verse.book_id==8)
|
|
||||||
print q.first()
|
|
||||||
|
|
||||||
q = session.query(Verse, Book).filter(Verse.chapter==1).filter(Verse.verse==1).filter(Book.name=='Genesis')
|
|
||||||
print "--"
|
|
||||||
#print q.first()[0].text
|
|
||||||
#print q.first()[1].name
|
|
||||||
#print "----"
|
|
||||||
ch =1
|
|
||||||
vs = 1
|
|
||||||
bk = 'Genesis'
|
|
||||||
s = text (""" select text FROM verse,book where verse.book_id == book.id AND verse.chapter == :c and verse.verse == :v and book.name == :b """)
|
|
||||||
print self.db.execute(s, c=ch, v=vs , b=bk).fetchall()
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,6 +30,11 @@ class BibleHTTPImpl:
|
|||||||
Init confirms the bible exists and stores the database path.
|
Init confirms the bible exists and stores the database path.
|
||||||
"""
|
"""
|
||||||
bible = {}
|
bible = {}
|
||||||
|
biblesoure = ""
|
||||||
|
|
||||||
|
def setBibleSource(self,biblesource):
|
||||||
|
self.biblesource = biblesource
|
||||||
|
|
||||||
def getBibleChapter(self, version, book, chapter):
|
def getBibleChapter(self, version, book, chapter):
|
||||||
urlstring = "http://bible.crosswalk.com/OnlineStudyBible/bible.cgi?word="+book+"+"+str(chapter)+"&version="+version
|
urlstring = "http://bible.crosswalk.com/OnlineStudyBible/bible.cgi?word="+book+"+"+str(chapter)+"&version="+version
|
||||||
print urlstring
|
print urlstring
|
||||||
|
@ -22,8 +22,8 @@ mypath=os.path.split(os.path.abspath(__file__))[0]
|
|||||||
sys.path.insert(0,(os.path.join(mypath, '..', '..', '..')))
|
sys.path.insert(0,(os.path.join(mypath, '..', '..', '..')))
|
||||||
|
|
||||||
from openlp.utils import ConfigHelper
|
from openlp.utils import ConfigHelper
|
||||||
from openlp.plugins.biblemanager.BibleDBImpl import *
|
from openlp.plugins.biblemanager.BibleDBImpl import BibleDBImpl
|
||||||
from openlp.plugins.biblemanager.BibleHTTPImpl import *
|
from openlp.plugins.biblemanager.BibleHTTPImpl import BibleHTTPImpl
|
||||||
|
|
||||||
class BibleManager:
|
class BibleManager:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -42,8 +42,15 @@ class BibleManager:
|
|||||||
for f in files:
|
for f in files:
|
||||||
b = f.split('.')[0]
|
b = f.split('.')[0]
|
||||||
self.bibleDBCache[b] = BibleDBImpl(b)
|
self.bibleDBCache[b] = BibleDBImpl(b)
|
||||||
print self.bibleDBCache
|
biblesource = self.bibleDBCache[b].getMeta("WEB") # look to see if lazy load bible and get create getter.
|
||||||
print self.bibleHTTPCache
|
if biblesource:
|
||||||
|
nhttp = BibleHTTPImpl()
|
||||||
|
nhttp.setBibleSource(biblesource)
|
||||||
|
self.bibleHTTPCache[b] = nhttp
|
||||||
|
#
|
||||||
|
|
||||||
|
#print self.bibleDBCache
|
||||||
|
#print self.bibleHTTPCache
|
||||||
|
|
||||||
def registerHTTPBible(self, name, biblesource, proxy, proxyport, proxyid, proxypass):
|
def registerHTTPBible(self, name, biblesource, proxy, proxyport, proxyid, proxypass):
|
||||||
"""
|
"""
|
||||||
@ -54,7 +61,9 @@ class BibleManager:
|
|||||||
nbible = BibleDBImpl(name) # Create new Bible
|
nbible = BibleDBImpl(name) # Create new Bible
|
||||||
nbible.createTables() # Create Database
|
nbible.createTables() # Create Database
|
||||||
self.bibleDBCache[name] = nbible
|
self.bibleDBCache[name] = nbible
|
||||||
|
|
||||||
nhttp = BibleHTTPImpl()
|
nhttp = BibleHTTPImpl()
|
||||||
|
nhttp.setBibleSource(biblesource)
|
||||||
self.bibleHTTPCache[name] = nhttp
|
self.bibleHTTPCache[name] = nhttp
|
||||||
nbible.loadMeta("WEB", biblesource)
|
nbible.loadMeta("WEB", biblesource)
|
||||||
|
|
||||||
@ -98,22 +107,32 @@ class BibleManager:
|
|||||||
If the end verse(everse) is less then the start verse(sverse)
|
If the end verse(everse) is less then the start verse(sverse)
|
||||||
then only one verse is returned
|
then only one verse is returned
|
||||||
"""
|
"""
|
||||||
print self.bibleDBCache
|
#print self.bibleDBCache
|
||||||
print self.bibleHTTPCache
|
#print self.bibleHTTPCache
|
||||||
|
print "getchapter ", bible, book, chapter, sverse, everse
|
||||||
c = self.bibleDBCache[bible].getBibleChapter(book, chapter) # check to see if book/chapter exists
|
c = self.bibleDBCache[bible].getBibleChapter(book, chapter) # check to see if book/chapter exists
|
||||||
|
print c
|
||||||
if not c:
|
if not c:
|
||||||
|
self._loadBook(bible,book)
|
||||||
self._loadChapter(bible, book, chapter)
|
self._loadChapter(bible, book, chapter)
|
||||||
if everse < sverse:
|
if everse < sverse:
|
||||||
everse = sverse
|
everse = sverse
|
||||||
text = self.bibleDBCache[bible].getBibleText(book, chapter, sverse, everse)
|
text = self.bibleDBCache[bible].getBibleText(book, chapter, sverse, everse)
|
||||||
print text
|
#print text
|
||||||
|
#self.bibleDBCache[bible].dumpBible()
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
def _loadBook(self, bible, book):
|
||||||
|
print "loadbook ", bible, book
|
||||||
|
cl = self.bibleDBCache[bible].getBibleBook(book)
|
||||||
|
#print cl
|
||||||
|
if not cl :
|
||||||
|
self.bibleDBCache[bible].createBook(book)
|
||||||
|
|
||||||
def _loadChapter(self, bible, book, chapter):
|
def _loadChapter(self, bible, book, chapter):
|
||||||
cl = self.bibleHTTPCache[bible].getBibleChapter(bible, book, chapter)
|
print "loadChapter ", bible, book, chapter
|
||||||
for v , t in cl.iteritems():
|
chaptlist = self.bibleHTTPCache[bible].getBibleChapter(bible, book, chapter)
|
||||||
#self.bibleDBCache[bible].loadVerse(book, chapter, v, t)
|
self.bibleDBCache[bible].createChapter(book, chapter, chaptlist)
|
||||||
print v , t
|
|
||||||
|
|
||||||
def _isNewBible(self, name):
|
def _isNewBible(self, name):
|
||||||
"""
|
"""
|
||||||
|
@ -81,3 +81,5 @@ class TestBibleManager:
|
|||||||
print c
|
print c
|
||||||
c = self.bm.getVerseText('asv','re',1,1,2)
|
c = self.bm.getVerseText('asv','re',1,1,2)
|
||||||
print c
|
print c
|
||||||
|
c = self.bm.getVerseText('asv','re',1,5,9)
|
||||||
|
print c
|
||||||
|
Loading…
Reference in New Issue
Block a user