Update BibleManager and Database Impl

bzr-revno: 42
This commit is contained in:
Tim Bentley 2008-10-27 21:17:22 +00:00
parent ff3160d5bf
commit ee21f9fad4
4 changed files with 148 additions and 64 deletions

View File

@ -24,9 +24,7 @@ import string
from sqlalchemy import * from sqlalchemy import *
from sqlalchemy.sql import select from sqlalchemy.sql import select
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, mapper
from sqlalchemy.orm import sessionmaker
mypath=os.path.split(os.path.abspath(__file__))[0] 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, '..', '..')))
@ -34,12 +32,37 @@ from openlp.utils import ConfigHelper
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s') logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
Base = declarative_base() metadata = MetaData()
#Define the tables and indexes #Define the tables and indexes
class BibleMeta(Base): meta_table = Table('meta', metadata,
__tablename__ = "meta" Column('key', String(255), primary_key=True),
key = Column(String(255), primary_key=True) Column('value', String(255)),
value = Column(String(255)) )
testament_table = Table('testament', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(30)),
)
book_table = Table('book', metadata,
Column('id', Integer, primary_key=True),
Column('testament_id', None , ForeignKey('testament.id')),
Column('name', String(30)),
Column('abbrev', String(30)),
)
Index('idx_name', book_table.c.name, book_table.c.id)
Index('idx_abbrev', book_table.c.abbrev, book_table.c.id)
verse_table = Table('verse', metadata,
Column('id', Integer, primary_key=True),
Column('book_id', None, ForeignKey('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)
class BibleMeta(object):
def __init__(self, key, value): def __init__(self, key, value):
self.key = key self.key = key
self.value =value self.value =value
@ -47,38 +70,23 @@ class BibleMeta(Base):
def __repr__(self): def __repr__(self):
return "<biblemeta('%s','%s')>" %(self.key, self.value) return "<biblemeta('%s','%s')>" %(self.key, self.value)
class Testament(Base): class Testament(object):
__tablename__ = "testament"
id = Column(Integer, primary_key=True)
name = Column(String(30))
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
def __repr__(self): def __repr__(self):
return "<testament('%s')>" %(self.name) return "<testament('%s')>" %(self.name)
class Books(Base): class Book(object):
__tablename__ = "books"
id = Column(Integer, primary_key=True)
testament_id = Column(Integer)
name = Column(String(30))
abbrev = Column(String(30))
def __init__(self, testament_id, name, abbrev): def __init__(self, testament_id, name, abbrev):
self.testament_id = testament_id self.testament_id = testament_id
self.name = name self.name = name
self.abbrev = abbrev self.abbrev = abbrev
def __repr__(self): def __repr__(self):
return "<books('%s','%s','%s')>" %(self.testament_id, self.name, self.abbrev) return "<book('%s','%s','%s','%s')>" %(self.id, self.testament_id, self.name, self.abbrev)
class Verses(Base): class Verse(object):
__tablename__ = "verses"
id = Column(Integer, primary_key=True)
book_id = Column(Integer)
chapter = Column(Integer)
verse = Column(Integer)
text = Column(Text)
def __init__(self, book_id, chapter, verse, text): def __init__(self, book_id, chapter, verse, text):
self.book_id = book_id self.book_id = book_id
self.chapter = chapter self.chapter = chapter
@ -86,8 +94,12 @@ class Verses(Base):
self.text = text self.text = text
def __repr__(self): def __repr__(self):
return "<verses('%s','%s','%s','%s')>" %(self.book_id, self.chapter, self.verse, self.text) return "<verse('%s','%s','%s','%s')>" %(self.book_id, self.chapter, self.verse, self.text)
mapper(BibleMeta, meta_table)
mapper(Testament, testament_table)
mapper(Book, book_table)
mapper(Verse, verse_table)
class BibleDBImpl: class BibleDBImpl:
def __init__(self, biblename): def __init__(self, biblename):
@ -99,17 +111,23 @@ class BibleDBImpl:
#print self.biblefile #print self.biblefile
self.db = create_engine("sqlite:///"+self.biblefile) self.db = create_engine("sqlite:///"+self.biblefile)
self.db.echo = False self.db.echo = False
self.metadata = MetaData() #self.metadata = metaData()
self.metadata = Base.metadata metadata.bind = self.db
self.metadata.bind = self.db metadata.bind.echo = False
self.metadata.bind.echo = False
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
os.remove(self.biblefile) os.remove(self.biblefile)
self.metadata.create_all(self.db) meta_table.create()
testament_table.create()
book_table.create()
verse_table.create()
self.Session = sessionmaker() self.Session = sessionmaker()
self.Session.configure(bind=self.db) self.Session.configure(bind=self.db)
# i1 = Index('idx_name', Books.c.name, Books.c.id)
# i1.create(bind=self.db)
# i2 = Index('idx_abbrev', Books.c.abbrev, Books.c.id)
# i2.create(bind=self.db)
def loadData(self, booksfile, versesfile): def loadData(self, booksfile, versesfile):
session = self.Session() session = self.Session()
@ -134,9 +152,9 @@ class BibleDBImpl:
fverse=open(versesfile, 'r') fverse=open(versesfile, 'r')
for line in fbooks: for line in fbooks:
print line #print line
p = line.split(",") p = line.split(",")
bookmeta = Books(int(p[1]), p[2], p[3]) bookmeta = Book(int(p[1]), p[2], p[3])
session.add(bookmeta) session.add(bookmeta)
session.commit() session.commit()
@ -146,24 +164,39 @@ class BibleDBImpl:
book_ptr = "" 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
if book_ptr is not p[0]: if book_ptr is not p[0]:
query = session.query(Books).filter(Books.name==p[0]) query = session.query(Book).filter(Book.name==p[0])
print query.first().id #print query.first().id
book_ptr = p[0] book_ptr = p[0]
print p[3] #print p[3]
versemeta = Verses(book_id=query.first().id, chapter=int(p[1]), verse=int(p[2]), text=p[3]) versemeta = Verse(book_id=query.first().id, chapter=int(p[1]), verse=int(p[2]), text=p[3])
session.add(versemeta) session.add(versemeta)
session.commit() session.commit()
def getBibleText(self, bookname, chapter, verse):
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 """)
return self.db.execute(s, c=chapter, v=verse , b=bookname).fetchone()
def Run_Tests(self): def Run_Tests(self):
metadata.bind.echo = True
print "test print" print "test print"
session = self.Session() session = self.Session()
print session.query(Books).filter(Books.name=='"John"').all() print session.query(Book).filter(Book.name=='"John"').all()
print session.query(Verses).filter(Verses.book_id==9).all() q = session.query(Verse).filter(Verse.book_id==8)
#print b print q.first().text
#v = self.db.execute(verses.select(verses.c.chapter==1 and verses.c.verse==1 and verses.c.book_id == b[0])).fetchone()
#print v 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

@ -22,7 +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.database.BibleImpl import * from openlp.database.BibleDBImpl import *
from openlp.database.BibleHTTPImpl import *
class BibleManager: class BibleManager:
def __init__(self): def __init__(self):
@ -42,11 +43,32 @@ class BibleManager:
files = os.listdir(self.biblePath) files = os.listdir(self.biblePath)
for f in files: for f in files:
b = f.split('.')[0] b = f.split('.')[0]
self.biblelist[b] = BibleImpl(b) self.biblelist[b] = BibleDBImpl(b)
#print self.biblelist #print self.biblelist
def registerBible(self, name, biblesource, proxy, proxyport, proxyid, proxypass):
"""
Return a list of bibles from a given URL.
The selected Bible can then be registered and LazyLoaded into a database
"""
def registerBible(self, name, booksfile, versefile):
"""
Method to load a bible from a set of files into a database.
If the database exists it is deleted and the database is reloaded
from scratch.
"""
newbible = True
for b , o in self.biblelist.iteritems():
print b
if b == name :
newbible = False
print "bible already registered"
if newbible == True :
nbible = BibleDBImpl(name) # Create new Bible
nbible.createTables() # Create Database
nbible.loadData(booksfile, versefile)
self.biblelist[name] = nbible
def getBibles(self): def getBibles(self):
""" """
Returns a list of Books of the bible Returns a list of Books of the bible
@ -75,8 +97,6 @@ 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
""" """
if everse < sverse: if everse < sverse:
print "adjusted end verse" text = self.biblelist[bible].getBibleText(book, chapter, sverse)
everse = sverse return text
return ["In the Beginning was the Word","God made the world as saw it was good"]

View File

@ -1,17 +1,34 @@
"""
OpenLP - Open Source Lyrics Projection
Copyright (c) 2008 Raoul Snyman
Portions copyright (c) 2008 Martin Thompson, Tim Bentley
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, os.path import os, os.path
import sys import sys
mypath=os.path.split(os.path.abspath(__file__))[0] 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.database.BibleImpl import * from openlp.database.BibleDBImpl import *
if __name__ == "__main__": if __name__ == "__main__":
bi = BibleImpl("TheMessage") bi = BibleDBImpl("TheMessage")
bi.createTables() bi.createTables()
bi.loadData('biblebooks_msg_short.csv','bibleverses_msg_short.csv') bi.loadData('biblebooks_msg_short.csv','bibleverses_msg_short.csv')
bi.Run_Tests() bi.Run_Tests()
b2 = BibleImpl("NIV") # b2 = BibleDBImpl("NIV")
b2.createTables() # b2.createTables()
b2.loadData('biblebooks_niv_short.csv','bibleverses_niv_short.csv') # b2.loadData('biblebooks_niv_short.csv','bibleverses_niv_short.csv')
b2.Run_Tests() # b2.Run_Tests()

View File

@ -31,7 +31,18 @@ class TestBibleManager(unittest.TestCase):
def setUp(self): def setUp(self):
self.bm = BibleManager() self.bm = BibleManager()
def testRegisterBibleFiles(self):
# make sure the shuffled sequence does not lose any elements
print "testRegisterBibleFiles"
self.bm.registerBible("TheMessage",'biblebooks_msg_short.csv','bibleverses_msg_short.csv')
self.bm.registerBible("NIV",'biblebooks_niv_short.csv','bibleverses_niv_short.csv')
b = self.bm.getBibles()
for b1 in b:
print b1
self.assert_(b1 in b)
def testGetBibles(self): def testGetBibles(self):
print "testGetBibles"
# make sure the shuffled sequence does not lose any elements # make sure the shuffled sequence does not lose any elements
b = self.bm.getBibles() b = self.bm.getBibles()
for b1 in b: for b1 in b:
@ -39,19 +50,22 @@ class TestBibleManager(unittest.TestCase):
self.assert_(b1 in b) self.assert_(b1 in b)
def testGetBooks(self): def testGetBooks(self):
print "testGetBooks"
c = self.bm.getBibleBooks("NIV") c = self.bm.getBibleBooks("NIV")
for c1 in c: for c1 in c:
print c1 print c1
self.assert_(c1 in c) self.assert_(c1 in c)
def testGetBooks(self): def testGetVerseCount(self):
print "testGetVerseCount"
self.failUnless(self.bm.getBookVerseCount("NIV", "GEN", 1) == 28, "Wrong Book Count") self.failUnless(self.bm.getBookVerseCount("NIV", "GEN", 1) == 28, "Wrong Book Count")
def testGetVerseText(self): def testGetVerseText(self):
c = self.bm.getVerseText("NIV","GEN",1,2,1) print "testGetVerseText"
for c1 in c: c = self.bm.getVerseText("TheMessage",'"Genesis"',1,2,1)
print c1 print c
self.assert_(c1 in c) c = self.bm.getVerseText("NIV",'"Genesis"',1,2,1)
print c
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()