-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
36 lines (30 loc) · 984 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
33
34
35
36
import argparse
import importlib
import visitors
import sys
from parser import parse_any
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--visitor", required=True)
parser.add_argument("--verbose", action="store_true")
parser.add_argument("path", action="append")
args = parser.parse_args()
try:
visitor_cls = importlib.import_module(
"visitors.{}".format(args.visitor))
except ModuleNotFoundError:
print("Unknown visitor:", args.visitor)
sys.exit(1)
for path in args.path:
for filename, path, tree in parse_any(path):
if args.verbose:
print("Reading", filename)
visitor = visitor_cls.Visitor()
visitor.visit(tree)
if not tree.was_changed:
continue
print("Refactored", filename)
with open(path, "w") as f:
f.write(str(tree))
if __name__ == "__main__":
main()