1
1
import os
2
2
import ast
3
3
import inspect
4
- from pprint import pprint
5
4
6
5
from jinja2 import nodes , Environment , PackageLoader , meta , exceptions
7
6
from bs4 import BeautifulSoup
10
9
11
10
def get_decorators (source ):
12
11
decorators = {}
13
-
14
12
def visit_FunctionDef (node ):
15
13
decorators [node .name ] = []
16
14
for n in node .decorator_list :
@@ -26,37 +24,54 @@ def visit_FunctionDef(node):
26
24
node_iter = ast .NodeVisitor ()
27
25
node_iter .visit_FunctionDef = visit_FunctionDef
28
26
node_iter .visit (ast .parse (inspect .getsource (source )))
29
-
30
27
return decorators
31
28
32
29
def get_functions (source ):
33
30
functions = []
34
-
35
31
def visit_Call (node ):
36
32
name = node .func .attr if isinstance (node .func , ast .Attribute ) else node .func .id
37
33
if name == 'getattr' :
38
34
functions .append (name + ':' + node .args [0 ].id + ':' + node .args [1 ].s + ':' + str (node .args [2 ].value ))
39
35
else :
40
- functions .append (name + ':' + ':' .join ([a .s for a in node .args ]))
36
+ if name is not 'connect' :
37
+ functions .append (name + ':' + ':' .join ([a .s for a in node .args ]))
38
+
41
39
42
40
node_iter = ast .NodeVisitor ()
43
41
node_iter .visit_Call = visit_Call
44
42
node_iter .visit (ast .parse (inspect .getsource (source )))
45
-
46
43
return functions
47
44
48
-
49
45
def get_statements (source ):
50
- statment = []
46
+ statements = []
51
47
52
48
def visit_If (node ):
53
- print (ast .dump (node ))
49
+ statement = node .test .left .id
50
+ if isinstance (node .test .ops [0 ], ast .Is ) or isinstance (node .test .ops [0 ], ast .Eq ):
51
+ statement += ':true'
52
+ statement += (':' + str (node .test .comparators [0 ].value ))
53
+ statements .append (statement )
54
54
55
55
node_iter = ast .NodeVisitor ()
56
56
node_iter .visit_If = visit_If
57
57
node_iter .visit (ast .parse (inspect .getsource (source )))
58
-
59
- return statment
58
+ return statements
59
+
60
+ def source_dict (source ):
61
+ return build_dict (ast .parse (inspect .getsource (source )))
62
+
63
+ def build_dict (node ):
64
+ result = {}
65
+ result ['node_type' ] = node .__class__ .__name__
66
+ for attr in dir (node ):
67
+ if not attr .startswith ("_" ) and attr != 'ctx' and attr != 'lineno' and attr != 'col_offset' :
68
+ value = getattr (node , attr )
69
+ if isinstance (value , ast .AST ):
70
+ value = build_dict (value )
71
+ elif isinstance (value , list ):
72
+ value = [build_dict (n ) for n in value ]
73
+ result [attr ] = value
74
+ return result
60
75
61
76
def list_routes (app ):
62
77
rules = []
@@ -97,3 +112,5 @@ def parsed_content(name):
97
112
98
113
def template_extends (name ):
99
114
return list (meta .find_referenced_templates (parsed_content (name )))
115
+
116
+
0 commit comments