2009-07-14 19:44:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
2009-07-13 20:08:43 +00:00
|
|
|
|
2009-09-08 19:58:05 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2010-12-26 11:04:47 +00:00
|
|
|
# Copyright (c) 2008-2011 Raoul Snyman #
|
2011-05-26 16:25:54 +00:00
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Gerald Britton, Jonathan #
|
|
|
|
# Corwin, Michael Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, #
|
|
|
|
# Armin Köhler, Joshua Millar, Stevan Pettit, Andreas Preikschat, Mattias #
|
|
|
|
# Põldaru, Christian Richter, Philip Ridout, Jeffrey Smith, Maikel #
|
|
|
|
# Stuivenberg, Martin Thompson, Jon Tibble, Frode Woldsund #
|
2009-09-08 19:58:05 +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-07-13 20:08:43 +00:00
|
|
|
|
2008-10-30 20:44:54 +00:00
|
|
|
import logging
|
2010-01-17 16:48:45 +00:00
|
|
|
import chardet
|
2010-03-15 18:47:07 +00:00
|
|
|
import re
|
2008-10-30 20:44:54 +00:00
|
|
|
|
2011-01-06 20:48:22 +00:00
|
|
|
from PyQt4 import QtCore
|
2010-06-15 00:44:06 +00:00
|
|
|
from sqlalchemy import Column, ForeignKey, or_, Table, types
|
|
|
|
from sqlalchemy.orm import class_mapper, mapper, relation
|
|
|
|
from sqlalchemy.orm.exc import UnmappedClassError
|
2009-12-30 17:29:08 +00:00
|
|
|
|
2011-02-24 14:20:35 +00:00
|
|
|
from openlp.core.lib import Receiver, translate
|
2010-06-22 15:09:49 +00:00
|
|
|
from openlp.core.lib.db import BaseModel, init_db, Manager
|
2011-02-02 23:12:31 +00:00
|
|
|
from openlp.core.lib.ui import critical_error_message_box
|
2008-12-07 20:04:07 +00:00
|
|
|
|
2009-12-27 23:54:00 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2010-06-15 00:44:06 +00:00
|
|
|
class BibleMeta(BaseModel):
|
|
|
|
"""
|
|
|
|
Bible Meta Data
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2010-11-12 21:51:18 +00:00
|
|
|
|
2010-06-15 00:44:06 +00:00
|
|
|
class Testament(BaseModel):
|
|
|
|
"""
|
|
|
|
Bible Testaments
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2010-11-12 21:51:18 +00:00
|
|
|
|
2010-06-15 00:44:06 +00:00
|
|
|
class Book(BaseModel):
|
|
|
|
"""
|
|
|
|
Song model
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2010-11-12 21:51:18 +00:00
|
|
|
|
2010-06-15 00:44:06 +00:00
|
|
|
class Verse(BaseModel):
|
|
|
|
"""
|
|
|
|
Topic model
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2010-11-12 21:51:18 +00:00
|
|
|
|
2010-06-15 00:44:06 +00:00
|
|
|
def init_schema(url):
|
|
|
|
"""
|
2010-10-01 14:18:15 +00:00
|
|
|
Setup a bible database connection and initialise the database schema.
|
2010-06-15 00:44:06 +00:00
|
|
|
|
|
|
|
``url``
|
2010-10-01 14:18:15 +00:00
|
|
|
The database to setup.
|
2010-06-15 00:44:06 +00:00
|
|
|
"""
|
|
|
|
session, metadata = init_db(url)
|
|
|
|
|
|
|
|
meta_table = Table(u'metadata', metadata,
|
|
|
|
Column(u'key', types.Unicode(255), primary_key=True, index=True),
|
|
|
|
Column(u'value', types.Unicode(255)),
|
|
|
|
)
|
|
|
|
testament_table = Table(u'testament', metadata,
|
|
|
|
Column(u'id', types.Integer, primary_key=True),
|
|
|
|
Column(u'name', types.Unicode(50)),
|
|
|
|
)
|
|
|
|
book_table = Table(u'book', metadata,
|
|
|
|
Column(u'id', types.Integer, primary_key=True),
|
|
|
|
Column(u'testament_id', types.Integer, ForeignKey(u'testament.id')),
|
|
|
|
Column(u'name', types.Unicode(50), index=True),
|
|
|
|
Column(u'abbreviation', types.Unicode(5), index=True),
|
|
|
|
)
|
|
|
|
verse_table = Table(u'verse', metadata,
|
|
|
|
Column(u'id', types.Integer, primary_key=True, index=True),
|
|
|
|
Column(u'book_id', types.Integer, ForeignKey(u'book.id'), index=True),
|
|
|
|
Column(u'chapter', types.Integer, index=True),
|
|
|
|
Column(u'verse', types.Integer, index=True),
|
|
|
|
Column(u'text', types.UnicodeText, index=True),
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
class_mapper(BibleMeta)
|
|
|
|
except UnmappedClassError:
|
|
|
|
mapper(BibleMeta, meta_table)
|
|
|
|
try:
|
|
|
|
class_mapper(Testament)
|
|
|
|
except UnmappedClassError:
|
|
|
|
mapper(Testament, testament_table,
|
|
|
|
properties={'books': relation(Book, backref='testament')})
|
|
|
|
try:
|
|
|
|
class_mapper(Book)
|
|
|
|
except UnmappedClassError:
|
|
|
|
mapper(Book, book_table,
|
|
|
|
properties={'verses': relation(Verse, backref='book')})
|
|
|
|
try:
|
|
|
|
class_mapper(Verse)
|
|
|
|
except UnmappedClassError:
|
|
|
|
mapper(Verse, verse_table)
|
|
|
|
|
|
|
|
metadata.create_all(checkfirst=True)
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
|
|
|
class BibleDB(QtCore.QObject, Manager):
|
2009-12-27 23:54:00 +00:00
|
|
|
"""
|
|
|
|
This class represents a database-bound Bible. It is used as a base class
|
|
|
|
for all the custom importers, so that the can implement their own import
|
|
|
|
methods, but benefit from the database methods in here via inheritance,
|
|
|
|
rather than depending on yet another object.
|
|
|
|
"""
|
|
|
|
|
2010-01-31 19:49:01 +00:00
|
|
|
def __init__(self, parent, **kwargs):
|
2009-12-30 17:29:08 +00:00
|
|
|
"""
|
|
|
|
The constructor loads up the database and creates and initialises the
|
|
|
|
tables if the database doesn't exist.
|
|
|
|
|
|
|
|
**Required keyword arguments:**
|
|
|
|
|
|
|
|
``path``
|
|
|
|
The path to the bible database file.
|
|
|
|
|
|
|
|
``name``
|
|
|
|
The name of the database. This is also used as the file name for
|
|
|
|
SQLite databases.
|
|
|
|
"""
|
2010-02-27 09:18:26 +00:00
|
|
|
log.info(u'BibleDB loaded')
|
2010-01-31 19:49:01 +00:00
|
|
|
QtCore.QObject.__init__(self)
|
2010-06-14 02:42:47 +00:00
|
|
|
self.bible_plugin = parent
|
2009-12-30 17:29:08 +00:00
|
|
|
if u'path' not in kwargs:
|
2009-12-27 23:54:00 +00:00
|
|
|
raise KeyError(u'Missing keyword argument "path".')
|
2010-03-16 19:46:19 +00:00
|
|
|
if u'name' not in kwargs and u'file' not in kwargs:
|
|
|
|
raise KeyError(u'Missing keyword argument "name" or "file".')
|
2010-02-06 15:33:23 +00:00
|
|
|
self.stop_import_flag = False
|
2010-03-16 19:46:19 +00:00
|
|
|
if u'name' in kwargs:
|
|
|
|
self.name = kwargs[u'name']
|
|
|
|
if not isinstance(self.name, unicode):
|
|
|
|
self.name = unicode(self.name, u'utf-8')
|
|
|
|
self.file = self.clean_filename(self.name)
|
|
|
|
if u'file' in kwargs:
|
|
|
|
self.file = kwargs[u'file']
|
2010-06-15 00:44:06 +00:00
|
|
|
Manager.__init__(self, u'bibles', init_schema, self.file)
|
2010-03-16 19:46:19 +00:00
|
|
|
if u'file' in kwargs:
|
|
|
|
self.get_name()
|
2010-06-18 01:26:01 +00:00
|
|
|
self.wizard = None
|
2011-02-24 14:20:35 +00:00
|
|
|
QtCore.QObject.connect(Receiver.get_receiver(),
|
|
|
|
QtCore.SIGNAL(u'openlp_stop_wizard'), self.stop_import)
|
2010-03-16 19:46:19 +00:00
|
|
|
|
2010-06-10 13:28:41 +00:00
|
|
|
def stop_import(self):
|
|
|
|
"""
|
|
|
|
Stops the import of the Bible.
|
|
|
|
"""
|
2010-12-28 11:18:56 +00:00
|
|
|
log.debug(u'Stopping import')
|
2010-06-10 13:28:41 +00:00
|
|
|
self.stop_import_flag = True
|
|
|
|
|
2010-03-16 19:46:19 +00:00
|
|
|
def get_name(self):
|
2010-03-27 12:37:21 +00:00
|
|
|
"""
|
|
|
|
Returns the version name of the Bible.
|
|
|
|
"""
|
2010-06-15 00:44:06 +00:00
|
|
|
version_name = self.get_object(BibleMeta, u'Version')
|
2011-04-13 18:53:05 +00:00
|
|
|
self.name = version_name.value if version_name else None
|
2010-03-16 19:46:19 +00:00
|
|
|
return self.name
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2010-03-15 18:47:07 +00:00
|
|
|
def clean_filename(self, old_filename):
|
2010-03-27 12:37:21 +00:00
|
|
|
"""
|
|
|
|
Clean up the version name of the Bible and convert it into a valid
|
|
|
|
file name.
|
|
|
|
|
|
|
|
``old_filename``
|
|
|
|
The "dirty" file name or version name.
|
|
|
|
"""
|
2010-04-30 21:00:17 +00:00
|
|
|
if not isinstance(old_filename, unicode):
|
2010-03-16 19:46:19 +00:00
|
|
|
old_filename = unicode(old_filename, u'utf-8')
|
2010-03-16 20:43:41 +00:00
|
|
|
old_filename = re.sub(r'[^\w]+', u'_', old_filename).strip(u'_')
|
2010-03-16 19:46:19 +00:00
|
|
|
return old_filename + u'.sqlite'
|
2010-03-15 18:47:07 +00:00
|
|
|
|
2010-01-17 16:48:45 +00:00
|
|
|
def register(self, wizard):
|
2009-12-30 17:29:08 +00:00
|
|
|
"""
|
|
|
|
This method basically just initialialises the database. It is called
|
|
|
|
from the Bible Manager when a Bible is imported. Descendant classes
|
2010-03-27 12:37:21 +00:00
|
|
|
may want to override this method to supply their own custom
|
2009-12-30 17:29:08 +00:00
|
|
|
initialisation as well.
|
2010-03-27 12:37:21 +00:00
|
|
|
|
|
|
|
``wizard``
|
|
|
|
The actual Qt wizard form.
|
2009-12-30 17:29:08 +00:00
|
|
|
"""
|
2010-01-17 16:48:45 +00:00
|
|
|
self.wizard = wizard
|
|
|
|
self.create_meta(u'dbversion', u'2')
|
2011-01-27 03:13:43 +00:00
|
|
|
self.setup_testaments()
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def setup_testaments(self):
|
|
|
|
"""
|
|
|
|
Initialise the testaments section of a bible with suitable defaults.
|
|
|
|
"""
|
2010-06-28 13:38:29 +00:00
|
|
|
self.save_object(Testament.populate(name=u'Old Testament'))
|
|
|
|
self.save_object(Testament.populate(name=u'New Testament'))
|
|
|
|
self.save_object(Testament.populate(name=u'Apocrypha'))
|
2008-10-30 20:44:54 +00:00
|
|
|
|
2009-12-30 17:29:08 +00:00
|
|
|
def create_book(self, name, abbrev, testament=1):
|
2010-03-27 12:37:21 +00:00
|
|
|
"""
|
|
|
|
Add a book to the database.
|
|
|
|
|
|
|
|
``name``
|
|
|
|
The name of the book.
|
|
|
|
|
|
|
|
``abbrev``
|
|
|
|
The abbreviation of the book.
|
|
|
|
|
|
|
|
``testament``
|
|
|
|
*Defaults to 1.* The id of the testament this book belongs to.
|
|
|
|
"""
|
2010-01-17 16:48:45 +00:00
|
|
|
log.debug(u'create_book %s,%s', name, abbrev)
|
2009-12-30 17:29:08 +00:00
|
|
|
book = Book.populate(name=name, abbreviation=abbrev,
|
|
|
|
testament_id=testament)
|
2010-06-28 13:38:29 +00:00
|
|
|
self.save_object(book)
|
2009-12-30 17:29:08 +00:00
|
|
|
return book
|
|
|
|
|
|
|
|
def create_chapter(self, book_id, chapter, textlist):
|
2010-03-27 12:37:21 +00:00
|
|
|
"""
|
2010-12-04 20:42:03 +00:00
|
|
|
Add a chapter and its verses to a book.
|
2010-03-27 12:37:21 +00:00
|
|
|
|
|
|
|
``book_id``
|
|
|
|
The id of the book being appended.
|
|
|
|
|
|
|
|
``chapter``
|
|
|
|
The chapter number.
|
|
|
|
|
|
|
|
``textlist``
|
|
|
|
A dict of the verses to be inserted. The key is the verse number,
|
|
|
|
and the value is the verse text.
|
|
|
|
"""
|
2010-01-24 07:08:14 +00:00
|
|
|
log.debug(u'create_chapter %s,%s', book_id, chapter)
|
2010-11-13 20:13:24 +00:00
|
|
|
# Text list has book and chapter as first two elements of the array.
|
2009-07-14 19:44:15 +00:00
|
|
|
for verse_number, verse_text in textlist.iteritems():
|
2009-12-30 17:29:08 +00:00
|
|
|
verse = Verse.populate(
|
2011-04-13 18:53:05 +00:00
|
|
|
book_id=book_id,
|
|
|
|
chapter=chapter,
|
|
|
|
verse=verse_number,
|
|
|
|
text=verse_text
|
2009-12-30 17:29:08 +00:00
|
|
|
)
|
2009-07-14 19:44:15 +00:00
|
|
|
self.session.add(verse)
|
2010-06-15 00:44:06 +00:00
|
|
|
self.session.commit()
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2009-12-30 17:29:08 +00:00
|
|
|
def create_verse(self, book_id, chapter, verse, text):
|
2010-03-27 12:37:21 +00:00
|
|
|
"""
|
|
|
|
Add a single verse to a chapter.
|
|
|
|
|
|
|
|
``book_id``
|
|
|
|
The id of the book being appended.
|
|
|
|
|
|
|
|
``chapter``
|
|
|
|
The chapter number.
|
|
|
|
|
|
|
|
``verse``
|
|
|
|
The verse number.
|
|
|
|
|
|
|
|
``text``
|
|
|
|
The verse text.
|
|
|
|
"""
|
2010-01-17 16:48:45 +00:00
|
|
|
if not isinstance(text, unicode):
|
|
|
|
details = chardet.detect(text)
|
|
|
|
text = unicode(text, details[u'encoding'])
|
2009-12-30 17:29:08 +00:00
|
|
|
verse = Verse.populate(
|
|
|
|
book_id=book_id,
|
|
|
|
chapter=chapter,
|
|
|
|
verse=verse,
|
|
|
|
text=text
|
|
|
|
)
|
|
|
|
self.session.add(verse)
|
|
|
|
return verse
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2009-12-30 17:29:08 +00:00
|
|
|
def create_meta(self, key, value):
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-10-01 13:50:29 +00:00
|
|
|
Utility method to save BibleMeta objects in a Bible database.
|
2010-06-15 15:42:52 +00:00
|
|
|
|
|
|
|
``key``
|
2010-10-01 13:50:29 +00:00
|
|
|
The key for this instance.
|
2010-06-15 15:42:52 +00:00
|
|
|
|
|
|
|
``value``
|
2010-10-01 13:50:29 +00:00
|
|
|
The value for this instance.
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'save_meta %s/%s', key, value)
|
2010-06-28 13:38:29 +00:00
|
|
|
self.save_object(BibleMeta.populate(key=key, value=value))
|
2008-11-01 11:33:02 +00:00
|
|
|
|
2009-12-30 17:29:08 +00:00
|
|
|
def get_book(self, book):
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-10-01 13:50:29 +00:00
|
|
|
Return a book object from the database.
|
2010-06-15 15:42:52 +00:00
|
|
|
|
|
|
|
``book``
|
2010-10-01 13:50:29 +00:00
|
|
|
The name of the book to return.
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-02-04 19:34:12 +00:00
|
|
|
log.debug(u'BibleDb.get_book("%s")', book)
|
2010-06-30 22:05:51 +00:00
|
|
|
db_book = self.get_object_filtered(Book, Book.name.like(book + u'%'))
|
2009-12-30 17:29:08 +00:00
|
|
|
if db_book is None:
|
2010-06-30 22:05:51 +00:00
|
|
|
db_book = self.get_object_filtered(Book,
|
|
|
|
Book.abbreviation.like(book + u'%'))
|
2009-12-30 17:29:08 +00:00
|
|
|
return db_book
|
2008-11-06 18:49:36 +00:00
|
|
|
|
2010-07-24 02:14:08 +00:00
|
|
|
def get_books(self):
|
|
|
|
"""
|
|
|
|
A wrapper so both local and web bibles have a get_books() method that
|
2011-02-25 17:05:01 +00:00
|
|
|
manager can call. Used in the media manager advanced search tab.
|
2010-07-24 02:14:08 +00:00
|
|
|
"""
|
|
|
|
return self.get_all_objects(Book, order_by_ref=Book.id)
|
|
|
|
|
2011-05-14 21:25:22 +00:00
|
|
|
def get_verses(self, reference_list, show_error=True):
|
2009-12-30 17:29:08 +00:00
|
|
|
"""
|
|
|
|
This is probably the most used function. It retrieves the list of
|
|
|
|
verses based on the user's query.
|
|
|
|
|
|
|
|
``reference_list``
|
|
|
|
This is the list of references the media manager item wants. It is
|
2010-10-09 06:00:50 +00:00
|
|
|
a list of tuples, with the following format::
|
2009-12-30 17:29:08 +00:00
|
|
|
|
|
|
|
(book, chapter, start_verse, end_verse)
|
|
|
|
|
|
|
|
Therefore, when you are looking for multiple items, simply break
|
|
|
|
them up into references like this, bundle them into a list. This
|
|
|
|
function then runs through the list, and returns an amalgamated
|
2010-10-09 06:00:50 +00:00
|
|
|
list of ``Verse`` objects. For example::
|
2009-12-30 17:29:08 +00:00
|
|
|
|
|
|
|
[(u'Genesis', 1, 1, 1), (u'Genesis', 2, 2, 3)]
|
|
|
|
"""
|
2010-01-31 20:36:54 +00:00
|
|
|
log.debug(u'BibleDB.get_verses: %s', reference_list)
|
2009-12-30 17:29:08 +00:00
|
|
|
verse_list = []
|
|
|
|
for book, chapter, start_verse, end_verse in reference_list:
|
|
|
|
db_book = self.get_book(book)
|
|
|
|
if db_book:
|
2010-01-24 07:08:14 +00:00
|
|
|
book = db_book.name
|
2010-02-04 19:34:12 +00:00
|
|
|
log.debug(u'Book name corrected to "%s"', book)
|
2011-01-15 23:19:25 +00:00
|
|
|
if end_verse == -1:
|
|
|
|
end_verse = self.get_verse_count(book, chapter)
|
2010-06-16 01:48:52 +00:00
|
|
|
verses = self.session.query(Verse)\
|
|
|
|
.filter_by(book_id=db_book.id)\
|
|
|
|
.filter_by(chapter=chapter)\
|
|
|
|
.filter(Verse.verse >= start_verse)\
|
|
|
|
.filter(Verse.verse <= end_verse)\
|
|
|
|
.order_by(Verse.verse)\
|
|
|
|
.all()
|
|
|
|
verse_list.extend(verses)
|
|
|
|
else:
|
|
|
|
log.debug(u'OpenLP failed to find book %s', book)
|
2011-05-14 21:25:22 +00:00
|
|
|
if show_error:
|
|
|
|
critical_error_message_box(
|
|
|
|
translate('BiblesPlugin', 'No Book Found'),
|
|
|
|
translate('BiblesPlugin', 'No matching book '
|
2011-05-15 20:53:25 +00:00
|
|
|
'could be found in this Bible. Check that you '
|
|
|
|
'have spelled the name of the book correctly.'))
|
2009-12-30 17:29:08 +00:00
|
|
|
return verse_list
|
|
|
|
|
|
|
|
def verse_search(self, text):
|
|
|
|
"""
|
|
|
|
Search for verses containing text ``text``.
|
|
|
|
|
|
|
|
``text``
|
|
|
|
The text to search for. If the text contains commas, it will be
|
|
|
|
split apart and OR'd on the list of values. If the text just
|
|
|
|
contains spaces, it will split apart and AND'd on the list of
|
|
|
|
values.
|
|
|
|
"""
|
2010-01-31 20:36:54 +00:00
|
|
|
log.debug(u'BibleDB.verse_search("%s")', text)
|
2010-10-09 19:36:05 +00:00
|
|
|
verses = self.session.query(Verse)
|
2009-12-30 17:29:08 +00:00
|
|
|
if text.find(u',') > -1:
|
2011-04-13 18:53:05 +00:00
|
|
|
keywords = \
|
|
|
|
[u'%%%s%%' % keyword.strip() for keyword in text.split(u',')]
|
|
|
|
or_clause = [Verse.text.like(keyword) for keyword in keywords]
|
2010-10-09 19:36:05 +00:00
|
|
|
verses = verses.filter(or_(*or_clause))
|
2009-12-30 17:29:08 +00:00
|
|
|
else:
|
2011-04-13 18:53:05 +00:00
|
|
|
keywords = \
|
|
|
|
[u'%%%s%%' % keyword.strip() for keyword in text.split(u' ')]
|
2009-12-30 17:29:08 +00:00
|
|
|
for keyword in keywords:
|
2010-10-09 19:36:05 +00:00
|
|
|
verses = verses.filter(Verse.text.like(keyword))
|
2009-12-30 17:29:08 +00:00
|
|
|
verses = verses.all()
|
|
|
|
return verses
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2009-12-30 17:29:08 +00:00
|
|
|
def get_chapter_count(self, book):
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-10-01 13:50:29 +00:00
|
|
|
Return the number of chapters in a book.
|
2010-06-15 15:42:52 +00:00
|
|
|
|
|
|
|
``book``
|
2010-10-01 13:50:29 +00:00
|
|
|
The book to get the chapter count for.
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-01-31 20:36:54 +00:00
|
|
|
log.debug(u'BibleDB.get_chapter_count("%s")', book)
|
2009-12-30 17:29:08 +00:00
|
|
|
count = self.session.query(Verse.chapter).join(Book)\
|
2011-05-15 13:06:16 +00:00
|
|
|
.filter(Book.name == book)\
|
2009-12-30 17:29:08 +00:00
|
|
|
.distinct().count()
|
|
|
|
if not count:
|
2009-10-12 10:18:54 +00:00
|
|
|
return 0
|
|
|
|
else:
|
2009-12-30 17:29:08 +00:00
|
|
|
return count
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2009-12-30 17:29:08 +00:00
|
|
|
def get_verse_count(self, book, chapter):
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-10-01 13:50:29 +00:00
|
|
|
Return the number of verses in a chapter.
|
2010-06-15 15:42:52 +00:00
|
|
|
|
|
|
|
``book``
|
2010-10-01 13:50:29 +00:00
|
|
|
The book containing the chapter.
|
2010-06-15 15:42:52 +00:00
|
|
|
|
|
|
|
``chapter``
|
2010-10-01 13:50:29 +00:00
|
|
|
The chapter to get the verse count for.
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-01-31 20:36:54 +00:00
|
|
|
log.debug(u'BibleDB.get_verse_count("%s", %s)', book, chapter)
|
2009-12-30 17:29:08 +00:00
|
|
|
count = self.session.query(Verse).join(Book)\
|
2011-05-15 13:06:16 +00:00
|
|
|
.filter(Book.name == book)\
|
|
|
|
.filter(Verse.chapter == chapter)\
|
2009-12-30 17:29:08 +00:00
|
|
|
.count()
|
|
|
|
if not count:
|
2009-10-12 10:18:54 +00:00
|
|
|
return 0
|
|
|
|
else:
|
2009-12-30 17:29:08 +00:00
|
|
|
return count
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2008-12-16 19:13:31 +00:00
|
|
|
def dump_bible(self):
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-10-01 13:50:29 +00:00
|
|
|
Utility debugging method to dump the contents of a bible.
|
2010-06-15 15:42:52 +00:00
|
|
|
"""
|
2010-02-04 19:34:12 +00:00
|
|
|
log.debug(u'.........Dumping Bible Database')
|
2010-12-28 11:18:56 +00:00
|
|
|
log.debug(u'...............................Books ')
|
2009-07-14 19:44:15 +00:00
|
|
|
books = self.session.query(Book).all()
|
|
|
|
log.debug(books)
|
2010-02-04 19:34:12 +00:00
|
|
|
log.debug(u'...............................Verses ')
|
2009-07-14 19:44:15 +00:00
|
|
|
verses = self.session.query(Verse).all()
|
2010-12-28 11:18:56 +00:00
|
|
|
log.debug(verses)
|