-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
32 lines (26 loc) · 894 Bytes
/
main.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
import ast
import astor
class RewriteImport(ast.NodeTransformer):
def visit_Import(self, node):
targets = []
values = []
for name in node.names:
targets.append(
ast.Name(id=name.asname if name.asname is not None else name.name)
)
values.append(
ast.Call(
func=ast.Name(id="__import__"),
args=[ast.Constant(name.name)],
keywords=[],
)
)
return ast.Assign(
targets=[ast.Tuple(elts=targets) if len(targets) > 1 else targets[0]],
value=ast.Tuple(elts=values) if len(values) > 1 else values[0],
)
if __name__ == "__main__":
from sys import argv
with open(argv[1]) as f:
tree = RewriteImport().visit(ast.parse(f.read()))
print(astor.to_source(tree))