forked from openlp/openlp
further bible refactors
This commit is contained in:
parent
2416009860
commit
f08d0c28a5
@ -140,10 +140,10 @@ class BiblePlugin(Plugin):
|
|||||||
|
|
||||||
def uses_theme(self, theme):
|
def uses_theme(self, theme):
|
||||||
"""
|
"""
|
||||||
Called to find out if the bible plugin is currently using a theme. Returns ``1`` if the theme is being used,
|
Called to find out if the bible plugin is currently using a theme.
|
||||||
otherwise returns ``0``.
|
|
||||||
|
|
||||||
:param theme: The theme
|
:param theme: The theme
|
||||||
|
:return: 1 if the theme is being used, otherwise returns 0
|
||||||
"""
|
"""
|
||||||
if str(self.settings_tab.bible_theme) == theme:
|
if str(self.settings_tab.bible_theme) == theme:
|
||||||
return 1
|
return 1
|
||||||
@ -151,11 +151,11 @@ class BiblePlugin(Plugin):
|
|||||||
|
|
||||||
def rename_theme(self, old_theme, new_theme):
|
def rename_theme(self, old_theme, new_theme):
|
||||||
"""
|
"""
|
||||||
Rename the theme the bible plugin is using making the plugin use the
|
Rename the theme the bible plugin is using, making the plugin use the new name.
|
||||||
new name.
|
|
||||||
|
|
||||||
:param old_theme: The name of the theme the plugin should stop using. Unused for this particular plugin.
|
:param old_theme: The name of the theme the plugin should stop using. Unused for this particular plugin.
|
||||||
:param new_theme: The new name the plugin should now use.
|
:param new_theme: The new name the plugin should now use.
|
||||||
|
:return: None
|
||||||
"""
|
"""
|
||||||
self.settings_tab.bible_theme = new_theme
|
self.settings_tab.bible_theme = new_theme
|
||||||
self.settings_tab.save()
|
self.settings_tab.save()
|
||||||
|
@ -173,7 +173,7 @@ class BibleStrings(object):
|
|||||||
|
|
||||||
def update_reference_separators():
|
def update_reference_separators():
|
||||||
"""
|
"""
|
||||||
Updates separators and matches for parsing and formating scripture references.
|
Updates separators and matches for parsing and formatting scripture references.
|
||||||
"""
|
"""
|
||||||
default_separators = [
|
default_separators = [
|
||||||
'|'.join([
|
'|'.join([
|
||||||
@ -215,7 +215,7 @@ def update_reference_separators():
|
|||||||
# escape reserved characters
|
# escape reserved characters
|
||||||
for character in '\\.^$*+?{}[]()':
|
for character in '\\.^$*+?{}[]()':
|
||||||
source_string = source_string.replace(character, '\\' + character)
|
source_string = source_string.replace(character, '\\' + character)
|
||||||
# add various unicode alternatives
|
# add various Unicode alternatives
|
||||||
source_string = source_string.replace('-', '(?:[-\u00AD\u2010\u2011\u2012\u2014\u2014\u2212\uFE63\uFF0D])')
|
source_string = source_string.replace('-', '(?:[-\u00AD\u2010\u2011\u2012\u2014\u2014\u2212\uFE63\uFF0D])')
|
||||||
source_string = source_string.replace(',', '(?:[,\u201A])')
|
source_string = source_string.replace(',', '(?:[,\u201A])')
|
||||||
REFERENCE_SEPARATORS['sep_{role}'.format(role=role)] = '\s*(?:{source})\s*'.format(source=source_string)
|
REFERENCE_SEPARATORS['sep_{role}'.format(role=role)] = '\s*(?:{source})\s*'.format(source=source_string)
|
||||||
|
@ -35,7 +35,6 @@ class BibleImport(OpenLPMixin, BibleDB):
|
|||||||
"""
|
"""
|
||||||
Helper class to import bibles from a third party source into OpenLP
|
Helper class to import bibles from a third party source into OpenLP
|
||||||
"""
|
"""
|
||||||
# TODO: Test
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.filename = kwargs['filename'] if 'filename' in kwargs else None
|
self.filename = kwargs['filename'] if 'filename' in kwargs else None
|
||||||
|
@ -73,12 +73,7 @@ class OpenSongBible(BibleImport):
|
|||||||
for book in bible.b:
|
for book in bible.b:
|
||||||
if self.stop_import_flag:
|
if self.stop_import_flag:
|
||||||
break
|
break
|
||||||
book_ref_id = self.get_book_ref_id_by_name(str(book.attrib['n']), len(bible.b), language_id)
|
db_book = self.find_and_create_book(str(book.attrib['n']), len(bible.b), language_id)
|
||||||
if not book_ref_id:
|
|
||||||
log.error('Importing books from "{name}" failed'.format(name=self.filename))
|
|
||||||
return False
|
|
||||||
book_details = BiblesResourcesDB.get_book_by_id(book_ref_id)
|
|
||||||
db_book = self.create_book(book.attrib['n'], book_ref_id, book_details['testament_id'])
|
|
||||||
chapter_number = 0
|
chapter_number = 0
|
||||||
for chapter in book.c:
|
for chapter in book.c:
|
||||||
if self.stop_import_flag:
|
if self.stop_import_flag:
|
||||||
|
@ -98,7 +98,7 @@ class OSISBible(BibleImport):
|
|||||||
language_id = self.get_language_id(language[0] if language else None, bible_name=self.filename)
|
language_id = self.get_language_id(language[0] if language else None, bible_name=self.filename)
|
||||||
if not language_id:
|
if not language_id:
|
||||||
return False
|
return False
|
||||||
num_books = int(osis_bible_tree.xpath("count(//ns:div[@type='book'])", namespaces=NS))
|
no_of_books = int(osis_bible_tree.xpath("count(//ns:div[@type='book'])", namespaces=NS))
|
||||||
# Precompile a few xpath-querys
|
# Precompile a few xpath-querys
|
||||||
verse_in_chapter = etree.XPath('count(//ns:chapter[1]/ns:verse)', namespaces=NS)
|
verse_in_chapter = etree.XPath('count(//ns:chapter[1]/ns:verse)', namespaces=NS)
|
||||||
text_in_verse = etree.XPath('count(//ns:verse[1]/text())', namespaces=NS)
|
text_in_verse = etree.XPath('count(//ns:verse[1]/text())', namespaces=NS)
|
||||||
@ -109,12 +109,7 @@ class OSISBible(BibleImport):
|
|||||||
break
|
break
|
||||||
# Remove div-tags in the book
|
# Remove div-tags in the book
|
||||||
etree.strip_tags(book, '{http://www.bibletechnologies.net/2003/OSIS/namespace}div')
|
etree.strip_tags(book, '{http://www.bibletechnologies.net/2003/OSIS/namespace}div')
|
||||||
book_ref_id = self.get_book_ref_id_by_name(book.get('osisID'), num_books, language_id)
|
db_book = self.find_and_create_book(book.get('osisID'), no_of_books, language_id)
|
||||||
if not book_ref_id:
|
|
||||||
log.error('Importing books from "{name}" failed'.format(name=self.filename))
|
|
||||||
return False
|
|
||||||
book_details = BiblesResourcesDB.get_book_by_id(book_ref_id)
|
|
||||||
db_book = self.create_book(book_details['name'], book_ref_id, book_details['testament_id'])
|
|
||||||
# Find out if chapter-tags contains the verses, or if it is used as milestone/anchor
|
# Find out if chapter-tags contains the verses, or if it is used as milestone/anchor
|
||||||
if int(verse_in_chapter(book)) > 0:
|
if int(verse_in_chapter(book)) > 0:
|
||||||
# The chapter tags contains the verses
|
# The chapter tags contains the verses
|
||||||
|
@ -54,7 +54,7 @@ class ZefaniaBible(BibleImport):
|
|||||||
language_id = self.get_language_id(language[0] if language else None, bible_name=self.filename)
|
language_id = self.get_language_id(language[0] if language else None, bible_name=self.filename)
|
||||||
if not language_id:
|
if not language_id:
|
||||||
return False
|
return False
|
||||||
num_books = int(xmlbible.xpath('count(//BIBLEBOOK)'))
|
no_of_books = int(xmlbible.xpath('count(//BIBLEBOOK)'))
|
||||||
self.wizard.progress_bar.setMaximum(int(xmlbible.xpath('count(//CHAPTER)')))
|
self.wizard.progress_bar.setMaximum(int(xmlbible.xpath('count(//CHAPTER)')))
|
||||||
for BIBLEBOOK in xmlbible:
|
for BIBLEBOOK in xmlbible:
|
||||||
if self.stop_import_flag:
|
if self.stop_import_flag:
|
||||||
@ -64,7 +64,7 @@ class ZefaniaBible(BibleImport):
|
|||||||
if not bname and not bnumber:
|
if not bname and not bnumber:
|
||||||
continue
|
continue
|
||||||
if bname:
|
if bname:
|
||||||
book_ref_id = self.get_book_ref_id_by_name(bname, num_books, language_id)
|
book_ref_id = self.get_book_ref_id_by_name(bname, no_of_books, language_id)
|
||||||
else:
|
else:
|
||||||
log.debug('Could not find a name, will use number, basically a guess.')
|
log.debug('Could not find a name, will use number, basically a guess.')
|
||||||
book_ref_id = int(bnumber)
|
book_ref_id = int(bnumber)
|
||||||
|
@ -29,8 +29,10 @@ from lxml import etree, objectify
|
|||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from openlp.core.common.languages import Language
|
from openlp.core.common.languages import Language
|
||||||
|
from openlp.core.lib.exceptions import ValidationError
|
||||||
from openlp.plugins.bibles.lib.bibleimport import BibleImport
|
from openlp.plugins.bibles.lib.bibleimport import BibleImport
|
||||||
from tests.functional import MagicMock, patch
|
from openlp.plugins.bibles.lib.db import BibleDB
|
||||||
|
from tests.functional import ANY, MagicMock, patch
|
||||||
|
|
||||||
|
|
||||||
class TestBibleImport(TestCase):
|
class TestBibleImport(TestCase):
|
||||||
@ -39,23 +41,79 @@ class TestBibleImport(TestCase):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
test_file = BytesIO(b'<?xml version="1.0" encoding="UTF-8" ?>\n'
|
test_file = BytesIO(
|
||||||
|
b'<?xml version="1.0" encoding="UTF-8" ?>\n'
|
||||||
b'<root>\n'
|
b'<root>\n'
|
||||||
b' <data><div>Test<p>data</p><a>to</a>keep</div></data>\n'
|
b' <data><div>Test<p>data</p><a>to</a>keep</div></data>\n'
|
||||||
b' <data><unsupported>Test<x>data</x><y>to</y>discard</unsupported></data>\n'
|
b' <data><unsupported>Test<x>data</x><y>to</y>discard</unsupported></data>\n'
|
||||||
b'</root>')
|
b'</root>'
|
||||||
|
)
|
||||||
self.file_patcher = patch('builtins.open', return_value=test_file)
|
self.file_patcher = patch('builtins.open', return_value=test_file)
|
||||||
self.log_patcher = patch('openlp.plugins.bibles.lib.bibleimport.log')
|
|
||||||
self.setup_patcher = patch('openlp.plugins.bibles.lib.db.BibleDB._setup')
|
|
||||||
|
|
||||||
self.addCleanup(self.file_patcher.stop)
|
self.addCleanup(self.file_patcher.stop)
|
||||||
self.addCleanup(self.log_patcher.stop)
|
|
||||||
self.addCleanup(self.setup_patcher.stop)
|
|
||||||
|
|
||||||
self.file_patcher.start()
|
self.file_patcher.start()
|
||||||
|
self.log_patcher = patch('openlp.plugins.bibles.lib.bibleimport.log')
|
||||||
|
self.addCleanup(self.log_patcher.stop)
|
||||||
self.mock_log = self.log_patcher.start()
|
self.mock_log = self.log_patcher.start()
|
||||||
|
self.setup_patcher = patch('openlp.plugins.bibles.lib.db.BibleDB._setup')
|
||||||
|
self.addCleanup(self.setup_patcher.stop)
|
||||||
self.setup_patcher.start()
|
self.setup_patcher.start()
|
||||||
|
|
||||||
|
def init_kwargs_none_test(self):
|
||||||
|
"""
|
||||||
|
Test the initialisation of the BibleImport Class when no key word arguments are supplied
|
||||||
|
"""
|
||||||
|
# GIVEN: A patched BibleDB._setup, BibleImport class and mocked parent
|
||||||
|
# WHEN: Creating an instance of BibleImport with no key word arguments
|
||||||
|
instance = BibleImport(MagicMock())
|
||||||
|
|
||||||
|
# THEN: The filename attribute should be None
|
||||||
|
self.assertIsNone(instance.filename)
|
||||||
|
self.assertIsInstance(instance, BibleDB)
|
||||||
|
|
||||||
|
def init_kwargs_set_test(self):
|
||||||
|
"""
|
||||||
|
Test the initialisation of the BibleImport Class when supplied with select keyword arguments
|
||||||
|
"""
|
||||||
|
# GIVEN: A patched BibleDB._setup, BibleImport class and mocked parent
|
||||||
|
# WHEN: Creating an instance of BibleImport with selected key word arguments
|
||||||
|
kwargs = {'filename': 'bible.xml'}
|
||||||
|
instance = BibleImport(MagicMock(), **kwargs)
|
||||||
|
|
||||||
|
# THEN: The filename keyword should be set to bible.xml
|
||||||
|
self.assertEqual(instance.filename, 'bible.xml')
|
||||||
|
self.assertIsInstance(instance, BibleDB)
|
||||||
|
|
||||||
|
def check_for_compression_test(self):
|
||||||
|
"""
|
||||||
|
Test the check_for_compression method when called with a path to an uncompressed file
|
||||||
|
"""
|
||||||
|
# GIVEN: A mocked is_zipfile which returns False and an instance of BibleImport
|
||||||
|
with patch('openlp.plugins.bibles.lib.bibleimport.is_zipfile', return_value=False) as mocked_is_zip:
|
||||||
|
instance = BibleImport(MagicMock())
|
||||||
|
|
||||||
|
# WHEN: Calling check_for_compression
|
||||||
|
result = instance.check_for_compression('filename.tst')
|
||||||
|
|
||||||
|
# THEN: None should be returned
|
||||||
|
self.assertIsNone(result)
|
||||||
|
mocked_is_zip.assert_called_once_with('filename.tst')
|
||||||
|
|
||||||
|
def check_for_compression_zip_file_test(self):
|
||||||
|
"""
|
||||||
|
Test the check_for_compression method when called with a path to a compressed file
|
||||||
|
"""
|
||||||
|
# GIVEN: A patched is_zipfile which returns True and an instance of BibleImport
|
||||||
|
with patch('openlp.plugins.bibles.lib.bibleimport.is_zipfile', return_value=True),\
|
||||||
|
patch('openlp.plugins.bibles.lib.bibleimport.critical_error_message_box') as mocked_message_box:
|
||||||
|
instance = BibleImport(MagicMock())
|
||||||
|
|
||||||
|
# WHEN: Calling check_for_compression
|
||||||
|
# THEN: A Validation error should be raised and the user should be notified.
|
||||||
|
with self.assertRaises(ValidationError) as context:
|
||||||
|
instance.check_for_compression('filename.tst')
|
||||||
|
self.assertTrue(mocked_message_box.called)
|
||||||
|
self.assertEqual(context.exception.msg, '"filename.tst" is compressed')
|
||||||
|
|
||||||
def get_language_id_language_found_test(self):
|
def get_language_id_language_found_test(self):
|
||||||
"""
|
"""
|
||||||
Test get_language_id() when called with a name found in the languages list
|
Test get_language_id() when called with a name found in the languages list
|
||||||
@ -81,8 +139,7 @@ class TestBibleImport(TestCase):
|
|||||||
Test get_language_id() when called with a name not found in the languages list
|
Test get_language_id() when called with a name not found in the languages list
|
||||||
"""
|
"""
|
||||||
# GIVEN: A mocked languages.get_language which returns language and an instance of BibleImport
|
# GIVEN: A mocked languages.get_language which returns language and an instance of BibleImport
|
||||||
with patch('openlp.core.common.languages.get_language', return_value=None) \
|
with patch('openlp.core.common.languages.get_language', return_value=None) as mocked_languages_get_language, \
|
||||||
as mocked_languages_get_language, \
|
|
||||||
patch('openlp.plugins.bibles.lib.db.BibleDB.get_language', return_value=20) as mocked_db_get_language:
|
patch('openlp.plugins.bibles.lib.db.BibleDB.get_language', return_value=20) as mocked_db_get_language:
|
||||||
instance = BibleImport(MagicMock())
|
instance = BibleImport(MagicMock())
|
||||||
instance.save_meta = MagicMock()
|
instance.save_meta = MagicMock()
|
||||||
|
@ -46,10 +46,10 @@ class TestCSVImport(TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||||
self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry')
|
|
||||||
self.addCleanup(self.manager_patcher.stop)
|
self.addCleanup(self.manager_patcher.stop)
|
||||||
self.addCleanup(self.registry_patcher.stop)
|
|
||||||
self.manager_patcher.start()
|
self.manager_patcher.start()
|
||||||
|
self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry')
|
||||||
|
self.addCleanup(self.registry_patcher.stop)
|
||||||
self.registry_patcher.start()
|
self.registry_patcher.start()
|
||||||
|
|
||||||
def test_create_importer(self):
|
def test_create_importer(self):
|
||||||
|
@ -42,14 +42,12 @@ class TestZefaniaImport(TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry')
|
self.registry_patcher = patch('openlp.plugins.bibles.lib.db.Registry')
|
||||||
|
self.addCleanup(self.registry_patcher.stop)
|
||||||
self.registry_patcher.start()
|
self.registry_patcher.start()
|
||||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||||
|
self.addCleanup(self.manager_patcher.stop)
|
||||||
self.manager_patcher.start()
|
self.manager_patcher.start()
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.registry_patcher.stop()
|
|
||||||
self.manager_patcher.stop()
|
|
||||||
|
|
||||||
def test_create_importer(self):
|
def test_create_importer(self):
|
||||||
"""
|
"""
|
||||||
Test creating an instance of the Zefania file importer
|
Test creating an instance of the Zefania file importer
|
||||||
|
Loading…
Reference in New Issue
Block a user