Skip to content

Commit

Permalink
Show module type
Browse files Browse the repository at this point in the history
  • Loading branch information
si-23 committed Oct 29, 2024
1 parent edf2d29 commit 66dc4d8
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions py_import_cycles/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .files import get_outputs_filepaths, iter_python_files
from .graphs import make_graph
from .log import logger, setup_logging
from .modules import Module, ModuleFactory
from .modules import Module, ModuleFactory, NamespacePackage, PyModule, RegularPackage
from .visitors import visit_python_file


Expand Down Expand Up @@ -143,13 +143,25 @@ def main() -> int:
# '----------------------------------------------------------------------'


def _show_module(module: Module) -> str:
match module:
case NamespacePackage():
return f"{module.name}/"
case RegularPackage():
return f"{module.name}.__init__"
case PyModule():
return f"{module.name}"
case _:
raise TypeError(module)


def _make_readable_imports_by_module(
imports_by_module: Mapping[Module, Sequence[Module]],
) -> Sequence[str]:
lines = []
for ibm, ms in imports_by_module.items():
if ms:
lines.append(f" {ibm.name} imports: {', '.join(str(m.name) for m in ms)}")
lines.append(f" {_show_module(ibm)} imports: {', '.join(_show_module(m) for m in ms)}")
return lines


Expand All @@ -168,7 +180,7 @@ def _log_or_show_cycles(
) -> None:
if verbose:
for line in _make_readable_cycles(
lambda nr, ic: [f" Cycle {nr}:"] + [f" {m.name}" for m in ic],
lambda nr, ic: [f" Cycle {nr}:"] + [f" {_show_module(m)}" for m in ic],
sorted_cycles,
):
sys.stderr.write(f"{line}\n")
Expand All @@ -178,7 +190,7 @@ def _log_or_show_cycles(
"Import cycles:\n%s",
"\n".join(
_make_readable_cycles(
lambda nr, ic: [f" Cycle {nr}: {' > '.join(str(m.name) for m in ic)}"],
lambda nr, ic: [f" Cycle {nr}: {' > '.join(_show_module(m) for m in ic)}"],
sorted_cycles,
)
),
Expand Down

0 comments on commit 66dc4d8

Please sign in to comment.