add download of all books a webbible contains

add dialog while the import of a webbible if the book could not be assign to a reference_book
add dialog while tho import of a webbible if the language is unknown
change the bible database
This commit is contained in:
Armin Köhler 2011-03-17 19:36:54 +01:00
parent 251c70fa47
commit 1c59fd656f
12 changed files with 504 additions and 48 deletions

View File

@ -52,5 +52,6 @@ from the .ui files later if necessary.
""" """
from bibleimportform import BibleImportForm from bibleimportform import BibleImportForm
from bibleimportrequestform import BibleImportRequest
__all__ = ['BibleImportForm'] __all__ = ['BibleImportForm']

View File

@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2011 Raoul Snyman #
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Meinert Jordan, Armin Köhler, Andreas Preikschat, #
# Christian Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon #
# Tibble, Carsten Tinggaard, Frode Woldsund #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
from PyQt4 import QtCore, QtGui
from openlp.core.lib import translate
from openlp.core.lib.ui import create_accept_reject_button_box
class Ui_BibleImportRequest(object):
def setupUi(self, bibleImportRequest):
bibleImportRequest.setObjectName("BibleImportRequest")
bibleImportRequest.resize(400, 175)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.MinimumExpanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(bibleImportRequest.sizePolicy()
.hasHeightForWidth())
bibleImportRequest.setSizePolicy(sizePolicy)
self.widget = QtGui.QWidget(bibleImportRequest)
self.widget.setGeometry(QtCore.QRect(10, 15, 381, 151))
self.widget.setObjectName("widget")
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setObjectName("verticalLayout")
self.headlineLabel = QtGui.QLabel(self.widget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(11)
font.setWeight(75)
font.setBold(True)
self.headlineLabel.setFont(font)
self.headlineLabel.setObjectName("HeadlineLabel")
self.verticalLayout.addWidget(self.HeadlineLabel)
self.infoLabel = QtGui.QLabel(self.widget)
self.infoLabel.setObjectName("InfoLabel")
self.verticalLayout.addWidget(self.infoLabel)
self.formLayout = QtGui.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.requestLabel = QtGui.QLabel(self.widget)
self.requestLabel.setObjectName("RequestLabel")
self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole,
self.requestLabel)
self.requestComboBox = QtGui.QComboBox(self.widget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.requestComboBox.sizePolicy()
.hasHeightForWidth())
self.requestComboBox.setSizePolicy(sizePolicy)
self.requestComboBox.setObjectName("RequestComboBox")
self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole,
self.requestComboBox)
self.verticalLayout.addLayout(self.formLayout)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum,
QtGui.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.formLayout.addWidget(
create_accept_reject_button_box(bibleImportRequest))
self.retranslateUi(bibleImportRequest)
QtCore.QMetaObject.connectSlotsByName(bibleImportRequest)
def retranslateUi(self, bibleImportRequest):
bibleImportRequest.setWindowTitle(
translate("BiblesPlugin.bibleImportRequest", "Dialog"))
self.headlineLabel.setText(
translate("BiblesPlugin.bibleImportRequest", "Choose Book:"))
self.infoLabel.setText(translate("BiblesPlugin.bibleImportRequest",
"The following books cannot be clearly attributed. \n"
"Please choose the book it is."))
self.requestLabel.setText(translate("BiblesPlugin.bibleImportRequest",
"Book:"))

View File

