forked from ronreiter/interactive-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
363 lines (274 loc) · 12.3 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import json
import re
import markdown
import os
import cgi
import urllib
import time
import functools
import logging
from flask import Flask, render_template, request, make_response, session
#import redis
import sys
from ideone import Ideone
import constants
#cache = redis.Redis(host=constants.CACHE_HOST)
# Flask app
app = Flask(__name__)
app.secret_key = constants.SECRET_KEY
sections = re.compile(r"Tutorial\n[=\-]+\n+(.*)\n*Tutorial Code\n[=\-]+\n+(.*)\n*Expected Output\n[=\-]+\n+(.*)\n*Solution\n[=\-]+\n*(.*)\n*", re.MULTILINE | re.DOTALL)
WIKI_WORD_PATTERN = re.compile('\[\[([^]|]+\|)?([^]]+)\]\]')
DEFAULT_DOMAIN = constants.LEARNPYTHON_DOMAIN
LANGUAGES = {
"en": "English",
"pl": "Polish",
"fa": "Persian",
"es": "Spanish",
"it": "Italian",
"de": "German",
"cn": "Chinese",
}
tutorial_data = {}
def run_code(code, language):
ideone_api = Ideone(
constants.IDEONE_USERNAME,
constants.IDEONE_PASSWORD,
api_url='http://ronreiter.compilers.sphere-engine.com/api/1/service.wsdl')
code = ideone_api.create_submission(code, language_name=language, std_input="1 2 3")["link"]
result = None
while True:
time.sleep(1)
result = ideone_api.submission_details(code)
if result["status"] in [1,3]:
continue
break
data = { "code" : code }
if result["stderr"] or result["cmpinfo"]:
data["output"] = "exception"
if result["cmpinfo"]:
data["text"] = result["cmpinfo"]
elif result["stderr"]:
data["text"] = result["stderr"]
else:
data["output"] = "text"
data["text"] = result["output"]
return data
def pageurl(value, language):
if value.startswith("http"):
return value
else:
return urllib.quote("/%s/%s" % (language, value.replace(' ', '_')))
def _wikify_one(language, pat):
"""
Wikifies one link.
"""
page_title = pat.group(2)
if pat.group(1):
page_name = pat.group(1).rstrip('|')
else:
page_name = page_title
# interwiki
if ':' in page_name and not page_name.startswith("http"):
parts = page_name.split(':', 2)
if page_name == page_title:
page_title = parts[1]
link = "<a href='%s'>%s</a>" % (pageurl(page_name, language), page_title)
return link
def wikify(text, language):
text, count = WIKI_WORD_PATTERN.subn(functools.partial(_wikify_one, language), text)
return markdown.markdown(text.decode("utf-8")).strip()
def untab(text):
lines = text.strip("\n").split("\n")
if not all([x.startswith(" ") or x == "" for x in lines]):
return "\n".join(lines)
return "\n".join([x[4:] if len(x) >= 4 else "" for x in lines])
def init_tutorials():
for domain in os.listdir(os.path.join(os.path.dirname(__file__), "tutorials")):
logging.warning("loading data for domain: %s", domain)
tutorial_data[domain] = {}
if not os.path.isdir(os.path.join(os.path.dirname(__file__), "tutorials", domain)):
continue
for language in os.listdir(os.path.join(os.path.dirname(__file__), "tutorials", domain)):
tutorial_data[domain][language] = {}
tutorials_path = os.path.join(os.path.dirname(__file__), "tutorials", domain, language)
if not os.path.isdir(tutorials_path):
continue
tutorials = os.listdir(tutorials_path)
# place the index file first
tutorials.remove("Welcome.md")
tutorials = ["Welcome.md"] + tutorials
for tutorial_file in tutorials:
if not tutorial_file.endswith(".md"):
continue
tutorial = tutorial_file[:-3]
logging.debug("loading tutorial %s" % tutorial)
if not tutorial in tutorial_data[domain][language]:
tutorial_data[domain][language][tutorial] = {}
tutorial_dict = tutorial_data[domain][language][tutorial]
try:
tutorial_dict["text"] = open(os.path.join(os.path.dirname(__file__), "tutorials", domain, language, tutorial_file)).read().replace("\r\n", "\n")
except Exception, e:
tutorial_dict["text"] = "There was an error reading the tutorial. Exception: %s" % e.message
# create links by looking at all lines that are not code lines
stripped_text = "\n".join([x for x in tutorial_dict["text"].split("\n") if not x.startswith(" ")])
links = [x[0].strip("|") if x[0] else x[1] for x in WIKI_WORD_PATTERN.findall(stripped_text)]
tutorial_dict["links"] = links
tutorial_sections = sections.findall(tutorial_dict["text"])
if tutorial_sections:
text, code, output, solution = tutorial_sections[0]
tutorial_dict["page_title"] = tutorial.decode("utf8")
tutorial_dict["text"] = wikify(text, language)
tutorial_dict["code"] = untab(code)
tutorial_dict["output"] = untab(output)
tutorial_dict["solution"] = untab(solution)
tutorial_dict["is_tutorial"] = True
else:
tutorial_dict["page_title"] = ""
tutorial_dict["text"] = wikify(tutorial_dict["text"], language)
tutorial_dict["code"] = constants.DOMAIN_DATA[domain]["default_code"]
tutorial_dict["is_tutorial"] = False
for link in links:
if not link in tutorial_data[domain][language]:
tutorial_data[domain][language][link] = {
"page_title" : link.decode("utf8"),
"text" : "You can contribute this page by forking the repository at: " +
"<a href='https://github.com/ronreiter/interactive-tutorials'>" +
"https://github.com/ronreiter/interactive-tutorials" +
"</a>."
}
if not "back_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["back_chapter"] = tutorial.decode("utf-8").replace(" ", "_")
elif not link.startswith("http"):
logging.warn("Warning! duplicate links to tutorial %s from tutorial %s/%s", link, language, tutorial)
num_links = len(links)
page_index = links.index(link)
if page_index > 0:
if not "previous_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["previous_chapter"] = links[page_index - 1].decode("utf-8").replace(" ", "_")
if page_index < (num_links - 1):
if not "next_chapter" in tutorial_data[domain][language][link]:
tutorial_data[domain][language][link]["next_chapter"] = links[page_index + 1].decode("utf-8").replace(" ", "_")
init_tutorials()
def get_languages():
return sorted(tutorial_data[get_host()].keys() if not is_development_mode() else tutorial_data[DEFAULT_DOMAIN].keys())
def get_host():
return request.host[4:] if request.host.startswith("www.") else request.host
def is_development_mode():
return get_host() in ["localhost:5000", "192.241.245.44"]
def get_domain_data():
return constants.DOMAIN_DATA[get_host()] if not is_development_mode() else constants.DOMAIN_DATA[DEFAULT_DOMAIN]
def get_tutorial_data(tutorial_id, language="en"):
return tutorial_data[get_host()][language][tutorial_id] if not is_development_mode() else tutorial_data[DEFAULT_DOMAIN][language][tutorial_id]
def get_tutorial(tutorial_id, language="en"):
td = get_tutorial_data(tutorial_id, language)
if not td:
return {
"page_title": cgi.escape(tutorial_id),
"text": "Page not found."
}
else:
return td
#app.add_url_rule('/favicon.ico', redirect_to=url_for('static/img/favicons', filename=get_domain_data()["favicon"]))
@app.route("/favicon.ico")
def favicon():
return open(os.path.join(os.path.dirname(__file__), "static/img/favicons/" + get_domain_data()["favicon"]), "rb").read()
@app.route("/", methods=["GET", "POST"])
@app.route("/<language>/", methods=["GET", "POST"])
def default_index(language="en"):
return index("Welcome", language)
@app.route("/about")
@app.route("/privacy")
@app.route("/tos")
def static_file():
return make_response(render_template(
request.path.strip("/") + ".html",
domain_data=get_domain_data(),
domain_data_json=json.dumps(get_domain_data()),
language_code="en",
))
@app.route("/signin")
def signin():
email = request.args.get("email", None)
password = request.args.get("password", None)
user = users.findOne({"email": email})
if user:
session["user_id"] = str(user._id)
return make_response(json.dumps({"status": "error", "error": "no_user"}))
@app.route("/signup")
def signup():
email = request.args.get("email", None)
password = request.args.get("password", None)
confirm = request.args.get("confirm", None)
if not email or not password or not confirm:
return make_response(json.dumps({"status": "error", "error": "missing_field"}))
if not re.findall("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", email):
return make_response(json.dumps({"status": "error", "error": "invalid_email"}))
if password != confirm:
return make_response(json.dumps({"status": "error", "error": "passwords_dont_match"}))
id = users.insert({
"email": email,
"password": password,
})
session["user_id"] = str(id)
@app.route("/recruit-coders-jobs")
def jobs():
return make_response(render_template("recruit-coders-jobs.html", domain_data=get_domain_data()))
@app.route("/<language>/progress")
def progress(language):
return make_response(render_template(
"progress.html",
domain_data=get_domain_data(),
))
@app.route("/<title>", methods=["GET", "POST"])
@app.route("/<language>/<title>", methods=["GET", "POST"])
def index(title, language="en"):
tutorial = title.replace("_", " ").encode("utf-8")
current_tutorial_data = get_tutorial(tutorial, language)
domain_data = get_domain_data()
domain_data["language_code"] = language
if request.method == "GET":
title_suffix = "Learn %s - Free Interactive %s Tutorial" % (domain_data["language_uppercase"], domain_data["language_uppercase"])
html_title = "%s - %s" % (title.replace("_", " "), title_suffix) if title != "Welcome" else title_suffix
if not "uid" in session:
session["uid"] = os.urandom(16).encode("hex")
uid = session["uid"]
return make_response(render_template(
"index.html",
tutorial_page=tutorial != "Welcome",
domain_data=domain_data,
tutorial_data=current_tutorial_data,
tutorial_data_json=json.dumps(current_tutorial_data),
domain_data_json=json.dumps(domain_data),
html_title=html_title,
language_code=language,
language_name=LANGUAGES[language],
languages=get_languages(),
uid=uid,
**current_tutorial_data
))
# POST method handling
data = run_code(request.json["code"], request.json["language"])
if "output" in current_tutorial_data and current_tutorial_data["output"] == data["output"]:
data["solved"] = True
else:
data["solved"] = False
return make_response(json.dumps(data))
@app.route("/robots.txt")
def robots():
return make_response("User-agent: *\nAllow: /")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--domain",
help="Default domain when running in development mode",
# default=constants.LEARNPYTHON_DOMAIN,
required=True,
choices=constants.DOMAIN_DATA.keys()
)
parser.add_argument("-p", "--port", help="port to listen to", default=5000, type=int)
args = parser.parse_args()
DEFAULT_DOMAIN = args.domain
logging.info("listening on port %s", args.port)
app.run(debug=True, port=args.port)