Fixes for code and tests.

This commit is contained in:
Tomas Groth 2015-02-26 20:58:54 +00:00
parent ecc30163af
commit 4fb55448f8
4 changed files with 66 additions and 26 deletions

View File

@ -329,8 +329,8 @@ class BibleImportForm(OpenLPWizard):
self.open_song_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:')) self.open_song_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:'))
self.web_source_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Location:')) self.web_source_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Location:'))
self.zefania_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:')) self.zefania_file_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Bible file:'))
self.web_update_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Click to fetch bible list')) self.web_update_label.setText(translate('BiblesPlugin.ImportWizardForm', 'Click to download bible list'))
self.web_update_button.setText(translate('BiblesPlugin.ImportWizardForm', 'Fetch list')) self.web_update_button.setText(translate('BiblesPlugin.ImportWizardForm', 'Download bible list'))
self.web_source_combo_box.setItemText(WebDownload.Crosswalk, translate('BiblesPlugin.ImportWizardForm', self.web_source_combo_box.setItemText(WebDownload.Crosswalk, translate('BiblesPlugin.ImportWizardForm',
'Crosswalk')) 'Crosswalk'))
self.web_source_combo_box.setItemText(WebDownload.BibleGateway, translate('BiblesPlugin.ImportWizardForm', self.web_source_combo_box.setItemText(WebDownload.BibleGateway, translate('BiblesPlugin.ImportWizardForm',
@ -504,6 +504,7 @@ class BibleImportForm(OpenLPWizard):
self.web_bible_list = {} self.web_bible_list = {}
self.web_source_combo_box.setEnabled(False) self.web_source_combo_box.setEnabled(False)
self.web_translation_combo_box.setEnabled(False) self.web_translation_combo_box.setEnabled(False)
self.web_update_button.setEnabled(False)
self.web_progress_bar.setVisible(True) self.web_progress_bar.setVisible(True)
self.web_progress_bar.setValue(0) self.web_progress_bar.setValue(0)
proxy_server = self.field('proxy_server') proxy_server = self.field('proxy_server')
@ -518,13 +519,14 @@ class BibleImportForm(OpenLPWizard):
'An error occurred while downloading the list of bibles from %s.')) 'An error occurred while downloading the list of bibles from %s.'))
self.web_bible_list[download_type] = {} self.web_bible_list[download_type] = {}
for (bible_name, bible_key, language_code) in bibles: for (bible_name, bible_key, language_code) in bibles:
self.web_bible_list[download_type][bible_name] = bible_key self.web_bible_list[download_type][bible_name] = (bible_key, language_code)
self.web_progress_bar.setValue(download_type + 1) self.web_progress_bar.setValue(download_type + 1)
# Update combo box if something got into the list # Update combo box if something got into the list
if self.web_bible_list: if self.web_bible_list:
self.on_web_source_combo_box_index_changed(0) self.on_web_source_combo_box_index_changed(0)
self.web_source_combo_box.setEnabled(True) self.web_source_combo_box.setEnabled(True)
self.web_translation_combo_box.setEnabled(True) self.web_translation_combo_box.setEnabled(True)
self.web_update_button.setEnabled(True)
self.web_progress_bar.setVisible(False) self.web_progress_bar.setVisible(False)
def register_fields(self): def register_fields(self):
@ -611,14 +613,15 @@ class BibleImportForm(OpenLPWizard):
self.progress_bar.setMaximum(1) self.progress_bar.setMaximum(1)
download_location = self.field('web_location') download_location = self.field('web_location')
bible_version = self.web_translation_combo_box.currentText() bible_version = self.web_translation_combo_box.currentText()
bible = self.web_bible_list[download_location][bible_version] (bible, language_id) = self.web_bible_list[download_location][bible_version]
importer = self.manager.import_bible( importer = self.manager.import_bible(
BibleFormat.WebDownload, name=license_version, BibleFormat.WebDownload, name=license_version,
download_source=WebDownload.Names[download_location], download_source=WebDownload.Names[download_location],
download_name=bible, download_name=bible,
proxy_server=self.field('proxy_server'), proxy_server=self.field('proxy_server'),
proxy_username=self.field('proxy_username'), proxy_username=self.field('proxy_username'),
proxy_password=self.field('proxy_password') proxy_password=self.field('proxy_password'),
language_id=language_id
) )
elif bible_type == BibleFormat.Zefania: elif bible_type == BibleFormat.Zefania:
# Import an Zefania bible. # Import an Zefania bible.

View File

