forked from openlp/openlp
Moved data for web bibles to am SQLite database.
This commit is contained in:
parent
ce6738a976
commit
78c525ab76
@ -148,7 +148,7 @@ def main():
|
||||
usage = u'Usage: %prog [options] [qt-options]'
|
||||
parser = OptionParser(usage=usage)
|
||||
parser.add_option("-l", "--log-level", dest="loglevel",
|
||||
default="info", metavar="LEVEL",
|
||||
default="warning", metavar="LEVEL",
|
||||
help="Set logging to LEVEL level. Valid values are "
|
||||
"\"debug\", \"info\", \"warning\".")
|
||||
parser.add_option("-p", "--portable", dest="portable",
|
||||
|
@ -27,6 +27,7 @@ import urllib2
|
||||
import chardet
|
||||
import logging
|
||||
import re
|
||||
import sqlite3
|
||||
|
||||
only_verses = re.compile(r'([\w .]+)[ ]+([0-9]+)[ ]*[:|v|V][ ]*([0-9]+)'
|
||||
r'(?:[ ]*-[ ]*([0-9]+|end))?(?:[ ]*,[ ]*([0-9]+)(?:[ ]*-[ ]*([0-9]+|end))?)?',
|
||||
@ -56,10 +57,33 @@ def parse_reference(reference):
|
||||
match = chapter_range.match(reference)
|
||||
if match:
|
||||
log.debug('Found a chapter range.')
|
||||
reference_list.extend([
|
||||
(match.group(1), match.group(2), match.group(3), -1),
|
||||
(match.group(1), match.group(4), 1, match.group(5))
|
||||
])
|
||||
book = match.group(1)
|
||||
from_verse = match.group(3)
|
||||
to_verse = match.group(5)
|
||||
if int(match.group(2)) == int(match.group(4)):
|
||||
reference_list.append(
|
||||
(match.group(1), int(match.group(2)), from_verse, to_verse)
|
||||
)
|
||||
else:
|
||||
if int(match.group(2)) > int(match.group(4)):
|
||||
from_chapter = int(match.group(4))
|
||||
to_chapter = int(match.group(2))
|
||||
else:
|
||||
from_chapter = int(match.group(2))
|
||||
to_chapter = int(match.group(4))
|
||||
for chapter in xrange(from_chapter, to_chapter + 1):
|
||||
if chapter == from_chapter:
|
||||
reference_list.append(
|
||||
(match.group(1), chapter, from_verse, -1)
|
||||
)
|
||||
elif chapter == to_chapter:
|
||||
reference_list.append(
|
||||
(match.group(1), chapter, 1, to_verse)
|
||||
)
|
||||
else:
|
||||
reference_list.append(
|
||||
(match.group(1), chapter, 1, -1)
|
||||
)
|
||||
else:
|
||||
match = only_verses.match(reference)
|
||||
if match:
|
||||
@ -89,9 +113,11 @@ def parse_reference(reference):
|
||||
])
|
||||
else:
|
||||
log.debug('Didn\'t find anything.')
|
||||
log.debug(reference_list)
|
||||
return reference_list
|
||||
|
||||
class SearchResults:
|
||||
|
||||
class SearchResults(object):
|
||||
"""
|
||||
Encapsulate a set of search results. This is Bible-type independant.
|
||||
"""
|
||||
|
@ -197,7 +197,7 @@ class BibleDB(QtCore.QObject):
|
||||
for book, chapter, start_verse, end_verse in reference_list:
|
||||
db_book = self.get_book(book)
|
||||
if end_verse == -1:
|
||||
end_verse = self.get_chapter_count(book)
|
||||
end_verse = self.get_verse_count(book, chapter)
|
||||
if db_book:
|
||||
book = db_book.name
|
||||
log.debug(u'Book name corrected to "%s"', book)
|
||||
|
@ -25,6 +25,8 @@
|
||||
|
||||
import logging
|
||||
import urllib2
|
||||
import os
|
||||
import sqlite3
|
||||
|
||||
from BeautifulSoup import BeautifulSoup
|
||||
|
||||
@ -33,6 +35,89 @@ from common import BibleCommon, SearchResults
|
||||
from db import BibleDB
|
||||
from openlp.plugins.bibles.lib.models import Book
|
||||
|
||||
class HTTPBooks(object):
|
||||
cursor = None
|
||||
|
||||
@staticmethod
|
||||
def get_cursor():
|
||||
if HTTPBooks.cursor is None:
|
||||
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||
u'..', u'resources', u'httpbooks.sqlite')
|
||||
conn = sqlite3.connect(filepath)
|
||||
HTTPBooks.cursor = conn.cursor()
|
||||
return HTTPBooks.cursor
|
||||
|
||||
@staticmethod
|
||||
def run_sql(query, parameters=()):
|
||||
cursor = HTTPBooks.get_cursor()
|
||||
cursor.execute(query, parameters)
|
||||
return cursor.fetchall()
|
||||
|
||||
@staticmethod
|
||||
def get_books():
|
||||
books = HTTPBooks.run_sql(u'SELECT id, testament_id, name, '
|
||||
u'abbreviation, chapters FROM books 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):
|
||||
if not isinstance(name, unicode):
|
||||
name = unicode(name)
|
||||
books = HTTPBooks.run_sql(u'SELECT id, testament_id, name, '
|
||||
u'abbreviation, chapters FROM books WHERE name = ? OR '
|
||||
u'abbreviation = ?', (name, name))
|
||||
if len(books) > 0:
|
||||
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
|
||||
|
||||
@staticmethod
|
||||
def get_chapter(name, chapter):
|
||||
if not isinstance(name, int):
|
||||
chapter = int(chapter)
|
||||
book = HTTPBooks.get_book(name)
|
||||
chapters = HTTPBooks.run_sql(u'SELECT id, book_id, chapter, '
|
||||
u'verses FROM chapters WHERE book_id = ?', (book[u'id'],))
|
||||
if len(chapters) > 0:
|
||||
return {
|
||||
u'id': chapters[0][0],
|
||||
u'book_id': chapters[0][1],
|
||||
u'chapter': chapters[0][2],
|
||||
u'verses': chapters[0][3]
|
||||
}
|
||||
else:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_chapter_count(book):
|
||||
details = HTTPBooks.get_book(book)
|
||||
if details:
|
||||
return details[u'chapters']
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def get_verse_count(book, chapter):
|
||||
details = HTTPBooks.get_chapter(book, chapter)
|
||||
if details:
|
||||
return details[u'verses']
|
||||
return 0
|
||||
|
||||
|
||||
class BGExtract(BibleCommon):
|
||||
global log
|
||||
log = logging.getLogger(u'BibleHTTPMgr(BG_extract)')
|
||||
@ -219,7 +304,7 @@ class HTTPBible(BibleDB):
|
||||
Receiver.send_message(u'bible_nobook')
|
||||
return []
|
||||
db_book = self.create_book(book_details[u'name'],
|
||||
book_details[u'abbr'], book_details[u'test'])
|
||||
book_details[u'abbreviation'], book_details[u'testament_id'])
|
||||
book = db_book.name
|
||||
if self.get_verse_count(book, reference[1]) == 0:
|
||||
Receiver.send_message(u'bible_showprogress')
|
||||
@ -256,21 +341,17 @@ class HTTPBible(BibleDB):
|
||||
return None
|
||||
|
||||
def get_books(self):
|
||||
return [Book.populate(name=book['name']) for book in self.books]
|
||||
return [Book.populate(name=book['name']) for book in HTTPBooks.get_books()]
|
||||
|
||||
def lookup_book(self, book):
|
||||
return HTTPBooks.get_book(book)
|
||||
|
||||
def get_chapter_count(self, book):
|
||||
return self.lookup_book(book)[u'chap']
|
||||
return HTTPBooks.get_chapter_count(book)
|
||||
|
||||
def get_verse_count(self, book, chapter):
|
||||
return HTTPBooks.get_verse_count(book, chapter)
|
||||
|
||||
def set_proxy_server(self, server):
|
||||
self.proxy_server = server
|
||||
|
||||
def set_books(self, books):
|
||||
self.books = books
|
||||
|
||||
def lookup_book(self, name):
|
||||
log.debug('Looking up "%s" in %s', name, self.books)
|
||||
for book in self.books:
|
||||
if book[u'name'] == name or book[u'abbr'] == name:
|
||||
return book
|
||||
return None
|
||||
|
||||
|
@ -106,8 +106,6 @@ class BibleManager(object):
|
||||
self.parent = parent
|
||||
self.web = u'Web'
|
||||
self.db_cache = None
|
||||
self.http_cache = None
|
||||
self.http_books = {}
|
||||
self.path = self.config.get_data_path()
|
||||
self.proxy_name = self.config.get_config(u'proxy name')
|
||||
self.suffix = u'sqlite'
|
||||
@ -115,54 +113,30 @@ class BibleManager(object):
|
||||
self.reload_bibles()
|
||||
self.media = None
|
||||
|
||||
def load_http_books(self):
|
||||
filepath = os.path.split(os.path.abspath(__file__))[0]
|
||||
filepath = os.path.abspath(os.path.join(
|
||||
filepath, u'..', u'resources', u'httpbooks.csv'))
|
||||
books_file = None
|
||||
try:
|
||||
self.http_books = []
|
||||
books_file = open(filepath, u'r')
|
||||
dialect = csv.Sniffer().sniff(books_file.read(1024))
|
||||
books_file.seek(0)
|
||||
books_reader = csv.reader(books_file, dialect)
|
||||
for line in books_reader:
|
||||
self.http_books.append({
|
||||
u'name': unicode(line[0]),
|
||||
u'abbr': unicode(line[1]),
|
||||
u'test': line[2],
|
||||
u'chap': line[3]
|
||||
})
|
||||
except:
|
||||
log.exception(u'Failed to load http books.')
|
||||
finally:
|
||||
if books_file:
|
||||
books_file.close()
|
||||
|
||||
def reload_bibles(self):
|
||||
"""
|
||||
Reloads the Bibles from the available Bible databases on disk. If a web
|
||||
Bible is encountered, an instance of HTTPBible is loaded instead of the
|
||||
BibleDB class.
|
||||
"""
|
||||
log.debug(u'Reload bibles')
|
||||
files = self.config.get_files(self.suffix)
|
||||
log.debug(u'Bible Files %s', files)
|
||||
self.db_cache = {}
|
||||
self.http_cache = {}
|
||||
self.load_http_books()
|
||||
self.web_bibles_present = False
|
||||
for filename in files:
|
||||
name, extension = os.path.splitext(filename)
|
||||
self.db_cache[name] = BibleDB(self.parent, path=self.path, name=name, config=self.config)
|
||||
# look to see if lazy load bible exists and get create getter.
|
||||
source = self.db_cache[name].get_meta(u'download source')
|
||||
if source:
|
||||
self.web_bibles_present = True
|
||||
download_name = self.db_cache[name].get_meta(u'download name').value
|
||||
meta_proxy = self.db_cache[name].get_meta(u'proxy url')
|
||||
web_bible = HTTPBible(self.parent, path=self.path, name=name,
|
||||
config=self.config, download_source=source.value,
|
||||
download_name=download_name)
|
||||
meta_proxy = self.db_cache[name].get_meta(u'proxy url')
|
||||
if meta_proxy:
|
||||
web_bible.set_proxy_server(meta_proxy.value)
|
||||
web_bible.set_books(self.http_books)
|
||||
del self.db_cache[name]
|
||||
#del self.db_cache[name]
|
||||
self.db_cache[name] = web_bible
|
||||
log.debug(u'Bibles reloaded')
|
||||
|
||||
@ -180,10 +154,10 @@ class BibleManager(object):
|
||||
Register a bible in the bible cache, and then import the verses.
|
||||
|
||||
``type``
|
||||
What type of Bible, one of the BibleFormat values.
|
||||
What type of Bible, one of the ``BibleFormat`` values.
|
||||
|
||||
``**kwargs``
|
||||
Keyword arguments to send to the actualy importer class.
|
||||
Keyword arguments to send to the actual importer class.
|
||||
"""
|
||||
class_ = BibleFormat.get_class(type)
|
||||
kwargs['path'] = self.path
|
||||
@ -193,73 +167,61 @@ class BibleManager(object):
|
||||
self.db_cache[name] = importer
|
||||
return importer.do_import()
|
||||
|
||||
def get_bibles(self, mode=BibleMode.Full):
|
||||
def get_bibles(self):
|
||||
"""
|
||||
Returns a list of Books of the bible. When ``mode`` is set to
|
||||
``BibleMode.Full`` this method returns all the Bibles for the Advanced
|
||||
Search, and when the mode is ``BibleMode.Partial`` this method returns
|
||||
all the bibles for the Quick Search.
|
||||
Returns a list of the names of available Bibles.
|
||||
"""
|
||||
log.debug(u'get_bibles')
|
||||
bible_list = []
|
||||
for bible_name, bible_object in self.db_cache.iteritems():
|
||||
#if getattr(bible_object, 'download_source', None):
|
||||
# bible_name = u'%s (%s)' % (bible_name, self.web)
|
||||
bible_list.append(bible_name)
|
||||
return bible_list
|
||||
return [name for name, bible in self.db_cache.iteritems()]
|
||||
|
||||
def is_bible_web(self, bible):
|
||||
pos_end = bible.find(u' (%s)' % self.web)
|
||||
if pos_end != -1:
|
||||
return True, bible[:pos_end]
|
||||
return False, bible
|
||||
|
||||
def get_bible_books(self, bible):
|
||||
def get_books(self, bible):
|
||||
"""
|
||||
Returns a list of the books of the bible
|
||||
"""
|
||||
log.debug(u'get_bible_books')
|
||||
return [{'name': book.name, 'total': self.db_cache[bible].get_chapter_count(book.name)} for book in self.db_cache[bible].get_books()]
|
||||
Returns a list of Bible books, and the number of chapters in that book.
|
||||
|
||||
def get_book_chapter_count(self, book):
|
||||
``bible``
|
||||
Unicode. The Bible to get the list of books from.
|
||||
"""
|
||||
log.debug(u'BibleManager.get_books("%s")', bible)
|
||||
return [
|
||||
{
|
||||
u'name': book.name,
|
||||
u'chapters': self.db_cache[bible].get_chapter_count(book.name)
|
||||
}
|
||||
for book in self.db_cache[bible].get_books()
|
||||
]
|
||||
|
||||
def get_chapter_count(self, bible, book):
|
||||
"""
|
||||
Returns the number of Chapters for a given book
|
||||
"""
|
||||
log.debug(u'get_book_chapter_count %s', book)
|
||||
return self.book_chapters[book]
|
||||
return self.db_cache[bible].get_chapter_count(book)
|
||||
|
||||
def get_book_verse_count(self, bible, book, chapter):
|
||||
def get_verse_count(self, bible, book, chapter):
|
||||
"""
|
||||
Returns all the number of verses for a given
|
||||
book and chapterMaxBibleBookVerses
|
||||
"""
|
||||
log.debug(u'get_book_verse_count %s,%s,%s', bible, book, chapter)
|
||||
web, bible = self.is_bible_web(bible)
|
||||
if web:
|
||||
count = self.db_cache[bible].get_verse_count(book, chapter)
|
||||
if count == 0:
|
||||
# Make sure the first chapter has been downloaded
|
||||
self.get_verse_text(bible, book, chapter, chapter, 1, 1)
|
||||
count = self.db_cache[bible].get_verse_count(book, chapter)
|
||||
return count
|
||||
else:
|
||||
return self.db_cache[bible].get_verse_count(book, chapter)
|
||||
log.debug(u'BibleManager.get_verse_count("%s", "%s", %s)', bible, book, chapter)
|
||||
return self.db_cache[bible].get_verse_count(book, chapter)
|
||||
|
||||
def get_verses(self, bible, versetext):
|
||||
"""
|
||||
Returns all the number of verses for a given
|
||||
book and chapterMaxBibleBookVerses
|
||||
Parses a scripture reference, fetches the verses from the Bible
|
||||
specified, and returns a list of ``Verse`` objects.
|
||||
|
||||
``bible``
|
||||
Unicode. The Bible to use.
|
||||
|
||||
``versetext``
|
||||
Unicode. The scripture reference. Valid scripture references are:
|
||||
|
||||
- Genesis 1:1
|
||||
- Genesis 1:1-10
|
||||
- Genesis 1:1-2:10
|
||||
"""
|
||||
log.debug(u'get_verses_from_text %s, %s', bible, versetext)
|
||||
log.debug(u'BibleManager.get_verses("%s", "%s")', bible, versetext)
|
||||
reflist = parse_reference(versetext)
|
||||
#web_index = bible.find('(%s)' % self.web)
|
||||
#if web_index >= 0:
|
||||
# bible = bible[:web_index - 1]
|
||||
# log.debug('Updated bible name: %s', bible)
|
||||
#web, bible = self.is_bible_web(bible)
|
||||
#if web:
|
||||
# return self.http_cache[bible].get_verses(reflist)
|
||||
#else:
|
||||
return self.db_cache[bible].get_verses(reflist)
|
||||
|
||||
def save_meta_data(self, bible, version, copyright, permissions):
|
||||
@ -277,107 +239,8 @@ class BibleManager(object):
|
||||
Returns the meta data for a given key
|
||||
"""
|
||||
log.debug(u'get_meta %s,%s', bible, key)
|
||||
web, bible = self.is_bible_web(bible)
|
||||
return self.db_cache[bible].get_meta(key)
|
||||
|
||||
def get_verse_text(self, bible, book, schapter, echapter, 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``
|
||||
The name of the bible to be used
|
||||
|
||||
Rest can be guessed at !
|
||||
"""
|
||||
text = []
|
||||
self.media.setQuickMessage(u'')
|
||||
log.debug(u'get_verse_text %s,%s,%s,%s,%s,%s',
|
||||
bible, book, schapter, echapter, sverse, everse)
|
||||
# check to see if book/chapter exists fow HTTP bibles and load cache
|
||||
# if necessary
|
||||
web, bible = self.is_bible_web(bible)
|
||||
web_bible = False
|
||||
log.debug('Web Bibles: %s', self.http_cache)
|
||||
if self.http_cache[bible]:
|
||||
web_bible = True
|
||||
db_book = self.db_cache[bible].get_book(book)
|
||||
if db_book is None:
|
||||
log.debug(u'get_verse_text: new book')
|
||||
for chapter in range(schapter, echapter + 1):
|
||||
self.media.setQuickMessage(
|
||||
unicode(self.media.trUtf8('Downloading %s: %s')) %
|
||||
(book, chapter))
|
||||
search_results = \
|
||||
self.http_cache[bible].get_chapter(bible, book, chapter)
|
||||
if search_results and search_results.has_verselist():
|
||||
## We have found a book of the bible lets check to see
|
||||
## if it was there. By reusing the returned book name
|
||||
## we get a correct book. For example it is possible
|
||||
## to request ac and get Acts back.
|
||||
bookname = search_results.get_book()
|
||||
# check to see if book/chapter exists
|
||||
db_book = self.db_cache[bible].get_book(bookname)
|
||||
if db_book is None:
|
||||
## Then create book, chapter and text
|
||||
db_book = self.db_cache[bible].create_book(
|
||||
bookname, self.book_abbreviations[bookname],
|
||||
self.book_testaments[bookname])
|
||||
log.debug(u'New http book %s, %s, %s',
|
||||
db_book, db_book.id, db_book.name)
|
||||
self.db_cache[bible].create_chapter(
|
||||
db_book.id, search_results.get_chapter(),
|
||||
search_results.get_verselist())
|
||||
else:
|
||||
## Book exists check chapter and texts only.
|
||||
v = self.db_cache[bible].get_chapter(
|
||||
db_book.id, chapter)
|
||||
if v is None:
|
||||
self.media.setQuickMessage(
|
||||
unicode(self.media.trUtf8('%Downloading %s: %s'))\
|
||||
% (book, chapter))
|
||||
self.db_cache[bible].create_chapter(
|
||||
db_book.id, chapter,
|
||||
search_results.get_verselist())
|
||||
else:
|
||||
log.debug(u'get_verse_text : old book')
|
||||
for chapter in range(schapter, echapter + 1):
|
||||
v = self.db_cache[bible].get_chapter(db_book.id, chapter)
|
||||
if v is None:
|
||||
try:
|
||||
self.media.setQuickMessage(\
|
||||
unicode(self.media.trUtf8('Downloading %s: %s'))
|
||||
% (book, chapter))
|
||||
search_results = \
|
||||
self.http_cache[bible].get_chapter(
|
||||
bible, bookn, chapter)
|
||||
if search_results.has_verselist():
|
||||
self.db_cache[bible].create_chapter(
|
||||
db_book.id, search_results.get_chapter(),
|
||||
search_results.get_verselist())
|
||||
except:
|
||||
log.exception(u'Problem getting scripture online')
|
||||
#Now get verses from database
|
||||
if schapter == echapter:
|
||||
text = self.db_cache[bible].get_verses(
|
||||
[(book, schapter, sverse, everse)])
|
||||
else:
|
||||
verse_list = []
|
||||
for chapter in range(schapter, echapter + 1):
|
||||
if chapter == schapter:
|
||||
start = sverse
|
||||
end = self.get_verse_count(bible, book, chapter)
|
||||
elif chapter == echapter:
|
||||
start = 1
|
||||
end = everse
|
||||
else:
|
||||
start = 1
|
||||
end = self.get_verse_count(bible, book, chapter)
|
||||
verse_list.append((bible, chapter, start, end))
|
||||
text = self.db_cache[bible].get_verses(verse_list)
|
||||
return text
|
||||
|
||||
def exists(self, name):
|
||||
"""
|
||||
Check cache to see if new bible
|
||||
|
@ -60,6 +60,7 @@ class BibleMediaItem(MediaManagerItem):
|
||||
self.IconPath = u'songs/song'
|
||||
self.ListViewWithDnD_class = BibleListView
|
||||
self.servicePath = None
|
||||
self.lastReference = u''
|
||||
MediaManagerItem.__init__(self, parent, icon, title)
|
||||
# place to store the search results
|
||||
self.search_results = {}
|
||||
@ -323,21 +324,16 @@ class BibleMediaItem(MediaManagerItem):
|
||||
self.AdvancedSecondBibleComboBox.clear()
|
||||
self.QuickSecondBibleComboBox.addItem(u'')
|
||||
self.AdvancedSecondBibleComboBox.addItem(u'')
|
||||
bibles = self.parent.manager.get_bibles(BibleMode.Full)
|
||||
bibles = self.parent.manager.get_bibles()
|
||||
# load bibles into the combo boxes
|
||||
first = True
|
||||
for bible in bibles:
|
||||
self.QuickVersionComboBox.addItem(bible)
|
||||
self.QuickSecondBibleComboBox.addItem(bible)
|
||||
# Without HTTP
|
||||
#bibles = self.parent.manager.get_bibles(BibleMode.Partial)
|
||||
first = True
|
||||
# load bibles into the combo boxes
|
||||
for bible in bibles:
|
||||
self.AdvancedVersionComboBox.addItem(bible)
|
||||
self.AdvancedSecondBibleComboBox.addItem(bible)
|
||||
if first:
|
||||
first = False
|
||||
# use the first bible as the trigger
|
||||
self.initialiseBible(bible)
|
||||
|
||||
def onListViewResize(self, width, height):
|
||||
@ -374,9 +370,6 @@ class BibleMediaItem(MediaManagerItem):
|
||||
self.AdvancedBookComboBox.itemData(item).toInt()[0])
|
||||
|
||||
def onNewClick(self):
|
||||
#self.bibleimportform = BibleImportForm(
|
||||
# self.parent.config, self.parent.manager, self)
|
||||
#self.bibleimportform.exec_()
|
||||
self.bibleimportform = ImportWizardForm(self, self.parent.config,
|
||||
self.parent.manager, self.parent)
|
||||
self.bibleimportform.exec_()
|
||||
@ -387,14 +380,13 @@ class BibleMediaItem(MediaManagerItem):
|
||||
self.adjustComboBox(frm, self.verses, self.AdvancedToVerse)
|
||||
|
||||
def onAdvancedToChapter(self):
|
||||
text1 = unicode(self.AdvancedFromChapter.currentText())
|
||||
text2 = unicode(self.AdvancedToChapter.currentText())
|
||||
if text1 != text2:
|
||||
frm = unicode(self.AdvancedFromChapter.currentText())
|
||||
to = unicode(self.AdvancedToChapter.currentText())
|
||||
if frm != to:
|
||||
bible = unicode(self.AdvancedVersionComboBox.currentText())
|
||||
book = unicode(self.AdvancedBookComboBox.currentText())
|
||||
# get the verse count for new chapter
|
||||
verses = self.parent.manager.get_book_verse_count(
|
||||
bible, book, int(text2))
|
||||
verses = self.parent.manager.get_verse_count(bible, book, int(to))
|
||||
self.adjustComboBox(1, verses, self.AdvancedToVerse)
|
||||
|
||||
def onAdvancedSearchButton(self):
|
||||
@ -408,6 +400,7 @@ class BibleMediaItem(MediaManagerItem):
|
||||
versetext = u'%s %s:%s-%s:%s' % (book, chapter_from, verse_from, \
|
||||
chapter_to, verse_to)
|
||||
self.search_results = self.parent.manager.get_verses(bible, versetext)
|
||||
self.lastReference = versetext
|
||||
if self.ClearAdvancedSearchComboBox.currentIndex() == 0:
|
||||
self.ListView.clear()
|
||||
self.displayResults(bible)
|
||||
@ -418,7 +411,7 @@ class BibleMediaItem(MediaManagerItem):
|
||||
cf = int(self.AdvancedFromChapter.currentText())
|
||||
self.adjustComboBox(cf, self.chapters_from, self.AdvancedToChapter)
|
||||
# get the verse count for new chapter
|
||||
vse = self.parent.manager.get_book_verse_count(bible, book, cf)
|
||||
vse = self.parent.manager.get_verse_count(bible, book, cf)
|
||||
self.adjustComboBox(1, vse, self.AdvancedFromVerse)
|
||||
self.adjustComboBox(1, vse, self.AdvancedToVerse)
|
||||
|
||||
@ -428,12 +421,8 @@ class BibleMediaItem(MediaManagerItem):
|
||||
text = unicode(self.QuickSearchEdit.displayText())
|
||||
if self.ClearQuickSearchComboBox.currentIndex() == 0:
|
||||
self.ListView.clear()
|
||||
#if self.QuickSearchComboBox.currentIndex() == 1:
|
||||
# self.search_results = \
|
||||
# self.parent.manager.get_verses(bible, text)
|
||||
#else:
|
||||
# self.searchByReference(bible, text)
|
||||
self.search_results = self.parent.manager.get_verses(bible, text)
|
||||
self.lastReference = text
|
||||
if self.search_results:
|
||||
self.displayResults(bible)
|
||||
|
||||
@ -446,60 +435,62 @@ class BibleMediaItem(MediaManagerItem):
|
||||
raw_slides = []
|
||||
raw_footer = []
|
||||
bible_text = u''
|
||||
#If we want to use a 2nd translation / version
|
||||
bible2 = u''
|
||||
if self.SearchTabWidget.currentIndex() == 0:
|
||||
bible2 = unicode(self.QuickSecondBibleComboBox.currentText())
|
||||
else:
|
||||
bible2 = unicode(self.AdvancedSecondBibleComboBox.currentText())
|
||||
if bible2:
|
||||
self.searchByReference(bible2, self.lastReference)
|
||||
bible2_verses = self.search_results
|
||||
bible2_version = self.parent.manager.get_meta_data(bible2, u'Version')
|
||||
bible2_copyright = self.parent.manager.get_meta_data(bible2, u'Copyright')
|
||||
bible2_permission = self.parent.manager.get_meta_data(bible2, u'Permission')
|
||||
# Let's loop through the main lot, and assemble our verses
|
||||
for item in items:
|
||||
bitem = self.ListView.item(item.row())
|
||||
text = unicode((bitem.data(QtCore.Qt.UserRole)).toString())
|
||||
search_verse = text[:text.find(u'(')]
|
||||
bible = text[text.find(u'(') + 1:-1]
|
||||
self.searchByReference(bible, search_verse)
|
||||
book = self.search_results[0].book.name
|
||||
chapter = unicode(self.search_results[0].chapter)
|
||||
verse = unicode(self.search_results[0].verse)
|
||||
text = self.search_results[0].text
|
||||
reference = bitem.data(QtCore.Qt.UserRole).toPyObject()
|
||||
bible = unicode(reference[QtCore.QString('bible')])
|
||||
book = unicode(reference[QtCore.QString('book')])
|
||||
chapter = unicode(reference[QtCore.QString('chapter')])
|
||||
verse = unicode(reference[QtCore.QString('verse')])
|
||||
text = unicode(reference[QtCore.QString('text')])
|
||||
version = unicode(reference[QtCore.QString('version')])
|
||||
copyright = unicode(reference[QtCore.QString('copyright')])
|
||||
permission = unicode(reference[QtCore.QString('permission')])
|
||||
if self.parent.settings_tab.display_style == 1:
|
||||
loc = self.formatVerse(old_chapter, chapter, verse, u'(u', u')')
|
||||
verse_text = self.formatVerse(old_chapter, chapter, verse, u'(u', u')')
|
||||
elif self.parent.settings_tab.display_style == 2:
|
||||
loc = self.formatVerse(old_chapter, chapter, verse, u'{', u'}')
|
||||
verse_text = self.formatVerse(old_chapter, chapter, verse, u'{', u'}')
|
||||
elif self.parent.settings_tab.display_style == 3:
|
||||
loc = self.formatVerse(old_chapter, chapter, verse, u'[', u']')
|
||||
verse_text = self.formatVerse(old_chapter, chapter, verse, u'[', u']')
|
||||
else:
|
||||
loc = self.formatVerse(old_chapter, chapter, verse, u'', u'')
|
||||
verse_text = self.formatVerse(old_chapter, chapter, verse, u'', u'')
|
||||
old_chapter = chapter
|
||||
footer = u'%s (%s %s)' % (book, self.version, self.copyright)
|
||||
footer = u'%s (%s %s)' % (book, version, copyright)
|
||||
#If not found throws and error so add.s
|
||||
try:
|
||||
raw_footer.index(footer)
|
||||
except:
|
||||
if footer not in raw_footer:
|
||||
raw_footer.append(footer)
|
||||
#If we want to use a 2nd translation / version
|
||||
bible2 = u''
|
||||
if self.SearchTabWidget.currentIndex() == 0:
|
||||
bible2 = unicode(self.QuickSecondBibleComboBox.currentText())
|
||||
else:
|
||||
bible2 = unicode(self.AdvancedSecondBibleComboBox.currentText())
|
||||
if len(bible2) > 0:
|
||||
self.searchByReference(bible2, search_verse)
|
||||
footer = u'%s (%s %s)' % (book, self.version, self.copyright)
|
||||
if bible2:
|
||||
footer = u'%s (%s %s)' % (book, version, copyright)
|
||||
#If not found throws and error so add.s
|
||||
try:
|
||||
raw_footer.index(footer)
|
||||
except:
|
||||
if footer not in raw_footer:
|
||||
raw_footer.append(footer)
|
||||
bible_text = u'%s %s \n\n\n %s %s)' % \
|
||||
(loc, text, loc, self.search_results[0].text)
|
||||
bible_text = u'%s %s \n\n %s %s)' % \
|
||||
(verse_text, text, verse_text, bible2_verses[item.row()].text)
|
||||
raw_slides.append(bible_text)
|
||||
bible_text = u''
|
||||
else:
|
||||
#Paragraph style force new line per verse
|
||||
if self.parent.settings_tab.layout_style == 1:
|
||||
text = text + u'\n\n'
|
||||
bible_text = u'%s %s %s' % (bible_text, loc, text)
|
||||
bible_text = u'%s %s %s' % (bible_text, verse_text, text)
|
||||
#if we are verse per slide then create slide
|
||||
if self.parent.settings_tab.layout_style == 0:
|
||||
raw_slides.append(bible_text)
|
||||
bible_text = u''
|
||||
service_item.title = u'%s %s' % (book, loc)
|
||||
|
||||
service_item.title = u'%s %s' % (book, verse_text)
|
||||
if len(self.parent.settings_tab.bible_theme) == 0:
|
||||
service_item.theme = None
|
||||
else:
|
||||
@ -513,14 +504,14 @@ class BibleMediaItem(MediaManagerItem):
|
||||
return True
|
||||
|
||||
def formatVerse(self, old_chapter, chapter, verse, opening, closing):
|
||||
loc = opening
|
||||
verse_text = opening
|
||||
if old_chapter != chapter:
|
||||
loc += chapter + u':'
|
||||
verse_text += chapter + u':'
|
||||
elif not self.parent.settings_tab.show_new_chapters:
|
||||
loc += chapter + u':'
|
||||
loc += verse
|
||||
loc += closing
|
||||
return loc
|
||||
verse_text += chapter + u':'
|
||||
verse_text += verse
|
||||
verse_text += closing
|
||||
return verse_text
|
||||
|
||||
def reloadBibles(self):
|
||||
log.debug(u'Reloading Bibles')
|
||||
@ -529,24 +520,23 @@ class BibleMediaItem(MediaManagerItem):
|
||||
|
||||
def initialiseBible(self, bible):
|
||||
log.debug(u'initialiseBible %s', bible)
|
||||
book_data = self.parent.manager.get_bible_books(bible)
|
||||
book_data = self.parent.manager.get_books(bible)
|
||||
self.AdvancedBookComboBox.clear()
|
||||
first = True
|
||||
for book in book_data:
|
||||
row = self.AdvancedBookComboBox.count()
|
||||
self.AdvancedBookComboBox.addItem(book[u'name'])
|
||||
self.AdvancedBookComboBox.setItemData(
|
||||
row, QtCore.QVariant(book[u'total']))
|
||||
row, QtCore.QVariant(book[u'chapters']))
|
||||
if first:
|
||||
first = False
|
||||
self.initialiseChapterVerse(
|
||||
bible, book[u'name'], book[u'total'])
|
||||
bible, book[u'name'], book[u'chapters'])
|
||||
|
||||
def initialiseChapterVerse(self, bible, book, chapters):
|
||||
log.debug(u'initialiseChapterVerse %s, %s', bible, book)
|
||||
self.chapters_from = chapters
|
||||
self.verses = self.parent.manager.get_book_verse_count(bible,
|
||||
book, 1)
|
||||
self.verses = self.parent.manager.get_verse_count(bible, book, 1)
|
||||
if self.verses == 0:
|
||||
self.AdvancedSearchButton.setEnabled(False)
|
||||
self.AdvancedMessage.setText(self.trUtf8('Bible not fully loaded'))
|
||||
@ -565,12 +555,30 @@ class BibleMediaItem(MediaManagerItem):
|
||||
combo.addItem(unicode(i))
|
||||
|
||||
def displayResults(self, bible):
|
||||
version = self.parent.manager.get_meta_data(bible, u'Version')
|
||||
copyright = self.parent.manager.get_meta_data(bible, u'Copyright')
|
||||
permission = self.parent.manager.get_meta_data(bible, u'Permission')
|
||||
if not permission:
|
||||
permission = u''
|
||||
else:
|
||||
permission = permission.value
|
||||
for count, verse in enumerate(self.search_results):
|
||||
bible_text = u' %s %d:%d (%s)' % (verse.book.name,
|
||||
verse.chapter, verse.verse, bible)
|
||||
bible_text = u' %s %d:%d (%s)' % \
|
||||
(verse.book.name, verse.chapter, verse.verse, bible)
|
||||
bible_verse = QtGui.QListWidgetItem(bible_text)
|
||||
bible_verse.setData(QtCore.Qt.UserRole,
|
||||
QtCore.QVariant(bible_text))
|
||||
#bible_verse.setData(QtCore.Qt.UserRole,
|
||||
# QtCore.QVariant(bible_text))
|
||||
vdict = {
|
||||
'bible': QtCore.QVariant(bible),
|
||||
'version': QtCore.QVariant(version.value),
|
||||
'copyright': QtCore.QVariant(copyright.value),
|
||||
'permission': QtCore.QVariant(permission),
|
||||
'book': QtCore.QVariant(verse.book.name),
|
||||
'chapter': QtCore.QVariant(verse.chapter),
|
||||
'verse': QtCore.QVariant(verse.verse),
|
||||
'text': QtCore.QVariant(verse.text)
|
||||
}
|
||||
bible_verse.setData(QtCore.Qt.UserRole, QtCore.QVariant(vdict))
|
||||
self.ListView.addItem(bible_verse)
|
||||
row = self.ListView.setCurrentRow(count)
|
||||
if row:
|
||||
@ -579,94 +587,3 @@ class BibleMediaItem(MediaManagerItem):
|
||||
def searchByReference(self, bible, search):
|
||||
log.debug(u'searchByReference %s, %s', bible, search)
|
||||
self.search_results = self.parent.manager.get_verses(bible, search)
|
||||
self.copyright = unicode(self.parent.manager.get_meta_data(
|
||||
bible, u'Copyright').value)
|
||||
self.permissions = unicode(self.parent.manager.get_meta_data(
|
||||
bible, u'Permissions').value)
|
||||
self.version = unicode(self.parent.manager.get_meta_data(
|
||||
bible, u'Version').value)
|
||||
|
||||
# def searchByReference(self, bible, search):
|
||||
# log.debug(u'searchByReference %s, %s', bible, search)
|
||||
# book = u''
|
||||
# start_chapter = u''
|
||||
# end_chapter = u''
|
||||
# start_verse = u''
|
||||
# end_verse = u''
|
||||
# search = search.replace(u' ', u' ').strip()
|
||||
# #original = search
|
||||
# message = None
|
||||
# # Remove book beware 0 index arrays
|
||||
# for i in range (len(search)-1, 0, - 1):
|
||||
# if search[i] == u' ':
|
||||
# book = search[:i]
|
||||
# # remove book from string
|
||||
# search = search[i:]
|
||||
# break
|
||||
# # allow V or v for verse instead of :
|
||||
# search = search.replace(u'v', ':')
|
||||
# search = search.replace(u'V', ':')
|
||||
# search = search.strip()
|
||||
# colon = search.find(u':')
|
||||
# if colon == -1:
|
||||
# # number : found
|
||||
# i = search.rfind(u' ')
|
||||
# if i == -1:
|
||||
# chapter = u''
|
||||
# else:
|
||||
# chapter = search[i:len(search)]
|
||||
# hyphen = chapter.find(u'-')
|
||||
# if hyphen != -1:
|
||||
# start_chapter= chapter[:hyphen]
|
||||
# end_chapter= chapter[hyphen + 1:len(chapter)]
|
||||
# else:
|
||||
# start_chapter = chapter
|
||||
# else:
|
||||
# # more complex
|
||||
# sp = search.split(u'-') #find first
|
||||
# sp1 = sp[0].split(u':')
|
||||
# if len(sp1) == 1:
|
||||
# start_chapter = sp1[0]
|
||||
# start_verse = 1
|
||||
# else:
|
||||
# start_chapter = sp1[0]
|
||||
# start_verse = sp1[1]
|
||||
# if len(sp)== 1:
|
||||
# end_chapter = start_chapter
|
||||
# end_verse = start_verse
|
||||
# else:
|
||||
# sp1 = sp[1].split(u':')
|
||||
# if len(sp1) == 1:
|
||||
# end_chapter = start_chapter
|
||||
# end_verse = sp1[0]
|
||||
# else:
|
||||
# end_chapter = sp1[0]
|
||||
# end_verse = sp1[1]
|
||||
# if end_chapter == u'':
|
||||
# end_chapter = start_chapter.rstrip()
|
||||
# if start_verse == u'':
|
||||
# if end_verse == u'':
|
||||
# start_verse = 1
|
||||
# else:
|
||||
# start_verse = end_verse
|
||||
# if end_verse == u'':
|
||||
# end_verse = 99
|
||||
# if start_chapter == u'':
|
||||
# message = self.trUtf8('No chapter found for search criteria')
|
||||
# log.debug(u'results = %s @ %s : %s @ %s : %s'% \
|
||||
# (unicode(book), unicode(start_chapter), unicode(end_chapter),
|
||||
# unicode(start_verse), unicode(end_verse)))
|
||||
# if message is None:
|
||||
# self.search_results = None
|
||||
# self.search_results = self.parent.manager.get_verse_text(
|
||||
# bible, book, int(start_chapter), int(end_chapter),
|
||||
# int(start_verse), int(end_verse))
|
||||
# self.copyright = unicode(self.parent.manager.get_meta_data(
|
||||
# bible, u'Copyright').value)
|
||||
# self.permissions = unicode(self.parent.manager.get_meta_data(
|
||||
# bible, u'Permissions').value)
|
||||
# self.version = unicode(self.parent.manager.get_meta_data(
|
||||
# bible, u'Version').value)
|
||||
# else:
|
||||
# QtGui.QMessageBox.information(
|
||||
# self, self.trUtf8('Information'), message)
|
@ -105,4 +105,4 @@ mapper(Testament, testament_table,
|
||||
properties={'books': relation(Book, backref='testament')})
|
||||
mapper(Book, book_table,
|
||||
properties={'verses': relation(Verse, backref='book')})
|
||||
mapper(Verse, verse_table)
|
||||
mapper(Verse, verse_table)
|
||||
|
Loading…
Reference in New Issue
Block a user