forked from enah/python-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
131 lines (107 loc) · 3.08 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from flask import Flask, request, render_template, flash, url_for, redirect
from pylint import epylint
from controlflow import exec_script_str
import re
import json
import tempfile
import argparse
try:
import secret
except:
# Generate secret key
import string
from random import choice
chars = string.letters + string.digits
key = ''.join([choice(chars) for i in range(50)])
s = "key = '{}'\n".format(key)
f = open("secret.py", 'w')
f.write(s)
f.close()
import secret
app = Flask(__name__)
EXAMPLE = """# Try this simple example
def memo(f):
cache = dict()
def wrapped(*args):
t = tuple(args)
if t not in cache:
cache[t] = f(*args)
return cache[t]
return wrapped
@memo
def fibo(n):
if n == 0:
return 0
if n == 1:
return 1
return fibo(n-1) + fibo(n-2)
print fibo(10)
"""
@app.route("/", methods=['GET'])
def home():
return render_template("upload.html",example=json.dumps(EXAMPLE))
@app.route("/visualize", methods=['POST'])
def visualize():
try:
request_file = request.files['code_file']
# TODO: implement file-size checker, this one doesn't work in flask
# if code.size > 1024*1024:
# flash("file size limit exceeded :(")
# return redirect("/")
if request_file.filename != "":
code = request_file.read()
else:
code = None
except:
code = None
if code is None:
code = request.form.get('code', None)
if not code:
flash("no code found")
return redirect("/")
data = dict()
result = exec_script_str(code)
if len(result)<=1:
result = "your code doesn't seem too short to be interesting!"
if type(result) == str:
flash(result)
return redirect("/")
data['branches'], data['stats'] = result
data['code'] = [None,] + code.split('\n')
with tempfile.NamedTemporaryFile() as f:
f.write(code)
f.seek(0)
out, err = epylint.py_run(f.name, True)
report = out.read()
raw_reports = report.split('\n')[1:]
reports = dict()
for report in raw_reports:
regex = re.compile(r'^[^:]+:(\d+): \[([-\w]+),([^\]]*)] (.*)$',re.I)
r = regex.match(report)
if r is None:
continue
line, warning, function, message = r.groups()
if warning in ("fixme",):
continue
line = int(line)
reports[line] = {
"type":warning,
"function":function,
"message":message
}
data['warnings'] = reports
json_data = json.dumps(data)
return render_template("visualizer.html",
data=json_data)
def main():
parser = argparse.ArgumentParser(description='Runs the viewer')
parser.add_argument('-p', '--production', action="store_true")
args = parser.parse_args()
PROD = args.production
app.secret_key = secret.key
if PROD:
app.run(host="0.0.0.0", port=80)
else:
app.run(debug=True, port = 8000)
if __name__ == '__main__':
main()