-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
117 lines (86 loc) · 3.56 KB
/
server.py
File metadata and controls
117 lines (86 loc) · 3.56 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
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
import importlib
# from imp import reload
import os
import sys
from flask import Flask, json, render_template, request
app = Flask(__name__)
SCRIPT_PATH = "sketchresponse/grader_scripts/"
PACKAGE = "sketchresponse.grader_scripts."
ENABLE_DATA_PRINTING = False
@app.route("/")
def list_grader_scripts():
path = SCRIPT_PATH # os.path.expanduser(u'~')
return render_template("dirtree.html", tree=make_tree(path))
@app.route("/<grader_module_name>")
@app.route("/<path:path>/<grader_module_name>")
def new_local_frontend(path=None, grader_module_name=None):
assert grader_module_name is not None
print(type(grader_module_name))
if path is not None:
path = path.replace("/", ".")
if not path.endswith("."):
path = path + "."
grader_module_name = path + grader_module_name
module_path = PACKAGE + grader_module_name
grader_module = importlib.import_module(module_path)
# importlib.reload(grader_module)
return render_template("index_sketch_tool.html", hash=grader_module.problemconfig)
@app.route("/<grader_module_name>/check", methods=["POST"])
@app.route("/<path:path>/<grader_module_name>/check", methods=["POST"])
def check_local(path=None, grader_module_name=None):
assert grader_module_name is not None
if path is not None:
path = path.replace("/", ".")
if not path.endswith("."):
path = path + "."
grader_module_name = path + grader_module_name
module_path = PACKAGE + grader_module_name
grader_module = importlib.import_module(module_path)
# importlib.reload(grader_module)
submitted_data = request.get_json()
# Dispatch to grader in the required format
# Assume no 'expect' value for now
if ENABLE_DATA_PRINTING:
print(json.dumps(submitted_data))
grader_response = grader_module.grader(None, json.dumps(submitted_data))
# Allow boolean grader return values instead of dicts
if isinstance(grader_response, bool):
grader_response = {"ok": grader_response, "msg": ""}
return json.dumps(grader_response)
@app.route("/aws/<grader_module_name>")
def new_frontend(grader_module_name):
module_path = PACKAGE + grader_module_name
grader_module = importlib.import_module(module_path)
# importlib.reload(grader_module)
return render_template("index_aws.html", hash=grader_module.problemconfig)
@app.route("/aws/<grader_module_name>/check", methods=["POST"])
def check_aws(grader_module_name):
module_path = PACKAGE + grader_module_name
grader_module = importlib.import_module(module_path)
# importlib.reload(grader_module)
submitted_data = request.get_json()
# Dispatch to grader in the required format
# Assume no 'expect' value for now
grader_response = grader_module.grader(None, json.dumps(submitted_data))
# Allow boolean grader return values instead of dicts
if isinstance(grader_response, bool):
grader_response = {"ok": grader_response, "msg": ""}
return json.dumps(grader_response)
def make_tree(path):
tree = dict(name=os.path.basename(path), children=[])
try:
lst = os.listdir(path)
except OSError:
pass # ignore errors
else:
for name in lst:
fn = os.path.join(path, name)
if os.path.isdir(fn):
tree["children"].append(make_tree(fn))
else:
tree["children"].append(dict(name=fn.replace(SCRIPT_PATH, "")))
return tree
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "-p":
ENABLE_DATA_PRINTING = True
app.run(debug=True)