-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathastdump.py
More file actions
executable file
·42 lines (34 loc) · 1.22 KB
/
astdump.py
File metadata and controls
executable file
·42 lines (34 loc) · 1.22 KB
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
#!/usr/bin/env python3
'''This is a helper script. It takes in python statements on the
command line and dumps out a json-like representation of the AST
returned by ast.parse(expression, mode='eval').body
It is useful if you're making changes to the convert_java.py script
and want to know what the ast of some python expression looks like.
'''
import ast
import json
def convert_to_dict(node):
if isinstance(node, list):
return [convert_to_dict(n) for n in node]
elif isinstance(node, dict):
return {k: convert_to_dict(v) for k, v in node.items()}
elif isinstance(node, ast.AST):
nodedict = node.__dict__.copy()
nodedict['<type>'] = node.__class__.__name__
return convert_to_dict(nodedict)
elif isinstance(node, (int, float, str, bool)):
return node
else:
return repr(node)
def get_astjson(expr, mode='eval'):
asta = ast.parse(expr, mode=mode).body
if isinstance(asta, list):
asta = asta[0]
converted = convert_to_dict(asta)
return json.dumps(converted, indent=4, sort_keys=True)
def ppast(expr, mode='eval'):
astjson = get_astjson(expr, mode=mode)
print(astjson)
if __name__ == "__main__":
import sys
ppast(sys.argv[1])