-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
50 lines (39 loc) · 1.64 KB
/
__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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import ast
import os.path
from io import BufferedReader, FileIO
from visitors.python import PythonVisitor
# https://greentreesnakes.readthedocs.io/en/latest/tofrom.html
# https://python-ast-explorer.com
def main():
import argparse
parser = argparse.ArgumentParser(prog='python -m ast')
parser.add_argument('infile', type=argparse.FileType(mode='rb'), nargs='?',
default='-',
help='the file to parse; defaults to stdin')
parser.add_argument('-m', '--mode', default='exec',
choices=('exec', 'single', 'eval', 'func_type'),
help='specify what kind of code must be parsed')
parser.add_argument('--no-type-comments', default=True, action='store_false',
help="don't add information about type comments")
parser.add_argument('-a', '--include-attributes', action='store_true',
help='include attributes such as line numbers and '
'column offsets')
parser.add_argument('-i', '--indent', type=int, default=4,
help='indentation of nodes (number of spaces)')
args = parser.parse_args()
with args.infile as infile:
source = infile.read()
visitor = PythonVisitor(
module=os.path.abspath(infile.name),
rebuild_imports_tree=True
)
tree: ast.Module = \
visitor.parse(source, args.infile.name, args.mode,
type_comments=args.no_type_comments)
visitor.transpile(
tree,
indent=args.indent,
)
visitor.save_result_source()
if __name__ == '__main__':
main()