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 #
|
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
|
2011-03-24 19:04:02 +00:00
|
|
|
# Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, Armin Köhler, #
|
|
|
|
# Andreas Preikschat, Mattias Põldaru, Christian Richter, Philip Ridout, #
|
|
|
|
# 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
|
2011-03-12 10:23:42 +00:00
|
|
|
import os
|
2010-03-15 18:47:07 +00:00
|
|
|
import re
|
2011-03-12 10:23:42 +00:00
|
|
|
import sqlite3
|
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
|
2011-03-12 10:23:42 +00:00
|
|
|
from openlp.core.utils import AppLocation
|
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 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
|
|
|
|
|
|
|
|
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)),
|
|
|
|
)
|
2011-04-14 20:33:02 +00:00
|
|
|
|
2010-06-15 00:44:06 +00:00
|
|
|
book_table = Table(u'book', metadata,
|
|
|
|
Column(u'id', types.Integer, primary_key=True),
|
2011-03-17 18:36:54 +00:00
|
|
|
Column(u'book_reference_id', types.Integer),
|
|
|
|
Column(u'testament_reference_id', types.Integer),
|
2010-06-15 00:44:06 +00:00
|
|
|
Column(u'name', types.Unicode(50), index=True),
|
|
|
|
)
|
|
|
|
verse_table = Table(u'verse', metadata,
|
|
|
|
Column(u'id', types.Integer, primary_key=True, index=True),
|
2011-03-23 19:18:51 +00:00
|
|
|
Column(u'book_id', types.Integer, ForeignKey(
|
|
|
|
u'book.id'), index=True),
|
2010-06-15 00:44:06 +00:00
|
|
|
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(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.
|
|
|
|
"""
|
2011-04-03 18:44:08 +00:00
|
|
|
log.info(u'BibleDB loaded')
|
2009-12-27 23:54:00 +00:00
|
|
|
|
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']
|
2011-04-01 20:26:25 +00:00
|
|
|
Manager.__init__(self, u'bibles/bibles', init_schema, self.file)
|
2010-03-16 19:46:19 +00:00
|
|
|
if u'file' in kwargs:
|
|
|
|
self.get_name()
|
2011-04-03 20:09:18 +00:00
|
|
|
if u'path' in kwargs:
|
|
|
|
self.path = kwargs[u'path']
|
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')
|
2010-03-16 19:46:19 +00:00
|
|
|
if version_name:
|
|
|
|
self.name = version_name.value
|
|
|
|
else:
|
|
|
|
self.name = None
|
|
|
|
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
|
|
|
return self.name
|
|
|
|
|
2011-03-17 18:36:54 +00:00
|
|
|
def create_book(self, name, bk_ref_id, testament=1):
|
2010-03-27 12:37:21 +00:00
|
|
|
"""
|
|
|
|
Add a book to the database.
|
|
|
|
|
|
|
|
``name``
|
|
|
|
The name of the book.
|
|
|
|
|
2011-03-17 18:36:54 +00:00
|
|
|
``bk_ref_id``
|
|
|
|
The book_reference_id from bibles_resources.sqlite of the book.
|
2010-03-27 12:37:21 +00:00
|
|
|
|
|
|
|
``testament``
|
2011-03-17 18:36:54 +00:00
|
|
|
*Defaults to 1.* The testament_reference_id from
|
|
|
|
bibles_resources.sqlite of the testament this book belongs to.
|
2010-03-27 12:37:21 +00:00
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BibleDB.create_book("%s", "%s")', name, bk_ref_id)
|
2011-03-17 18:36:54 +00:00
|
|
|
book = Book.populate(name=name, book_reference_id=bk_ref_id,
|
|
|
|
testament_reference_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.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BibleDBcreate_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(
|
|
|
|
book_id = book_id,
|
2010-01-24 07:08:14 +00:00
|
|
|
chapter = chapter,
|
2009-12-30 17:29:08 +00:00
|
|
|
verse = verse_number,
|
|
|
|
text = verse_text
|
|
|
|
)
|
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
|
|
|
"""
|
2011-03-17 18:36:54 +00:00
|
|
|
if not isinstance(value, unicode):
|
|
|
|
value = unicode(value)
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BibleDB.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
|
|
|
"""
|
2011-03-23 19:18:51 +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
|
|
|
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-03-17 20:54:22 +00:00
|
|
|
def get_book_by_book_ref_id(self, id):
|
|
|
|
"""
|
|
|
|
Return a book object from the database.
|
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
``id``
|
|
|
|
The reference id of the book to return.
|
2011-03-17 20:54:22 +00:00
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BibleDB.get_book_by_book_ref_id("%s")', id)
|
2011-03-17 21:04:54 +00:00
|
|
|
db_book = self.get_object_filtered(Book,
|
|
|
|
Book.book_reference_id.like(id))
|
2011-03-17 20:54:22 +00:00
|
|
|
return db_book
|
|
|
|
|
2011-04-03 20:09:18 +00:00
|
|
|
def get_book_ref_id_by_name(self, book, language_id=None):
|
2011-04-11 16:21:54 +00:00
|
|
|
log.debug(u'BibleDB.get_book_ref_id_by_name:("%s", "%s")', book,
|
2011-04-03 20:09:18 +00:00
|
|
|
language_id)
|
|
|
|
if BiblesResourcesDB.get_book(book):
|
|
|
|
book_temp = BiblesResourcesDB.get_book(book)
|
|
|
|
book_id = book_temp[u'id']
|
|
|
|
elif BiblesResourcesDB.get_alternative_book_name(book, language_id):
|
|
|
|
book_id = BiblesResourcesDB.get_alternative_book_name(book,
|
|
|
|
language_id)
|
2011-04-14 08:40:30 +00:00
|
|
|
elif AlternativeBookNamesDB.get_book_reference_id(book,
|
2011-04-03 20:09:18 +00:00
|
|
|
language_id):
|
2011-04-14 08:40:30 +00:00
|
|
|
book_id = AlternativeBookNamesDB.get_book_reference_id(
|
2011-04-03 20:09:18 +00:00
|
|
|
book, language_id)
|
|
|
|
else:
|
|
|
|
from openlp.plugins.bibles.forms import BookNameForm
|
|
|
|
book_ref = None
|
|
|
|
book_name = BookNameForm(self.wizard)
|
|
|
|
if book_name.exec_(book):
|
|
|
|
book_ref = unicode(book_name.requestComboBox.currentText())
|
|
|
|
if not book_ref:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
book_temp = BiblesResourcesDB.get_book(book_ref)
|
|
|
|
if book_temp:
|
|
|
|
book_id = book_temp[u'id']
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
if book_id:
|
2011-04-14 08:40:30 +00:00
|
|
|
AlternativeBookNamesDB.create_alternative_book_name(
|
2011-04-03 20:09:18 +00:00
|
|
|
book, book_id, language_id)
|
|
|
|
if book_id:
|
|
|
|
return book_id
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
def get_verses(self, reference_list):
|
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
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
(book_reference_id, chapter, start_verse, end_verse)
|
2009-12-30 17:29:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
[(u'35', 1, 1, 1), (u'35', 2, 2, 3)]
|
2009-12-30 17:29:08 +00:00
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BibleDB.get_verses("%s")', reference_list)
|
2009-12-30 17:29:08 +00:00
|
|
|
verse_list = []
|
2011-03-23 19:18:51 +00:00
|
|
|
for book_id, chapter, start_verse, end_verse in reference_list:
|
|
|
|
db_book = self.get_book_by_book_ref_id(book_id)
|
2009-12-30 17:29:08 +00:00
|
|
|
if db_book:
|
2011-03-23 19:18:51 +00:00
|
|
|
book_id = db_book.book_reference_id
|
|
|
|
log.debug(u'Book name corrected to "%s"', db_book.name)
|
2011-01-15 23:19:25 +00:00
|
|
|
if end_verse == -1:
|
2011-03-23 19:18:51 +00:00
|
|
|
end_verse = self.get_verse_count(book_id, 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-02-02 23:12:31 +00:00
|
|
|
critical_error_message_box(
|
2011-01-15 19:24:50 +00:00
|
|
|
translate('BiblesPlugin', 'No Book Found'),
|
|
|
|
translate('BiblesPlugin', 'No matching book '
|
2011-01-01 10:33:14 +00:00
|
|
|
'could be found in this Bible. Check that you have '
|
2011-01-15 19:24:50 +00:00
|
|
|
'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:
|
|
|
|
or_clause = []
|
2010-06-01 17:13:54 +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:
|
|
|
|
or_clause.append(Verse.text.like(keyword))
|
2010-10-09 19:36:05 +00:00
|
|
|
verses = verses.filter(or_(*or_clause))
|
2009-12-30 17:29:08 +00:00
|
|
|
else:
|
2010-06-01 17:13:54 +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
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
def get_chapter_count(self, book_id):
|
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
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BibleDB.get_chapter_count("%s")', book_id)
|
2009-12-30 17:29:08 +00:00
|
|
|
count = self.session.query(Verse.chapter).join(Book)\
|
2011-03-23 19:18:51 +00:00
|
|
|
.filter(Book.book_reference_id==book_id)\
|
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
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
def get_verse_count(self, book_id, 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
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BibleDB.get_verse_count("%s", "%s")', book_id, chapter)
|
2009-12-30 17:29:08 +00:00
|
|
|
count = self.session.query(Verse).join(Book)\
|
2011-03-23 19:18:51 +00:00
|
|
|
.filter(Book.book_reference_id==book_id)\
|
2009-12-30 17:29:08 +00:00
|
|
|
.filter(Verse.chapter==chapter)\
|
|
|
|
.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
|
|
|
|
2011-04-02 20:22:35 +00:00
|
|
|
def get_language(self):
|
|
|
|
"""
|
2011-04-03 18:44:08 +00:00
|
|
|
If no language is given it calls a dialog window where the user could
|
|
|
|
choose the bible language.
|
|
|
|
Return the language id of a bible.
|
2011-04-02 20:22:35 +00:00
|
|
|
|
|
|
|
``book``
|
|
|
|
The language the bible is.
|
|
|
|
"""
|
|
|
|
log.debug(u'BibleDB.get_language()')
|
2011-04-03 18:44:08 +00:00
|
|
|
from openlp.plugins.bibles.forms import LanguageForm
|
|
|
|
language = None
|
|
|
|
lang = LanguageForm(self.wizard)
|
|
|
|
if lang.exec_():
|
|
|
|
language = unicode(lang.requestComboBox.currentText())
|
2011-04-02 20:22:35 +00:00
|
|
|
if not language:
|
|
|
|
return False
|
|
|
|
language = BiblesResourcesDB.get_language(language)
|
|
|
|
language_id = language[u'id']
|
|
|
|
self.create_meta(u'language_id', language_id)
|
|
|
|
return language_id
|
|
|
|
|
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)
|
2011-03-12 10:23:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BiblesResourcesDB(QtCore.QObject, Manager):
|
|
|
|
"""
|
|
|
|
This class represents the database-bound Bible Resources. It provide
|
|
|
|
some resources which are used in the Bibles plugin.
|
|
|
|
A wrapper class around a small SQLite database which contains the download
|
|
|
|
resources, a biblelist from the different download resources, the books,
|
|
|
|
chapter counts and verse counts for the web download Bibles, a language
|
2011-03-23 20:08:49 +00:00
|
|
|
reference, the testament reference and some alternative book names. This
|
2011-03-12 10:23:42 +00:00
|
|
|
class contains a singleton "cursor" so that only one connection to the
|
|
|
|
SQLite database is ever used.
|
|
|
|
"""
|
|
|
|
cursor = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_cursor():
|
|
|
|
"""
|
|
|
|
Return the cursor object. Instantiate one if it doesn't exist yet.
|
|
|
|
"""
|
|
|
|
if BiblesResourcesDB.cursor is None:
|
|
|
|
filepath = os.path.join(
|
|
|
|
AppLocation.get_directory(AppLocation.PluginsDir), u'bibles',
|
|
|
|
u'resources', u'bibles_resources.sqlite')
|
|
|
|
conn = sqlite3.connect(filepath)
|
|
|
|
BiblesResourcesDB.cursor = conn.cursor()
|
|
|
|
return BiblesResourcesDB.cursor
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def run_sql(query, parameters=()):
|
|
|
|
"""
|
|
|
|
Run an SQL query on the database, returning the results.
|
|
|
|
|
|
|
|
``query``
|
|
|
|
The actual SQL query to run.
|
|
|
|
|
|
|
|
``parameters``
|
|
|
|
Any variable parameters to add to the query.
|
|
|
|
"""
|
|
|
|
cursor = BiblesResourcesDB.get_cursor()
|
|
|
|
cursor.execute(query, parameters)
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_books():
|
|
|
|
"""
|
|
|
|
Return a list of all the books of the Bible.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_books()')
|
2011-03-12 10:23:42 +00:00
|
|
|
books = BiblesResourcesDB.run_sql(u'SELECT id, testament_id, name, '
|
|
|
|
u'abbreviation, chapters FROM book_reference ORDER BY id')
|
|
|
|
book_list = []
|
|
|
|
for book in books:
|
|
|
|
book_list.append({
|
|
|
|
u'id': book[0],
|
|
|
|
u'testament_id': book[1],
|
|
|
|
u'name': unicode(book[2]),
|
|
|
|
u'abbreviation': unicode(book[3]),
|
|
|
|
u'chapters': book[4]
|
|
|
|
})
|
|
|
|
return book_list
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_book(name):
|
|
|
|
"""
|
|
|
|
Return a book by name or abbreviation.
|
|
|
|
|
|
|
|
``name``
|
|
|
|
The name or abbreviation of the book.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_book("%s")', name)
|
2011-03-12 10:23:42 +00:00
|
|
|
if not isinstance(name, unicode):
|
|
|
|
name = unicode(name)
|
|
|
|
books = BiblesResourcesDB.run_sql(u'SELECT id, testament_id, name, '
|
|
|
|
u'abbreviation, chapters FROM book_reference WHERE name = ? OR '
|
|
|
|
u'abbreviation = ?', (name, name))
|
|
|
|
if books:
|
|
|
|
return {
|
|
|
|
u'id': books[0][0],
|
|
|
|
u'testament_id': books[0][1],
|
|
|
|
u'name': unicode(books[0][2]),
|
|
|
|
u'abbreviation': unicode(books[0][3]),
|
|
|
|
u'chapters': books[0][4]
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2011-03-17 18:36:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_book_by_id(id):
|
|
|
|
"""
|
|
|
|
Return a book by id.
|
|
|
|
|
|
|
|
``id``
|
|
|
|
The id of the book.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_book_by_id("%s")', id)
|
2011-03-17 18:36:54 +00:00
|
|
|
if not isinstance(id, int):
|
|
|
|
id = int(id)
|
|
|
|
books = BiblesResourcesDB.run_sql(u'SELECT id, testament_id, name, '
|
|
|
|
u'abbreviation, chapters FROM book_reference WHERE id = ?',
|
|
|
|
(id, ))
|
|
|
|
if books:
|
|
|
|
return {
|
|
|
|
u'id': books[0][0],
|
|
|
|
u'testament_id': books[0][1],
|
|
|
|
u'name': unicode(books[0][2]),
|
|
|
|
u'abbreviation': unicode(books[0][3]),
|
|
|
|
u'chapters': books[0][4]
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2011-03-12 10:23:42 +00:00
|
|
|
@staticmethod
|
2011-03-23 19:18:51 +00:00
|
|
|
def get_chapter(book_id, chapter):
|
2011-03-12 10:23:42 +00:00
|
|
|
"""
|
|
|
|
Return the chapter details for a specific chapter of a book.
|
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
``book_id``
|
|
|
|
The id of a book.
|
2011-03-12 10:23:42 +00:00
|
|
|
|
|
|
|
``chapter``
|
|
|
|
The chapter number.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_chapter("%s", "%s")', book_id, chapter)
|
|
|
|
if not isinstance(chapter, int):
|
2011-03-12 10:23:42 +00:00
|
|
|
chapter = int(chapter)
|
|
|
|
chapters = BiblesResourcesDB.run_sql(u'SELECT id, book_reference_id, '
|
|
|
|
u'chapter, verse_count FROM chapters WHERE book_reference_id = ?',
|
2011-03-23 19:18:51 +00:00
|
|
|
(book_id,))
|
2011-03-12 10:23:42 +00:00
|
|
|
if chapters:
|
|
|
|
return {
|
|
|
|
u'id': chapters[chapter-1][0],
|
|
|
|
u'book_reference_id': chapters[chapter-1][1],
|
|
|
|
u'chapter': chapters[chapter-1][2],
|
|
|
|
u'verse_count': chapters[chapter-1][3]
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
2011-03-23 19:18:51 +00:00
|
|
|
def get_chapter_count(book_id):
|
2011-03-12 10:23:42 +00:00
|
|
|
"""
|
|
|
|
Return the number of chapters in a book.
|
|
|
|
|
2011-03-23 19:18:51 +00:00
|
|
|
``book_id``
|
|
|
|
The id of the book.
|
2011-03-12 10:23:42 +00:00
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_chapter_count("%s")', book_id)
|
|
|
|
details = BiblesResourcesDB.get_book_by_id(book_id)
|
2011-03-12 10:23:42 +00:00
|
|
|
if details:
|
|
|
|
return details[u'chapters']
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
2011-03-23 19:18:51 +00:00
|
|
|
def get_verse_count(book_id, chapter):
|
2011-03-12 10:23:42 +00:00
|
|
|
"""
|
|
|
|
Return the number of verses in a chapter.
|
|
|
|
|
|
|
|
``book``
|
2011-03-23 19:18:51 +00:00
|
|
|
The id of the book.
|
2011-03-12 10:23:42 +00:00
|
|
|
|
|
|
|
``chapter``
|
|
|
|
The number of the chapter.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_verse_count("%s", "%s")', book_id,
|
|
|
|
chapter)
|
|
|
|
details = BiblesResourcesDB.get_chapter(book_id, chapter)
|
2011-03-12 10:23:42 +00:00
|
|
|
if details:
|
|
|
|
return details[u'verse_count']
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_download_source(source):
|
|
|
|
"""
|
2011-03-13 19:45:46 +00:00
|
|
|
Return a download_source_id by source.
|
2011-03-12 10:23:42 +00:00
|
|
|
|
|
|
|
``name``
|
|
|
|
The name or abbreviation of the book.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_download_source("%s")', source)
|
2011-03-12 10:23:42 +00:00
|
|
|
if not isinstance(source, unicode):
|
|
|
|
source = unicode(source)
|
|
|
|
source = source.title()
|
|
|
|
dl_source = BiblesResourcesDB.run_sql(u'SELECT id, source FROM '
|
|
|
|
u'download_source WHERE source = ?', (source.lower(),))
|
|
|
|
if dl_source:
|
|
|
|
return {
|
|
|
|
u'id': dl_source[0][0],
|
|
|
|
u'source': dl_source[0][1]
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_webbibles(source):
|
|
|
|
"""
|
2011-03-13 19:45:46 +00:00
|
|
|
Return the bibles a webbible provide for download.
|
2011-03-12 10:23:42 +00:00
|
|
|
|
2011-03-17 18:36:54 +00:00
|
|
|
``source``
|
|
|
|
The source of the webbible.
|
2011-03-12 10:23:42 +00:00
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_webbibles("%s")', source)
|
2011-03-12 10:23:42 +00:00
|
|
|
if not isinstance(source, unicode):
|
|
|
|
source = unicode(source)
|
|
|
|
source = BiblesResourcesDB.get_download_source(source)
|
|
|
|
bibles = BiblesResourcesDB.run_sql(u'SELECT id, name, abbreviation, '
|
|
|
|
u'language_id, download_source_id FROM webbibles WHERE '
|
|
|
|
u'download_source_id = ?', (source[u'id'],))
|
2011-03-13 19:45:46 +00:00
|
|
|
if bibles:
|
|
|
|
bibles_temp = []
|
|
|
|
for bible in bibles:
|
|
|
|
bibles_temp.append({
|
|
|
|
u'id': bible[0],
|
|
|
|
u'name': bible[1],
|
|
|
|
u'abbreviation': bible[2],
|
|
|
|
u'language_id': bible[3],
|
|
|
|
u'download_source_id': bible[4]
|
|
|
|
})
|
|
|
|
return bibles_temp
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
2011-03-17 18:36:54 +00:00
|
|
|
def get_webbible(abbreviation, source):
|
|
|
|
"""
|
|
|
|
Return the bibles a webbible provide for download.
|
|
|
|
|
|
|
|
``abbreviation``
|
|
|
|
The abbreviation of the webbible.
|
|
|
|
|
|
|
|
``source``
|
|
|
|
The source of the webbible.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_webbibles("%s", "%s")', abbreviation,
|
|
|
|
source)
|
2011-03-17 18:36:54 +00:00
|
|
|
if not isinstance(abbreviation, unicode):
|
|
|
|
abbreviation = unicode(abbreviation)
|
|
|
|
if not isinstance(source, unicode):
|
|
|
|
source = unicode(source)
|
|
|
|
source = BiblesResourcesDB.get_download_source(source)
|
|
|
|
bible = BiblesResourcesDB.run_sql(u'SELECT id, name, abbreviation, '
|
|
|
|
u'language_id, download_source_id FROM webbibles WHERE '
|
|
|
|
u'download_source_id = ? AND abbreviation = ?', (source[u'id'],
|
|
|
|
abbreviation))
|
|
|
|
if bible:
|
|
|
|
bibles_temp = {
|
|
|
|
u'id': bible[0][0],
|
|
|
|
u'name': bible[0][1],
|
|
|
|
u'abbreviation': bible[0][2],
|
|
|
|
u'language_id': bible[0][3],
|
|
|
|
u'download_source_id': bible[0][4]
|
|
|
|
}
|
|
|
|
return bibles_temp
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
2011-03-23 20:08:49 +00:00
|
|
|
def get_alternative_book_name(name, language_id=None):
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
|
|
|
Return a book_reference_id if the name matches.
|
2011-04-14 08:40:30 +00:00
|
|
|
|
|
|
|
``name``
|
|
|
|
The name to search the id.
|
|
|
|
|
|
|
|
``language_id``
|
|
|
|
The language_id for which language should be searched
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-03-23 20:08:49 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_alternative_book_name("%s", "%s")',
|
|
|
|
name, language_id)
|
2011-03-13 19:45:46 +00:00
|
|
|
if language_id:
|
|
|
|
id = BiblesResourcesDB.run_sql(u'SELECT book_reference_id '
|
2011-03-23 20:08:49 +00:00
|
|
|
u'FROM alternative_book_names WHERE name = ? and language_id '
|
|
|
|
u'= ? ORDER BY id', (name, language_id))
|
2011-03-13 19:45:46 +00:00
|
|
|
else:
|
|
|
|
id = BiblesResourcesDB.run_sql(u'SELECT book_reference_id '
|
2011-03-23 20:08:49 +00:00
|
|
|
u'FROM alternative_book_names WHERE name = ? ORDER BY id', (
|
|
|
|
name, ))
|
2011-03-13 19:45:46 +00:00
|
|
|
if id:
|
|
|
|
return int(id[0][0])
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_language(name):
|
|
|
|
"""
|
|
|
|
Return a dict containing the language id, name and code by name or
|
|
|
|
abbreviation.
|
|
|
|
|
|
|
|
``name``
|
|
|
|
The name or abbreviation of the language.
|
|
|
|
"""
|
2011-04-02 19:17:48 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_language("%s")', name)
|
2011-03-13 19:45:46 +00:00
|
|
|
if not isinstance(name, unicode):
|
|
|
|
name = unicode(name)
|
|
|
|
name = name.title()
|
|
|
|
language = BiblesResourcesDB.run_sql(u'SELECT id, name, code FROM '
|
|
|
|
u'language WHERE name = ? OR code = ?', (name, name.lower()))
|
|
|
|
if language:
|
|
|
|
return {
|
|
|
|
u'id': language[0][0],
|
|
|
|
u'name': unicode(language[0][1]),
|
|
|
|
u'code': unicode(language[0][2])
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2011-03-17 18:36:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_languages():
|
|
|
|
"""
|
|
|
|
Return a dict containing all languages with id, name and code.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_languages()')
|
2011-03-17 18:36:54 +00:00
|
|
|
languages = BiblesResourcesDB.run_sql(u'SELECT id, name, code FROM '
|
|
|
|
u'language ORDER by name')
|
|
|
|
if languages:
|
|
|
|
languages_temp = []
|
|
|
|
for language in languages:
|
|
|
|
languages_temp.append({
|
|
|
|
u'id': language[0],
|
|
|
|
u'name': unicode(language[1]),
|
|
|
|
u'code': unicode(language[2])
|
|
|
|
})
|
|
|
|
return languages_temp
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2011-03-13 19:45:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_testament_reference():
|
|
|
|
"""
|
|
|
|
Return a list of all testaments and their id of the Bible.
|
|
|
|
"""
|
2011-03-23 19:18:51 +00:00
|
|
|
log.debug(u'BiblesResourcesDB.get_testament_reference()')
|
2011-03-13 19:45:46 +00:00
|
|
|
testaments = BiblesResourcesDB.run_sql(u'SELECT id, name FROM '
|
|
|
|
u'testament_reference ORDER BY id')
|
|
|
|
testament_list = []
|
|
|
|
for testament in testaments:
|
|
|
|
testament_list.append({
|
|
|
|
u'id': testament[0],
|
|
|
|
u'name': unicode(testament[1])
|
|
|
|
})
|
|
|
|
return testament_list
|
|
|
|
|
2011-04-14 08:40:30 +00:00
|
|
|
|
2011-03-23 20:08:49 +00:00
|
|
|
class AlternativeBookNamesDB(QtCore.QObject, Manager):
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-03-23 20:08:49 +00:00
|
|
|
This class represents a database-bound alternative book names system.
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-04-14 08:40:30 +00:00
|
|
|
cursor = None
|
|
|
|
conn = None
|
2011-03-13 19:45:46 +00:00
|
|
|
|
2011-04-14 08:40:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_cursor():
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-04-14 08:40:30 +00:00
|
|
|
Return the cursor object. Instantiate one if it doesn't exist yet.
|
|
|
|
If necessary loads up the database and creates the tables if the
|
|
|
|
database doesn't exist.
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-04-14 08:40:30 +00:00
|
|
|
if AlternativeBookNamesDB.cursor is None:
|
|
|
|
filepath = os.path.join(
|
|
|
|
AppLocation.get_directory(AppLocation.DataDir), u'bibles',
|
|
|
|
u'resources', u'alternative_book_names.sqlite')
|
|
|
|
log.debug(u'Filepath: %s' % filepath)
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
#create new DB, create table alternative_book_names
|
|
|
|
AlternativeBookNamesDB.conn = sqlite3.connect(filepath)
|
|
|
|
AlternativeBookNamesDB.conn.execute(u'CREATE TABLE '
|
|
|
|
u'alternative_book_names(id INTEGER NOT NULL, '
|
|
|
|
u'book_reference_id INTEGER, language_id INTEGER, name '
|
|
|
|
u'VARCHAR(50), PRIMARY KEY (id))')
|
|
|
|
else:
|
|
|
|
#use existing DB
|
|
|
|
AlternativeBookNamesDB.conn = sqlite3.connect(filepath)
|
|
|
|
AlternativeBookNamesDB.cursor = AlternativeBookNamesDB.conn.cursor()
|
|
|
|
return AlternativeBookNamesDB.cursor
|
2011-03-13 19:45:46 +00:00
|
|
|
|
2011-04-14 08:40:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def run_sql(query, parameters=(), commit=None):
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-04-14 08:40:30 +00:00
|
|
|
Run an SQL query on the database, returning the results.
|
|
|
|
|
|
|
|
``query``
|
|
|
|
The actual SQL query to run.
|
|
|
|
|
|
|
|
``parameters``
|
|
|
|
Any variable parameters to add to the query
|
|
|
|
|
|
|
|
``commit``
|
|
|
|
If a commit statement is necessary this should be True.
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-04-14 08:40:30 +00:00
|
|
|
cursor = AlternativeBookNamesDB.get_cursor()
|
|
|
|
cursor.execute(query, parameters)
|
|
|
|
if commit:
|
|
|
|
AlternativeBookNamesDB.conn.commit()
|
|
|
|
return cursor.fetchall()
|
2011-03-13 19:45:46 +00:00
|
|
|
|
2011-04-14 08:40:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_book_reference_id(name, language_id=None):
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-04-14 08:40:30 +00:00
|
|
|
Return a book_reference_id if the name matches.
|
2011-03-13 19:45:46 +00:00
|
|
|
|
|
|
|
``name``
|
|
|
|
The name to search the id.
|
|
|
|
|
2011-04-14 08:40:30 +00:00
|
|
|
``language_id``
|
|
|
|
The language_id for which language should be searched
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-04-14 08:40:30 +00:00
|
|
|
log.debug(u'AlternativeBookNamesDB.get_book_reference_id("%s", "%s")',
|
|
|
|
name, language_id)
|
|
|
|
if language_id:
|
2011-04-14 20:18:23 +00:00
|
|
|
id = AlternativeBookNamesDB.run_sql(u'SELECT book_reference_id FROM'
|
|
|
|
u' alternative_book_names WHERE name = ? AND language_id = ?',
|
|
|
|
(name, language_id))
|
2011-03-13 19:45:46 +00:00
|
|
|
else:
|
2011-04-14 20:18:23 +00:00
|
|
|
id = AlternativeBookNamesDB.run_sql(u'SELECT book_reference_id FROM'
|
|
|
|
u' alternative_book_names WHERE name = ?', name)
|
2011-03-13 19:45:46 +00:00
|
|
|
if not id:
|
|
|
|
return None
|
|
|
|
else:
|
2011-04-14 08:40:30 +00:00
|
|
|
return id[0][0]
|
2011-03-13 19:45:46 +00:00
|
|
|
|
2011-04-14 08:40:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_alternative_book_name(name, book_reference_id, language_id):
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-03-23 20:08:49 +00:00
|
|
|
Add an alternative book name to the database.
|
2011-03-13 19:45:46 +00:00
|
|
|
|
|
|
|
``name``
|
2011-03-23 20:08:49 +00:00
|
|
|
The name of the alternative book name.
|
2011-03-13 19:45:46 +00:00
|
|
|
|
|
|
|
``book_reference_id``
|
|
|
|
The book_reference_id of the book.
|
|
|
|
|
|
|
|
``language_id``
|
2011-03-23 20:08:49 +00:00
|
|
|
The language to which the alternative book name belong.
|
2011-03-13 19:45:46 +00:00
|
|
|
"""
|
2011-03-23 20:08:49 +00:00
|
|
|
log.debug(u'AlternativeBookNamesDB.create_alternative_book_name("%s", '
|
|
|
|
'"%s", "%s"', name, book_reference_id, language_id)
|
2011-04-14 08:40:30 +00:00
|
|
|
alternative_book_name = AlternativeBookNamesDB.run_sql(u'INSERT INTO '
|
|
|
|
u'alternative_book_names(book_reference_id, language_id, name) '
|
|
|
|
u'VALUES (?, ?, ?)', (book_reference_id, language_id, name), True)
|
2011-03-23 20:08:49 +00:00
|
|
|
return alternative_book_name
|