Fix Database with previous version and add extra API's

bzr-revno: 115
This commit is contained in:
Tim Bentley 2008-11-24 19:41:29 +00:00
parent dec6feb03c
commit 4afceb8377
2 changed files with 95 additions and 90 deletions

View File

@ -45,7 +45,7 @@ class BibleInvalidDatabaseError(Exception):
metadata = MetaData()
#Define the tables and indexes
meta_table = Table('meta', metadata,
meta_table = Table('metadata', metadata,
Column('key', String(255), primary_key=True),
Column('value', String(255)),
)
@ -57,21 +57,23 @@ testament_table = Table('testament', metadata,
book_table = Table('book', metadata,
Column('id', Integer, primary_key=True),
Column('testament_id', None , ForeignKey('testament.id')),
Column('testament_id', Integer),
Column('name', String(30)),
Column('abbrev', String(30)),
Column('abbrev', String(5)),
)
Index('idx_name', book_table.c.name, book_table.c.id)
Index('idx_abbrev', book_table.c.abbrev, book_table.c.id)
#Column('book_id', None, ForeignKey('book.id')),
verse_table = Table('verse', metadata,
Column('id', Integer, primary_key=True),
Column('book_id', None, ForeignKey('book.id')),
Column('book_id' ),
Column('chapter', Integer),
Column('verse', Integer),
Column('text', Text),
)
Index('idx_chapter_verse_book', verse_table.c.chapter, verse_table.c.verse, verse_table.c.book_id, verse_table.c.id)
Index('idx_chapter_verse_text', verse_table.c.text, verse_table.c.verse, verse_table.c.book_id, verse_table.c.id)
class BibleMeta(object):
def __init__(self, key, value):
@ -121,7 +123,7 @@ class BibleDBImpl(BibleCommon):
path = ConfigHelper.getBiblePath()
#log.debug( path
#log.debug( biblename
self.biblefile = os.path.join(path, biblename+".bible")
self.biblefile = os.path.join(path, biblename+".bible3")
#log.debug( self.biblefile
#log.debug( btype
if btype == 'sqlite':
@ -145,7 +147,7 @@ class BibleDBImpl(BibleCommon):
testament_table.create()
book_table.create()
verse_table.create()
self.saveMeta("dbversion", "0.1")
self.saveMeta("dbversion", "2")
self._loadTestaments()
def addVerse(self, bookid, chap, verse, text):
@ -156,26 +158,26 @@ class BibleDBImpl(BibleCommon):
session.add(versemeta)
session.commit()
def createChapter(self, bookname, chap, textlist):
log.debug( "createChapter %s,%s,%s", bookname, chap, textlist)
def createChapter(self, bookid, chap, textlist):
log.debug( "createChapter %s,%s,%s", bookid, chap, textlist)
metadata.bind.echo = False
session = self.Session()
s = text (""" select id FROM book where book.name == :b """)
data = self.db.execute(s, b=bookname).fetchone()
id = data[0] # id is first record in list.
#s = text (""" select id FROM book where book.name == :b """)
#data = self.db.execute(s, b=bookname).fetchone()
#id = data[0] # id is first record in list.
#log.debug( "id = " , id
for v , t in textlist.iteritems():
versemeta = Verse(book_id=id, chapter=int(chap), verse=int(v), text=(t))
versemeta = Verse(book_id=bookid, chapter=int(chap), verse=int(v), text=(t))
session.add(versemeta)
session.commit()
def createBook(self, bookid, bookname, bookabbrev):
log.debug( "createBook %s,%s,%s", bookid, bookname, bookabbrev)
metadata.bind.echo = False
session = self.Session()
bookmeta = Book(int(5), bookname, bookabbrev)
session.add(bookmeta)
session.commit()
# def createBook(self, bookid, bookname, bookabbrev):
# log.debug( "createBook %s,%s,%s", bookid, bookname, bookabbrev)
# metadata.bind.echo = False
# session = self.Session()
# bookmeta = Book(int(5), bookname, bookabbrev)
# session.add(bookmeta)
# session.commit()
def saveMeta(self, key, value):
metadata.bind.echo = False
@ -185,7 +187,7 @@ class BibleDBImpl(BibleCommon):
session.commit()
def getMeta(self, key):
s = text (""" select value FROM meta where key == :k """)
s = text (""" select value FROM metadata where key == :k """)
return self.db.execute(s, k=key).fetchone()
def deleteMeta(self, key):
@ -193,18 +195,35 @@ class BibleDBImpl(BibleCommon):
s = text (""" delete FROM meta where key == :k """)
self.db.execute(s, k=key)
def _loadTestaments(self):
log.debug("loadTestaments")
metadata.bind.echo = False
session = self.Session()
testmeta = ONTestament(name="Old Testament")
session.add(testmeta)
testmeta = ONTestament(name="New Testament")
session.add(testmeta)
testmeta = ONTestament(name="Apocrypha")
session.add(testmeta)
session.commit()
# def _loadTestaments(self):
# log.debug("loadTestaments")
# metadata.bind.echo = False
# session = self.Session()
# testmeta = ONTestament(name="Old Testament")
# session.add(testmeta)
# testmeta = ONTestament(name="New Testament")
# session.add(testmeta)
# testmeta = ONTestament(name="Apocrypha")
# session.add(testmeta)
# session.commit()
def getBibleBooks(self):
log.debug( "getBibleBook ")
metadata.bind.echo = False
s = text (""" select name FROM book order by id """)
return self.db.execute(s).fetchall()
def getMaxBibleBookVerses(self, bookname, chapter):
log.debug( "getMaxBibleBookVerses %s,%s ", bookname , chapter)
metadata.bind.echo = False
s = text (""" select max(verse.verse) from verse,book where chapter = :c and book_id = book.id and book.name = :b """)
return self.db.execute(s, c=chapter, b=bookname).fetchone()
def getMaxBibleBookChapters(self, bookname):
log.debug( "getMaxBibleBookChapters %s ", bookname )
metadata.bind.echo = False
s = text (""" select max(verse.chapter) from verse,book where book_id = book.id and book.name = :b """)
return self.db.execute(s, b=bookname).fetchone()
def getBibleBook(self, bookname):
log.debug( "getBibleBook %s", bookname)
metadata.bind.echo = False
@ -224,11 +243,18 @@ class BibleDBImpl(BibleCommon):
return self.db.execute(s, c=chapter, b=bookname).fetchone()
def getBibleText(self, bookname, chapter, sverse, everse):
log.debug( "getBibleText %s,%s,%s,%s", bookname, chapter, sverse, everse)
log.debug( "getBibleText %s,%s,%s,%s ", 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 """)
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 getVersesFromText(self,versetext):
log.debug( "getBibleText %s",versetext)
metadata.bind.echo = False
versetext = "%"+versetext+"%"
s = text (""" select book.name, verse.chapter, verse.verse, verse.text FROM verse , book where verse.book_id == book.id and verse.text like :t """)
return self.db.execute(s, t=versetext).fetchall()
def dumpBible(self):
log.debug( ".........Dumping Bible Database")
log.debug( "...............................Books ")

View File

@ -22,10 +22,11 @@ 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.BibleOSISImpl import BibleOSISImpl
from openlp.plugins.biblemanager.BibleCSVImpl import BibleCSVImpl
from openlp.plugins.biblemanager.BibleDBImpl import BibleDBImpl
from openlp.plugins.biblemanager.BibleHTTPImpl import BibleHTTPImpl
from openlp.plugins.biblemanager.BibleCSVImpl import BibleCSVImpl
from openlp.plugins.biblemanager.BibleOSISImpl import BibleOSISImpl
from openlp.plugins.biblemanager.BibleLibrary import BibleLibrary
from openlp.plugins.plugin import Plugin
import logging
@ -50,17 +51,10 @@ class BibleManager(Plugin):
log.debug( "Bible Initialising")
self.bibleDBCache = {} # dict of bible database classes
self.bibleHTTPCache = {} # dict of bible http readers
self.booksOfBible = {} # books of the bible linked to bibleid
self.listOfBooks = [] # list of books for display
self.booksChapters = {} # number of chapters for each bible id
self.BGids = {} # BibleGateway IDs id for bibleid
#self.verses = {} # number of verses for each book
self.verseData = {} # number of versers in each chapter by bookid
self.biblePath = ConfigHelper.getBiblePath()
self.bibleLibrary = BibleLibrary()
#log.debug( self.biblePath )
files = os.listdir(self.biblePath)
fbibles=open("../resources/bibles_en.txt", 'r')
fbibledata=open("../resources/bible_books.txt", 'r')
for f in files:
b = f.split('.')[0]
self.bibleDBCache[b] = BibleDBImpl(b)
@ -72,24 +66,7 @@ class BibleManager(Plugin):
proxy = self.bibleDBCache[b].getMeta("proxy") # look to see if lazy load bible exists and get create getter.
nhttp.setProxy(proxy) # tell The Server where to get the verses from.
#
#Load in memory objects
for line in fbibles:
p = line.split(",")
self.booksOfBible[p[0]] = p[1].replace('\n', '')
self.listOfBooks.insert(int(p[1].replace('\n', '')), p[0])
for line in fbibledata:
p = line.split(",")
self.booksChapters[p[0]]=p[1]
self.BGids[p[0]]=p[2].replace('\n', '')
v = p[3].replace('\n', '')
self.verseData[p[0]] = v
#log.debug( "\nbooks of the Bible", self.booksOfBible)
#log.debug( "\n", self.booksChapters)
#log.debug( "\n", self.CWids)
#log.debug( "\n", self.verseData)
#log.debug( self.bibleDBCache)
#log.debug( self.bibleHTTPCache )
log.debug( "Bible Initialised")
def registerHTTPBible(self, biblename, biblesource, proxyurl=None, proxyid=None, proxypass=None):
@ -173,51 +150,53 @@ class BibleManager(Plugin):
def getBibleBooks(self,bible):
"""
Returns a list of the books of the bible
Returns a list of the books of the bible from the database
"""
return self.listOfBooks
return self.bibleDBCache[bible].getBibleBooks()
def getBookChapterCount(self, book):
def getBookChapterCount(self,bible, book):
"""
Returns all the number of Chapters for a given
book
Returns the number of Chapters for a given book
"""
log.debug( "getBookChapterCount %s", book)
bookid = self.booksOfBible[book]
return self.booksChapters[bookid]
log.debug( "getBookChapterCount %s,%s", bible, book)
return self.bibleDBCache[bible].getMaxBibleBookChapters(book)
def getBookVerseCount(self, book, chapter):
def getBookVerseCount(self, bible, book, chapter):
"""
Returns all the number of verses for a given
book and chapter
book and chapterMaxBibleBookVerses
"""
log.debug( "getBookVerseCount %s,%s", book, chapter)
bookid = self.booksOfBible[book]
v = self.verseData[bookid].split(":")
log.debug( v)
return v[chapter-1] # sub 1 for zero indexing
log.debug( "getBookVerseCount %s,%s,%s", bible, book, chapter)
return self.bibleDBCache[bible].getMaxBibleBookVerses(book, chapter)
def getVerseText(self, bible, bookname, chapter, sverse, everse = 0 ):
def getVersesFromText(self, bible, versetext):
"""
Returns all the number of verses for a given
book and chapterMaxBibleBookVerses
"""
log.debug( "getVersesFromText %s,%s", bible, versetext)
return self.bibleDBCache[bible].getVersesFromText(versetext)
def getVerseText(self, bible, bookname, chapter, sverse, everse = 0 ):
"""
Returns a list of verses for a given Book, Chapter and ranges of verses.
If the end verse(everse) is less then the start verse(sverse)
then only one verse is returned
bible - Which bible to use.
bookname - full text description so needs to be converted.
Rest can be guessed at !
"""
#log.debug( self.bibleDBCache)
#log.debug( self.bibleHTTPCache)
log.debug( "getVerseText %s,%s,%s,%s,%s", bible, bookname, chapter, sverse, everse)
bookid = self.booksOfBible[bookname] # convert to id ie Genesis --> 1 Revelation --> 73
# SORT OUT BOOKNAME BOOK ID.
# NAME COMES IN TO ID AND BACK TO NAME ?
c = self.bibleDBCache[bible].getBibleChapter(bookname, chapter) # check to see if book/chapter exists
bookabbrev = ""
log.debug( "Bible Chapter %s", c )
if not c:
self._loadBook(bible,bookid, bookname, bookabbrev)
self._loadChapter(bible, bookid,bookname, chapter)
log.debug( "getVerseText %s,%s,%s,%s,%s", bible,bookname, chapter, sverse, everse)
# bookid = self.booksOfBible[bookname] # convert to id ie Genesis --> 1 Revelation --> 73
# # SORT OUT BOOKNAME BOOK ID.
# # NAME COMES IN TO ID AND BACK TO NAME ?
# c = self.bibleDBCache[bible].getBibleChapter(bookname, chapter) # check to see if book/chapter exists
# bookabbrev = ""
# log.debug( "Bible Chapter %s", c )
# if not c:
# self._loadBook(bible,bookid, bookname, bookabbrev)
# self._loadChapter(bible, bookid,bookname, chapter)
if everse < sverse:
everse = sverse
text = self.bibleDBCache[bible].getBibleText(bookname, chapter, sverse, everse)
@ -232,7 +211,7 @@ class BibleManager(Plugin):
if not cl :
self.bibleDBCache[bible].createBook(bookid, bookname, bookabbrev)
def _loadChapter(self, bible, bookid,bookname, chapter):
def _loadChapter(self, bible, bookid, bookname, chapter):
log.debug( "loadChapter %s,%s,%s,%s", bible, bookid,bookname, chapter)
try :
chaptlist = self.bibleHTTPCache[bible].getBibleChapter(bible, bookid,bookname, chapter)