diff --git a/openlp/plugins/bibles/forms/__init__.py b/openlp/plugins/bibles/forms/__init__.py index 15f14de9e..d0966c7ce 100644 --- a/openlp/plugins/bibles/forms/__init__.py +++ b/openlp/plugins/bibles/forms/__init__.py @@ -52,5 +52,6 @@ from the .ui files later if necessary. """ from bibleimportform import BibleImportForm +from bibleimportrequestform import BibleImportRequest __all__ = ['BibleImportForm'] diff --git a/openlp/plugins/bibles/forms/bibleimportrequestdialog.py b/openlp/plugins/bibles/forms/bibleimportrequestdialog.py new file mode 100644 index 000000000..d58c1df9f --- /dev/null +++ b/openlp/plugins/bibles/forms/bibleimportrequestdialog.py @@ -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:")) diff --git a/openlp/plugins/bibles/forms/bibleimportrequestform.py b/openlp/plugins/bibles/forms/bibleimportrequestform.py new file mode 100644 index 000000000..caac8e83b --- /dev/null +++ b/openlp/plugins/bibles/forms/bibleimportrequestform.py @@ -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) diff --git a/openlp/plugins/bibles/lib/csvbible.py b/openlp/plugins/bibles/lib/csvbible.py index b96382df2..b5644db81 100644 --- a/openlp/plugins/bibles/lib/csvbible.py +++ b/openlp/plugins/bibles/lib/csvbible.py @@ -135,6 +135,7 @@ class CSVBible(BibleDB): self.wizard.progressBar.setMinimum(0) self.wizard.progressBar.setMaximum(66) success = True + #TODO: include create_meta language books_file = None book_list = {} # Populate the Tables @@ -148,6 +149,8 @@ class CSVBible(BibleDB): self.wizard.incrementProgressBar(unicode( translate('BibleDB.Wizard', 'Importing books... %s')) % 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']), unicode(line[3], details['encoding']), int(line[1])) book_list[int(line[0])] = unicode(line[2], details['encoding']) diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index 3eb7c3af1..68edc55ef 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -95,9 +95,9 @@ def init_schema(url): ) book_table = Table(u'book', metadata, Column(u'id', types.Integer, primary_key=True), - Column(u'testament_id', types.Integer, ForeignKey(u'testament.id')), + Column(u'book_reference_id', types.Integer), + Column(u'testament_reference_id', types.Integer), Column(u'name', types.Unicode(50), index=True), - Column(u'abbreviation', types.Unicode(5), index=True), ) verse_table = Table(u'verse', metadata, Column(u'id', types.Integer, primary_key=True, index=True), @@ -114,8 +114,7 @@ def init_schema(url): try: class_mapper(Testament) except UnmappedClassError: - mapper(Testament, testament_table, - properties={'books': relation(Book, backref='testament')}) + mapper(Testament, testament_table) try: class_mapper(Book) 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'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. ``name`` The name of the book. - ``abbrev`` - The abbreviation of the book. + ``bk_ref_id`` + The book_reference_id from bibles_resources.sqlite of the book. ``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) - book = Book.populate(name=name, abbreviation=abbrev, - testament_id=testament) + log.debug(u'create_book %s,%s', name, bk_ref_id) + book = Book.populate(name=name, book_reference_id=bk_ref_id, + testament_reference_id=testament) self.save_object(book) return book @@ -334,6 +334,8 @@ class BibleDB(QtCore.QObject, Manager): ``value`` The value for this instance. """ + if not isinstance(value, unicode): + value = unicode(value) log.debug(u'save_meta %s/%s', key, 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) 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 def get_books(self): @@ -358,7 +357,7 @@ class BibleDB(QtCore.QObject, Manager): """ 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 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)] """ - log.debug(u'BibleDB.get_verses: %s', reference_list) + log.debug(u'BibleDB.get_verses: %s - %s', reference_list, + en_reference_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) if db_book: book = db_book.name log.debug(u'Book name corrected to "%s"', book) 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)\ .filter_by(book_id=db_book.id)\ .filter_by(chapter=chapter)\ @@ -545,9 +547,9 @@ class BiblesResourcesDB(QtCore.QObject, Manager): ``name`` The name or abbreviation of the book. """ + log.debug(u'get_book: %s', name) if not isinstance(name, unicode): name = unicode(name) - name = name.title() books = BiblesResourcesDB.run_sql(u'SELECT id, testament_id, name, ' u'abbreviation, chapters FROM book_reference WHERE name = ? OR ' u'abbreviation = ?', (name, name)) @@ -562,6 +564,30 @@ class BiblesResourcesDB(QtCore.QObject, Manager): else: 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 def get_chapter(name, chapter): """ @@ -644,8 +670,8 @@ class BiblesResourcesDB(QtCore.QObject, Manager): """ Return the bibles a webbible provide for download. - ``name`` - The name of the webbible. + ``source`` + The source of the webbible. """ if not isinstance(source, unicode): source = unicode(source) @@ -668,7 +694,39 @@ class BiblesResourcesDB(QtCore.QObject, Manager): return None @staticmethod - def get_spelling(name, language_id=None): + 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 + def get_spelling(name, language_id=None): """ Return a book_reference_id if the name matches. """ @@ -678,7 +736,7 @@ class BiblesResourcesDB(QtCore.QObject, Manager): (name, language_id)) else: id = BiblesResourcesDB.run_sql(u'SELECT book_reference_id ' - u'FROM spelling WHERE name = ? ORDER BY id', (name, )) + u'FROM spelling WHERE name = ? ORDER BY id', (name, )) if id: return int(id[0][0]) else: @@ -707,6 +765,25 @@ class BiblesResourcesDB(QtCore.QObject, Manager): else: 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 def get_testament_reference(): """ @@ -766,7 +843,7 @@ class SpellingDB(QtCore.QObject, Manager): 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`` The name to search the id. @@ -785,7 +862,7 @@ class SpellingDB(QtCore.QObject, Manager): if not id: return None else: - return id + return id[0] def create_spelling(self, name, book_reference_id, language_id): """ diff --git a/openlp/plugins/bibles/lib/http.py b/openlp/plugins/bibles/lib/http.py index d2202909c..29c86e0bd 100644 --- a/openlp/plugins/bibles/lib/http.py +++ b/openlp/plugins/bibles/lib/http.py @@ -41,7 +41,8 @@ from openlp.core.lib import Receiver, translate from openlp.core.lib.ui import critical_error_message_box from openlp.core.utils import AppLocation, get_web_page 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__) @@ -123,6 +124,52 @@ class BGExtract(object): return None 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'