@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2011 Raoul Snyman #
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Meinert Jordan, Armin Köhler, Andreas Preikschat, #
# Christian Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon #
# Tibble, Carsten Tinggaard, Frode Woldsund #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
"""
Module implementing BibleImportRequest.
"""
import logging
from PyQt4.QtGui import QDialog
from openlp.core.lib import translate
from openlp.core.lib.ui import critical_error_message_box
from openlp.plugins.bibles.forms.bibleimportrequestdialog import \
Ui_BibleImportRequest
from openlp.plugins.bibles.lib.db import BiblesResourcesDB
log = logging.getLogger(__name__)
class BibleImportRequest(QDialog, Ui_BibleImportRequest):
"""
Class documentation goes here.
"""
log.info(u'BibleImportRequest loaded')
def __init__(self, parent = None):
"""
Constructor
"""
QDialog.__init__(self, parent)
self.setupUi(self)
def exec_(self, case, name=None):
items = []
self.requestComboBox.addItem(u'')
if case == u'language':
self.headlineLabel.setText(translate(
"BiblesPlugin.BibleImportRequest", "Choose Language:"))
self.infoLabel.setText(translate("BiblesPlugin.BibleImportRequest",
"Please choose the language the bible is."))
self.requestLabel.setText(
translate("BiblesPlugin.BibleImportRequest", "Language:"))
items = BiblesResourcesDB.get_languages()
elif case == u'book':
self.requestLabel.setText(
translate("BiblesPlugin.BibleImportRequest", name))
items = BiblesResourcesDB.get_books()
for item in items:
self.requestComboBox.addItem(item[u'name'])
return QDialog.exec_(self)
def accept(self):
if self.requestComboBox.currentText() == u"":
critical_error_message_box(
message=translate('BiblesPlugin.BibleImportRequest',
'You need to choose an item.'))
self.requestComboBox.setFocus()
return False
else:
return QDialog.accept(self)

View File

@ -135,6 +135,7 @@ class CSVBible(BibleDB):
self.wizard.progressBar.setMinimum(0) self.wizard.progressBar.setMinimum(0)
self.wizard.progressBar.setMaximum(66) self.wizard.progressBar.setMaximum(66)
success = True success = True
#TODO: include create_meta language
books_file = None books_file = None
book_list = {} book_list = {}
# Populate the Tables # Populate the Tables
@ -148,6 +149,8 @@ class CSVBible(BibleDB):
self.wizard.incrementProgressBar(unicode( self.wizard.incrementProgressBar(unicode(
translate('BibleDB.Wizard', 'Importing books... %s')) % translate('BibleDB.Wizard', 'Importing books... %s')) %
unicode(line[2], details['encoding'])) unicode(line[2], details['encoding']))
#TODO: change create_book to the new database model
#(name, bk_ref_id, testament)
self.create_book(unicode(line[2], details['encoding']), self.create_book(unicode(line[2], details['encoding']),
unicode(line[3], details['encoding']), int(line[1])) unicode(line[3], details['encoding']), int(line[1]))
book_list[int(line[0])] = unicode(line[2], details['encoding']) book_list[int(line[0])] = unicode(line[2], details['encoding'])

View File

