forked from KSP-SpaceDock/SpaceDock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (76 loc) · 3.01 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
from KerbalStuff.app import app
from KerbalStuff.config import _cfg, _cfgi
import os
import scss
import coffeescript
from shutil import rmtree, copyfile
app.static_folder = os.path.join(os.getcwd(), "static")
def prepare():
if os.path.exists(app.static_folder):
rmtree(app.static_folder)
os.makedirs(app.static_folder)
compiler = scss.Scss(scss_opts = {
'style': 'compressed' if not app.debug else None
}, search_paths=[
os.path.join(os.getcwd(), 'styles')
])
# Compile styles (scss)
d = os.walk('styles')
for f in list(d)[0][2]:
if os.path.splitext(f)[1] == ".scss":
with open(os.path.join('styles', f)) as r:
output = compiler.compile(r.read())
parts = f.rsplit('.')
css = '.'.join(parts[:-1]) + ".css"
with open(os.path.join(app.static_folder, css), "w") as w:
w.write(output)
w.flush()
else:
copyfile(os.path.join('styles', f), os.path.join(app.static_folder, f))
# Compile scripts (coffeescript)
d = os.walk('scripts')
for f in list(d)[0][2]:
outputpath = os.path.join(app.static_folder, os.path.basename(f))
inputpath = os.path.join('scripts', f)
if os.path.splitext(f)[1] == ".js":
copyfile(inputpath, outputpath)
elif os.path.splitext(f)[1] == ".manifest":
with open(inputpath) as r:
manifest = r.read().split('\n')
javascript = ''
for script in manifest:
script = script.strip(' ')
if script == '' or script.startswith('#'):
continue
bare = False
if script.startswith('bare: '):
bare = True
script = script[6:]
with open(os.path.join('scripts', script)) as r:
coffee = r.read().replace("{{ support_mail }}", _cfg('support-mail'))
if script.endswith('.js'):
javascript += coffee # straight up copy
else:
javascript += coffeescript.compile(coffee, bare=bare)
output = '.'.join(f.rsplit('.')[:-1]) + '.js'
# TODO: Bug the slimit guys to support python 3
#if not app.debug:
# javascript = minify(javascript)
with open(os.path.join(app.static_folder, output), "w") as w:
w.write(javascript)
w.flush()
d = os.walk('images')
for f in list(d)[0][2]:
outputpath = os.path.join(app.static_folder, os.path.basename(f))
inputpath = os.path.join('images', f)
copyfile(inputpath, outputpath)
@app.before_first_request
def compile_first():
pass
#prepare()
@app.before_request
def compile_if_debug():
if app.debug and _cfg("debug-static-recompile").lower() in ['true','yes']:
prepare()
if __name__ == '__main__':
app.run(host=_cfg("debug-host"), port=_cfgi('debug-port'), debug=True)