From 7d7291145b84e17bfa266131456f6be69e6bfb5c Mon Sep 17 00:00:00 2001 From: Nathan Michlo Date: Tue, 14 May 2024 02:40:18 +0200 Subject: [PATCH] add raw modules --- pydependence/_cli.py | 6 ++-- pydependence/_core/module_imports_ast.py | 33 +++++++++++++++---- tests/test_module_data.py | 41 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/pydependence/_cli.py b/pydependence/_cli.py index 556f0e6..0ac932d 100644 --- a/pydependence/_cli.py +++ b/pydependence/_cli.py @@ -31,7 +31,7 @@ import pydantic from packaging.requirements import Requirement -from pydependence._core.module_imports_ast import ManualImportInfo +from pydependence._core.module_imports_ast import ManualImportInfo, ManualSource from pydependence._core.modules_scope import ( ModulesScope, RestrictMode, @@ -131,7 +131,9 @@ def get_output_name(self) -> str: return self.scope def get_manual_imports(self): - return [ManualImportInfo(target=r) for r in self.raw] + if not self.raw: + return [] + return [ManualImportInfo.from_target(r) for r in self.raw] def _write_requirements(self, mapped_requirements: OutMappedRequirements) -> None: raise NotImplementedError( diff --git a/pydependence/_core/module_imports_ast.py b/pydependence/_core/module_imports_ast.py index f997143..9e4402a 100644 --- a/pydependence/_core/module_imports_ast.py +++ b/pydependence/_core/module_imports_ast.py @@ -140,27 +140,48 @@ class ImportSourceEnum(str, Enum): # type_check = 'type_check' # TODO -MANUAL_IMPORT_SOURCE = "" - - @dataclasses.dataclass class BasicImportInfo: # target target: str - is_lazy: bool - # source source_name: str + is_lazy: bool @property def root_target(self) -> str: return self.target.split(".")[0] +class ManualSource: + def __init__(self, orig_name: str): + self.orig_name = orig_name + + def __str__(self): + return f"" + + def __repr__(self): + return f"{self.__class__.__name__}({self.orig_name})" + + def __eq__(self, other): + if isinstance(str, other): + return str(self) == other + elif isinstance(other, ManualSource): + return self.orig_name == other.orig_name + return False + + def __hash__(self): + return hash(str(self)) + + @dataclasses.dataclass class ManualImportInfo(BasicImportInfo): - source_name: Literal[""] = MANUAL_IMPORT_SOURCE + source_name: ManualSource is_lazy: Literal[False] = False + @classmethod + def from_target(cls, target: str) -> "ManualImportInfo": + return cls(target=target, source_name=ManualSource(target)) + @dataclasses.dataclass class LocImportInfo(BasicImportInfo): diff --git a/tests/test_module_data.py b/tests/test_module_data.py index 5f08923..343c52c 100644 --- a/tests/test_module_data.py +++ b/tests/test_module_data.py @@ -31,6 +31,7 @@ from pydependence._core.module_imports_ast import ( ImportSourceEnum, LocImportInfo, + ManualImportInfo, load_imports_from_module_info, ) from pydependence._core.module_imports_loader import ( @@ -51,6 +52,7 @@ _find_modules, ) from pydependence._core.requirements_map import ( + DEFAULT_REQUIREMENTS_ENV, ImportMatcherBase, ImportMatcherGlob, ImportMatcherScope, @@ -851,6 +853,7 @@ def mapper(): ReqMatcher("glob_extern", ImportMatcherGlob("extern_C.*")), # purposely wrong, correct is `extern_a4i` ReqMatcher("glob_extern_WRONG", ImportMatcherGlob("extern_a4.*")), + ReqMatcher("glob_manual1", ImportMatcherGlob("manual1.*")), ], "asdf": [ ReqMatcher("glob_extern", ImportMatcherGlob("extern_a4i.*")), @@ -1191,6 +1194,44 @@ def test_toml_array_gen(mapper: RequirementsMapper): "]" ) + # >>> OUTPUT REQUIREMENTS WITH MANUAL ADDITIONS <<< # + + # update! + mapped = mapper.generate_output_requirements( + imports + + [ + ManualImportInfo.from_target("manual2_no_match"), + ManualImportInfo.from_target("manual1"), + ], + requirements_env="asdf", + ) + + # NOTE: bug in tomlkit prevents indents from being applied to comments when array is not in a table? + assert mapped.as_toml_array( + notice=False, + sources=True, + sources_compact=False, + sources_roots=False, + indent_size=4, + ).as_string() == ( + "[\n" + ' "foo",\n' + " # ← t_ast_parser\n" + ' "glob_extern",\n' + " # ← A.a1\n" + " # ← A.a2\n" + " # ← A.a3.a3i\n" + " # ← A.a4.a4i\n" + " # ← C\n" + ' "glob_manual1",\n' + " # ← \n" + ' "manual2_no_match",\n' + " # ← \n" + ' "package",\n' + " # ← t_ast_parser\n" + "]" + ) + # ========================================================================= # # TEST CLI #