Exemplo n.º 1
0
    def _setup(self, module_path, versification, source_type, cipherkey):
        """
        Preform setup separate from __init__ to allow easier patching when testing

        :raise IOError: If files cannot be opened.
        :raise ValueError: If unknown module_type is supplied.
        """
        try:
            self._module_path = path_like_to_str(module_path)
        except TypeError:
            raise TypeError(u'`module_path` should be a str, PathLike object or an instance of pathlib.Path')

        self._testaments = {}
        self._load_testament('ot')
        self._load_testament('nt')
        if not self._testaments:
            raise IOError(u'Could not open OT or NT for module')

        # Create cleaner to remove OSIS or GBF tags
        if source_type:
            if source_type.upper() == u'THML':
                self._cleaner = ThMLCleaner()
            elif source_type.upper() == u'GBF':
                self._cleaner = GBFCleaner()
            else:
                self._cleaner = OSISCleaner()
        else:
            self._cleaner = OSISCleaner()
        self._structure = BibleStructure(versification, self._testaments)

        # Initialise decryption if needed
        if cipherkey:
            self._sappire_decryptor = Sapphire(cipherkey)
        else:
            self._sappire_decryptor = None
Exemplo n.º 2
0
    def __init__(self, path=None, encoding=None):
        """
        Initialize the SwordModules object.

        :param path: Path the SWORD datapath or to a zip-file containing a module. Defaults to the platforms expected
                     SWORD datapath.
        :param encoding: The encoding to use when reading the text.
        """
        self._encoding = encoding
        if path is None:
            # Based on the platform find the SWORD data path
            if sys.platform.startswith(u'win32'):
                self._sword_path = os.path.join(os.getenv(u'APPDATA'),
                                                u'Sword')
            elif sys.platform.startswith(u'darwin'):
                self._sword_path = os.path.join(os.getenv(u'HOME'), u'Library',
                                                u'Application Support',
                                                u'Sword')
            else:  # Linux etc.
                self._sword_path = os.path.join(os.getenv(u'HOME'), u'.sword')
        else:
            try:
                self._sword_path = path_like_to_str(path)
            except TypeError:
                raise TypeError(
                    u'`path` should be a str, PathLike object or an instance of pathlib.Path'
                )

        self._modules = {}
        self._temp_folder = None
Exemplo n.º 3
0
    def test_unicode_object(self):
        # GIVEN: A `unicode` object
        unicode_object = os.path.join(u'test', u'path')

        # WHEN: Calling with path_like_to_str with an `unicode` object
        result = path_like_to_str(unicode_object)

        # THEN: The returned valus should be a unicode and equal to the object passed in
        self.assertIsInstance(result, unicode)
        self.assertEqual(result, unicode_object)
Exemplo n.º 4
0
    def test_str_object(self):
        # GIVEN: A `str` object
        str_object = os.path.join(u'test', u'path')

        # WHEN: Calling with path_like_to_str with an `pathlib.Path` object
        result = path_like_to_str(str_object)

        # THEN: The returned valus should be a str and equal to the object passed in
        self.assertIsInstance(result, str)
        self.assertEqual(result, str_object)
Exemplo n.º 5
0
    def test_path_object(self):
        # GIVEN: A `pathlib.Path` object
        path_object = pathlib.Path(u'test', u'path')

        # WHEN: Calling with path_like_to_str with an `pathlib.Path` object
        result = path_like_to_str(path_object)

        # Then a string of the Path should have been returned
        self.assertIsInstance(result, str)
        self.assertEqual(result, os.path.join(u'test', u'path'))
Exemplo n.º 6
0
    def test_path_like_object(self):
        # GIVEN: An object that implements the `os.PathLike` `__fspath__` method
        path_like_object = pathlib.Path(u'test', u'path')

        # WHEN: Calling with path_like_to_str with an `os.PathLike` object
        result = path_like_to_str(path_like_object)

        # Then a string of the Path should have been returned
        self.assertIsInstance(result, str)
        self.assertEqual(result, os.path.join(u'test', u'path'))