-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_mindmap.py
More file actions
130 lines (95 loc) · 3.95 KB
/
Copy pathpython_mindmap.py
File metadata and controls
130 lines (95 loc) · 3.95 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
import ast
import json
# import test im
MAIN_CLASS = 'Pcb'
INPUT_FILE = 'layout.py'
OUTPUT_FILE = 'out.md'
def parse_classes(file):
class_list = {}
with open(file, "r") as f:
p = ast.parse(f.read())
# get all classes from the given python file.
classes = [c for c in ast.walk(p) if isinstance(c, ast.ClassDef)]
for x in classes:
class_name = x.name
class_data = {
'variables': [],
'methods': []
}
for ib in x.body:
if ib.name == '__init__':
for ib2 in ib.body:
if isinstance(ib2, ast.Expr):
continue
elif isinstance(ib2, ast.Assign):
if isinstance(ib2.targets[0], ast.Name):
isself = False
varname = ib2.targets[0].id
elif isinstance(ib2.targets[0], ast.Attribute):
isself = True
varname = ib2.targets[0].attr
vartype = None
containtype = None
elif isinstance(ib2, ast.AnnAssign):
if isinstance(ib2.target, ast.Name):
isself = False
varname = ib2.target.id
elif isinstance(ib2.target, ast.Attribute):
isself = True
varname = ib2.target.attr
try:
vartype = ib2.annotation.value.id
except:
vartype = ib2.annotation.id
containtype = []
try:
if isinstance(ib2.annotation.slice, ast.Tuple):
for ct in ib2.annotation.slice.dims:
containtype.append(ct.id)
elif isinstance(ib2.annotation.slice, ast.Slice):
containtype = ib2.annotation.slice.lower.id
containtype += ': '
containtype += ib2.annotation.slice.upper.id
else:
containtype = ib2.annotation.slice.id
except:
containtype = None
if isinstance(ib2.value, ast.Name):
varvalue = ib2.value.id
elif isinstance(ib2.value, ast.List):
varvalue = 'list'
elif isinstance(ib2.value, ast.Dict):
varvalue = 'dict'
elif isinstance(ib2.value, ast.Constant):
varvalue = ib2.value.value
elif isinstance(ib2.value, ast.Call):
varvalue = ib2.value.func.id + '()'
else:
varvalue = ib2.value
class_data['variables'].append(
[isself, varname, vartype, containtype, varvalue])
else:
class_data['methods'].append(ib.name + '()')
class_list[class_name] = class_data
return class_list
def nested_variables(td, classesDict):
varlist = []
for var in td['variables']:
varlist.append(var[1])
if var[3] != None:
td = classesDict[var[3]]
varlist.append(nested_variables(td, classesDict))
return varlist
def generate_map(classesDict, main_class):
td = classesDict[main_class]
for var in td['variables']:
print(var[1])
if var[3] != None:
td = classesDict[var[3]]
var_res = nested_variables(td, classesDict)
print(var_res)
# print(classesDict[var[3]])
print("break")
classesDict = parse_classes(INPUT_FILE)
generate_map(classesDict, MAIN_CLASS)
print('break')