-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (41 loc) · 1.22 KB
/
app.py
File metadata and controls
55 lines (41 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
43
44
45
46
47
48
49
50
51
52
53
54
55
from flask import Flask, request, send_from_directory
import ast
import json
app = Flask(__name__, static_url_path="", static_folder="build")
def classname(cls):
return cls.__class__.__name__
def jsonify_ast(node, level=0):
fields = {}
for k in node._fields:
fields[k] = '...'
v = getattr(node, k)
if isinstance(v, ast.AST):
if v._fields: fields[k] = jsonify_ast(v)
else:
fields[k] = classname(v)
elif isinstance(v, list):
fields[k] = []
for e in v:
fields[k].append(jsonify_ast(e))
elif isinstance(v, str) or isinstance(v, int) or isinstance(v, float):
fields[k] = v
elif v is None:
fields[k] = None
else:
fields[k] = 'unrecognized'
ret = { classname(node): fields }
return ret
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/<path:path>')
def static_proxy(path):
return send_from_directory('build', path)
@app.route('/api', methods=['POST'])
def ast2json():
code = request.data
tree = ast.parse(code)
data = jsonify_ast(tree)
return data
if __name__ == '__main__':
app.run()