@ -95,9 +95,9 @@ def init_schema(url):
) )
book_table = Table(u'book', metadata, book_table = Table(u'book', metadata,
Column(u'id', types.Integer, primary_key=True), Column(u'id', types.Integer, primary_key=True),
Column(u'testament_id', types.Integer, ForeignKey(u'testament.id')), Column(u'book_reference_id', types.Integer),
Column(u'testament_reference_id', types.Integer),
Column(u'name', types.Unicode(50), index=True), Column(u'name', types.Unicode(50), index=True),
Column(u'abbreviation', types.Unicode(5), index=True),
) )
verse_table = Table(u'verse', metadata, verse_table = Table(u'verse', metadata,
Column(u'id', types.Integer, primary_key=True, index=True), Column(u'id', types.Integer, primary_key=True, index=True),
@ -114,8 +114,7 @@ def init_schema(url):
try: try:
class_mapper(Testament) class_mapper(Testament)
except UnmappedClassError: except UnmappedClassError:
mapper(Testament, testament_table, mapper(Testament, testament_table)
properties={'books': relation(Book, backref='testament')})
try: try:
class_mapper(Book) class_mapper(Book)
except UnmappedClassError: except UnmappedClassError:
@ -251,22 +250,23 @@ class BibleDB(QtCore.QObject, Manager):
self.save_object(Testament.populate(name=u'New Testament')) self.save_object(Testament.populate(name=u'New Testament'))
self.save_object(Testament.populate(name=u'Apocrypha')) self.save_object(Testament.populate(name=u'Apocrypha'))
def create_book(self, name, abbrev, testament=1): def create_book(self, name, bk_ref_id, testament=1):
""" """
Add a book to the database. Add a book to the database.
``name`` ``name``
The name of the book. The name of the book.
``abbrev`` ``bk_ref_id``
The abbreviation of the book. The book_reference_id from bibles_resources.sqlite of the book.
``testament`` ``testament``
*Defaults to 1.* The id of the testament this book belongs to. *Defaults to 1.* The testament_reference_id from
bibles_resources.sqlite of the testament this book belongs to.
""" """
log.debug(u'create_book %s,%s', name, abbrev) log.debug(u'create_book %s,%s', name, bk_ref_id)
book = Book.populate(name=name, abbreviation=abbrev, book = Book.populate(name=name, book_reference_id=bk_ref_id,
testament_id=testament) testament_reference_id=testament)
self.save_object(book) self.save_object(book)
return book return book
@ -334,6 +334,8 @@ class BibleDB(QtCore.QObject, Manager):
``value`` ``value``
The value for this instance. The value for this instance.
""" """
if not isinstance(value, unicode):
value = unicode(value)
log.debug(u'save_meta %s/%s', key, value) log.debug(u'save_meta %s/%s', key, value)
self.save_object(BibleMeta.populate(key=key, value=value)) self.save_object(BibleMeta.populate(key=key, value=value))
@ -346,9 +348,6 @@ class BibleDB(QtCore.QObject, Manager):
""" """
log.debug(u'BibleDb.get_book("%s")', book) log.debug(u'BibleDb.get_book("%s")', book)
db_book = self.get_object_filtered(Book, Book.name.like(book + u'%')) db_book = self.get_object_filtered(Book, Book.name.like(book + u'%'))
if db_book is None:
db_book = self.get_object_filtered(Book,
Book.abbreviation.like(book + u'%'))
return db_book return db_book
def get_books(self): def get_books(self):
@ -358,7 +357,7 @@ class BibleDB(QtCore.QObject, Manager):
""" """
return self.get_all_objects(Book, order_by_ref=Book.id) return self.get_all_objects(Book, order_by_ref=Book.id)
def get_verses(self, reference_list): def get_verses(self, reference_list, en_reference_list):
""" """
This is probably the most used function. It retrieves the list of This is probably the most used function. It retrieves the list of
verses based on the user's query. verses based on the user's query.
@ -376,15 +375,18 @@ class BibleDB(QtCore.QObject, Manager):
[(u'Genesis', 1, 1, 1), (u'Genesis', 2, 2, 3)] [(u'Genesis', 1, 1, 1), (u'Genesis', 2, 2, 3)]
""" """
log.debug(u'BibleDB.get_verses: %s', reference_list) log.debug(u'BibleDB.get_verses: %s - %s', reference_list,
en_reference_list)
verse_list = [] verse_list = []
for book, chapter, start_verse, end_verse in reference_list: for (book, chapter, start_verse, end_verse), (en_book, en_chapter,
en_start_verse, en_end_verse) in zip(reference_list,
en_reference_list):
db_book = self.get_book(book) db_book = self.get_book(book)
if db_book: if db_book:
book = db_book.name book = db_book.name
log.debug(u'Book name corrected to "%s"', book) log.debug(u'Book name corrected to "%s"', book)
if end_verse == -1: if end_verse == -1:
end_verse = self.get_verse_count(book, chapter) end_verse = self.get_verse_count(en_book, chapter)
verses = self.session.query(Verse)\ verses = self.session.query(Verse)\
.filter_by(book_id=db_book.id)\ .filter_by(book_id=db_book.id)\
.filter_by(chapter=chapter)\ .filter_by(chapter=chapter)\
@ -545,9 +547,9 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
``name`` ``name``
The name or abbreviation of the book. The name or abbreviation of the book.
""" """
log.debug(u'get_book: %s', name)
if not isinstance(name, unicode): if not isinstance(name, unicode):
name = unicode(name) name = unicode(name)
name = name.title()
books = BiblesResourcesDB.run_sql(u'SELECT id, testament_id, name, ' books = BiblesResourcesDB.run_sql(u'SELECT id, testament_id, name, '
u'abbreviation, chapters FROM book_reference WHERE name = ? OR ' u'abbreviation, chapters FROM book_reference WHERE name = ? OR '
u'abbreviation = ?', (name, name)) u'abbreviation = ?', (name, name))
@ -562,6 +564,30 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
else: else:
return None return None
@staticmethod
def get_book_by_id(id):
"""
Return a book by id.
``id``
The id of the book.
"""
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
@staticmethod @staticmethod
def get_chapter(name, chapter): def get_chapter(name, chapter):
""" """
@ -644,8 +670,8 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
""" """
Return the bibles a webbible provide for download. Return the bibles a webbible provide for download.
``name`` ``source``
The name of the webbible. The source of the webbible.
""" """
if not isinstance(source, unicode): if not isinstance(source, unicode):
source = unicode(source) source = unicode(source)
@ -667,6 +693,38 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
else: else:
return None return None
@staticmethod
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.
"""
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 @staticmethod
def get_spelling(name, language_id=None): def get_spelling(name, language_id=None):
""" """
@ -707,6 +765,25 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
else: else:
return None return None
@staticmethod
def get_languages():
"""
Return a dict containing all languages with id, name and code.
"""
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
@staticmethod @staticmethod
def get_testament_reference(): def get_testament_reference():
""" """
@ -766,7 +843,7 @@ class SpellingDB(QtCore.QObject, Manager):
def get_book_reference_id(self, name, language=None): def get_book_reference_id(self, name, language=None):
""" """
Return the book_reference_id of a name. Return the book_reference_id of a book by name.
``name`` ``name``
The name to search the id. The name to search the id.
@ -785,7 +862,7 @@ class SpellingDB(QtCore.QObject, Manager):
if not id: if not id:
return None return None
else: else:
return id return id[0]
def create_spelling(self, name, book_reference_id, language_id): def create_spelling(self, name, book_reference_id, language_id):
""" """

View File

@ -41,7 +41,8 @@ from openlp.core.lib import Receiver, translate
from openlp.core.lib.ui import critical_error_message_box from openlp.core.lib.ui import critical_error_message_box
from openlp.core.utils import AppLocation, get_web_page from openlp.core.utils import AppLocation, get_web_page
from openlp.plugins.bibles.lib import SearchResults from openlp.plugins.bibles.lib import SearchResults
from openlp.plugins.bibles.lib.db import BibleDB, BiblesResourcesDB, Book from openlp.plugins.bibles.lib.db import BibleDB, BiblesResourcesDB, \
SpellingDB, Book
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -123,6 +124,52 @@ class BGExtract(object):
return None return None
return SearchResults(bookname, chapter, verse_list) return SearchResults(bookname, chapter, verse_list)
def get_books_from_http(self, version):
"""
Load a list of all books a bible contaions from BibleGateway website.
``version``
The version of the bible like NIV for New International Version
"""
log.debug(u'get_books_from_http %s', version)
url_params = urllib.urlencode(
{u'search': 'Bible-List', u'version': u'%s' % version})
reference_url = u'http://www.biblegateway.com/passage/?%s' % url_params
page = get_web_page(reference_url)
if not page:
send_error_message(u'download')
return None
page_source = page.read()
page_source = unicode(page_source, 'utf8')
page_source_temp = re.search(u'<table id="booklist".*?>.*?</table>', \
page_source, re.DOTALL)
if page_source_temp:
soup = page_source_temp.group(0)
else:
soup = None
try:
soup = BeautifulSoup(soup)
except HTMLParseError:
log.exception(u'BeautifulSoup could not parse the bible page.')
if not soup:
send_error_message(u'parse')
return None
Receiver.send_message(u'openlp_process_events')
content = soup.find(u'table', {u'id': u'booklist'})
content = content.findAll(u'tr')
#log.debug(content)
if not content:
log.exception(u'No books found in the Biblegateway response.')
send_error_message(u'parse')
return None
books = []
for book in content:
book = book.find(u'td')
if book:
books.append(book.contents[0])
log.debug(book.contents[0])
return books
class BSExtract(object): class BSExtract(object):
""" """
@ -168,6 +215,31 @@ class BSExtract(object):
verses[versenumber] = verse.contents[1].rstrip(u'\n') verses[versenumber] = verse.contents[1].rstrip(u'\n')
return SearchResults(bookname, chapter, verses) return SearchResults(bookname, chapter, verses)
def get_books_from_http(self, version):
"""
Load a list of all books a bible contains from Bibleserver mobile
website.
``version``
The version of the bible like NIV for New International Version
"""
log.debug(u'get_books_from_http %s', version)
chapter_url = u'http://m.bibleserver.com/overlay/selectBook?'\
'translation=%s' % (version)
soup = get_soup_for_bible_ref(chapter_url)
if not soup:
return None
content = soup.find(u'ul')
if not content:
log.exception(u'No books found in the Bibleserver response.')
send_error_message(u'parse')
return None
content = content.findAll(u'li')
books = []
for book in content:
books.append(book.contents[0].contents[0])
return books
class CWExtract(object): class CWExtract(object):
""" """
@ -237,6 +309,33 @@ class CWExtract(object):
verses[versenumber] = versetext verses[versenumber] = versetext
return SearchResults(bookname, chapter, verses) return SearchResults(bookname, chapter, verses)
def get_books_from_http(self, version):
"""
Load a list of all books a bible contain from the Crosswalk website.
``version``
The version of the bible like NIV for New International Version
"""
log.debug(u'get_books_from_http %s', version)
chapter_url = u'http://www.biblestudytools.com/%s/'\
% (version)
soup = get_soup_for_bible_ref(chapter_url)
if not soup:
return None
content = soup.find(u'div', {u'class': u'Body'})
content = content.find(u'ul', {u'class': u'parent'})
if not content:
log.exception(u'No books found in the Crosswalk response.')
send_error_message(u'parse')
return None
content = content.findAll(u'li')
books = []
for book in content:
book = book.find(u'a')
books.append(book.contents[0])
log.debug(book.contents[0])
return books
class HTTPBible(BibleDB): class HTTPBible(BibleDB):
log.info(u'%s HTTPBible loaded' , __name__) log.info(u'%s HTTPBible loaded' , __name__)
@ -252,6 +351,7 @@ class HTTPBible(BibleDB):
Init confirms the bible exists and stores the database path. Init confirms the bible exists and stores the database path.
""" """
BibleDB.__init__(self, parent, **kwargs) BibleDB.__init__(self, parent, **kwargs)
self.parent = parent
self.download_source = kwargs[u'download_source'] self.download_source = kwargs[u'download_source']
self.download_name = kwargs[u'download_name'] self.download_name = kwargs[u'download_name']
# TODO: Clean up proxy stuff. We probably want one global proxy per # TODO: Clean up proxy stuff. We probably want one global proxy per
@ -259,6 +359,8 @@ class HTTPBible(BibleDB):
self.proxy_server = None self.proxy_server = None
self.proxy_username = None self.proxy_username = None
self.proxy_password = None self.proxy_password = None
if u'path' in kwargs:
self.path = kwargs[u'path']
if u'proxy_server' in kwargs: if u'proxy_server' in kwargs:
self.proxy_server = kwargs[u'proxy_server'] self.proxy_server = kwargs[u'proxy_server']
if u'proxy_username' in kwargs: if u'proxy_username' in kwargs:
@ -283,9 +385,37 @@ class HTTPBible(BibleDB):
if self.proxy_password: if self.proxy_password:
# Store the proxy password. # Store the proxy password.
self.create_meta(u'proxy password', self.proxy_password) self.create_meta(u'proxy password', self.proxy_password)
if self.download_source.lower() == u'crosswalk':
handler = CWExtract(self.proxy_server)
elif self.download_source.lower() == u'biblegateway':
handler = BGExtract(self.proxy_server)
elif self.download_source.lower() == u'bibleserver':
handler = BSExtract(self.proxy_server)
books = handler.get_books_from_http(self.download_name)
if not books:
log.exception(u'Importing books from %s - download name: "%s" '\
'failed' % (self.download_source, self.download_name))
return False
bible = BiblesResourcesDB.get_webbible(self.download_name,
self.download_source.lower())
if bible[u'language_id']:
language_id = bible[u'language_id']
else:
language = self.parent.mediaItem.importRequest(u'language')
language = BiblesResourcesDB.get_language(language)
language_id = language[u'id']
# Store the language_id.
self.create_meta(u'language_id', language_id)
for book in books:
book_ref_id = self.parent.manager.get_book_ref_id_by_name(book,
language_id)
book_details = BiblesResourcesDB.get_book_by_id(book_ref_id)
log.debug(u'Book details: Name:%s; id:%s; testament_id:%s',
book, book_ref_id, book_details[u'testament_id'])
self.create_book(book, book_ref_id, book_details[u'testament_id'])
return True return True
def get_verses(self, reference_list): def get_verses(self, reference_list, en_reference_list):
""" """
A reimplementation of the ``BibleDB.get_verses`` method, this one is A reimplementation of the ``BibleDB.get_verses`` method, this one is
specifically for web Bibles. It first checks to see if the particular specifically for web Bibles. It first checks to see if the particular
@ -299,6 +429,13 @@ class HTTPBible(BibleDB):
(book, chapter, start_verse, end_verse) (book, chapter, start_verse, end_verse)
``en_reference_list``
This is the list of references the media manager item wants. It is
a list of tuples, with the following format with englisch book
names::
(book, chapter, start_verse, end_verse)
Therefore, when you are looking for multiple items, simply break Therefore, when you are looking for multiple items, simply break
them up into references like this, bundle them into a list. This them up into references like this, bundle them into a list. This
function then runs through the list, and returns an amalgamated function then runs through the list, and returns an amalgamated
@ -311,17 +448,12 @@ class HTTPBible(BibleDB):
book = reference[0] book = reference[0]
db_book = self.get_book(book) db_book = self.get_book(book)
if not db_book: if not db_book:
book_details = BiblesResourcesDB.get_book(book)
if not book_details:
critical_error_message_box( critical_error_message_box(
translate('BiblesPlugin', 'No Book Found'), translate('BiblesPlugin', 'No Book Found'),
translate('BiblesPlugin', 'No matching ' translate('BiblesPlugin', 'No matching '
'book could be found in this Bible. Check that you ' 'book could be found in this Bible. Check that you '
'have spelled the name of the book correctly.')) 'have spelled the name of the book correctly.'))
return [] return []
db_book = self.create_book(book_details[u'name'],
book_details[u'abbreviation'],
book_details[u'testament_id'])
book = db_book.name book = db_book.name
if BibleDB.get_verse_count(self, book, reference[1]) == 0: if BibleDB.get_verse_count(self, book, reference[1]) == 0:
Receiver.send_message(u'cursor_busy') Receiver.send_message(u'cursor_busy')
@ -340,7 +472,7 @@ class HTTPBible(BibleDB):
Receiver.send_message(u'openlp_process_events') Receiver.send_message(u'openlp_process_events')
Receiver.send_message(u'cursor_normal') Receiver.send_message(u'cursor_normal')
Receiver.send_message(u'openlp_process_events') Receiver.send_message(u'openlp_process_events')
return BibleDB.get_verses(self, reference_list) return BibleDB.get_verses(self, reference_list, en_reference_list)
def get_chapter(self, book, chapter): def get_chapter(self, book, chapter):
""" """
@ -360,8 +492,7 @@ class HTTPBible(BibleDB):
""" """
Return the list of books. Return the list of books.
""" """
return [Book.populate(name=book['name']) return self.get_all_objects(Book, order_by_ref=Book.id)
for book in BiblesResourcesDB.get_books()]
def get_chapter_count(self, book): def get_chapter_count(self, book):
""" """

View File

@ -220,19 +220,28 @@ class BibleManager(object):
Unicode. The Bible to get the list of books from. Unicode. The Bible to get the list of books from.
""" """
log.debug(u'BibleManager.get_books("%s")', bible) log.debug(u'BibleManager.get_books("%s")', bible)
return [ language_id = self.get_meta_data(bible, u'language_id')
books = []
for book in self.db_cache[bible].get_books():
book_id = self.get_book_ref_id_by_name(book.name, int(
language_id.value))
book_temp = BiblesResourcesDB.get_book_by_id(book_id)
book_ref = book_temp[u'name']
books.append(
{ {
u'name': book.name, u'name': book.name,
u'chapters': self.db_cache[bible].get_chapter_count(book.name) u'chapters': self.db_cache[bible].get_chapter_count(book_ref)
} })
for book in self.db_cache[bible].get_books() return books
]
def get_chapter_count(self, bible, book): def get_chapter_count(self, bible, book):
""" """
Returns the number of Chapters for a given book. Returns the number of Chapters for a given book.
""" """
log.debug(u'get_book_chapter_count %s', book) log.debug(u'BibleManager.get_book_chapter_count ("%s", "%s")', bible,
book)
language_id = self.get_meta_data(bible, u'language_id')
book = self.get_book_ref(book, int(language_id.value))
return self.db_cache[bible].get_chapter_count(book) return self.db_cache[bible].get_chapter_count(book)
def get_verse_count(self, bible, book, chapter): def get_verse_count(self, bible, book, chapter):
@ -242,6 +251,8 @@ class BibleManager(object):
""" """
log.debug(u'BibleManager.get_verse_count("%s", "%s", %s)', log.debug(u'BibleManager.get_verse_count("%s", "%s", %s)',
bible, book, chapter) bible, book, chapter)
language_id = self.get_meta_data(bible, u'language_id')
book = self.get_book_ref(book, int(language_id.value))
return self.db_cache[bible].get_verse_count(book, chapter) return self.db_cache[bible].get_verse_count(book, chapter)
def get_verses(self, bible, versetext): def get_verses(self, bible, versetext):
@ -275,7 +286,14 @@ class BibleManager(object):
return None return None
reflist = parse_reference(versetext) reflist = parse_reference(versetext)
if reflist: if reflist:
return self.db_cache[bible].get_verses(reflist) log.debug(u'reflist:%s', reflist)
en_reflist = []
for item in reflist:
if item:
book = self.get_book_ref(item[0])
en_reflist.append((book, item[1], item[2], item[3]))
log.debug(u'en_reflist:%s', en_reflist)
return self.db_cache[bible].get_verses(reflist, en_reflist)
else: else:
Receiver.send_message(u'openlp_information_message', { Receiver.send_message(u'openlp_information_message', {
u'title': translate('BiblesPlugin.BibleManager', u'title': translate('BiblesPlugin.BibleManager',
@ -293,6 +311,40 @@ class BibleManager(object):
}) })
return None return None
def get_book_ref(self, book, language_id=None):
log.debug(u'BibleManager.get_book_ref("%s", "%s")', book, language_id)
book_id = self.get_book_ref_id_by_name(book, language_id)
book_temp = BiblesResourcesDB.get_book_by_id(book_id)
log.debug(u'get_book_ref - Return:%s', book_temp[u'name'])
return book_temp[u'name']
def get_book_ref_id_by_name(self, book, language_id=None):
log.debug(u'BibleManager.get_book_ref_id_by_name:("%s", "%s")', book,
language_id)
if BiblesResourcesDB.get_book(book):
book_temp = BiblesResourcesDB.get_book(book)
book_id = book_temp[u'id']
elif BiblesResourcesDB.get_spelling(book, language_id):
book_id = BiblesResourcesDB.get_spelling(book, language_id)
elif self.spelling_cache[u'spelling'].get_book_reference_id(book,
language_id):
book_id = self.spelling_cache[u'spelling'].\
get_book_reference_id(book, language_id)
else:
book_ref = self.parent.mediaItem.importRequest(u'book', book)
log.debug(book_ref)
book_temp = BiblesResourcesDB.get_book(book_ref)
log.debug(book_temp)
book_id = book_temp[u'id']
if book_id:
self.spelling_cache[u'spelling'].create_spelling(book, book_id,
language_id)
if book_id:
log.debug(u'get_book_ref_id_by_name - Return:%s', book_id)
return book_id
else:
return None
def verse_search(self, bible, second_bible, text): def verse_search(self, bible, second_bible, text):
""" """
Does a verse search for the given bible and text. Does a verse search for the given bible and text.

View File

@ -36,6 +36,7 @@ from openlp.core.lib.ui import UiStrings, add_widget_completer, \
from openlp.plugins.bibles.forms import BibleImportForm from openlp.plugins.bibles.forms import BibleImportForm
from openlp.plugins.bibles.lib import LayoutStyle, DisplayStyle, \ from openlp.plugins.bibles.lib import LayoutStyle, DisplayStyle, \
get_reference_match get_reference_match
from openlp.plugins.bibles.forms import BibleImportRequest
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -286,6 +287,11 @@ class BibleMediaItem(MediaManagerItem):
if self.import_wizard.exec_(): if self.import_wizard.exec_():
self.reloadBibles() self.reloadBibles()
def importRequest(self, case, name=None):
self.import_request = BibleImportRequest(self)
if self.import_request.exec_(case, name):
return unicode(self.import_request.RequestComboBox.currentText())
def loadBibles(self): def loadBibles(self):
log.debug(u'Loading Bibles') log.debug(u'Loading Bibles')
self.quickVersionComboBox.clear() self.quickVersionComboBox.clear()

View File

@ -56,6 +56,7 @@ class OpenLP1Bible(BibleDB):
cursor = connection.cursor() cursor = connection.cursor()
except: except:
return False return False
#TODO: include create_meta language
# Create all books. # Create all books.
cursor.execute(u'SELECT id, testament_id, name, abbreviation FROM book') cursor.execute(u'SELECT id, testament_id, name, abbreviation FROM book')
books = cursor.fetchall() books = cursor.fetchall()
@ -68,6 +69,8 @@ class OpenLP1Bible(BibleDB):
testament_id = int(book[1]) testament_id = int(book[1])
name = unicode(book[2], u'cp1252') name = unicode(book[2], u'cp1252')
abbreviation = unicode(book[3], u'cp1252') abbreviation = unicode(book[3], u'cp1252')
#TODO: change create_book to the new database model
#(name, bk_ref_id, testament)
self.create_book(name, abbreviation, testament_id) self.create_book(name, abbreviation, testament_id)
# Update the progess bar. # Update the progess bar.
self.wizard.incrementProgressBar(WizardStrings.ImportingType % name) self.wizard.incrementProgressBar(WizardStrings.ImportingType % name)

View File

@ -61,9 +61,12 @@ class OpenSongBible(BibleDB):
file = open(self.filename, u'r') file = open(self.filename, u'r')
opensong = objectify.parse(file) opensong = objectify.parse(file)
bible = opensong.getroot() bible = opensong.getroot()
#TODO: include create_meta language
for book in bible.b: for book in bible.b:
if self.stop_import_flag: if self.stop_import_flag:
break break
#TODO: change create_book to the new database model
#(name, bk_ref_id, testament)
db_book = self.create_book(unicode(book.attrib[u'n']), db_book = self.create_book(unicode(book.attrib[u'n']),
unicode(book.attrib[u'n'][:4])) unicode(book.attrib[u'n'][:4]))
for chapter in book.c: for chapter in book.c:

View File

@ -104,6 +104,7 @@ class OSISBible(BibleDB):
finally: finally:
if detect_file: if detect_file:
detect_file.close() detect_file.close()
#TODO: include create_meta language with try - except scheme
try: try:
osis = codecs.open(self.filename, u'r', details['encoding']) osis = codecs.open(self.filename, u'r', details['encoding'])
for file_record in osis: for file_record in osis:
@ -120,6 +121,8 @@ class OSISBible(BibleDB):
log.debug(u'New book: "%s"', self.books[book][0]) log.debug(u'New book: "%s"', self.books[book][0])
if book == u'Matt' or book == u'Jdt': if book == u'Matt' or book == u'Jdt':
testament += 1 testament += 1
#TODO: change create_book to the new database model
#(name, bk_ref_id, testament)
db_book = self.create_book( db_book = self.create_book(
unicode(self.books[book][0]), unicode(self.books[book][0]),
unicode(self.books[book][1]), unicode(self.books[book][1]),