-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb.py
97 lines (78 loc) · 2.83 KB
/
web.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
from flask import Flask, send_file, render_template, send_from_directory
import io
from urllib.parse import urlparse
import os
from typing import List
import glob
import log
from model import *
def serve(routes: List[AceRoute], recipeInfos: List[RecipeInfo]=[]):
"""Start a webserver which serves the `routes`, and additional files like index.html and out/"""
app = Flask(__name__)
app.add_url_rule('/', 'index', viewIndex(routes, recipeInfos))
app.add_url_rule('/out/<filename>', 'out', view_out)
print("")
print("Routes:")
print(" / Recipe overview")
print(" /out/<filename> out/ files")
print(" /static/<filename> static/ files")
for route in routes:
if isinstance(route.data, (AceStr, AceBytes)):
print(" {} ({}) Download: {} {}".format(route.url, route.data.index, route.download, route.downloadName))
else:
print(" {} Donload: {} {}".format(route.url, route.download, route.downloadName))
try:
if route.download:
app.add_url_rule(route.url, route.url, viewRouteDownload(route))
else:
app.add_url_rule(route.url, route.url, viewRoutePlain(route))
except AssertionError:
print("Double URL, dropped: {}".format(route.url))
route.url = route.url + " (doubled, dropped)"
print("")
app.run(
host=config.LISTEN_IP,
port=config.LISTEN_PORT)
# View for normal (text) files/route
def viewRoutePlain(route):
def view_func():
return route.data
return view_func
# View for downloadable (binary) files/route
def viewRouteDownload(route):
def view_func():
# TODO necessary to convert?
data = route.data
if isinstance(data, str):
data = bytes(data, 'utf-8')
if route.downloadMime is not None:
ret = send_file(
io.BytesIO(data),
as_attachment=True,
download_name=route.downloadName,
mimetype=route.downloadMime
)
return ret
else:
ret = send_file(
io.BytesIO(data),
download_name=route.downloadName,
)
return ret
return view_func
# View for the index.html and all its information
def viewIndex(routes, recipeInfos):
def view_func():
files = sorted(glob.glob("out/out_*.*"))
#log = log.GlobalLog.getvalue()
log = '\n'.join(config.makerCallstack.values())
return render_template(
'index.html',
files=files,
routes=routes,
log=log,
recipeInfos=recipeInfos)
return view_func
# View for file in out/ directory
def view_out(filename):
return send_from_directory('out/', filename, as_attachment=False)