def test_import_module_at_path_module(self): with tempfile.TemporaryDirectory() as tmpdir: path = pathlib.Path(tmpdir).joinpath('foo', 'bar.py') path.parent.mkdir(parents=True) init = path.parent.joinpath('__init__.py') init.touch() x = 1 with path.open('w') as f: f.write('x={x!r}'.format(x=x)) modname = 'foo.bar' modpath = str(path) # e.g. /tmp/foo/bar.py' mod = import_module_at_path(modname, modpath) self.assertIsInstance(mod, types.ModuleType) self.assertEqual(mod.__name__, modname) self.assertEqual(mod.x, x) with tempfile.TemporaryDirectory() as tmpdir: path = pathlib.Path(tmpdir).joinpath('foo') path.mkdir(parents=True) init = path.joinpath('__init__.py') init.touch() x = 'hello' with init.open('w') as f: f.write('x={x!r}'.format(x=x)) modname = 'foo' modpath = str(path) mod = import_module_at_path(modname, modpath) self.assertIsInstance(mod, types.ModuleType) self.assertEqual(mod.__name__, modname) self.assertEqual(mod.x, x)
def mock_contrib_module(self, modname, content, n): with tempfile.TemporaryDirectory() as tmpdir: modpath = pathlib.Path(tmpdir).joinpath(modname) modpath.mkdir() create_contrib_modules_at_dir(modpath, content, n=n) mod = import_module_at_path(modname, modpath) features = _collect_contrib_features(mod) yield mod, features
def from_path(cls, path): """Create a Project instance from an fs path to the containing dir Args: path (PathLike): path to directory that contains the project """ path = pathlib.Path(path) config = load_config_in_dir(path) package_slug = config.get('project.package_slug') package = import_module_at_path(package_slug, path.joinpath('src', package_slug)) return cls(package)
def test_import_module_at_path_package(tmp_path): path = tmp_path.joinpath('foo') path.mkdir(parents=True) init = path.joinpath('__init__.py') init.touch() x = 'hello' with init.open('w') as f: f.write(f'x={x!r}') modname = 'foo' modpath = str(path) mod = import_module_at_path(modname, modpath) assert isinstance(mod, types.ModuleType) assert mod.__name__ == modname assert mod.x == x
def test_import_module_at_path_module(tmp_path): path = tmp_path.joinpath('foo', 'bar.py') path.parent.mkdir(parents=True) init = path.parent.joinpath('__init__.py') init.touch() x = 1 with path.open('w') as f: f.write(f'x={x!r}') modname = 'foo.bar' modpath = str(path) # e.g. /tmp/foo/bar.py' mod = import_module_at_path(modname, modpath) assert isinstance(mod, types.ModuleType) assert mod.__name__ == modname assert mod.x == x
def from_path(cls, path: Pathy, ascend: bool = False): """Create a Project instance from an fs path to the containing dir Args: path: path to directory that contains the project ascend: if the config file is not found in the given directory, then search in parent directories, stopping at a file system boundary """ path = pathlib.Path(path) try: config = load_config_in_dir(path) package_slug = config.get('project.package_slug') package = import_module_at_path(package_slug, path.joinpath('src', package_slug)) return cls(package) except ConfigurationError: if ascend: parent = path.parent if parent.exists() and not is_mount(parent): return cls.from_path(parent, ascend=ascend) raise
def _import(modname): relpath = modname_to_relpath(modname, project_root=base, add_init=False) abspath = base.joinpath(relpath) return import_module_at_path(modname, abspath)