Add lazy load of bible

bzr-revno: 64
This commit is contained in:
Tim Bentley 2008-11-01 11:33:02 +00:00
parent 85e3022672
commit e714b1e5ce
4 changed files with 109 additions and 58 deletions

View File

@ -116,7 +116,6 @@ class BibleDBImpl:
metadata.bind.echo = False
self.Session = sessionmaker()
self.Session.configure(bind=self.db)
self.book_ptr = ""
def createTables(self):
if os.path.exists(self.biblefile): # delete bible file and set it up again
@ -127,37 +126,57 @@ class BibleDBImpl:
verse_table.create()
self.loadMeta("dbversion", "0.1")
def loadVerse(self, book, chapter, verse, text):
metadata.bind.echo = True
def createChapter(self, bk, chap, textlist):
print "createChapter ", bk, chap, textlist
metadata.bind.echo = False
session = self.Session()
if self.book_ptr is not book:
query = session.query(Book).filter(Book.name==book)
#print query.first().id
self.book_ptr = book
#print text
versemeta = Verse(book_id=query.first().id, chapter=int(chapter), verse=int(verse), text=(text))
s = text (""" select id FROM book where book.name == :b """)
data = self.db.execute(s, b=bk).fetchone()
id = data[0] # id is first record in list.
#print "id = " , id
for v , t in textlist.iteritems():
versemeta = Verse(book_id=id, chapter=int(chap), verse=int(v), text=(t))
session.add(versemeta)
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):
metadata.bind.echo = False
session = self.Session()
bmeta= BibleMeta(key, value)
session.add(bmeta)
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")
def getMeta(self, key):
s = text (""" select value FROM meta where key == :k """)
return self.db.execute(s, k=key).fetchone()
def _loadTestaments(self):
metadata.bind.echo = False
session = self.Session()
#Populate the Tables
testmeta = ONTestament(name="Old Testament")
session.add(testmeta)
testmeta = ONTestament(name="New Testament")
session.add(testmeta)
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')
fverse=open(versesfile, 'r')
@ -170,48 +189,54 @@ class BibleDBImpl:
session.add(bookmeta)
session.commit()
book_ptr = ""
for line in fverse:
#print line
p = line.split(",", 3) # split into 3 units and leave the rest as a single field
p[0] = self._cleanText(p[0])
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()
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):
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()
def getBibleText(self, bookname, chapter, sverse, everse):
metadata.bind.echo = True
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 """)
print "getBibleText ", bookname, chapter, sverse, everse
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()
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):
text = text.replace('\n', '')
text = text.replace('\r', '')
text = text.replace('"', '')
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()

View File

@ -30,6 +30,11 @@ class BibleHTTPImpl:
Init confirms the bible exists and stores the database path.
"""
bible = {}
biblesoure = ""
def setBibleSource(self,biblesource):
self.biblesource = biblesource
def getBibleChapter(self, version, book, chapter):
urlstring = "http://bible.crosswalk.com/OnlineStudyBible/bible.cgi?word="+book+"+"+str(chapter)+"&version="+version
print urlstring

View File

@ -22,8 +22,8 @@ mypath=os.path.split(os.path.abspath(__file__))[0]
sys.path.insert(0,(os.path.join(mypath, '..', '..', '..')))
from openlp.utils import ConfigHelper
from openlp.plugins.biblemanager.BibleDBImpl import *
from openlp.plugins.biblemanager.BibleHTTPImpl import *
from openlp.plugins.biblemanager.BibleDBImpl import BibleDBImpl
from openlp.plugins.biblemanager.BibleHTTPImpl import BibleHTTPImpl
class BibleManager:
def __init__(self):
@ -42,8 +42,15 @@ class BibleManager:
for f in files:
b = f.split('.')[0]
self.bibleDBCache[b] = BibleDBImpl(b)
print self.bibleDBCache
print self.bibleHTTPCache
biblesource = self.bibleDBCache[b].getMeta("WEB") # look to see if lazy load bible and get create getter.
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):
"""
@ -54,7 +61,9 @@ class BibleManager:
nbible = BibleDBImpl(name) # Create new Bible
nbible.createTables() # Create Database
self.bibleDBCache[name] = nbible
nhttp = BibleHTTPImpl()
nhttp.setBibleSource(biblesource)
self.bibleHTTPCache[name] = nhttp
nbible.loadMeta("WEB", biblesource)
@ -98,22 +107,32 @@ class BibleManager:
If the end verse(everse) is less then the start verse(sverse)
then only one verse is returned
"""
print self.bibleDBCache
print self.bibleHTTPCache
#print self.bibleDBCache
#print self.bibleHTTPCache
print "getchapter ", bible, book, chapter, sverse, everse
c = self.bibleDBCache[bible].getBibleChapter(book, chapter) # check to see if book/chapter exists
print c
if not c:
self._loadBook(bible,book)
self._loadChapter(bible, book, chapter)
if everse < sverse:
everse = sverse
text = self.bibleDBCache[bible].getBibleText(book, chapter, sverse, everse)
print text
#print text
#self.bibleDBCache[bible].dumpBible()
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):
cl = self.bibleHTTPCache[bible].getBibleChapter(bible, book, chapter)
for v , t in cl.iteritems():
#self.bibleDBCache[bible].loadVerse(book, chapter, v, t)
print v , t
print "loadChapter ", bible, book, chapter
chaptlist = self.bibleHTTPCache[bible].getBibleChapter(bible, book, chapter)
self.bibleDBCache[bible].createChapter(book, chapter, chaptlist)
def _isNewBible(self, name):
"""

View File

@ -81,3 +81,5 @@ class TestBibleManager:
print c
c = self.bm.getVerseText('asv','re',1,1,2)
print c
c = self.bm.getVerseText('asv','re',1,5,9)
print c