forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
207 lines (162 loc) · 6.05 KB
/
app.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from datetime import datetime
from functools import wraps
import hedy
import json
import jsonbin
import logging
import os
import requests
import uuid
from flaskext.markdown import Markdown
# app.py
from flask import Flask, request, jsonify, render_template, session
from flask_compress import Compress
logging.basicConfig(
level=logging.DEBUG,
format='[%(asctime)s] %(levelname)-8s: %(message)s')
app = Flask(__name__, static_url_path='')
# Unique random key for sessions
app.config['SECRET_KEY'] = uuid.uuid4().hex
Compress(app)
Markdown(app)
logger = jsonbin.JsonBinLogger.from_env_vars()
@app.route('/levels-text/', methods=['GET'])
def levels():
level = request.args.get("level", None)
#read levels from file
try:
file = open("levels.json", "r")
contents = str(file.read())
response = (json.loads(contents))
file.close()
except Exception as E:
print(f"error opening level {level}")
response["Error"] = str(E)
return jsonify(response)
@app.route('/parse/', methods=['GET'])
def parse():
# Retrieve the name from url parameter
code = request.args.get("code", None)
level = int(request.args.get("level", None))
# For debugging
print(f"got code {code}")
response = {}
# Check if user sent code
if not code:
response["Error"] = "no code found, please send code."
# is so, parse
else:
try:
result = hedy.transpile(code, level)
response["Code"] = result
except hedy.HedyException as E:
texts = load_texts()
error_template = texts['HedyErrorMessages'][E.error_code]
response["Error"] = error_template.format(**E.arguments)
except Exception as E:
print(f"error transpiling {code}")
response["Error"] = str(E)
logger.log({
'session': session_id(),
'date': str(datetime.now()),
'level': level,
'code': code,
'server_error': response.get('Error')
})
return jsonify(response)
@app.route('/report_error', methods=['POST'])
def report_error():
post_body = request.json
logger.log({
'session': session_id(),
'date': str(datetime.now()),
'level': post_body.get('level'),
'code': post_body.get('code'),
'client_error': post_body.get('client_error')
})
# @app.route('/post/', methods=['POST'])
# for now we do not need a post but I am leaving it in for a potential future
# routing to index.html
@app.route('/index.html', methods=['GET'])
@app.route('/', methods=['GET'])
def index():
session_id() # Run this for the side effect of generating a session ID
level = requested_level()
lang = requested_lang()
arguments_dict = {}
arguments_dict['level'] = level
arguments_dict['lang'] = lang
try:
with open("static/levels.json", "r") as file:
response_levels = json.load(file)
response_texts_lang = load_texts()
except Exception as E:
print(f"error opening level {level}")
return jsonify({"Error": str(E)})
arguments_dict['page_title'] = response_texts_lang['Page_Title']
arguments_dict['run_button'] = response_texts_lang['Run_code_button']
arguments_dict['advance_button'] = response_texts_lang['Advance_button']
arguments_dict['enter_text'] = response_texts_lang['Enter_Text']
arguments_dict['enter'] = response_texts_lang['Enter']
level_and_lang_dict = [r for r in response_levels if int(r['Level']) == level and r['Language'] == lang][0]
maxlevel = max(int(r['Level']) for r in response_levels)
arguments_dict['commands'] = level_and_lang_dict['Commands']
arguments_dict['introtext'] = level_and_lang_dict['Intro_text']
arguments_dict['startcode'] = level_and_lang_dict['Start_code']
next_level_available = level != maxlevel
arguments_dict['nextlevel'] = level + 1 if next_level_available else None
arguments_dict['latest'] = 'March 13th'
return render_template("index.html", **arguments_dict)
# routing to docs.html
@app.route('/docs.html', methods=['GET'])
def docs():
level = request.args.get("level", 1)
lang = requested_lang()
arguments_dict = {}
arguments_dict['level'] = level
arguments_dict['pagetitle'] = f'Level{level}'
arguments_dict['mkd'] = load_docs()
return render_template("docs_per_level.html", **arguments_dict)
@app.route('/error_messages.js', methods=['GET'])
def error():
try:
lang_texts = load_texts()
error_messages = lang_texts["ClientErrorMessages"]
except Exception as E:
print(f"error opening texts.json")
error_messages = {"Error": str(E)}
return render_template("error_messages.js", error_messages=json.dumps(error_messages))
@app.errorhandler(500)
def internal_error(exception):
import traceback
print(traceback.format_exc())
return "<h1>500 Internal Server Error</h1>"
def session_id():
"""Returns or sets the current session ID."""
if 'session_id' not in session:
session['session_id'] = uuid.uuid4().hex
return session['session_id']
def requested_lang():
"""Return the user's requested language code."""
return request.args.get("lang", 'nl')
def requested_level():
"""Return the user's requested level."""
return int(request.args.get("level", 1))
def load_docs():
"""Load the markdown docs for the given language and level. """
lang = requested_lang()
level = requested_level()
with open(f'static/{lang}-level{level}.md', "r") as file:
markdown = file.read()
return markdown if markdown else f'No docs available for {lang} at level {level}'
def load_texts():
"""Load the texts for the given language.
If the language is unknown, default to English.
"""
with open("static/texts.json", "r") as file:
texts_file = json.load(file)
texts = texts_file.get(requested_lang().lower())
return texts if texts else texts_file.get('en')
if __name__ == '__main__':
# Threaded option to enable multiple instances for multiple user access support
app.run(threaded=True, port=5000)