diff --git a/openlp/plugins/bibles/lib/__init__.py b/openlp/plugins/bibles/lib/__init__.py index 73e5ebe8b..97e4ce72e 100644 --- a/openlp/plugins/bibles/lib/__init__.py +++ b/openlp/plugins/bibles/lib/__init__.py @@ -355,37 +355,8 @@ def parse_reference(reference, bible, language_selection, book_ref_id=False): log.debug(u'Matched reference %s' % reference) book = match.group(u'book') if not book_ref_id: - book_names = BibleStrings().BookNames - # escape reserved characters - book_escaped = book - for character in u'\\.^$*+?{}[]()': - book_escaped = book_escaped.replace( - character, u'\\' + character) - regex_book = re.compile(u'\s*%s\s*' % u'\s*'.join( - book_escaped.split()), re.UNICODE | re.IGNORECASE) - if language_selection == LanguageSelection.Bible: - db_book = bible.get_book(book) - if db_book: - book_ref_id = db_book.book_reference_id - elif language_selection == LanguageSelection.Application: - books = filter(lambda key: - regex_book.match(unicode(book_names[key])), book_names.keys()) - books = filter(None, map(BiblesResourcesDB.get_book, books)) - for value in books: - if bible.get_book_by_book_ref_id(value[u'id']): - book_ref_id = value[u'id'] - break - elif language_selection == LanguageSelection.English: - books = BiblesResourcesDB.get_books_like(book) - if books: - book_list = filter( - lambda value: regex_book.match(value[u'name']), books) - if not book_list: - book_list = books - for value in book_list: - if bible.get_book_by_book_ref_id(value[u'id']): - book_ref_id = value[u'id'] - break + book_ref_id = bible.get_book_ref_id_by_localised_name( + book, language_selection) elif not bible.get_book_by_book_ref_id(book_ref_id): book_ref_id = False ranges = match.group(u'ranges') diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index acf7cf461..86f209dc7 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -29,6 +29,7 @@ import chardet import logging import os +import re import sqlite3 from PyQt4 import QtCore @@ -352,6 +353,53 @@ class BibleDB(QtCore.QObject, Manager): book, book_id, language_id) return book_id + def get_book_ref_id_by_localised_name(self, book, + language_selection): + """ + Return the id of a named book. + + ``book`` + The name of the book, according to the selected language. + + ``language_selection`` + The language selection the user has chosen in the settings + section of the Bible. + """ + log.debug(u'get_book_ref_id_by_localised_name("%s", "%s")', + book, language_selection) + from openlp.plugins.bibles.lib import LanguageSelection, \ + BibleStrings + book_names = BibleStrings().BookNames + # escape reserved characters + book_escaped = book + for character in u'\\.^$*+?{}[]()': + book_escaped = book_escaped.replace( + character, u'\\' + character) + regex_book = re.compile(u'\s*%s\s*' % u'\s*'.join( + book_escaped.split()), re.UNICODE | re.IGNORECASE) + if language_selection == LanguageSelection.Bible: + db_book = self.get_book(book) + if db_book: + return db_book.book_reference_id + elif language_selection == LanguageSelection.Application: + books = filter(lambda key: + regex_book.match(unicode(book_names[key])), book_names.keys()) + books = filter(None, map(BiblesResourcesDB.get_book, books)) + for value in books: + if self.get_book_by_book_ref_id(value[u'id']): + return value[u'id'] + elif language_selection == LanguageSelection.English: + books = BiblesResourcesDB.get_books_like(book) + if books: + book_list = filter( + lambda value: regex_book.match(value[u'name']), books) + if not book_list: + book_list = books + for value in book_list: + if self.get_book_by_book_ref_id(value[u'id']): + return value[u'id'] + return False + def get_verses(self, reference_list, show_error=True): """ This is probably the most used function. It retrieves the list of diff --git a/openlp/plugins/bibles/lib/manager.py b/openlp/plugins/bibles/lib/manager.py index 66e0d93bc..4ca5e8a8c 100644 --- a/openlp/plugins/bibles/lib/manager.py +++ b/openlp/plugins/bibles/lib/manager.py @@ -277,8 +277,10 @@ class BibleManager(object): """ log.debug(u'BibleManager.get_verse_count("%s", "%s", %s)', bible, book, chapter) - db_book = self.db_cache[bible].get_book(book) - book_ref_id = db_book.book_reference_id + language_selection = self.get_language_selection(bible) + book_ref_id = self.db_cache[bible]. \ + get_book_ref_id_by_localised_name( + book, language_selection) return self.db_cache[bible].get_verse_count(book_ref_id, chapter) def get_verse_count_by_book_ref_id(self, bible, book_ref_id, chapter):