def test_return_module_name(self): # foo_a => import bar modset = ModuleSet([FOO.a, BAR]) got = modset.get_imports(modset.by_name['foo.foo_a'], return_fqn=True) name = got.pop() assert len(got) == 0 assert name == 'bar'
def test_init_no_packge(self): # if a module of a package is added but no __init__.py # its packages is not added to the list of packages modset = ModuleSet([FOO.a]) assert 0 == len(modset.pkgs) assert 1 == len(modset.by_path) assert modset.by_path[FOO.a].fqn == ['foo', 'foo_a']
def test_from_import_object(self): # foo_a => from foo.foo_c import obj_c modset = ModuleSet([FOO.init, FOO.a, FOO.b, FOO.c]) got = modset.get_imports(modset.by_name['foo.foo_a']) assert len(got) == 2 assert FOO.b in got # doesnt matter for this test assert FOO.c in got
def test_init_with_packge(self): modset = ModuleSet([FOO.init, FOO.a]) assert set(['foo']) == modset.pkgs assert 2 == len(modset.by_path) assert modset.by_path[FOO.init].fqn == ['foo', '__init__'] assert modset.by_path[FOO.a].fqn == ['foo', 'foo_a'] assert 2 == len(modset.by_name) assert modset.by_name['foo.__init__'].fqn == ['foo', '__init__'] assert modset.by_name['foo.foo_a'].fqn == ['foo', 'foo_a']
def task_imports(): """find imports from a python module""" base_path = pathlib.Path("youtube_dl_gui") pkg_modules = ModuleSet(base_path.glob("**/*.py")) for name, module in pkg_modules.by_name.items(): yield { "name": name, "file_dep": [module.path], "actions": [(get_imports, (pkg_modules, module.path))], }
def task_imports(): """find imports from a python module""" base_path = pathlib.Path('projects/requests/requests') pkg_modules = ModuleSet(base_path.glob('**/*.py')) for name, module in pkg_modules.by_name.items(): yield { 'name': name, 'file_dep': [module.path], 'actions': [(get_imports, (pkg_modules, module.path))], }
import pathlib import pygraphviz from import_deps import PyModule, ModuleSet DOIT_CONFIG = { 'default_tasks': ['imports', 'dot', 'draw'], } base_path = pathlib.Path('projects/requests/requests') PKG_MODULES = ModuleSet(base_path.glob('**/*.py')) def get_imports(pkg_modules, module_path): module = pkg_modules.by_path[module_path] imports = pkg_modules.get_imports(module, return_fqn=True) return {'modules': list(sorted(imports))} def task_imports(): """find imports from a python module""" for name, module in PKG_MODULES.by_name.items(): yield { 'name': name, 'file_dep': [module.path], 'actions': [(get_imports, (PKG_MODULES, module.path))], }
def test_mod_imports(self): # foo_a => import bar modset = ModuleSet([FOO.init, FOO.a, FOO.b, FOO.c, BAR]) got = modset.mod_imports('foo.foo_a') imports = list(sorted(got)) assert imports == ['bar', 'foo.foo_b', 'foo.foo_c']
def test_import_module(self): # foo_a => import bar modset = ModuleSet([FOO.a, BAR]) got = modset.get_imports(modset.by_name['foo.foo_a']) assert len(got) == 1 assert BAR in got
def test_relative_intra_import_pkg_obj(self): # foo_c => from . import foo_i modset = ModuleSet([FOO.init, FOO.c]) got = modset.get_imports(modset.by_name['foo.foo_c']) assert len(got) == 1 assert FOO.init in got
def test_import_obj(self): # foo_b => import baz.obj_baz modset = ModuleSet([FOO.b, BAZ]) got = modset.get_imports(modset.by_name['foo.foo_b']) assert len(got) == 1 assert BAZ in got
def test_from_pkg_import_obj(self): # baz => from foo import obj_1 modset = ModuleSet([FOO.init, BAZ]) got = modset.get_imports(modset.by_name['baz']) assert len(got) == 1 assert FOO.init in got
def test_from_pkg_import_module(self): # foo_a => from foo import foo_b modset = ModuleSet([FOO.init, FOO.a, FOO.b]) got = modset.get_imports(modset.by_name['foo.foo_a']) assert len(got) == 1 assert FOO.b in got
def test_import_pkg(self): # bar => import foo modset = ModuleSet([FOO.init, BAR]) got = modset.get_imports(modset.by_name['bar']) assert len(got) == 1 assert FOO.init in got
def test_import_not_tracked(self): modset = ModuleSet([FOO.a]) got = modset.get_imports(modset.by_name['foo.foo_a']) assert len(got) == 0
def get_imports(module_path): module = PyModule(module_path) base_path = module.pkg_path().resolve() mset = ModuleSet(base_path.glob('**/*.py')) imports = mset.get_imports(module, return_fqn=True) return {'modules': list(sorted(imports))}
def test_relative_intra_import_module(self): # foo_d => from . import foo_c modset = ModuleSet([FOO.init, FOO.c, FOO.d]) got = modset.get_imports(modset.by_name['foo.foo_d']) assert len(got) == 1 assert FOO.c in got
def test_relative_parent(self): # foo.sub.sub_a => from .. import foo_d modset = ModuleSet([FOO.init, FOO.d, SUB.init, SUB.a]) got = modset.get_imports(modset.by_name['foo.sub.sub_a']) assert len(got) == 1 assert FOO.d in got
def __init__(self, py_files, json_file='deps.json'): self.json_file = json_file self.py_files = list(set(py_files)) self.py_mods = ModuleSet(self.py_files) self._graph = None # DepGraph cached on first use
def test_init_subpackge(self): modset = ModuleSet([FOO.init, SUB.init, SUB.a]) assert set(['foo', 'foo.sub']) == modset.pkgs assert 3 == len(modset.by_path) assert modset.by_path[SUB.a].fqn == ['foo', 'sub', 'sub_a']