pythonimporttree 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import libcst as cst
|
|
3
|
+
from libcst.helpers import get_full_name_for_node
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def build_import_tree(directory: Path, entry: str = "main.py") -> dict:
|
|
7
|
+
"""Bouw een boom van lokale imports met entry als wortel.
|
|
8
|
+
|
|
9
|
+
Resultaat: {"file": "main.py", "imports": [andere knopen, ...]}.
|
|
10
|
+
Alleen .py-bestanden rechtstreeks in directory worden meegenomen.
|
|
11
|
+
import x, import x as y en from x import y worden ondersteund.
|
|
12
|
+
Externe modules en relatieve/package-imports vallen buiten deze toets.
|
|
13
|
+
Imports worden statisch gezocht, ook binnen functies en if-blokken.
|
|
14
|
+
Een cyclus eindigt in een knoop zonder kinderen, zodat de boom eindig is.
|
|
15
|
+
Er wordt geen geanalyseerde Pythoncode uitgevoerd.
|
|
16
|
+
"""
|
|
17
|
+
files = {path.stem: path for path in python_files(directory)}
|
|
18
|
+
if entry not in {path.name for path in files.values()}:
|
|
19
|
+
raise click.ClickException(f"Startbestand {entry} ontbreekt in {directory}")
|
|
20
|
+
|
|
21
|
+
class ImportCollector(cst.CSTVisitor):
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.names = set()
|
|
24
|
+
|
|
25
|
+
def visit_Import(self, node):
|
|
26
|
+
for alias in node.names:
|
|
27
|
+
name = get_full_name_for_node(alias.name)
|
|
28
|
+
if name:
|
|
29
|
+
self.names.add(name.split(".")[0])
|
|
30
|
+
|
|
31
|
+
def visit_ImportFrom(self, node):
|
|
32
|
+
if not node.relative and node.module is not None:
|
|
33
|
+
name = get_full_name_for_node(node.module)
|
|
34
|
+
if name:
|
|
35
|
+
self.names.add(name.split(".")[0])
|
|
36
|
+
|
|
37
|
+
dependencies = {}
|
|
38
|
+
for name, path in files.items():
|
|
39
|
+
visitor = ImportCollector()
|
|
40
|
+
cst.parse_module(read_source(path)).visit(visitor)
|
|
41
|
+
dependencies[name] = sorted(visitor.names & files.keys())
|
|
42
|
+
|
|
43
|
+
def build(name, ancestors):
|
|
44
|
+
node = {"file": files[name].name, "imports": []}
|
|
45
|
+
if name not in ancestors:
|
|
46
|
+
node["imports"] = [build(child, ancestors | {name})
|
|
47
|
+
for child in dependencies[name]]
|
|
48
|
+
return node
|
|
49
|
+
|
|
50
|
+
return build(Path(entry).stem, set())
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: pythonimporttree
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
Author: Vincent Nys
|
|
6
|
+
Author-email: Vincent Nys <vincentnys@gmail.com>
|
|
7
|
+
Requires-Dist: libcst>=1.9.0
|
|
8
|
+
Requires-Dist: requests>=2.34.2
|
|
9
|
+
Requires-Python: >=3.14
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
pythonimporttree/__init__.py,sha256=Geul_1pGltx0sZkv2cnG52Y0JB9FknI0xrLWunBHkOU,2026
|
|
2
|
+
pythonimporttree/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
pythonimporttree-0.1.0.dist-info/WHEEL,sha256=G_-rrEibZr7OEM7zdH1dSO6s-VdjfnS5PA4CLAdI9Vw,80
|
|
4
|
+
pythonimporttree-0.1.0.dist-info/METADATA,sha256=UGw9Fm4aD_65ZzN3wJ96vIdbLa9cEXrVTEwa8Ruxhlk,290
|
|
5
|
+
pythonimporttree-0.1.0.dist-info/RECORD,,
|