예제 #1
0
    def test_detect_module_path(self):
        collection = ImportDependencyCollection.build(dependencies=[
            build_dep(src_module_path="main",
                      import_paths=["foo", "bar.BazClass"]),
            build_dep(src_module_path="foo", import_paths=[]),
            build_dep(src_module_path="bar", import_paths=[]),
        ])

        assert (collection.detect_module_path(
            import_path=ImportPath.from_str("os.path")) is None)
        assert collection.detect_module_path(import_path=ImportPath.from_str(
            "foo")) == ModulePath.from_str("foo")
        assert collection.detect_module_path(import_path=ImportPath.from_str(
            "bar.XXX")) == ModulePath.from_str("bar")
예제 #2
0
    def test_import_from_parent_path(self):
        import_from = parse_import_from("from .. import jig_ast")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert len(import_modules) == 1
        assert ImportPath.from_str("jig.collector.jig_ast") in import_modules
예제 #3
0
    def test_build_import_dependency(self):
        source_file_path = SourceFilePath(root_path=Path("/root"),
                                          file_path=Path("/root/main.py"))
        assert str(source_file_path.module_path) == "main"

        code = SourceCode.build(file=SourceFile(
            source_file_path=source_file_path,
            size=100,
            content="from os import path, environ",
        ))

        assert code.build_import_dependency() == SourceCodeImportDependency(
            source_module_path=ModulePath.from_str("main"),
            import_paths=ImportPathCollection([
                ImportPath.from_str("os.path"),
                ImportPath.from_str("os.environ")
            ]),
        )
예제 #4
0
    def import_from_to_import_paths(self, import_from: ImportFrom) -> List[ImportPath]:
        """
        このソースファイルパスを基準にimport from を解決し、ModulePathのリストを返します。
        :param import_from:
        :return:
        """
        level = import_from.level if import_from.level is not None else 0

        if level < 1:
            assert import_from.module is not None

            p = ImportPath.from_str(import_from.module)
            return [p.join(alias.name) for alias in import_from.names]

        module_path = self.module_path_with_level(level)
        p = ImportPath.from_str(str(module_path))

        if import_from.module:
            p += ImportPath.from_str(import_from.module)

        return [p.join(alias.name) for alias in import_from.names]
예제 #5
0
def mod_collections(*modules) -> ImportPathCollection:
    return ImportPathCollection([ImportPath.from_str(module) for module in modules])
예제 #6
0
 def test_from_str(self):
     p = ImportPath(names=["jig", "collector", "domain"])
     assert p == ImportPath.from_str("jig.collector.domain")
예제 #7
0
def path(path: str) -> ImportPath:
    return ImportPath.from_str(path)
예제 #8
0
def collection(*args):
    modules = [ImportPath.from_str(p) for p in args]

    return ImportPathCollection(modules)
예제 #9
0
    def test_collector(self):
        os.chdir(self.get_code_path())
        source_code_collection = SourceCodeCollector(
            root_path=Path(self.get_code_path())).collect(Path("jig"))
        assert len(source_code_collection) == 10
        filenames = sorted([
            str(code.file.source_file_path.relative_path_from_root)
            for code in source_code_collection
        ])
        assert filenames == [
            "jig/__init__.py",
            "jig/collector/__init__.py",
            "jig/collector/application/__init__.py",
            "jig/collector/domain/__init__.py",
            "jig/collector/jig_ast/__init__.py",
            "jig/collector/jig_ast/class_def.py",
            "jig/collector/jig_ast/imports.py",
            "jig/visualizer/__init__.py",
            "jig/visualizer/application/__init__.py",
            "jig/visualizer/domain/__init__.py",
        ]

        jig_init_py = source_code_collection.get_by_relative_path(
            "jig/__init__.py")
        assert jig_init_py.file.content == ""
        assert jig_init_py.module_path == ModulePath.from_str("jig")
        assert jig_init_py.import_paths == ImportPathCollection([])
        assert jig_init_py.class_defs == []

        jig_collector_jig_ast_init = source_code_collection.get_by_relative_path(
            "jig/collector/jig_ast/__init__.py")
        assert jig_collector_jig_ast_init.module_path == ModulePath.from_str(
            "jig.collector.jig_ast")
        assert jig_collector_jig_ast_init.import_paths == ImportPathCollection([
            ImportPath.from_str("dataclasses"),
            ImportPath.from_str("typed_ast.ast3"),
            ImportPath.from_str("typing.List"),
            ImportPath.from_str("jig.collector.jig_ast.class_def.ClassDef"),
            ImportPath.from_str(
                "jig.collector.jig_ast.class_def.ClassDefVisitor"),
            ImportPath.from_str("jig.collector.jig_ast.imports.Import"),
            ImportPath.from_str("jig.collector.jig_ast.imports.ImportFrom"),
            ImportPath.from_str(
                "jig.collector.jig_ast.imports.ImportFromVisitor"),
            ImportPath.from_str("jig.collector.jig_ast.imports.ImportVisitor"),
        ])

        jig_collector_application = source_code_collection.get_by_relative_path(
            "jig/collector/application/__init__.py")
        assert jig_collector_application.module_path == ModulePath.from_str(
            "jig.collector.application")
        assert jig_collector_application.import_paths == ImportPathCollection([
            ImportPath.from_str("dataclasses"),
            ImportPath.from_str("os"),
            ImportPath.from_str("typing.List"),
            ImportPath.from_str("jig.collector.domain.FilePath"),
            ImportPath.from_str("jig.collector.domain.SourceCode"),
            ImportPath.from_str("jig.collector.domain.SourceFile"),
        ])
예제 #10
0
def build_dep(src_module_path: str,
              import_paths: List[str]) -> SourceCodeImportDependency:
    return SourceCodeImportDependency.build(
        source_module_path=ModulePath.from_str(src_module_path),
        import_paths=[ImportPath.from_str(dest) for dest in import_paths],
    )
예제 #11
0
    def build_by_import_ast(cls, import_ast: Import) -> "ImportPathCollection":
        imports = [ImportPath.from_str(name.name) for name in import_ast.names]

        return ImportPathCollection(imports)