Added language detection when importing

Fixes: https://launchpad.net/bugs/1214875
This commit is contained in:
Tomas Groth 2014-08-27 15:10:33 +02:00
parent 71a1b26e1c
commit 7fecaa1d70
4 changed files with 20 additions and 6 deletions

View File

@ -88,6 +88,7 @@ class OpenSongBible(BibleDB):
'Incorrect Bible file type supplied. This looks like a Zefania XML bible, '
'please use the Zefania import option.'))
return False
# No language info in the opensong format, so ask the user
language_id = self.get_language(bible_name)
if not language_id:
log.error('Importing books from "%s" failed' % self.filename)

View File

@ -65,12 +65,19 @@ class OSISBible(BibleDB):
# NOTE: We don't need to do any of the normal encoding detection here, because lxml does it's own encoding
# detection, and the two mechanisms together interfere with each other.
import_file = open(self.filename, 'rb')
language_id = self.get_language(bible_name)
osis_bible_tree = etree.parse(import_file)
namespace = {'ns': 'http://www.bibletechnologies.net/2003/OSIS/namespace'}
# Find bible language
language_id = None
language = osis_bible_tree.xpath("//ns:osisText/@xml:lang", namespaces=namespace)
if language:
language_id = BiblesResourcesDB.get_language(language[0])
# The language couldn't be detected, ask the user
if not language_id:
language_id = self.get_language(bible_name)
if not language_id:
log.error('Importing books from "%s" failed' % self.filename)
return False
osis_bible_tree = etree.parse(import_file)
namespace = {'ns': 'http://www.bibletechnologies.net/2003/OSIS/namespace'}
num_books = int(osis_bible_tree.xpath("count(//ns:div[@type='book'])", namespaces=namespace))
self.wizard.increment_progress_bar(translate('BiblesPlugin.OsisImport',
'Removing unused tags (this may take a few minutes)...'))

View File

@ -64,11 +64,18 @@ class ZefaniaBible(BibleDB):
# NOTE: We don't need to do any of the normal encoding detection here, because lxml does it's own encoding
# detection, and the two mechanisms together interfere with each other.
import_file = open(self.filename, 'rb')
language_id = self.get_language(bible_name)
zefania_bible_tree = etree.parse(import_file)
# Find bible language
language_id = None
language = zefania_bible_tree.xpath("/XMLBIBLE/INFORMATION/language/text()")
if language:
language_id = BiblesResourcesDB.get_language(language[0])
# The language couldn't be detected, ask the user
if not language_id:
language_id = self.get_language(bible_name)
if not language_id:
log.error('Importing books from "%s" failed' % self.filename)
return False
zefania_bible_tree = etree.parse(import_file)
num_books = int(zefania_bible_tree.xpath("count(//BIBLEBOOK)"))
# Strip tags we don't use - keep content
etree.strip_tags(zefania_bible_tree, ('STYLE', 'GRAM', 'NOTE', 'SUP', 'XREF'))

View File

@ -157,6 +157,5 @@ class TestOsisImport(TestCase):
# THEN: The create_verse() method should have been called with each verse in the file.
self.assertTrue(importer.create_verse.called)
print(importer.create_verse.call_list())
for verse_tag, verse_text in test_data['verses']:
importer.create_verse.assert_any_call(importer.create_book().id, '1', verse_tag, verse_text)