def setup_method(self, method): self.temp_folder = utils.make_tmpdir() # Create a simple package pkg_location = utils.create_working_package(self.temp_folder) mod_location = os.path.join(pkg_location, 'app.py') utils.write_file(mod_location, SIMPLE_CLASS) self.finder = PikeFinder([self.temp_folder]) self.loader = PikeLoader('pike_tests.app', mod_location)
def test_loading_py(compiled_loader): finder = PikeFinder([compiled_loader]) # Loading module source loader = finder.find_module('compile_test') module = loader.load_module('compile_test') if six.PY3: assert type(module.__loader__).__name__ == 'SourceFileLoader' else: assert module.__file__.endswith('__init__.py')
def test_loading_pyc(compiled_loader): finder = PikeFinder([compiled_loader]) # Loading compiled module loader = finder.find_module('compile_test.app') module = loader.load_module('compile_test.app') if six.PY3: assert type(module.__loader__).__name__ == 'SourcelessFileLoader' assert module.__cached__.endswith('.pyc') else: assert module.__file__.endswith('app.pyc')
def __init__(self, search_paths=None): """The Pike plugin manager The manager allows for the dynamic loading of Python packages for any location on a user's filesystem. :param list search_paths: List of path strings to include during module importing. These paths are only in addition to existing Python import search paths. Using PikeManager as a context manager: .. code-block:: python from pike.manager import PikeManager with PikeManager(['/path/containing/package']) as mgr: import module_in_the_package Using PikeManager instance: .. code-block:: python from pike.manager import PikeManager mgr = PikeManager(['/path/container/package']) import module_in_the_package mgr.cleanup() """ self.search_paths = search_paths or [] self.module_finder = PikeFinder(search_paths) self.add_to_meta_path()
def pike_finder(): """Fixture: Return PikeFinder object :return: PikeFinder object """ finder_path = local(__file__).dirpath().dirpath() return PikeFinder([str(finder_path)])
class TestLoader(object): def setup_method(self, method): self.temp_folder = utils.make_tmpdir() # Create a simple package pkg_location = utils.create_working_package(self.temp_folder) mod_location = os.path.join(pkg_location, 'app.py') utils.write_file(mod_location, SIMPLE_CLASS) self.finder = PikeFinder([self.temp_folder]) self.loader = PikeLoader('pike_tests.app', mod_location) def teardown_method(self, method): utils.remove_dir(self.temp_folder) def test_load_module_raises_import_error_with_bad_fullname(self): with pytest.raises(ImportError): self.loader.load_module('bam') def test_is_package(self): loader = self.finder.find_module('pike_tests') assert loader.is_package() def test_module_isnt_package(self): loader = self.finder.find_module('pike_tests.app') assert not loader.is_package() def test_load_package_module(self): loader = self.finder.find_module('pike_tests') module = loader.load_module('pike_tests') assert module is not None def test_second_load_pulls_previously_loaded_module(self): first_load = self.loader.load_module('pike_tests.app') second_load = self.loader.load_module('pike_tests.app') assert first_load == second_load def test_load_module_by_path_with_invalid_path(self): module = self.loader.load_module_by_path('name', 'something.bam') assert module is None
def loader_finder(): temp_folder = utils.make_tmpdir() # Create a simple package pkg_location = utils.create_working_package(temp_folder) mod_location = os.path.join(pkg_location, 'app.py') utils.write_file(mod_location, SIMPLE_CLASS) finder = PikeFinder([temp_folder]) loader = PikeLoader('pike_tests.app', mod_location) yield loader, finder utils.remove_dir(temp_folder)