deproc-python 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.
- deproc/plugins/python/__init__.py +11 -0
- deproc/plugins/python/linker/__init__.py +3 -0
- deproc/plugins/python/linker/main.py +93 -0
- deproc/plugins/python/linker/models.py +21 -0
- deproc/plugins/python/parser/__init__.py +3 -0
- deproc/plugins/python/parser/main.py +552 -0
- deproc/plugins/python/parser/models.py +70 -0
- deproc/plugins/python/parser/utils/extraction.py +189 -0
- deproc/plugins/python/parser/utils/misc.py +8 -0
- deproc/plugins/python/parser/utils/tree_sitter_python.py +29 -0
- deproc/plugins/python/resolver/__init__.py +3 -0
- deproc/plugins/python/resolver/main.py +235 -0
- deproc/plugins/python/resolver/models.py +10 -0
- deproc/plugins/python/symbol_cache/__init__.py +3 -0
- deproc/plugins/python/symbol_cache/main.py +64 -0
- deproc/plugins/python/symbol_cache/models.py +10 -0
- deproc_python-0.1.0.dist-info/METADATA +22 -0
- deproc_python-0.1.0.dist-info/RECORD +20 -0
- deproc_python-0.1.0.dist-info/WHEEL +5 -0
- deproc_python-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .linker import PythonLinker
|
|
2
|
+
from .parser import PythonSourceParser
|
|
3
|
+
from .resolver import PythonResolver
|
|
4
|
+
from .symbol_cache import PythonSymbolCache
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"PythonLinker",
|
|
8
|
+
"PythonResolver",
|
|
9
|
+
"PythonSourceParser",
|
|
10
|
+
"PythonSymbolCache",
|
|
11
|
+
]
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fnmatch
|
|
2
|
+
from deproc.core.interfaces import Linker
|
|
3
|
+
from deproc.core.context import Context
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from ..parser.models import PythonModule
|
|
6
|
+
from .models import (
|
|
7
|
+
Node,
|
|
8
|
+
PythonNamespacePackage,
|
|
9
|
+
PythonPackage,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
class PythonLinker(Linker[list[PythonModule], list[Node]]):
|
|
13
|
+
def _check_skip_patterns(self, path: str, skip_patterns: list[str]) -> bool:
|
|
14
|
+
return any(fnmatch.fnmatch(path, pattern) for pattern in skip_patterns)
|
|
15
|
+
|
|
16
|
+
def link_files(self, nodes: list[PythonModule], context: Context) -> list[Node]:
|
|
17
|
+
base_path = Path(context.base_path)
|
|
18
|
+
|
|
19
|
+
path_to_module: dict[str, PythonModule] = {}
|
|
20
|
+
has_init: dict[str, PythonModule] = {}
|
|
21
|
+
|
|
22
|
+
for node in nodes:
|
|
23
|
+
relative_path = node.path
|
|
24
|
+
dir_path = str(Path(relative_path).parent)
|
|
25
|
+
|
|
26
|
+
if node.path.endswith("__init__.py"):
|
|
27
|
+
has_init[dir_path] = node
|
|
28
|
+
continue
|
|
29
|
+
|
|
30
|
+
path_to_module[relative_path] = node
|
|
31
|
+
|
|
32
|
+
skip_patterns = context.skip_paths
|
|
33
|
+
top_level: list[Node] = []
|
|
34
|
+
for sub_path in base_path.iterdir():
|
|
35
|
+
if sub_path.name == "__pycache__":
|
|
36
|
+
continue
|
|
37
|
+
if self._check_skip_patterns(sub_path.name, skip_patterns):
|
|
38
|
+
continue
|
|
39
|
+
if not self.validate_path(str(sub_path)):
|
|
40
|
+
continue
|
|
41
|
+
sub_node = self.traverse_path(base_path, sub_path, path_to_module, has_init, context)
|
|
42
|
+
if sub_node:
|
|
43
|
+
sub_node.parent_id = None
|
|
44
|
+
top_level.append(sub_node)
|
|
45
|
+
|
|
46
|
+
return top_level
|
|
47
|
+
|
|
48
|
+
def validate_path(self, path: str) -> bool:
|
|
49
|
+
return path.endswith(".py") or path.endswith(".pyi") or path.endswith("__init__.py") or Path(path).is_dir()
|
|
50
|
+
|
|
51
|
+
def traverse_path(
|
|
52
|
+
self,
|
|
53
|
+
base_path: Path,
|
|
54
|
+
current_path: Path,
|
|
55
|
+
path_to_module: dict[str, PythonModule],
|
|
56
|
+
has_init: dict[str, PythonModule],
|
|
57
|
+
context: Context
|
|
58
|
+
) -> Node | None:
|
|
59
|
+
if not self.validate_path(str(current_path)):
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
relative_path = str(current_path.relative_to(base_path)).replace("\\", "/")
|
|
63
|
+
|
|
64
|
+
if relative_path.endswith(".py") or relative_path.endswith(".pyi"):
|
|
65
|
+
return path_to_module.get(relative_path)
|
|
66
|
+
|
|
67
|
+
package = None
|
|
68
|
+
fqn = relative_path.replace("/", ".")
|
|
69
|
+
|
|
70
|
+
if relative_path in has_init:
|
|
71
|
+
package = PythonPackage(
|
|
72
|
+
submodule_ids=[],
|
|
73
|
+
**vars(has_init[relative_path])
|
|
74
|
+
)
|
|
75
|
+
else:
|
|
76
|
+
package = PythonNamespacePackage(
|
|
77
|
+
path=relative_path,
|
|
78
|
+
fqn=fqn,
|
|
79
|
+
submodule_ids=[],
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
for sub_path in current_path.iterdir():
|
|
83
|
+
if sub_path.name == "__pycache__":
|
|
84
|
+
continue
|
|
85
|
+
if self._check_skip_patterns(sub_path.name, context.skip_paths):
|
|
86
|
+
continue
|
|
87
|
+
sub_node = self.traverse_path(base_path, sub_path, path_to_module, has_init, context)
|
|
88
|
+
if sub_node:
|
|
89
|
+
sub_node.parent_id = package.id
|
|
90
|
+
package.submodule_ids.append(sub_node.id)
|
|
91
|
+
|
|
92
|
+
context.entity_registry.add(package)
|
|
93
|
+
return package
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from deproc.core.interfaces.parser.models import Node
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from ..parser.models import (
|
|
4
|
+
PythonModule,
|
|
5
|
+
SymbolID,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class PythonNamespacePackage(Node):
|
|
10
|
+
fqn: str
|
|
11
|
+
submodule_ids: list[SymbolID] = field(default_factory=list)
|
|
12
|
+
|
|
13
|
+
@dataclass(kw_only=True)
|
|
14
|
+
class PythonPackage(PythonModule):
|
|
15
|
+
submodule_ids: list[SymbolID] = field(default_factory=list)
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"Node",
|
|
19
|
+
"PythonNamespacePackage",
|
|
20
|
+
"PythonPackage",
|
|
21
|
+
]
|
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
from deproc.core.interfaces import SourceParser
|
|
2
|
+
from deproc.core.context import Context
|
|
3
|
+
from deproc.utils import iter_children, first_child
|
|
4
|
+
from tree_sitter import Query, Node, QueryCursor
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
from .models import (
|
|
8
|
+
Annotation,
|
|
9
|
+
ControlFlowBlock,
|
|
10
|
+
ControlFlowGroup,
|
|
11
|
+
PythonFunctionLike,
|
|
12
|
+
PythonClass,
|
|
13
|
+
PythonImportStatement,
|
|
14
|
+
PythonImportAlias,
|
|
15
|
+
PythonModule,
|
|
16
|
+
SourceRange,
|
|
17
|
+
SymbolID,
|
|
18
|
+
)
|
|
19
|
+
from .utils.misc import visibility_from_name
|
|
20
|
+
from .utils.extraction import (
|
|
21
|
+
collect_from_assignment_node,
|
|
22
|
+
extract_decorator_details,
|
|
23
|
+
extract_docstring_range,
|
|
24
|
+
extract_signature,
|
|
25
|
+
)
|
|
26
|
+
from .utils.tree_sitter_python import (
|
|
27
|
+
create_source_range,
|
|
28
|
+
get_python_language,
|
|
29
|
+
get_python_parser,
|
|
30
|
+
node_text
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class PythonSourceParser(SourceParser):
|
|
35
|
+
def __init__(self):
|
|
36
|
+
self._parser = get_python_parser()
|
|
37
|
+
self._language = get_python_language()
|
|
38
|
+
self._current_source_file_id: str | None = None
|
|
39
|
+
|
|
40
|
+
self.all_exports_query = Query(
|
|
41
|
+
self._language,
|
|
42
|
+
"""
|
|
43
|
+
(expression_statement
|
|
44
|
+
(assignment
|
|
45
|
+
left: (identifier) @all_name (#eq? @all_name "__all__")
|
|
46
|
+
right: [(list) (tuple)] @all_values
|
|
47
|
+
)
|
|
48
|
+
)"""
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
def _sr(self, node: Node) -> SourceRange:
|
|
52
|
+
return create_source_range(node, source_id=self._current_source_file_id)
|
|
53
|
+
|
|
54
|
+
def _compute_fqn(self, file_path: str, context: Context) -> str:
|
|
55
|
+
base_path = context.base_path
|
|
56
|
+
relative_path = os.path.relpath(file_path, base_path)
|
|
57
|
+
without_extension = os.path.splitext(relative_path)[0]
|
|
58
|
+
fqn = without_extension.replace(os.sep, ".")
|
|
59
|
+
filename = os.path.basename(file_path)
|
|
60
|
+
if filename in ("__init__.py", "__init__.pyi") and fqn.endswith(".__init__"):
|
|
61
|
+
fqn = fqn[: -len(".__init__")]
|
|
62
|
+
return fqn
|
|
63
|
+
|
|
64
|
+
def parse_file(self, path: str, context: Context) -> PythonModule:
|
|
65
|
+
if not os.path.exists(path):
|
|
66
|
+
raise FileNotFoundError(f"File not found: {path}")
|
|
67
|
+
|
|
68
|
+
if not path.endswith((".py", ".pyi")):
|
|
69
|
+
raise ValueError(f"Unsupported file extension for Python parser: {path}")
|
|
70
|
+
|
|
71
|
+
source_bytes = open(path, "rb").read()
|
|
72
|
+
tree = self._parser.parse(source_bytes)
|
|
73
|
+
root_node = tree.root_node
|
|
74
|
+
|
|
75
|
+
docstring_range = extract_docstring_range(root_node)
|
|
76
|
+
|
|
77
|
+
relative_path = os.path.relpath(path, context.base_path).replace("\\", "/")
|
|
78
|
+
parent_fqn = self._compute_fqn(path, context)
|
|
79
|
+
|
|
80
|
+
source_file = PythonModule(
|
|
81
|
+
fqn=parent_fqn,
|
|
82
|
+
path=relative_path,
|
|
83
|
+
docstring_range=docstring_range,
|
|
84
|
+
source=source_bytes.decode("utf-8"),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
self._current_source_file_id = source_file.id
|
|
88
|
+
source_file.type_ids = self._extract_classes(root_node, context, parent_id=source_file.id, parent_fqn=parent_fqn)
|
|
89
|
+
source_file.function_ids = self._extract_functions(root_node, context, type="FUNCTION", parent_id=source_file.id, parent_fqn=parent_fqn)
|
|
90
|
+
source_file.variable_ids = self._extract_variables(root_node, context, parent_id=source_file.id, parent_fqn=parent_fqn)
|
|
91
|
+
source_file.import_stmt_ids = self._extract_import_statements(root_node, context, parent_id=source_file.id, module_fqn=parent_fqn)
|
|
92
|
+
source_file.all_exports = self._extract_all_exports(root_node)
|
|
93
|
+
source_file.control_flow_group_ids = self._extract_control_flow_groups(root_node, context, parent_id=source_file.id, parent_fqn=parent_fqn)
|
|
94
|
+
|
|
95
|
+
context.entity_registry.add(source_file)
|
|
96
|
+
return source_file
|
|
97
|
+
|
|
98
|
+
def _extract_classes(self, root: Node, context: Context, parent_id: SymbolID | None = None, parent_fqn: str | None = None) -> list[SymbolID]:
|
|
99
|
+
return self._traverse_block_for_classes(root, context, parent_id, parent_fqn)
|
|
100
|
+
|
|
101
|
+
def _traverse_block_for_classes(self, block_node: Node, context: Context, parent_id: SymbolID | None = None, parent_fqn: str | None = None) -> list[SymbolID]:
|
|
102
|
+
class_ids = []
|
|
103
|
+
|
|
104
|
+
for child in iter_children(block_node):
|
|
105
|
+
if child.type == "class_definition":
|
|
106
|
+
class_ids.append(self._process_class_node(child, [], context, parent_id, parent_fqn))
|
|
107
|
+
elif child.type == "decorated_definition":
|
|
108
|
+
definition = child.child_by_field_name("definition")
|
|
109
|
+
if definition and definition.type == "class_definition":
|
|
110
|
+
decorator_details = extract_decorator_details(child, source_file_id=self._current_source_file_id)
|
|
111
|
+
class_ids.append(self._process_class_node(definition, decorator_details, context, parent_id, parent_fqn))
|
|
112
|
+
|
|
113
|
+
return class_ids
|
|
114
|
+
|
|
115
|
+
def _process_class_node(
|
|
116
|
+
self,
|
|
117
|
+
node: Node,
|
|
118
|
+
decorator_details: list[Annotation],
|
|
119
|
+
context: Context,
|
|
120
|
+
parent_id: SymbolID | None = None,
|
|
121
|
+
parent_fqn: str | None = None
|
|
122
|
+
) -> SymbolID:
|
|
123
|
+
source_range = self._sr(node)
|
|
124
|
+
name_node = node.child_by_field_name("name")
|
|
125
|
+
name = node_text(name_node)
|
|
126
|
+
|
|
127
|
+
bases_node = node.child_by_field_name("superclasses")
|
|
128
|
+
bases = []
|
|
129
|
+
if bases_node:
|
|
130
|
+
bases = [
|
|
131
|
+
node_text(part)
|
|
132
|
+
for part in iter_children(bases_node)
|
|
133
|
+
if part.type not in (",", "(", ")")
|
|
134
|
+
]
|
|
135
|
+
|
|
136
|
+
docstring_range = extract_docstring_range(node, source_file_id=self._current_source_file_id)
|
|
137
|
+
|
|
138
|
+
cls_fqn = f"{parent_fqn}.{name}" if parent_fqn else name
|
|
139
|
+
body_node = node.child_by_field_name("body")
|
|
140
|
+
|
|
141
|
+
cls_obj = PythonClass(
|
|
142
|
+
fqn=cls_fqn,
|
|
143
|
+
parent_id=parent_id,
|
|
144
|
+
name=name,
|
|
145
|
+
source_range=source_range,
|
|
146
|
+
docstring_range=docstring_range,
|
|
147
|
+
annotations=decorator_details,
|
|
148
|
+
inherits=bases,
|
|
149
|
+
visibility=visibility_from_name(name),
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
cls_obj.method_ids = self._extract_functions(body_node, context, type="METHOD", parent_id=cls_obj.id, parent_fqn=cls_fqn) if body_node else []
|
|
153
|
+
cls_obj.inner_type_ids = self._traverse_block_for_classes(body_node, context, parent_id=cls_obj.id, parent_fqn=cls_fqn) if body_node else []
|
|
154
|
+
cls_obj.property_ids = self._extract_variables(body_node, context, parent_id=cls_obj.id, parent_fqn=cls_fqn) if body_node else []
|
|
155
|
+
context.entity_registry.add(cls_obj)
|
|
156
|
+
return cls_obj.id
|
|
157
|
+
|
|
158
|
+
def _extract_functions(self, block_node: Node, context: Context, type: str = "FUNCTION", parent_id: SymbolID | None = None, parent_fqn: str | None = None) -> list[SymbolID]:
|
|
159
|
+
function_ids: list[SymbolID] = []
|
|
160
|
+
if not block_node:
|
|
161
|
+
return function_ids
|
|
162
|
+
|
|
163
|
+
for child in iter_children(block_node):
|
|
164
|
+
if child.type == "function_definition":
|
|
165
|
+
function_ids.append(self._process_function_node(child, [], context, type, parent_id, parent_fqn))
|
|
166
|
+
elif child.type == "decorated_definition":
|
|
167
|
+
definition = child.child_by_field_name("definition")
|
|
168
|
+
if definition and definition.type == "function_definition":
|
|
169
|
+
decorator_details = extract_decorator_details(child, source_file_id=self._current_source_file_id)
|
|
170
|
+
function_ids.append(self._process_function_node(definition, decorator_details, context, type, parent_id, parent_fqn))
|
|
171
|
+
return function_ids
|
|
172
|
+
|
|
173
|
+
def _process_function_node(
|
|
174
|
+
self,
|
|
175
|
+
node: Node,
|
|
176
|
+
decorator_details: list[Annotation],
|
|
177
|
+
context: Context,
|
|
178
|
+
type: str = "FUNCTION",
|
|
179
|
+
parent_id: SymbolID | None = None,
|
|
180
|
+
parent_fqn: str | None = None
|
|
181
|
+
) -> SymbolID:
|
|
182
|
+
source_range = self._sr(node)
|
|
183
|
+
name_node = node.child_by_field_name("name")
|
|
184
|
+
name = node_text(name_node)
|
|
185
|
+
|
|
186
|
+
docstring_range = extract_docstring_range(node, source_file_id=self._current_source_file_id)
|
|
187
|
+
signature = extract_signature(node, source_file_id=self._current_source_file_id)
|
|
188
|
+
|
|
189
|
+
func_fqn = f"{parent_fqn}.{name}" if parent_fqn else name
|
|
190
|
+
|
|
191
|
+
func_obj = PythonFunctionLike(
|
|
192
|
+
fqn=func_fqn,
|
|
193
|
+
parent_id=parent_id,
|
|
194
|
+
name=name,
|
|
195
|
+
type=type,
|
|
196
|
+
source_range=source_range,
|
|
197
|
+
docstring_range=docstring_range,
|
|
198
|
+
signature=signature,
|
|
199
|
+
annotations=decorator_details,
|
|
200
|
+
visibility=visibility_from_name(name),
|
|
201
|
+
)
|
|
202
|
+
context.entity_registry.add(func_obj)
|
|
203
|
+
return func_obj.id
|
|
204
|
+
|
|
205
|
+
def _extract_variables(self, block_node: Node, context: Context, parent_id: SymbolID | None = None, parent_fqn: str | None = None) -> list[SymbolID]:
|
|
206
|
+
variable_ids: list[SymbolID] = []
|
|
207
|
+
|
|
208
|
+
for child in iter_children(block_node):
|
|
209
|
+
if child.type == "expression_statement":
|
|
210
|
+
inner = first_child(child)
|
|
211
|
+
if inner:
|
|
212
|
+
for v in collect_from_assignment_node(node=inner, parent_fqn=parent_fqn, parent_id=parent_id, source_file_id=self._current_source_file_id):
|
|
213
|
+
context.entity_registry.add(v)
|
|
214
|
+
variable_ids.append(v.id)
|
|
215
|
+
continue
|
|
216
|
+
for v in collect_from_assignment_node(node=child, parent_fqn=parent_fqn, parent_id=parent_id, source_file_id=self._current_source_file_id):
|
|
217
|
+
context.entity_registry.add(v)
|
|
218
|
+
variable_ids.append(v.id)
|
|
219
|
+
|
|
220
|
+
return variable_ids
|
|
221
|
+
|
|
222
|
+
def _extract_all_exports(self, root: Node) -> list[str] | None:
|
|
223
|
+
cursor = QueryCursor(self.all_exports_query)
|
|
224
|
+
captures_dict = cursor.captures(root)
|
|
225
|
+
|
|
226
|
+
for name, nodes in captures_dict.items():
|
|
227
|
+
for n in nodes:
|
|
228
|
+
if name == "all_values":
|
|
229
|
+
exports = []
|
|
230
|
+
for child in iter_children(n):
|
|
231
|
+
if child.type == "string":
|
|
232
|
+
export_name = node_text(child).strip().strip('"').strip("'")
|
|
233
|
+
exports.append(export_name)
|
|
234
|
+
return exports
|
|
235
|
+
|
|
236
|
+
return None
|
|
237
|
+
|
|
238
|
+
def _process_import_statement(
|
|
239
|
+
self,
|
|
240
|
+
node: Node,
|
|
241
|
+
context: Context,
|
|
242
|
+
parent_id: SymbolID | None = None,
|
|
243
|
+
module_fqn: str | None = None
|
|
244
|
+
) -> SymbolID:
|
|
245
|
+
source_range = self._sr(node)
|
|
246
|
+
alias_ids = []
|
|
247
|
+
|
|
248
|
+
import_stmt = PythonImportStatement(
|
|
249
|
+
type="generic_import",
|
|
250
|
+
parent_id=parent_id,
|
|
251
|
+
source_range=source_range,
|
|
252
|
+
path="",
|
|
253
|
+
wildcard=False,
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
for child in iter_children(node):
|
|
257
|
+
if child.type == "dotted_name":
|
|
258
|
+
child_source_range = self._sr(child)
|
|
259
|
+
import_alias = PythonImportAlias(
|
|
260
|
+
name=node_text(child),
|
|
261
|
+
alias=None,
|
|
262
|
+
parent_id=import_stmt.id,
|
|
263
|
+
source_range=child_source_range,
|
|
264
|
+
fqn=f"{module_fqn}.{node_text(child)}" if module_fqn else None,
|
|
265
|
+
)
|
|
266
|
+
context.entity_registry.add(import_alias)
|
|
267
|
+
alias_ids.append(import_alias.id)
|
|
268
|
+
|
|
269
|
+
elif child.type == "aliased_import":
|
|
270
|
+
name_node = child.child_by_field_name("name")
|
|
271
|
+
alias_node = child.child_by_field_name("alias")
|
|
272
|
+
child_source_range = self._sr(name_node) if name_node else source_range
|
|
273
|
+
if name_node:
|
|
274
|
+
alias_name = node_text(alias_node) if alias_node else node_text(name_node)
|
|
275
|
+
import_alias = PythonImportAlias(
|
|
276
|
+
name=node_text(name_node),
|
|
277
|
+
alias=node_text(alias_node) if alias_node else None,
|
|
278
|
+
parent_id=import_stmt.id,
|
|
279
|
+
source_range=child_source_range,
|
|
280
|
+
fqn=f"{module_fqn}.{alias_name}" if module_fqn else None,
|
|
281
|
+
)
|
|
282
|
+
context.entity_registry.add(import_alias)
|
|
283
|
+
alias_ids.append(import_alias.id)
|
|
284
|
+
|
|
285
|
+
import_stmt.name_ids = alias_ids
|
|
286
|
+
context.entity_registry.add(import_stmt)
|
|
287
|
+
return import_stmt.id
|
|
288
|
+
|
|
289
|
+
def _process_import_from_statement(
|
|
290
|
+
self,
|
|
291
|
+
node: Node,
|
|
292
|
+
context: Context,
|
|
293
|
+
parent_id: SymbolID | None = None,
|
|
294
|
+
module_fqn: str | None = None
|
|
295
|
+
) -> SymbolID:
|
|
296
|
+
source_range = self._sr(node)
|
|
297
|
+
module_node = node.child_by_field_name("module_name")
|
|
298
|
+
module_name = node_text(module_node) if module_node else ""
|
|
299
|
+
|
|
300
|
+
alias_ids: list[SymbolID] = []
|
|
301
|
+
|
|
302
|
+
import_stmt = PythonImportStatement(
|
|
303
|
+
type="from_import",
|
|
304
|
+
parent_id=parent_id,
|
|
305
|
+
source_range=source_range,
|
|
306
|
+
path=module_name,
|
|
307
|
+
wildcard=False,
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
for child in iter_children(node):
|
|
311
|
+
if child.type == "wildcard_import":
|
|
312
|
+
import_stmt.wildcard = True
|
|
313
|
+
|
|
314
|
+
elif child.type == "dotted_name":
|
|
315
|
+
if child != module_node:
|
|
316
|
+
import_alias = PythonImportAlias(
|
|
317
|
+
name=node_text(child),
|
|
318
|
+
alias=None,
|
|
319
|
+
parent_id=import_stmt.id,
|
|
320
|
+
source_range=self._sr(child),
|
|
321
|
+
fqn=f"{module_fqn}.{node_text(child)}" if module_fqn else None,
|
|
322
|
+
)
|
|
323
|
+
context.entity_registry.add(import_alias)
|
|
324
|
+
alias_ids.append(import_alias.id)
|
|
325
|
+
|
|
326
|
+
elif child.type == "aliased_import":
|
|
327
|
+
name_node = child.child_by_field_name("name")
|
|
328
|
+
alias_node = child.child_by_field_name("alias")
|
|
329
|
+
child_source_range = self._sr(name_node) if name_node else source_range
|
|
330
|
+
if name_node:
|
|
331
|
+
alias_name = node_text(alias_node) if alias_node else node_text(name_node)
|
|
332
|
+
import_alias = PythonImportAlias(
|
|
333
|
+
name=node_text(name_node),
|
|
334
|
+
alias=node_text(alias_node) if alias_node else None,
|
|
335
|
+
parent_id=import_stmt.id,
|
|
336
|
+
source_range=child_source_range,
|
|
337
|
+
fqn=f"{module_fqn}.{alias_name}" if module_fqn else None,
|
|
338
|
+
)
|
|
339
|
+
context.entity_registry.add(import_alias)
|
|
340
|
+
alias_ids.append(import_alias.id)
|
|
341
|
+
|
|
342
|
+
import_stmt.name_ids = alias_ids
|
|
343
|
+
context.entity_registry.add(import_stmt)
|
|
344
|
+
return import_stmt.id
|
|
345
|
+
|
|
346
|
+
def _extract_import_statements(
|
|
347
|
+
self,
|
|
348
|
+
root: Node,
|
|
349
|
+
context: Context,
|
|
350
|
+
parent_id: SymbolID | None = None,
|
|
351
|
+
module_fqn: str | None = None
|
|
352
|
+
) -> list[SymbolID]:
|
|
353
|
+
import_stmt_ids: list[SymbolID] = []
|
|
354
|
+
|
|
355
|
+
for child in iter_children(root):
|
|
356
|
+
if child.type == "import_statement":
|
|
357
|
+
import_stmt_id = self._process_import_statement(child, context, parent_id, module_fqn)
|
|
358
|
+
import_stmt_ids.append(import_stmt_id)
|
|
359
|
+
|
|
360
|
+
elif child.type == "import_from_statement":
|
|
361
|
+
import_stmt_id = self._process_import_from_statement(child, context, parent_id, module_fqn)
|
|
362
|
+
import_stmt_ids.append(import_stmt_id)
|
|
363
|
+
|
|
364
|
+
return import_stmt_ids
|
|
365
|
+
|
|
366
|
+
def _extract_control_flow_groups(
|
|
367
|
+
self,
|
|
368
|
+
block_node: Node,
|
|
369
|
+
context: Context,
|
|
370
|
+
parent_id: SymbolID | None = None,
|
|
371
|
+
parent_fqn: str | None = None
|
|
372
|
+
) -> list[SymbolID]:
|
|
373
|
+
group_ids: list[SymbolID] = []
|
|
374
|
+
|
|
375
|
+
if not block_node:
|
|
376
|
+
return group_ids
|
|
377
|
+
|
|
378
|
+
for child in iter_children(block_node):
|
|
379
|
+
if child.type == "if_statement":
|
|
380
|
+
grp = ControlFlowGroup(
|
|
381
|
+
parent_id=parent_id,
|
|
382
|
+
group_type="if_statement",
|
|
383
|
+
source_range=self._sr(child),
|
|
384
|
+
)
|
|
385
|
+
grp.block_ids = self._process_if_node(child, context, parent_id=grp.id, parent_fqn=parent_fqn)
|
|
386
|
+
context.entity_registry.add(grp)
|
|
387
|
+
group_ids.append(grp.id)
|
|
388
|
+
|
|
389
|
+
elif child.type == "try_statement":
|
|
390
|
+
grp = ControlFlowGroup(
|
|
391
|
+
parent_id=parent_id,
|
|
392
|
+
group_type="try_statement",
|
|
393
|
+
source_range=self._sr(child),
|
|
394
|
+
)
|
|
395
|
+
grp.block_ids = self._process_try_node(child, context, parent_id=grp.id, parent_fqn=parent_fqn)
|
|
396
|
+
context.entity_registry.add(grp)
|
|
397
|
+
group_ids.append(grp.id)
|
|
398
|
+
|
|
399
|
+
return group_ids
|
|
400
|
+
|
|
401
|
+
def _process_if_node(self, node: Node, context: Context, parent_id: SymbolID | None = None, parent_fqn: str | None = None) -> list[SymbolID]:
|
|
402
|
+
result_ids: list[SymbolID] = []
|
|
403
|
+
source_range = self._sr(node)
|
|
404
|
+
|
|
405
|
+
condition_node = node.child_by_field_name("condition")
|
|
406
|
+
condition_range = self._sr(condition_node) if condition_node else None
|
|
407
|
+
|
|
408
|
+
consequence_node = node.child_by_field_name("consequence")
|
|
409
|
+
if consequence_node:
|
|
410
|
+
blk = ControlFlowBlock(
|
|
411
|
+
parent_id=parent_id,
|
|
412
|
+
branch="if",
|
|
413
|
+
source_range=source_range,
|
|
414
|
+
condition_range=condition_range,
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
blk.import_stmt_ids = self._extract_import_statements(consequence_node, context, parent_id=blk.id, module_fqn=parent_fqn)
|
|
418
|
+
blk.type_ids = self._traverse_block_for_classes(consequence_node, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
419
|
+
blk.function_ids = self._extract_functions(consequence_node, context, type="FUNCTION", parent_id=blk.id, parent_fqn=parent_fqn)
|
|
420
|
+
blk.variable_ids = self._extract_variables(consequence_node, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
421
|
+
blk.nested_group_ids = self._extract_control_flow_groups(consequence_node, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
422
|
+
|
|
423
|
+
context.entity_registry.add(blk)
|
|
424
|
+
result_ids.append(blk.id)
|
|
425
|
+
|
|
426
|
+
for child in iter_children(node):
|
|
427
|
+
if child.type == "elif_clause":
|
|
428
|
+
alt_cond_node = child.child_by_field_name("condition")
|
|
429
|
+
alt_cond_range = self._sr(alt_cond_node) if alt_cond_node else None
|
|
430
|
+
alt_body = child.child_by_field_name("consequence")
|
|
431
|
+
if alt_body:
|
|
432
|
+
blk = ControlFlowBlock(
|
|
433
|
+
parent_id=parent_id,
|
|
434
|
+
branch="elif",
|
|
435
|
+
source_range=self._sr(child),
|
|
436
|
+
condition_range=alt_cond_range,
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
blk.import_stmt_ids = self._extract_import_statements(alt_body, context, parent_id=blk.id, module_fqn=parent_fqn)
|
|
440
|
+
blk.type_ids = self._traverse_block_for_classes(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
441
|
+
blk.function_ids = self._extract_functions(alt_body, context, type="FUNCTION", parent_id=blk.id, parent_fqn=parent_fqn)
|
|
442
|
+
blk.variable_ids = self._extract_variables(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
443
|
+
blk.nested_group_ids = self._extract_control_flow_groups(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
444
|
+
|
|
445
|
+
context.entity_registry.add(blk)
|
|
446
|
+
result_ids.append(blk.id)
|
|
447
|
+
|
|
448
|
+
elif child.type == "else_clause":
|
|
449
|
+
alt_body = child.child_by_field_name("body")
|
|
450
|
+
if alt_body:
|
|
451
|
+
blk = ControlFlowBlock(
|
|
452
|
+
parent_id=parent_id,
|
|
453
|
+
branch="else",
|
|
454
|
+
source_range=self._sr(child),
|
|
455
|
+
condition_range=None,
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
blk.import_stmt_ids = self._extract_import_statements(alt_body, context, parent_id=blk.id, module_fqn=parent_fqn)
|
|
459
|
+
blk.type_ids = self._traverse_block_for_classes(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
460
|
+
blk.function_ids = self._extract_functions(alt_body, context, type="FUNCTION", parent_id=blk.id, parent_fqn=parent_fqn)
|
|
461
|
+
blk.variable_ids = self._extract_variables(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
462
|
+
blk.nested_group_ids = self._extract_control_flow_groups(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
463
|
+
|
|
464
|
+
context.entity_registry.add(blk)
|
|
465
|
+
result_ids.append(blk.id)
|
|
466
|
+
|
|
467
|
+
return result_ids
|
|
468
|
+
|
|
469
|
+
def _process_try_node(self, node: Node, context: Context, parent_id: SymbolID | None = None, parent_fqn: str | None = None) -> list[SymbolID]:
|
|
470
|
+
result_ids: list[SymbolID] = []
|
|
471
|
+
source_range = self._sr(node)
|
|
472
|
+
body_node = node.child_by_field_name("body")
|
|
473
|
+
if body_node:
|
|
474
|
+
blk = ControlFlowBlock(
|
|
475
|
+
parent_id=parent_id,
|
|
476
|
+
branch="try",
|
|
477
|
+
source_range=source_range,
|
|
478
|
+
condition_range=None,
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
blk.import_stmt_ids = self._extract_import_statements(body_node, context, parent_id=blk.id, module_fqn=parent_fqn)
|
|
482
|
+
blk.type_ids = self._traverse_block_for_classes(body_node, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
483
|
+
blk.function_ids = self._extract_functions(body_node, context, type="FUNCTION", parent_id=blk.id, parent_fqn=parent_fqn)
|
|
484
|
+
blk.variable_ids = self._extract_variables(body_node, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
485
|
+
blk.nested_group_ids = self._extract_control_flow_groups(body_node, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
486
|
+
|
|
487
|
+
context.entity_registry.add(blk)
|
|
488
|
+
result_ids.append(blk.id)
|
|
489
|
+
|
|
490
|
+
for child in iter_children(node):
|
|
491
|
+
if child.type in ("except_clause", "except_group_clause"):
|
|
492
|
+
body = child.child_by_field_name("body")
|
|
493
|
+
if body:
|
|
494
|
+
condition_range = self._sr(child.child_by_field_name("condition")) if child.child_by_field_name("condition") else None
|
|
495
|
+
blk = ControlFlowBlock(
|
|
496
|
+
parent_id=parent_id,
|
|
497
|
+
branch="except",
|
|
498
|
+
source_range=self._sr(child),
|
|
499
|
+
condition_range=condition_range,
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
blk.import_stmt_ids = self._extract_import_statements(body, context, parent_id=blk.id, module_fqn=parent_fqn)
|
|
503
|
+
blk.type_ids = self._traverse_block_for_classes(body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
504
|
+
blk.function_ids = self._extract_functions(body, context, type="FUNCTION", parent_id=blk.id, parent_fqn=parent_fqn)
|
|
505
|
+
blk.variable_ids = self._extract_variables(body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
506
|
+
blk.nested_group_ids = self._extract_control_flow_groups(body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
507
|
+
|
|
508
|
+
context.entity_registry.add(blk)
|
|
509
|
+
result_ids.append(blk.id)
|
|
510
|
+
|
|
511
|
+
elif child.type == "else_clause":
|
|
512
|
+
alt_body = child.child_by_field_name("body")
|
|
513
|
+
if alt_body:
|
|
514
|
+
blk = ControlFlowBlock(
|
|
515
|
+
parent_id=parent_id,
|
|
516
|
+
branch="else",
|
|
517
|
+
source_range=self._sr(child),
|
|
518
|
+
condition_range=None,
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
blk.import_stmt_ids = self._extract_import_statements(alt_body, context, parent_id=blk.id, module_fqn=parent_fqn)
|
|
522
|
+
blk.type_ids = self._traverse_block_for_classes(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
523
|
+
blk.function_ids = self._extract_functions(alt_body, context, type="FUNCTION", parent_id=blk.id, parent_fqn=parent_fqn)
|
|
524
|
+
blk.variable_ids = self._extract_variables(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
525
|
+
blk.nested_group_ids = self._extract_control_flow_groups(alt_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
526
|
+
|
|
527
|
+
context.entity_registry.add(blk)
|
|
528
|
+
result_ids.append(blk.id)
|
|
529
|
+
|
|
530
|
+
elif child.type == "finally_clause":
|
|
531
|
+
fin_body = None
|
|
532
|
+
for cc in iter_children(child):
|
|
533
|
+
if cc.type == "block":
|
|
534
|
+
fin_body = cc
|
|
535
|
+
if fin_body:
|
|
536
|
+
blk = ControlFlowBlock(
|
|
537
|
+
parent_id=parent_id,
|
|
538
|
+
branch="finally",
|
|
539
|
+
source_range=self._sr(child),
|
|
540
|
+
condition_range=None,
|
|
541
|
+
)
|
|
542
|
+
|
|
543
|
+
blk.import_stmt_ids = self._extract_import_statements(fin_body, context, parent_id=blk.id, module_fqn=parent_fqn)
|
|
544
|
+
blk.type_ids = self._traverse_block_for_classes(fin_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
545
|
+
blk.function_ids = self._extract_functions(fin_body, context, type="FUNCTION", parent_id=blk.id, parent_fqn=parent_fqn)
|
|
546
|
+
blk.variable_ids = self._extract_variables(fin_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
547
|
+
blk.nested_group_ids = self._extract_control_flow_groups(fin_body, context, parent_id=blk.id, parent_fqn=parent_fqn)
|
|
548
|
+
|
|
549
|
+
context.entity_registry.add(blk)
|
|
550
|
+
result_ids.append(blk.id)
|
|
551
|
+
|
|
552
|
+
return result_ids
|