-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_parser.py
More file actions
289 lines (249 loc) · 10.7 KB
/
python_parser.py
File metadata and controls
289 lines (249 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from tree_sitter import Language, Parser, Node
import tree_sitter_python as tspython
from codesage.analyzers.base import BaseParser
from codesage.analyzers.ast_models import FunctionNode, ClassNode, ImportNode, VariableNode
from codesage.snapshot.models import ASTSummary, ComplexityMetrics
from typing import List, Set
PY_COMPLEXITY_NODES = {
"if_statement",
"elif_clause",
"for_statement",
"while_statement",
"try_statement",
"with_statement",
"match_statement",
"case_clause",
"except_clause",
"return_statement",
}
SEMANTIC_TAGS_RULES = {
"execute": "db_op",
"fetchone": "db_op",
"fetchall": "db_op",
"commit": "db_op",
"rollback": "db_op",
"connect": "network",
"socket": "network",
"send": "network",
"recv": "network",
"get": "network", # requests.get
"post": "network", # requests.post
"open": "file_io",
"read": "file_io",
"write": "file_io",
"print": "io_op",
"input": "io_op",
}
class PythonParser(BaseParser):
def __init__(self):
super().__init__()
py_language = Language(tspython.language())
self.parser = Parser(py_language)
def _parse(self, source_code: bytes):
return self.parser.parse(source_code)
def extract_functions(self) -> List[FunctionNode]:
functions = []
if not self.tree:
return functions
for node in self._walk(self.tree.root_node):
if node.type in ("function_definition", "async_function_definition"):
# Check if the function is inside a class
parent = node.parent
while parent:
if parent.type == "class_definition":
break
parent = parent.parent
else:
functions.append(self._build_function_node(node))
return functions
def extract_classes(self) -> List[ClassNode]:
classes = []
if not self.tree:
return classes
for node in self._walk(self.tree.root_node):
if node.type == "class_definition":
name_node = node.child_by_field_name("name")
name = self._text(name_node) if name_node else ''
bases_node = node.child_by_field_name("superclasses")
methods = []
body = node.child_by_field_name("body")
if body:
for child in body.children:
if child.type in ("function_definition", "async_function_definition"):
methods.append(self._build_function_node(child))
base_classes = []
if bases_node:
for child in bases_node.children:
if child.type == "identifier":
base_classes.append(self._text(child))
is_exported = not name.startswith("_")
classes.append(ClassNode(
node_type="class",
name=name,
methods=methods,
base_classes=base_classes,
is_exported=is_exported
))
return classes
def extract_imports(self) -> List[ImportNode]:
imports = []
if not self.tree:
return imports
for node in self._walk(self.tree.root_node):
if node.type == "import_statement":
for name in node.children:
if name.type == "dotted_name":
alias_node = name.parent.child_by_field_name('alias')
imports.append(ImportNode(
node_type="import",
path=self._text(name),
alias=self._text(alias_node) if alias_node else None,
))
if node.type == "import_from_statement":
module_name_node = node.child_by_field_name('module_name')
if module_name_node:
module_name = self._text(module_name_node)
for name in node.children:
if name.type == "dotted_name":
alias_node = name.parent.child_by_field_name('alias')
imports.append(ImportNode(
node_type="import",
path=f"{module_name}.{self._text(name)}",
alias=self._text(alias_node) if alias_node else None,
is_relative='.' in module_name
))
return imports
def extract_variables(self) -> List[VariableNode]:
variables = []
if not self.tree:
return variables
# Scan for global assignment nodes
for node in self._walk(self.tree.root_node):
# We are looking for top-level assignments
if node.type == "expression_statement":
assignment = node.child(0)
if assignment.type in ("assignment", "annotated_assignment"):
# Ensure it is top-level (global)
# Parent of expression_statement should be module
if node.parent and node.parent.type == "module":
left = assignment.child_by_field_name("left")
if left and left.type == "identifier":
name = self._text(left)
type_name = None
if assignment.type == "annotated_assignment":
type_node = assignment.child_by_field_name("type")
if type_node:
type_name = self._text(type_node)
# Extract value (simplified)
right = assignment.child_by_field_name("right")
value = self._text(right) if right else None
is_exported = not name.startswith("_")
variables.append(VariableNode(
node_type="variable",
name=name,
value=value,
kind="global",
type_name=type_name,
is_exported=is_exported,
start_line=node.start_point[0],
end_line=node.end_point[0]
))
return variables
def _build_function_node(self, func_node):
name_node = func_node.child_by_field_name("name")
name = self._text(name_node) if name_node else ''
params_node = func_node.child_by_field_name("parameters")
return_type_node = func_node.child_by_field_name("return_type")
decorators = self._get_decorators(func_node)
is_async = False
if func_node.type == "async_function_definition":
is_async = True
else:
for child in func_node.children:
if child.type == "async":
is_async = True
break
return_type = None
if return_type_node:
type_text = self._text(return_type_node).strip()
if type_text:
return_type = f"-> {type_text}"
# Analyze function body for tags
tags = self._extract_tags(func_node)
is_exported = not name.startswith("_")
return FunctionNode(
node_type="function",
name=name,
params=[self._text(param) for param in params_node.children] if params_node else [],
return_type=return_type,
start_line=func_node.start_point[0],
end_line=func_node.end_point[0],
complexity=self.calculate_complexity(func_node),
is_async=is_async,
decorators=decorators,
tags=tags,
is_exported=is_exported
)
def _extract_tags(self, node: Node) -> Set[str]:
tags = set()
for child in self._walk(node):
if child.type == "call":
function_node = child.child_by_field_name("function")
if function_node:
# Handle object.method() calls
if function_node.type == "attribute":
attribute_node = function_node.child_by_field_name("attribute")
if attribute_node:
method_name = self._text(attribute_node)
if method_name in SEMANTIC_TAGS_RULES:
tags.add(SEMANTIC_TAGS_RULES[method_name])
# Handle direct function calls e.g. print()
elif function_node.type == "identifier":
func_name = self._text(function_node)
if func_name in SEMANTIC_TAGS_RULES:
tags.add(SEMANTIC_TAGS_RULES[func_name])
return tags
def _get_decorators(self, func_node):
parent = func_node.parent
if parent is None or parent.type != "decorated_definition":
return []
decorators = []
for child in parent.children:
if child.type == "decorator":
text = self._text(child).strip()
if "(" in text:
text = text.split("(", 1)[0]
decorators.append(text)
return decorators
def calculate_complexity(self, node: Node) -> int:
complexity = 1
for child in self._walk(node):
if child.type in PY_COMPLEXITY_NODES:
complexity += 1
return complexity
def get_ast_summary(self, source_code: str) -> ASTSummary:
self.parse(source_code)
return ASTSummary(
function_count=len(self.extract_functions()),
class_count=len(self.extract_classes()),
import_count=len(self.extract_imports()),
comment_lines=self._count_comment_lines()
)
def _count_comment_lines(self) -> int:
if not self.tree:
return 0
comment_lines = set()
for node in self._walk(self.tree.root_node):
if node.type == 'comment':
start_line = node.start_point[0]
end_line = node.end_point[0]
for i in range(start_line, end_line + 1):
comment_lines.add(i)
return len(comment_lines)
def get_complexity_metrics(self, source_code: str) -> ComplexityMetrics:
self.parse(source_code)
if not self.tree:
return ComplexityMetrics(cyclomatic=0)
return ComplexityMetrics(
cyclomatic=self.calculate_complexity(self.tree.root_node)
)