@ -791,13 +791,12 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
if not isinstance(name, str): if not isinstance(name, str):
name = str(name) name = str(name)
language = BiblesResourcesDB.run_sql( language = BiblesResourcesDB.run_sql(
'SELECT id, name, code, native_name FROM language WHERE name = ? OR code = ?', (name, name.lower())) 'SELECT id, name, code FROM language WHERE name = ? OR code = ?', (name, name.lower()))
if language: if language:
return { return {
'id': language[0][0], 'id': language[0][0],
'name': str(language[0][1]), 'name': str(language[0][1]),
'code': str(language[0][2]), 'code': str(language[0][2])
'native_name': str(language[0][3])
} }
else: else:
return None return None

View File

@ -254,6 +254,8 @@ class BGExtract(RegistryProperties):
if not soup: if not soup:
return None return None
div = soup.find('div', 'result-text-style-normal') div = soup.find('div', 'result-text-style-normal')
if not div:
return None
self._clean_soup(div) self._clean_soup(div)
span_list = div.find_all('span', 'text') span_list = div.find_all('span', 'text')
log.debug('Span list: %s', span_list) log.debug('Span list: %s', span_list)
@ -322,7 +324,13 @@ class BGExtract(RegistryProperties):
if not soup: if not soup:
return None return None
bible_select = soup.find('select', {'class': 'translation-dropdown'}) bible_select = soup.find('select', {'class': 'translation-dropdown'})
if not bible_select:
log.debug('No select tags found - did site change?')
return None
option_tags = bible_select.find_all('option') option_tags = bible_select.find_all('option')
if not option_tags:
log.debug('No option tags found - did site change?')
return None
current_lang = '' current_lang = ''
bibles = [] bibles = []
for ot in option_tags: for ot in option_tags:
@ -412,15 +420,21 @@ class BSExtract(RegistryProperties):
if not soup: if not soup:
return None return None
bible_links = soup.find_all('a', {'class': 'trlCell'}) bible_links = soup.find_all('a', {'class': 'trlCell'})
if not bible_links:
log.debug('No a tags found - did site change?')
return None
bibles = [] bibles = []
for link in bible_links: for link in bible_links:
bible_name = link.get_text() bible_name = link.get_text()
# Skip any audio # Skip any audio
if 'audio' in bible_name.lower(): if 'audio' in bible_name.lower():
continue continue
bible_link = link['href'] try:
bible_key = bible_link[bible_link.rfind('/') + 1:] bible_link = link['href']
css_classes = link['class'] bible_key = bible_link[bible_link.rfind('/') + 1:]
css_classes = link['class']
except KeyError:
log.debug('No href/class attribute found - did site change?')
language_code = '' language_code = ''
for css_class in css_classes: for css_class in css_classes:
if css_class.startswith('fl_'): if css_class.startswith('fl_'):
@ -512,11 +526,21 @@ class CWExtract(RegistryProperties):
if not soup: if not soup:
return None return None
bible_select = soup.find('select') bible_select = soup.find('select')
if not bible_select:
log.debug('No select tags found - did site change?')
return None
option_tags = bible_select.find_all('option') option_tags = bible_select.find_all('option')
if not option_tags:
log.debug('No option tags found - did site change?')
return None
bibles = [] bibles = []
for ot in option_tags: for ot in option_tags:
tag_text = ot.get_text().strip() tag_text = ot.get_text().strip()
tag_value = ot['value'] try:
tag_value = ot['value']
except KeyError:
log.exception('No value attribute found - did site change?')
return None
if not tag_value: if not tag_value:
continue continue
# The names of non-english bibles has their language in parentheses at the end # The names of non-english bibles has their language in parentheses at the end
@ -524,7 +548,6 @@ class CWExtract(RegistryProperties):
language = tag_text[tag_text.rfind('(') + 1:-1] language = tag_text[tag_text.rfind('(') + 1:-1]
if language in CROSSWALK_LANGUAGES: if language in CROSSWALK_LANGUAGES:
language_code = CROSSWALK_LANGUAGES[language] language_code = CROSSWALK_LANGUAGES[language]
tag_text = tag_text[:tag_text.rfind('(')]
else: else:
language_code = '' language_code = ''
# ... except for the latin vulgate # ... except for the latin vulgate
@ -555,6 +578,7 @@ class HTTPBible(BibleDB, RegistryProperties):
self.proxy_server = None self.proxy_server = None
self.proxy_username = None self.proxy_username = None
self.proxy_password = None self.proxy_password = None
self.language_id = None
if 'path' in kwargs: if 'path' in kwargs:
self.path = kwargs['path'] self.path = kwargs['path']
if 'proxy_server' in kwargs: if 'proxy_server' in kwargs:
@ -563,6 +587,8 @@ class HTTPBible(BibleDB, RegistryProperties):
self.proxy_username = kwargs['proxy_username'] self.proxy_username = kwargs['proxy_username']
if 'proxy_password' in kwargs: if 'proxy_password' in kwargs:
self.proxy_password = kwargs['proxy_password'] self.proxy_password = kwargs['proxy_password']
if 'language_id' in kwargs:
self.language_id = kwargs['language_id']
def do_import(self, bible_name=None): def do_import(self, bible_name=None):
""" """
@ -595,13 +621,12 @@ class HTTPBible(BibleDB, RegistryProperties):
return False return False
self.wizard.progress_bar.setMaximum(len(books) + 2) self.wizard.progress_bar.setMaximum(len(books) + 2)
self.wizard.increment_progress_bar(translate('BiblesPlugin.HTTPBible', 'Registering Language...')) self.wizard.increment_progress_bar(translate('BiblesPlugin.HTTPBible', 'Registering Language...'))
bible = BiblesResourcesDB.get_webbible(self.download_name, self.download_source.lower()) bible = None #BiblesResourcesDB.get_webbible(self.download_name, self.download_source.lower())
if bible['language_id']: if self.language_id:
language_id = bible['language_id'] self.save_meta('language_id', self.language_id)
self.save_meta('language_id', language_id)
else: else:
language_id = self.get_language(bible_name) self.language_id = self.get_language(bible_name)
if not language_id: if not self.language_id:
log.error('Importing books from %s failed' % self.filename) log.error('Importing books from %s failed' % self.filename)
return False return False
for book in books: for book in books:
@ -609,7 +634,7 @@ class HTTPBible(BibleDB, RegistryProperties):
break break
self.wizard.increment_progress_bar(translate( self.wizard.increment_progress_bar(translate(
'BiblesPlugin.HTTPBible', 'Importing %s...', 'Importing <book name>...') % book) 'BiblesPlugin.HTTPBible', 'Importing %s...', 'Importing <book name>...') % book)
book_ref_id = self.get_book_ref_id_by_name(book, len(books), language_id) book_ref_id = self.get_book_ref_id_by_name(book, len(books), self.language_id)
if not book_ref_id: if not book_ref_id:
log.error('Importing books from %s - download name: "%s" failed' % log.error('Importing books from %s - download name: "%s" failed' %
(self.download_source, self.download_name)) (self.download_source, self.download_name))

