Repository navigation
Expand file tree
/
Copy pathtest_ast.py
More file actions
executable file
·52 lines (43 loc) · 1.66 KB
/
Copy pathtest_ast.py
File metadata and controls
executable file
·52 lines (43 loc) · 1.66 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
#!/usr/bin/env python
"""Test script for AST-based class and function extraction."""
from __future__ import annotations
import ast
import sys
from collections import namedtuple
from typing import Any, List
code_model_data = namedtuple("code_model_data", "type line_number name") # noqa: PYI024
class_model_data = namedtuple("class_model_data", "name line_number") # noqa: PYI024
is_class = False
def extract_classes_and_functions(node_to_traverse, current_object):
global is_class
for node in node_to_traverse.body:
if isinstance(node, ast.ClassDef):
is_class = True
current_object.append({class_model_data(node.name, node.lineno): []})
extract_classes_and_functions(
node, current_object[-1:][0][class_model_data(node.name, node.lineno)]
)
is_class = False
if isinstance(node, ast.FunctionDef):
func = "function"
if is_class:
func = "method"
current_object.append(
code_model_data(type=func, line_number=node.lineno, name=node.name)
)
file = open(sys.argv[1], "r")
f = file.read()
node_to_traverse = ast.parse(f)
class_and_functions: List[Any] = []
extract_classes_and_functions(node_to_traverse, class_and_functions)
# pprint.pprint(class_and_functions)
for item in class_and_functions:
if isinstance(item, code_model_data):
print("Functions :")
print(item)
elif isinstance(item, dict):
class_info = list(item.keys())[0]
print(f"class {class_info.name} {class_info.line_number}")
methods = list(item.values())
for m in methods[0]:
print(m)