-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_ast_visitor.py
136 lines (103 loc) · 3.47 KB
/
clean_ast_visitor.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
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
"""
"""
from visitors import Visitor, AstTypes
class CleanAstVisitor(Visitor):
def __init__(self, ast):
self.ast = ast
self.super = super(CleanAstVisitor, self)
def delete_keys(self, node, *keys):
for key in keys:
if key in node:
del node[key]
def visit_Module(self, node):
self.delete_keys(
node,
AstTypes.Module.type_ignores,
)
self.super.visit_Module(node)
def visit_Assign(self, node):
self.delete_keys(
node,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
AstTypes.Assign.type_comment
)
self.visit_Assign_targets(node[AstTypes.Assign.targets])
self.visit_Assign_value(node[AstTypes.Assign.value])
def visit_Call(self, node):
keys = (
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
AstTypes.Call.starargs,
AstTypes.Call.kwargs,
AstTypes.Call.keywords
)
self.delete_keys(node, *keys)
self.visit_Call_func(node[AstTypes.Call.func])
self.visit_Call_args(node[AstTypes.Call.args])
def visit_Expr(self, node):
self.delete_keys(
node,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
)
self.super.visit_Expr(node)
def visit_BinOp(self, node):
self.delete_keys(
node,
AstTypes.Generic.lineno,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
)
self.visit_BinOp_operand(node[AstTypes.BinOp.left])
self.visit_BinOp_operand(node[AstTypes.BinOp.right])
def visit_While(self, node):
self.delete_keys(
node,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
)
self.visit_If_test(node['test'])
self.visit_body(node['body'])
def visit_If(self, node):
self.delete_keys(
node,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
)
self.visit_If_test(node['test'])
self.visit_body(node['body'])
self.visit_body(node['orelse'])
def visit_Name(self, node):
self.delete_keys(
node,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
AstTypes.Name.ctx
)
def visit_Constant(self, node):
self.delete_keys(
node,
AstTypes.Generic.lineno,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
AstTypes.Constant.kind
)
def visit_Break(self, node):
self.delete_keys(
node,
AstTypes.Generic.end_lineno,
AstTypes.Generic.col_offset,
AstTypes.Generic.end_col_offset,
)
if __name__ == '__main__':
from visitors import Driver
Driver.print_ast(CleanAstVisitor)