def test_decode_bytes_iso_8859_1(self): """ Test SwordBible._decode_bytes when called with a Py2 str / Py3 bytes object encoded with iso-8859-1 """ # GIVEN: A Py2 str / Py3 bytes object which has been encoded from iso-8859-1 bible_instance = SwordBible('module_path') unicode_data = u'Finnish Pyhä Raamattu (1933/1938)' byte_data = unicode_data.encode(u'iso-8859-1') # WHEN: Calling `_decode_bytes` result = bible_instance._decode_bytes(byte_data) # THEN: The result should be equal to the original unicode data. `_encoding` should be set to `iso-8859-1` as # the statment block failed. self.assertEqual(unicode_data, result) self.assertEqual(bible_instance._encoding, u'cp1252')
def test_decode_bytes_utf8(self): """ Test SwordBible._decode_bytes when called with a Py2 str / Py3 bytes object encoded with utf-8 """ # GIVEN: A Py2 str / Py3 bytes object which has been encoded from utf-8 bible_instance = SwordBible('module_path') unicode_data = u'Finnish Pyhä Raamattu (1933/1938)' byte_data = unicode_data.encode('utf-8') # WHEN: Calling `_decode_bytes` result = bible_instance._decode_bytes(byte_data) # THEN: The result should be equal to the original unicode data. `_encoding` should still be None, as it was # successfully decoded in the try statment block self.assertEqual(unicode_data, result) self.assertEqual(bible_instance._encoding, None)
def test_decode_bytes_specified_encoding(self): """ Test SwordBible._decode_bytes when called with a Py2 str / Py3 bytes object encoded by the encoding specified in the initialisation of the SwordBible object """ # GIVEN: A Py2 str / Py3 bytes object which has been encoded from utf-16, and when the encoding has # been specified. bible_instance = SwordBible('module_path', encoding='utf-16') unicode_data = u'Finnish Pyhä Raamattu (1933/1938)' byte_data = unicode_data.encode('utf-16') # WHEN: Calling `_decode_bytes` result = bible_instance._decode_bytes(byte_data) # THEN: The result should be equal to the original unicode data. `_encoding` should be set to encoding specified self.assertEqual(unicode_data, result) self.assertEqual(bible_instance._encoding, 'utf-16')
def test_instantiation_z_text_4(self): """ Test that the instance returned is an instance of the correct class. """ # GIVEN: The SwordBible class # WHEN: Creating an instance with a module_type of z text 4 instance = SwordBible('path', SwordModuleType.ZTEXT4) # THEN: The instance should be an instance of the :class:`pysword.bible.ZTextModule4` self.assertIsInstance(instance, ZTextModule4)
def test_instantiation_raw_text(self): """ Test that the instance returned is an instance of the correct class. """ # GIVEN: The SwordBible class # WHEN: Creating an instance with a module_type of raw text instance = SwordBible('path', SwordModuleType.RAWTEXT) # THEN: The instance should be an instance of the :class:`pysword.bible.RawTextModule` self.assertIsInstance(instance, RawTextModule) self.assertFalse(isinstance(instance, RawTextModule4))
def test_instantiation_default(self): """ Test that the instance returned is an instance of the correct class. """ # GIVEN: The SwordBible class # WHEN: Creating an instance without any module_type specified instance = SwordBible('path') # THEN: The instance should be an instance of the :class:`pysword.bible.RawTextModule` self.assertIsInstance(instance, ZTextModule) self.assertFalse(isinstance(instance, ZTextModule4))
def test_init(self, patched_sword_bible_setup): """ Test that the init loading of bible works """ # GIVEN: A bible path = os.path.join(TEST_RESOURCE_FOLDER, u'modules', u'texts', u'ztext', u'finpr') # WHEN: Loading the bible bible = SwordBible(path) # THEN: It should load self.assertIsNotNone(bible, u'The bible should have loaded')
def test_init_only_one_testament(self, mocked_testament): """ Test that when loading a bible with only one testament, only that testament is available """ # GIVEN: A mocked _get_ztext_files method in SwordBible mocked_testament.side_effect = [True, IOError('Not good')] # WHEN: Creating a SwordBible bible = SwordBible(u'test_path', SwordModuleType.ZTEXT, u'kjv', 'utf-8', u'OSIS') # THEN: Only 'nt' should have been loaded testaments = list(bible._testaments.keys()) self.assertListEqual(testaments, [u'ot'], u'Only "ot" files should be available')
def get_bible_from_module(self, module_key, cipher=None): """ Return a SwordBible object for the key given. :param module_key: The key to use for finding the module. :param cipher: Cipher key to use to decrypt module. Overrules any cipher in the module conf :return: a SwordBible object for the key given. """ # TODO could replace the try excepts with a simple.get bible_module = self._modules[module_key] if self._temp_folder: module_path = os.path.join(self._temp_folder, bible_module[u'datapath']) else: module_path = os.path.join(self._sword_path, bible_module[u'datapath']) module_type = bible_module[u'moddrv'].lower() try: module_versification = bible_module[u'versification'].lower() except KeyError: module_versification = u'kjv' try: module_encoding = bible_module[u'encoding'].lower() except KeyError: module_encoding = None try: source_type = bible_module[u'sourcetype'] except KeyError: source_type = None try: block_type = bible_module[u'blocktype'] except KeyError: block_type = BlockType.BOOK try: compress_type = bible_module[u'compress_type'] except KeyError: compress_type = CompressType.ZIP if cipher: cipherkey = cipher else: try: cipherkey = bible_module[u'cipherkey'] except KeyError: cipherkey = None return SwordBible(module_path, module_type, module_versification, module_encoding, source_type, block_type, compress_type, cipherkey)
from pysword.bible import SwordBible import pickle, scriptures, random, datetime, copy, inspect, os readingspath = os.path.dirname(inspect.getfile(inspect.currentframe())) bible = SwordBible(readingspath + '/kjv', 'ztext', 'kjv', 'utf8', 'OSIS') structure = bible.get_structure() books = structure.get_books() class Reading: """A reading that may be scheduled in a single day""" def __init__(self, book, chap1, verse1, chapN, verseN, topics=set(), kids=False): self.length = passage_length(book, chap1, verse1, chapN, verseN) if book == 'Revelation of John': book = 'Revelation' self.book = book self.chap1 = chap1 self.verse1 = verse1 self.chapN = chapN self.verseN = verseN self.topics = copy.copy(topics) self.kids = kids