-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_build_ast.py
127 lines (102 loc) · 2.75 KB
/
test_build_ast.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
import os.path
import astor
import pprint
import ast
def build_ast_class():
'''
ClassDef(name='TestWat',
bases=[Name(id='object')],
keywords=[],
starargs=None,
kwargs=None,
body=[
FunctionDef(name='__init__',
args=arguments(args=[arg(arg='self', annotation=None)],
vararg=arg(arg='args', annotation=None),
kwonlyargs=[],
kw_defaults=[],
kwarg=arg(arg='kwargs', annotation=None),
defaults=[]),
body=[
Expr(
value=Call(
func=Attribute(
value=Call(func=Name(id='super'), args=[], keywords=[], starargs=None, kwargs=None),
attr='__init__'),
args=[Name(id='self')],
keywords=[],
starargs=Name(id='args'),
kwargs=Name(id='kwargs')))],
decorator_list=[],
returns=None)],
decorator_list=[]),
'''
init_call = ast.Call(func=ast.Name(id='super', ctx=ast.Load()), args=[], keywords=[])
super_func = ast.Call(
func=ast.Attribute(value=init_call, attr='__init__', ctx=ast.Load()),
args=[ast.Name(id='self', ctx=ast.Load())],
starargs=ast.Name(id='args', ctx=ast.Load()),
kwargs=ast.Name(id='kwargs', ctx=ast.Load()),
keywords=[],
)
super_init = ast.Expr(
value=super_func,
lineno = 3,
col_offset = 0,
)
body = [super_init]
# body = [ast.Pass()]
sig = ast.arguments(
args=[ast.arg('self', None)],
vararg=ast.arg(arg='args', annotation=None),
kwarg=ast.arg(arg='kwargs', annotation=None),
varargannotation=None,
kwonlyargs=[],
kwargannotation=None,
defaults=[],
kw_defaults=[])
init_func = ast.FunctionDef(
name = "__init__",
args = sig,
body = body,
decorator_list = [],
lineno = 2,
col_offset = 0,
)
body = [
ast.Expr(value=ast.Str(s='\n\n\t')),
init_func
]
# print(body)
interface_class = ast.ClassDef(
name = "Test-Class",
bases = [],
body = body,
keywords = [],
decorator_list = [],
starargs = None,
kwargs = None,
lineno = 1,
col_offset = 0,
)
print("Interface class:", interface_class)
return interface_class
def build_module():
module_components = [
# ast.ImportFrom(module="ChromeController.transport", names=[ast.alias('ChromeSocketManager', None)], level=0),
# ast.ImportFrom(module="ChromeController.manager_base", names=[ast.alias('ChromeInterface', None)], level=0),
build_ast_class(),
]
mod = ast.Module(module_components, lineno=1, col_offset=1)
mod = ast.fix_missing_locations(mod)
print("Module:", mod)
return mod
def wat():
clsdef = build_module()
print("Clsdef: ", clsdef)
code = compile(clsdef, "no filename", "exec")
exec(code)
if __name__ == '__main__':
# wat()
wat()
# docstring_dbg()