-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app2.py
79 lines (69 loc) · 2.14 KB
/
flask_app2.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
# -*- coding: utf-8 -*-
"""
Flaskr
~~~~~~
A microblog example application written as Flask tutorial with
Flask and sqlite3.
:copyright: (c) 2010 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import json
import sys
from flask import Flask, request, redirect, url_for, abort, g, current_app,\
render_template
from lex import lexer
from parse import parser
import ast
import vistor
import code
# create our little application :)
app = Flask(__name__)
id = 1
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def capture(f):
"""
Decorator to capture standard output
"""
def captured(*args, **kwargs):
from cStringIO import StringIO
# setup the environment
backup = sys.stdout
backup2 = sys.stderr
try:
sys.stdout = StringIO() # capture output
sys.stderr = StringIO()
f(*args, **kwargs)
out = sys.stdout.getvalue() # release output
err = sys.stderr.getvalue()
finally:
sys.stdout.close() # close the stream
sys.stderr.close()
sys.stdout = backup # restore original stdout
sys.stderr = backup2
return out,err # captured output wrapped in a string
return captured
class repl(code.InteractiveConsole):
def __init__(self, locals=None, filename="<input>"):
code.InteractiveConsole.__init__(self, locals=locals,
filename=filename)
def runsource(self, source, filename="<input>", symbol="single"):
ast = parser.parse(lexer.lex(source))
vistor.exec_cmd_block(ast)
#exec source
@capture
def out(s):
try:
r = repl(locals=locals)
r.runsource(source=s)
except Exception ,err:
print >>sys.stderr , err
@app.route('/')
def index():
return render_template('index.html', hy_version="v0.1", server_software='cloud foxbase')
@app.route('/eval', methods = ["POST"])
def evals():
input = request.get_json()
o,err = out(input["code"])
return json.dumps({"stdout":o, "stderr":err})
if __name__ == '__main__':
app.run()