-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_one.py
65 lines (58 loc) · 2.11 KB
/
scan_one.py
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
import ast
import inspect
def walk_tree(node, className=None, parent=None):
if isinstance(node, list):
for e in node:
yield from walk_tree(e, parent=parent)
else:
if isinstance(node, ast.FunctionDef) or isinstance(node, ast.AsyncFunctionDef):
args = ','.join([a.arg for a in node.args.args])
docStr = ''
# print(type(node.body[0]))
if isinstance(node.body[0], ast.Expr) \
and isinstance(node.body[0].value, ast.Str):
docStr = node.body[0].value.s
# print(inspect.getmembers(node))
yield {
'type': 'func',
'class_name': className,
'name': node.name,
'args': args,
'doc_str': docStr,
'line_no': node.lineno
}
if isinstance(node, ast.ClassDef):
className=node.name
docStr = ''
if isinstance(node.body[0], ast.Expr) \
and isinstance(node.body[0].value, ast.Str):
docStr = node.body[0].value.s
yield {
'type': 'class',
'name': node.name,
'doc_str': docStr,
'line_no': node.lineno
}
if parent is None and isinstance(node, ast.Assign):
for var in node.targets:
if not isinstance(var, ast.Name):
continue
yield {
'type': 'global_var',
'name': var.id,
'line_no': node.lineno
}
if hasattr(node, 'body'):
for e in node.body:
yield from walk_tree(e, className=className, parent=node)
def scan_py_file(filePath):
with open(filePath, 'rt', encoding='utf-8') as f:
content = f.read()
# source = filePath.read_text()
tree = compile(content, filePath, "exec", ast.PyCF_ONLY_AST)
yield from walk_tree(tree.body)
if __name__ == '__main__':
import sys
iFile = sys.argv[1]
for d in scan_py_file(iFile):
print(d)