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,70 @@
|
|
|
1
|
+
from deproc.core.interfaces.parser.models import (
|
|
2
|
+
Annotation,
|
|
3
|
+
ComplexBinding,
|
|
4
|
+
ControlFlowBlock,
|
|
5
|
+
ControlFlowGroup,
|
|
6
|
+
Entity,
|
|
7
|
+
FunctionLike,
|
|
8
|
+
ImportStatement,
|
|
9
|
+
Signature,
|
|
10
|
+
SimpleBinding,
|
|
11
|
+
SourceFile,
|
|
12
|
+
SourceRange,
|
|
13
|
+
SymbolID,
|
|
14
|
+
TypeDefinition,
|
|
15
|
+
VariableDeclaration,
|
|
16
|
+
)
|
|
17
|
+
from dataclasses import dataclass, field
|
|
18
|
+
|
|
19
|
+
@dataclass(kw_only=True)
|
|
20
|
+
class PythonFunctionLike(FunctionLike):
|
|
21
|
+
annotations: list[Annotation]
|
|
22
|
+
visibility: str
|
|
23
|
+
|
|
24
|
+
@dataclass(kw_only=True)
|
|
25
|
+
class PythonConstant(VariableDeclaration):
|
|
26
|
+
type: str = field(default="CONSTANT")
|
|
27
|
+
|
|
28
|
+
@dataclass(kw_only=True)
|
|
29
|
+
class PythonTypeAlias(VariableDeclaration):
|
|
30
|
+
type: str = field(default="TYPE_ALIAS")
|
|
31
|
+
|
|
32
|
+
@dataclass(kw_only=True)
|
|
33
|
+
class PythonClass(TypeDefinition):
|
|
34
|
+
type: str = field(default="CLASS")
|
|
35
|
+
|
|
36
|
+
@dataclass(kw_only=True)
|
|
37
|
+
class PythonImportAlias(Entity):
|
|
38
|
+
name: str
|
|
39
|
+
alias: str | None
|
|
40
|
+
source_range: SourceRange
|
|
41
|
+
fqn: str | None = None
|
|
42
|
+
|
|
43
|
+
@dataclass(kw_only=True)
|
|
44
|
+
class PythonImportStatement(ImportStatement):
|
|
45
|
+
path: str
|
|
46
|
+
name_ids: list[SymbolID] = field(default_factory=list)
|
|
47
|
+
wildcard: bool = False
|
|
48
|
+
|
|
49
|
+
@dataclass(kw_only=True)
|
|
50
|
+
class PythonModule(SourceFile):
|
|
51
|
+
fqn: str
|
|
52
|
+
all_exports: list[str] | None = None
|
|
53
|
+
|
|
54
|
+
__all__ = [
|
|
55
|
+
"ComplexBinding",
|
|
56
|
+
"ControlFlowBlock",
|
|
57
|
+
"ControlFlowGroup",
|
|
58
|
+
"ImportStatement",
|
|
59
|
+
"PythonClass",
|
|
60
|
+
"PythonConstant",
|
|
61
|
+
"PythonFunctionLike",
|
|
62
|
+
"PythonModule",
|
|
63
|
+
"PythonTypeAlias",
|
|
64
|
+
"SimpleBinding",
|
|
65
|
+
"Signature",
|
|
66
|
+
"SourceFile",
|
|
67
|
+
"SourceRange",
|
|
68
|
+
"SymbolID",
|
|
69
|
+
"VariableDeclaration",
|
|
70
|
+
]
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
from ..models import (
|
|
2
|
+
Annotation,
|
|
3
|
+
ComplexBinding,
|
|
4
|
+
PythonConstant,
|
|
5
|
+
PythonTypeAlias,
|
|
6
|
+
SimpleBinding,
|
|
7
|
+
Signature,
|
|
8
|
+
SourceRange,
|
|
9
|
+
SymbolID,
|
|
10
|
+
VariableDeclaration,
|
|
11
|
+
)
|
|
12
|
+
from .tree_sitter_python import (
|
|
13
|
+
node_text,
|
|
14
|
+
create_source_range
|
|
15
|
+
)
|
|
16
|
+
from tree_sitter import Node
|
|
17
|
+
from deproc.utils import iter_children, first_child
|
|
18
|
+
|
|
19
|
+
def extract_docstring_range(node: Node, source_file_id: str | None = None) -> SourceRange | None:
|
|
20
|
+
if node.type == "module":
|
|
21
|
+
body = node
|
|
22
|
+
elif node.type in ("class_definition", "function_definition"):
|
|
23
|
+
body = node.child_by_field_name("body")
|
|
24
|
+
else:
|
|
25
|
+
return None
|
|
26
|
+
|
|
27
|
+
if not body:
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
for child in iter_children(body):
|
|
31
|
+
if child.type == "expression_statement":
|
|
32
|
+
first_child_node = first_child(child)
|
|
33
|
+
if first_child_node and first_child_node.type == "string":
|
|
34
|
+
return create_source_range(first_child_node, source_id=source_file_id)
|
|
35
|
+
elif child.type == "comment":
|
|
36
|
+
continue
|
|
37
|
+
else:
|
|
38
|
+
break
|
|
39
|
+
return None
|
|
40
|
+
|
|
41
|
+
def extract_decorator_details(decorated_node: Node, source_file_id: str | None = None) -> list[Annotation]:
|
|
42
|
+
decorators: list[Annotation] = []
|
|
43
|
+
for child in iter_children(decorated_node):
|
|
44
|
+
if child.type == "decorator":
|
|
45
|
+
source_range = create_source_range(child, source_id=source_file_id)
|
|
46
|
+
decorators.append(
|
|
47
|
+
Annotation(
|
|
48
|
+
name=node_text(child).strip(),
|
|
49
|
+
source_range=source_range
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
return decorators
|
|
53
|
+
|
|
54
|
+
def extract_signature(function_node: Node, source_file_id: str | None = None) -> Signature:
|
|
55
|
+
params_node = function_node.child_by_field_name("parameters")
|
|
56
|
+
arguments_range = create_source_range(params_node, source_id=source_file_id) if params_node else None
|
|
57
|
+
|
|
58
|
+
return_node = function_node.child_by_field_name("return_type")
|
|
59
|
+
return_type_range = create_source_range(return_node, source_id=source_file_id) if return_node else None
|
|
60
|
+
|
|
61
|
+
# Preferably use colon as end of signature
|
|
62
|
+
colon_node = None
|
|
63
|
+
for child in function_node.children:
|
|
64
|
+
if child.type == ":":
|
|
65
|
+
colon_node = child
|
|
66
|
+
break
|
|
67
|
+
|
|
68
|
+
end_point = colon_node.end_point if colon_node else (return_node.end_point if return_node else (params_node.end_point if params_node else function_node.start_point))
|
|
69
|
+
signature_range = SourceRange(
|
|
70
|
+
lineno=function_node.start_point.row + 1,
|
|
71
|
+
end_lineno=end_point.row + 1,
|
|
72
|
+
col_offset=function_node.start_point.column,
|
|
73
|
+
end_col_offset=end_point.column,
|
|
74
|
+
source_id=source_file_id,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
return Signature(
|
|
78
|
+
signature_range=signature_range,
|
|
79
|
+
arguments_range=arguments_range,
|
|
80
|
+
return_type_range=return_type_range
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def _select_variable_declaration_type(name: str) -> type[VariableDeclaration]:
|
|
84
|
+
if name.isupper() and len(name) > 0:
|
|
85
|
+
return PythonConstant
|
|
86
|
+
return VariableDeclaration
|
|
87
|
+
|
|
88
|
+
def collect_from_assignment_node(
|
|
89
|
+
node: Node,
|
|
90
|
+
parent_fqn: str,
|
|
91
|
+
parent_id: SymbolID,
|
|
92
|
+
source_file_id: str | None = None,
|
|
93
|
+
) -> list[VariableDeclaration]:
|
|
94
|
+
variables: list[VariableDeclaration] = []
|
|
95
|
+
|
|
96
|
+
source_range = create_source_range(node, source_id=source_file_id)
|
|
97
|
+
|
|
98
|
+
left = node.child_by_field_name("left")
|
|
99
|
+
name = node_text(left)
|
|
100
|
+
left_range = create_source_range(left, source_id=source_file_id) if left else None
|
|
101
|
+
|
|
102
|
+
if node.type == "assignment":
|
|
103
|
+
if left and left.type in ("pattern_list", "tuple_pattern"):
|
|
104
|
+
right = node.child_by_field_name("right")
|
|
105
|
+
value_range = create_source_range(right, source_id=source_file_id) if right else None
|
|
106
|
+
variable_binding = ComplexBinding(
|
|
107
|
+
source_range=left_range,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
variable_declaration_type = _select_variable_declaration_type(name)
|
|
111
|
+
variables.append(
|
|
112
|
+
variable_declaration_type(
|
|
113
|
+
parent_id=parent_id,
|
|
114
|
+
variable_binding=variable_binding,
|
|
115
|
+
source_range=source_range,
|
|
116
|
+
value_range=value_range,
|
|
117
|
+
type_annotation=None,
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
elif left and left.type == "identifier":
|
|
122
|
+
variable_binding = SimpleBinding(
|
|
123
|
+
name=name,
|
|
124
|
+
fqn=f"{parent_fqn}.{name}"
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
right = node.child_by_field_name("right")
|
|
128
|
+
value_range = create_source_range(right, source_id=source_file_id) if right else None
|
|
129
|
+
|
|
130
|
+
type_node = node.child_by_field_name("type")
|
|
131
|
+
type_range = create_source_range(type_node, source_id=source_file_id) if type_node else None
|
|
132
|
+
|
|
133
|
+
variable_declaration_type = _select_variable_declaration_type(name)
|
|
134
|
+
variables.append(
|
|
135
|
+
variable_declaration_type(
|
|
136
|
+
parent_id=parent_id,
|
|
137
|
+
type_annotation=type_range,
|
|
138
|
+
variable_binding=variable_binding,
|
|
139
|
+
source_range=source_range,
|
|
140
|
+
value_range=value_range,
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
elif node.type == "annotated_assignment":
|
|
145
|
+
if left and left.type == "identifier":
|
|
146
|
+
variable_binding = SimpleBinding(
|
|
147
|
+
name=name,
|
|
148
|
+
fqn=f"{parent_fqn}.{name}"
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
value_node = node.child_by_field_name("right")
|
|
152
|
+
value_range = create_source_range(value_node, source_id=source_file_id) if value_node else None
|
|
153
|
+
|
|
154
|
+
type_node = node.child_by_field_name("type")
|
|
155
|
+
type_range = create_source_range(type_node, source_id=source_file_id) if type_node else None
|
|
156
|
+
|
|
157
|
+
variable_declaration_type = _select_variable_declaration_type(name)
|
|
158
|
+
variables.append(
|
|
159
|
+
variable_declaration_type(
|
|
160
|
+
parent_id=parent_id,
|
|
161
|
+
variable_binding=variable_binding,
|
|
162
|
+
source_range=source_range,
|
|
163
|
+
value_range=value_range,
|
|
164
|
+
type_annotation=type_range,
|
|
165
|
+
)
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
elif node.type == "type_alias_statement":
|
|
169
|
+
name_node = node.child_by_field_name("name")
|
|
170
|
+
value_node = node.child_by_field_name("value")
|
|
171
|
+
if name_node:
|
|
172
|
+
name = node_text(name_node)
|
|
173
|
+
variable_binding = SimpleBinding(
|
|
174
|
+
name=name,
|
|
175
|
+
fqn=f"{parent_fqn}.{name}"
|
|
176
|
+
)
|
|
177
|
+
value_range = create_source_range(value_node, source_id=source_file_id) if value_node else None
|
|
178
|
+
|
|
179
|
+
variables.append(
|
|
180
|
+
PythonTypeAlias(
|
|
181
|
+
parent_id=parent_id,
|
|
182
|
+
variable_binding=variable_binding,
|
|
183
|
+
source_range=source_range,
|
|
184
|
+
value_range=value_range,
|
|
185
|
+
type_annotation=None,
|
|
186
|
+
)
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
return variables
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from tree_sitter import (
|
|
2
|
+
Language,
|
|
3
|
+
Node,
|
|
4
|
+
Parser
|
|
5
|
+
)
|
|
6
|
+
from ..models import SourceRange
|
|
7
|
+
import tree_sitter_python
|
|
8
|
+
|
|
9
|
+
def get_python_language() -> Language:
|
|
10
|
+
return Language(tree_sitter_python.language())
|
|
11
|
+
|
|
12
|
+
def get_python_parser() -> Parser:
|
|
13
|
+
PY_LANGUAGE = get_python_language()
|
|
14
|
+
parser = Parser(PY_LANGUAGE)
|
|
15
|
+
return parser
|
|
16
|
+
|
|
17
|
+
def node_text(node: Node) -> str:
|
|
18
|
+
if not node:
|
|
19
|
+
return ""
|
|
20
|
+
return node.text.decode("utf-8")
|
|
21
|
+
|
|
22
|
+
def create_source_range(node: Node, source_id: str | None = None) -> SourceRange:
|
|
23
|
+
return SourceRange(
|
|
24
|
+
lineno=node.start_point.row + 1,
|
|
25
|
+
end_lineno=node.end_point.row + 1,
|
|
26
|
+
col_offset=node.start_point.column,
|
|
27
|
+
end_col_offset=node.end_point.column,
|
|
28
|
+
source_id=source_id,
|
|
29
|
+
)
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
from deproc.core.interfaces.parser.models import Entity
|
|
2
|
+
from deproc.core.interfaces.resolver import Resolver
|
|
3
|
+
from deproc.core.context import Context
|
|
4
|
+
from .models import (
|
|
5
|
+
PythonResolverResult
|
|
6
|
+
)
|
|
7
|
+
from .models import (
|
|
8
|
+
ResolvedIDs,
|
|
9
|
+
UnresolvedIDs
|
|
10
|
+
)
|
|
11
|
+
from ..symbol_cache import PythonSymbolCache
|
|
12
|
+
from ..parser.models import (
|
|
13
|
+
PythonModule,
|
|
14
|
+
PythonImportAlias,
|
|
15
|
+
SymbolID,
|
|
16
|
+
)
|
|
17
|
+
import logging
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
class PythonResolver(Resolver[PythonResolverResult]):
|
|
22
|
+
def _get_symbol(
|
|
23
|
+
self,
|
|
24
|
+
symbol_id: SymbolID,
|
|
25
|
+
context: Context
|
|
26
|
+
) -> Entity | None:
|
|
27
|
+
symbol = context.entity_registry.get(symbol_id)
|
|
28
|
+
if not symbol:
|
|
29
|
+
logger.warning(f"Symbol not found for ID: {symbol_id}")
|
|
30
|
+
return None
|
|
31
|
+
return symbol
|
|
32
|
+
|
|
33
|
+
def _get_module(
|
|
34
|
+
self,
|
|
35
|
+
symbol_id: SymbolID,
|
|
36
|
+
context: Context
|
|
37
|
+
) -> PythonModule | None:
|
|
38
|
+
symbol = self._get_symbol(symbol_id, context)
|
|
39
|
+
if not symbol:
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
if isinstance(symbol, PythonModule):
|
|
43
|
+
return symbol
|
|
44
|
+
|
|
45
|
+
parent_id = getattr(symbol, "parent_id", None)
|
|
46
|
+
if not parent_id:
|
|
47
|
+
return None
|
|
48
|
+
|
|
49
|
+
return self._get_module(parent_id, context)
|
|
50
|
+
|
|
51
|
+
def _get_target_module_fqn(
|
|
52
|
+
self,
|
|
53
|
+
import_statement_id: SymbolID,
|
|
54
|
+
context: Context
|
|
55
|
+
) -> str | None:
|
|
56
|
+
import_statement = self._get_symbol(import_statement_id, context)
|
|
57
|
+
if not import_statement:
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
path = getattr(import_statement, "path", None)
|
|
61
|
+
|
|
62
|
+
# Handle relative imports
|
|
63
|
+
if path and path.startswith("."):
|
|
64
|
+
parent_module = self._get_module(import_statement_id, context)
|
|
65
|
+
if parent_module:
|
|
66
|
+
parent_fqn = parent_module.fqn
|
|
67
|
+
if parent_fqn:
|
|
68
|
+
is_package = getattr(parent_module, "path", "").endswith(("__init__.py", "__init__.pyi"))
|
|
69
|
+
relative_parts = path.split(".")
|
|
70
|
+
parent_parts = parent_fqn.split(".")
|
|
71
|
+
num_leading_dots = len(path) - len(path.lstrip("."))
|
|
72
|
+
levels_to_pop = num_leading_dots - (1 if is_package else 0)
|
|
73
|
+
for _ in range(levels_to_pop):
|
|
74
|
+
if parent_parts:
|
|
75
|
+
parent_parts.pop()
|
|
76
|
+
relative_parts = [p for p in relative_parts if p]
|
|
77
|
+
target_fqn = ".".join(parent_parts + relative_parts)
|
|
78
|
+
return target_fqn
|
|
79
|
+
|
|
80
|
+
return path
|
|
81
|
+
|
|
82
|
+
def _extract_alias_ids(
|
|
83
|
+
self,
|
|
84
|
+
symbol_ids: set[SymbolID],
|
|
85
|
+
context: Context
|
|
86
|
+
) -> tuple[ResolvedIDs, UnresolvedIDs]:
|
|
87
|
+
alias_ids = set()
|
|
88
|
+
non_alias_ids = set()
|
|
89
|
+
|
|
90
|
+
for symbol_id in symbol_ids:
|
|
91
|
+
symbol = self._get_symbol(symbol_id, context)
|
|
92
|
+
if isinstance(symbol, PythonImportAlias):
|
|
93
|
+
alias_ids.add(symbol_id)
|
|
94
|
+
else:
|
|
95
|
+
non_alias_ids.add(symbol_id)
|
|
96
|
+
|
|
97
|
+
return alias_ids, non_alias_ids
|
|
98
|
+
|
|
99
|
+
def _populate_cache(
|
|
100
|
+
self,
|
|
101
|
+
module_fqn: str,
|
|
102
|
+
symbol_name: str,
|
|
103
|
+
resolved_ids: list[SymbolID],
|
|
104
|
+
unresolved_ids: list[SymbolID],
|
|
105
|
+
cache: PythonSymbolCache
|
|
106
|
+
):
|
|
107
|
+
if cache:
|
|
108
|
+
cache.set(module_fqn, symbol_name, resolved_ids, unresolved_ids)
|
|
109
|
+
|
|
110
|
+
def _populate_module_cache_key_maps(
|
|
111
|
+
self,
|
|
112
|
+
module_fqn: str,
|
|
113
|
+
cache_keys: set[tuple[str, str]],
|
|
114
|
+
cache: PythonSymbolCache
|
|
115
|
+
):
|
|
116
|
+
if cache:
|
|
117
|
+
cache.add_cache_keys_for_module(module_fqn, cache_keys)
|
|
118
|
+
|
|
119
|
+
def _populate_module_cache_key_maps_for_cached_result(
|
|
120
|
+
self,
|
|
121
|
+
module_fqn: str,
|
|
122
|
+
symbol_name: str,
|
|
123
|
+
cache_keys: set[tuple[str, str]],
|
|
124
|
+
cache: PythonSymbolCache
|
|
125
|
+
):
|
|
126
|
+
if not cache_keys:
|
|
127
|
+
return
|
|
128
|
+
|
|
129
|
+
current_cache_key = (module_fqn, symbol_name)
|
|
130
|
+
module_fqns = cache.get_modules_for_cache_key(current_cache_key)
|
|
131
|
+
|
|
132
|
+
for cache_key in cache_keys:
|
|
133
|
+
cache.add_modules_for_cache_key(cache_key, module_fqns)
|
|
134
|
+
|
|
135
|
+
def get_ids_by_fqn(
|
|
136
|
+
self,
|
|
137
|
+
module_fqn: str,
|
|
138
|
+
symbol_name: str,
|
|
139
|
+
context: Context
|
|
140
|
+
) -> set[SymbolID]:
|
|
141
|
+
return context.entity_registry.get_ids_by_fqn(f"{module_fqn}.{symbol_name}")
|
|
142
|
+
|
|
143
|
+
def resolve_symbol(
|
|
144
|
+
self,
|
|
145
|
+
module_fqn: str,
|
|
146
|
+
symbol_name: str,
|
|
147
|
+
context: Context,
|
|
148
|
+
visited: set[SymbolID] | None = None,
|
|
149
|
+
cache_keys: set[tuple[str, str]] | None = None
|
|
150
|
+
) -> tuple[ResolvedIDs, UnresolvedIDs]:
|
|
151
|
+
symbol_cache: PythonSymbolCache = context.get_symbol_cache("python")
|
|
152
|
+
if symbol_cache:
|
|
153
|
+
cached_result = symbol_cache.get(module_fqn, symbol_name)
|
|
154
|
+
if cached_result is not None:
|
|
155
|
+
self._populate_module_cache_key_maps_for_cached_result(module_fqn, symbol_name, cache_keys, symbol_cache)
|
|
156
|
+
return cached_result
|
|
157
|
+
|
|
158
|
+
resolved_ids = self.get_ids_by_fqn(module_fqn, symbol_name, context)
|
|
159
|
+
if not resolved_ids:
|
|
160
|
+
logger.warning(f"Symbols not found for module: {module_fqn}, symbol: {symbol_name}, caching empty sets")
|
|
161
|
+
self._populate_cache(module_fqn, symbol_name, set(), set(), symbol_cache)
|
|
162
|
+
self._populate_module_cache_key_maps(module_fqn, cache_keys or set(), symbol_cache)
|
|
163
|
+
return set(), set()
|
|
164
|
+
|
|
165
|
+
found_alias_ids, resolved_ids = self._extract_alias_ids(resolved_ids, context)
|
|
166
|
+
unresolved_ids = set()
|
|
167
|
+
|
|
168
|
+
if cache_keys is None:
|
|
169
|
+
cache_keys = set()
|
|
170
|
+
cache_keys.add((module_fqn, symbol_name))
|
|
171
|
+
|
|
172
|
+
if found_alias_ids:
|
|
173
|
+
resolved_alias_ids, unresolved_ids = self.resolve_alias_ids(found_alias_ids, context, visited, cache_keys)
|
|
174
|
+
resolved_ids.update(resolved_alias_ids)
|
|
175
|
+
|
|
176
|
+
self._populate_cache(module_fqn, symbol_name, resolved_ids, unresolved_ids, symbol_cache)
|
|
177
|
+
self._populate_module_cache_key_maps(module_fqn, cache_keys, symbol_cache)
|
|
178
|
+
|
|
179
|
+
return resolved_ids, unresolved_ids
|
|
180
|
+
|
|
181
|
+
def resolve_alias_ids(
|
|
182
|
+
self,
|
|
183
|
+
alias_ids: set[SymbolID],
|
|
184
|
+
context: Context,
|
|
185
|
+
visited: set[SymbolID] | None = None,
|
|
186
|
+
cache_keys: set[tuple[str, str]] | None = None
|
|
187
|
+
) -> tuple[ResolvedIDs, UnresolvedIDs]:
|
|
188
|
+
if visited is None:
|
|
189
|
+
visited = set()
|
|
190
|
+
|
|
191
|
+
if not alias_ids:
|
|
192
|
+
return set(), set()
|
|
193
|
+
|
|
194
|
+
resolved_ids = set()
|
|
195
|
+
unresolved_ids = set()
|
|
196
|
+
for symbol_id in alias_ids:
|
|
197
|
+
# Handle worst case circular import scenario explicitly
|
|
198
|
+
if symbol_id in visited:
|
|
199
|
+
continue
|
|
200
|
+
visited.add(symbol_id)
|
|
201
|
+
|
|
202
|
+
symbol = self._get_symbol(symbol_id, context)
|
|
203
|
+
if isinstance(symbol, PythonImportAlias):
|
|
204
|
+
resolved_ids_from_alias, unresolved_ids_from_alias = self.resolve_alias(symbol, context, visited, cache_keys)
|
|
205
|
+
if resolved_ids_from_alias:
|
|
206
|
+
resolved_ids.update(resolved_ids_from_alias)
|
|
207
|
+
unresolved_ids.update(unresolved_ids_from_alias)
|
|
208
|
+
else:
|
|
209
|
+
unresolved_ids.add(symbol_id)
|
|
210
|
+
else:
|
|
211
|
+
resolved_ids.add(symbol_id)
|
|
212
|
+
|
|
213
|
+
return resolved_ids, unresolved_ids
|
|
214
|
+
|
|
215
|
+
def resolve_alias(
|
|
216
|
+
self,
|
|
217
|
+
alias: PythonImportAlias,
|
|
218
|
+
context: Context,
|
|
219
|
+
visited: set[SymbolID] | None = None,
|
|
220
|
+
cache_keys: set[tuple[str, str]] | None = None
|
|
221
|
+
) -> tuple[ResolvedIDs, UnresolvedIDs]:
|
|
222
|
+
import_statement_id = alias.parent_id
|
|
223
|
+
|
|
224
|
+
import_name = alias.name
|
|
225
|
+
target_module_path = self._get_target_module_fqn(import_statement_id, context)
|
|
226
|
+
|
|
227
|
+
if not target_module_path:
|
|
228
|
+
logger.warning(f"Target module path not found for alias: {alias.name}")
|
|
229
|
+
return set(), set()
|
|
230
|
+
|
|
231
|
+
return self.resolve_symbol(target_module_path, import_name, context, visited, cache_keys)
|
|
232
|
+
|
|
233
|
+
def resolve(self, module_fqn: str, symbol_name: str, context: Context) -> PythonResolverResult:
|
|
234
|
+
resolved_ids, unresolved_ids = self.resolve_symbol(module_fqn, symbol_name, context)
|
|
235
|
+
return PythonResolverResult(resolved_ids=resolved_ids, unresolved_ids=unresolved_ids)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from deproc.core.interfaces.parser.models import SymbolID
|
|
3
|
+
|
|
4
|
+
type ResolvedIDs = set[SymbolID]
|
|
5
|
+
type UnresolvedIDs = set[SymbolID]
|
|
6
|
+
|
|
7
|
+
@dataclass
|
|
8
|
+
class PythonResolverResult:
|
|
9
|
+
resolved_ids: ResolvedIDs
|
|
10
|
+
unresolved_ids: UnresolvedIDs
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from deproc.core.interfaces.symbol_cache import SymbolCache
|
|
3
|
+
from .models import (
|
|
4
|
+
cache_key,
|
|
5
|
+
cache_value,
|
|
6
|
+
module_fqn,
|
|
7
|
+
symbol_name,
|
|
8
|
+
ResolvedIDs,
|
|
9
|
+
UnresolvedIDs
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
class PythonSymbolCache(SymbolCache[cache_key, cache_value]):
|
|
13
|
+
def __init__(self):
|
|
14
|
+
self.language = "python"
|
|
15
|
+
self.cache: dict[cache_key, cache_value] = {}
|
|
16
|
+
self.module_to_cache_keys: dict[module_fqn, set[cache_key]] = {}
|
|
17
|
+
self.cache_key_to_modules: dict[cache_key, set[module_fqn]] = {}
|
|
18
|
+
|
|
19
|
+
def get(self, module_fqn: module_fqn, symbol_name: symbol_name) -> cache_value | None:
|
|
20
|
+
key: cache_key = (module_fqn, symbol_name)
|
|
21
|
+
return self.cache.get(key)
|
|
22
|
+
|
|
23
|
+
def set(self, module_fqn: module_fqn, symbol_name: symbol_name, resolved_ids: ResolvedIDs, unresolved_ids: UnresolvedIDs) -> None:
|
|
24
|
+
key: cache_key = (module_fqn, symbol_name)
|
|
25
|
+
self.cache[key] = (resolved_ids, unresolved_ids)
|
|
26
|
+
|
|
27
|
+
def _link(self, module_fqn: module_fqn, key: cache_key) -> None:
|
|
28
|
+
if module_fqn not in self.module_to_cache_keys:
|
|
29
|
+
self.module_to_cache_keys[module_fqn] = set()
|
|
30
|
+
self.module_to_cache_keys[module_fqn].add(key)
|
|
31
|
+
|
|
32
|
+
if key not in self.cache_key_to_modules:
|
|
33
|
+
self.cache_key_to_modules[key] = set()
|
|
34
|
+
self.cache_key_to_modules[key].add(module_fqn)
|
|
35
|
+
|
|
36
|
+
def add_cache_keys_for_module(self, module_fqn: module_fqn, keys: set[cache_key]) -> None:
|
|
37
|
+
for key in keys:
|
|
38
|
+
self._link(module_fqn, key)
|
|
39
|
+
|
|
40
|
+
def get_cache_keys_for_module(self, module_fqn: module_fqn) -> set[cache_key]:
|
|
41
|
+
return self.module_to_cache_keys.get(module_fqn, set())
|
|
42
|
+
|
|
43
|
+
def add_modules_for_cache_key(self, key: cache_key, modules: set[module_fqn]) -> None:
|
|
44
|
+
for module_fqn in modules:
|
|
45
|
+
self._link(module_fqn, key)
|
|
46
|
+
|
|
47
|
+
def get_modules_for_cache_key(self, key: cache_key) -> set[module_fqn]:
|
|
48
|
+
return self.cache_key_to_modules.get(key, set())
|
|
49
|
+
|
|
50
|
+
def clear(self) -> None:
|
|
51
|
+
self.cache.clear()
|
|
52
|
+
self.module_to_cache_keys.clear()
|
|
53
|
+
self.cache_key_to_modules.clear()
|
|
54
|
+
|
|
55
|
+
def clear_module(self, module_fqn: module_fqn) -> None:
|
|
56
|
+
if module_fqn in self.module_to_cache_keys:
|
|
57
|
+
for key in self.module_to_cache_keys[module_fqn]:
|
|
58
|
+
if key in self.cache:
|
|
59
|
+
del self.cache[key]
|
|
60
|
+
if key in self.cache_key_to_modules:
|
|
61
|
+
self.cache_key_to_modules[key].discard(module_fqn)
|
|
62
|
+
if not self.cache_key_to_modules[key]:
|
|
63
|
+
del self.cache_key_to_modules[key]
|
|
64
|
+
del self.module_to_cache_keys[module_fqn]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: deproc-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python language plugin for deproc.
|
|
5
|
+
Author-email: Gurveer Singh Virk <gurveervirk.15517@gmail.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/gurveervirk/deproc
|
|
8
|
+
Project-URL: Repository, https://github.com/gurveervirk/deproc
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Requires-Dist: deproc-core==0.1.0
|
|
21
|
+
Requires-Dist: tree-sitter==0.25.2
|
|
22
|
+
Requires-Dist: tree-sitter-python==0.25.0
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
deproc/plugins/python/__init__.py,sha256=U6pyicyWuoAM55A6DDtiw0QApFdajcW_v-rpHzFcKzc,261
|
|
2
|
+
deproc/plugins/python/linker/__init__.py,sha256=XA3pp7LbhpAQkVuahHzttn98hHmiwNElMC_a8hep7pk,58
|
|
3
|
+
deproc/plugins/python/linker/main.py,sha256=s07Izz57shw0VSkVsVHI-_4LI-mB3QM3FmRgNcW0s1w,3224
|
|
4
|
+
deproc/plugins/python/linker/models.py,sha256=-ktMJGpNgfvUDXKzMIn5-5UbnA3hJkG9c4TxH2NBvfY,487
|
|
5
|
+
deproc/plugins/python/parser/__init__.py,sha256=e6ne-nP78rEceANmXpk8T_O0hVFZho1Y2_eKgyOxdZ8,70
|
|
6
|
+
deproc/plugins/python/parser/main.py,sha256=Japk-iqg-u4oPbqLsHslPpBMJK0qTxrMXoi-R3X-zg4,25840
|
|
7
|
+
deproc/plugins/python/parser/models.py,sha256=osIZeIqzLshN_H1SarkL4rK8tt6QsxGIDgz8wv_X-0M,1567
|
|
8
|
+
deproc/plugins/python/parser/utils/extraction.py,sha256=CP4_NwmGPlKztqAp7EgKHATIftlRulhLRb7BaRMzTiU,6953
|
|
9
|
+
deproc/plugins/python/parser/utils/misc.py,sha256=DcxIVBtkUkGKZOTsIHobqVsfu2IdCy1YJ_zSEliIqBY,223
|
|
10
|
+
deproc/plugins/python/parser/utils/tree_sitter_python.py,sha256=8QfrkHzpHQfdpPQXq7TIjdsj9FSJnwHVu5ps5Iz2gHs,766
|
|
11
|
+
deproc/plugins/python/resolver/__init__.py,sha256=jFJ3gtErEoc0Oj-SXbXu-wqtgOnHBvLix0TWMccHXmM,62
|
|
12
|
+
deproc/plugins/python/resolver/main.py,sha256=l4EFHPVdy5p5SM290fuZOjX7Nl3-3PBn-zzesLiRPmY,8290
|
|
13
|
+
deproc/plugins/python/resolver/models.py,sha256=Zrr0RGor8Ze6Nd7cvRJTiTTct1M3VGP51nfXPkID6KI,264
|
|
14
|
+
deproc/plugins/python/symbol_cache/__init__.py,sha256=Z3zPrQyFh4em3fdRil50wTshjhY3tplAa9Wh0gnpWrw,68
|
|
15
|
+
deproc/plugins/python/symbol_cache/main.py,sha256=PywpxX8ye-CeL9T1waaBiD4TYFplrx8X3bjaDn3abuE,2671
|
|
16
|
+
deproc/plugins/python/symbol_cache/models.py,sha256=0aZ4pUSZNBo4EhyxMl9Q3uW_7XS799c8FVFMN2C-9fw,249
|
|
17
|
+
deproc_python-0.1.0.dist-info/METADATA,sha256=9ee8R6_X2shgSRvRQ3LhBWhZbYm2BTrxFSr2TqNZ0iA,953
|
|
18
|
+
deproc_python-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
19
|
+
deproc_python-0.1.0.dist-info/top_level.txt,sha256=qyPIAnJVTLaewuAbLpShmn-777EtCI7b1b5sGgkzO10,7
|
|
20
|
+
deproc_python-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
deproc
|