adapt regex and some variable names

This commit is contained in:
Armin Köhler 2012-03-07 20:15:38 +01:00
parent 5391301b97
commit 905fc6d91f
1 changed files with 23 additions and 18 deletions

View File

@ -361,40 +361,45 @@ def parse_reference(reference, bible, language_selection, book_ref_id=False):
book = match.group(u'book') book = match.group(u'book')
if not book_ref_id: if not book_ref_id:
booknames = BibleStrings().Booknames booknames = BibleStrings().Booknames
regex_book = re.compile(u'^[1-4]?[\. ]{0,2}%s' % book.lower(), # escape reserved characters
re.UNICODE) 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: if language_selection == LanguageSelection.Bible:
db_book = bible.get_book(book) db_book = bible.get_book(book)
if db_book: if db_book:
book_ref_id = db_book.book_reference_id book_ref_id = db_book.book_reference_id
elif language_selection == LanguageSelection.Application: elif language_selection == LanguageSelection.Application:
book_list = [] book_list = []
for k, v in booknames.iteritems(): for key, value in booknames.iteritems():
if regex_book.search(unicode(v).lower()): if regex_book.match(unicode(value)):
book_list.append(k) book_list.append(key)
books = [] books = []
if book_list: if book_list:
for v in book_list: for value in book_list:
value = BiblesResourcesDB.get_book(v) item = BiblesResourcesDB.get_book(value)
if value: if item:
books.append(value) books.append(item)
if books: if books:
for v in books: for value in books:
if bible.get_book_by_book_ref_id(v[u'id']): if bible.get_book_by_book_ref_id(value[u'id']):
book_ref_id = v[u'id'] book_ref_id = value[u'id']
break break
elif language_selection == LanguageSelection.English: elif language_selection == LanguageSelection.English:
books = BiblesResourcesDB.get_books_like(book) books = BiblesResourcesDB.get_books_like(book)
if books: if books:
book_list = [] book_list = []
for v in books: for value in books:
if regex_book.search(v[u'name'].lower()): if regex_book.match(value[u'name']):
book_list.append(v) book_list.append(value)
if not book_list: if not book_list:
book_list = books book_list = books
for v in book_list: for value in book_list:
if bible.get_book_by_book_ref_id(v[u'id']): if bible.get_book_by_book_ref_id(value[u'id']):
book_ref_id = v[u'id'] book_ref_id = value[u'id']
break break
else: else:
if not bible.get_book_by_book_ref_id(book_ref_id): if not bible.get_book_by_book_ref_id(book_ref_id):