Skip to content

Commit

Permalink
add raw modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nmichlo committed May 14, 2024
1 parent 05e41d4 commit 7d72911
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
6 changes: 4 additions & 2 deletions pydependence/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
33 changes: 27 additions & 6 deletions pydependence/_core/module_imports_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,48 @@ class ImportSourceEnum(str, Enum):
# type_check = 'type_check' # TODO


MANUAL_IMPORT_SOURCE = "<manual>"


@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"<raw: {self.orig_name}>"

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>"] = 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):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_module_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -51,6 +52,7 @@
_find_modules,
)
from pydependence._core.requirements_map import (
DEFAULT_REQUIREMENTS_ENV,
ImportMatcherBase,
ImportMatcherGlob,
ImportMatcherScope,
Expand Down Expand Up @@ -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.*")),
Expand Down Expand Up @@ -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'
" # ← <raw: manual1>\n"
' "manual2_no_match",\n'
" # ← <raw: manual2_no_match>\n"
' "package",\n'
" # ← t_ast_parser\n"
"]"
)


# ========================================================================= #
# TEST CLI #
Expand Down

0 comments on commit 7d72911

Please sign in to comment.