View File

@ -124,8 +124,13 @@ class TestBibleHTTP(TestCase):
# GIVEN: A new Bible Server extraction class # GIVEN: A new Bible Server extraction class
handler = BSExtract() handler = BSExtract()
handler.get_bibles_from_http() # WHEN: downloading bible list from bibleserver
self.assertTrue(False) bibles = handler.get_bibles_from_http()
# THEN: The list should not be None, and some known bibles should be there
self.assertIsNotNone(bibles)
self.assertIn(('New Int. Readers Version', 'NIRV', 'en'), bibles)
self.assertIn(('Българската Библия', 'BLG', 'bg'), bibles)
def biblegateway_get_bibles_test(self): def biblegateway_get_bibles_test(self):
""" """
@ -134,8 +139,12 @@ class TestBibleHTTP(TestCase):
# GIVEN: A new Bible Gateway extraction class # GIVEN: A new Bible Gateway extraction class
handler = BGExtract() handler = BGExtract()
handler.get_bibles_from_http() # WHEN: downloading bible list from Crosswalk
self.assertTrue(False) bibles = handler.get_bibles_from_http()
# THEN: The list should not be None, and some known bibles should be there
self.assertIsNotNone(bibles)
self.assertIn(('Holman Christian Standard Bible', 'HCSB', 'en'), bibles)
def crosswalk_get_bibles_test(self): def crosswalk_get_bibles_test(self):
""" """
@ -144,5 +153,9 @@ class TestBibleHTTP(TestCase):
# GIVEN: A new Crosswalk extraction class # GIVEN: A new Crosswalk extraction class
handler = CWExtract() handler = CWExtract()
handler.get_bibles_from_http() # WHEN: downloading bible list from Crosswalk
self.assertTrue(False) bibles = handler.get_bibles_from_http()
# THEN: The list should not be None, and some known bibles should be there
self.assertIsNotNone(bibles)
self.assertIn(('Giovanni Diodati 1649 (Italian)', 'gdb', 'it'), bibles)