-
Notifications
You must be signed in to change notification settings - Fork 3
/
kulfon.py
278 lines (217 loc) · 7.11 KB
/
kulfon.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
import click
from glob import glob
from os.path import join, dirname
from subprocess import call
import json
import sys
import os
import colorama
import errno
import shutil
import yaml
import sass
import time
import hashlib
import logging
from livereload import Server, shell
from jinja2 import Environment, FileSystemLoader
from webassets import Environment as AssetsEnvironment
from webassets import Bundle
from webassets.ext.jinja2 import AssetsExtension
from webassets.script import CommandLineEnvironment
BASE_HTML="""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{% block title %}{{ data['title'] }}{% endblock %}</title>
<link rel="shortcut icon" type="image/x-icon" href="/static/favicon.ico">
<link href="/assets/styles.css" rel="stylesheet">
{% block head %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
{% for filename in javascripts %}
<script type="text/javascript" src="/assets/{{ filename }}"></script>
{% endfor %}
</body>
</html>
"""[1:-1]
INDEX_HTML="""
{% extends "layouts/base.html" %}
{% block content %}
<h1>Hello, {{ data['name'] }}, I'm Kulfon</h1>
{% endblock %}
"""[1:-1]
STYLES_SCSS="""
body {
color: red;
}
"""[1:-1]
DATA_YML="""
name: My Friend
title: My Title
"""
class DictAsMember(dict):
def __getattr__(self, name):
value = self[name]
if isinstance(value, dict):
value = DictAsMember(value)
return value
def md5(fname):
hash = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash.update(chunk)
return hash.hexdigest()
imports = []
vendor = []
for f in glob(join('bower_components', '*', 'bower.json')):
with open(f) as content:
bower_json = json.load(content)
elems = []
if isinstance(bower_json['main'], list):
elems = bower_json['main']
else:
elems = [bower_json['main']]
for el in elems:
imports.append(join('bower_components', bower_json['name'], dirname(el)))
_, ext = os.path.splitext(el)
if ext == '.js':
vendor.append(join('bower_components', bower_json['name'], el))
assets_env = AssetsEnvironment(directory='.', url='/assets')
assets_env.versions = 'hash:32'
# TODO it doesn't work
assets_env.config['closure_compressor_optimization'] = 'WHITESPACE_ONLY'
if vendor:
vendor = Bundle(*vendor, output='dist/assets/vendor.%(version)s.js', filters='rjsmin')
main = Bundle('javascripts/main.js', output='dist/assets/app.%(version)s.js', filters='rjsmin')
app = Bundle(vendor, main)
else:
main = Bundle('javascripts/main.js', output='dist/assets/app.%(version)s.js', filters='rjsmin')
app = Bundle(main)
assets_env.register('app', app)
port = os.environ.get('PORT')
PORT = int(port) if port else 5002
def load_data():
with open("data.yml", 'r') as stream:
global data
data = DictAsMember(yaml.load(stream))
def touch(fname, times=None):
with open(fname, 'a'):
os.utime(fname, times)
def fill(fname, content):
with open(fname, 'w') as file:
file.write(content)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
def is_partial(filename):
return os.path.dirname(filename).startswith('partials')
def is_layout(filename):
return os.path.dirname(filename).startswith('layouts')
def is_ignored(filename):
return any((x.startswith(".") for x in filename.split(os.path.sep)))
def is_nonhtml(filename):
return os.path.splitext(os.path.basename(filename))[1] != '.html'
def is_template(filename):
if is_partial(filename):
return False
if is_ignored(filename):
return False
if is_nonhtml(filename):
return False
if is_layout(filename):
return False
return True
def render(extensions=[], stylesheets=None, target='development'):
env = Environment(
loader=FileSystemLoader('./views'),
extensions=extensions,
keep_trailing_newline=True,
)
env.assets_environment = assets_env
javascripts = [os.path.basename(path) for path in assets_env['app'].urls()]
for t in env.list_templates(filter_func=is_template):
template = env.get_template(t)
if template.name == 'index.html':
filepath = 'index.html'
else:
filepath = os.path.join(os.path.splitext(template.name)[0], 'index.html')
mkdir_p(os.path.join('./dist', os.path.dirname(filepath)))
template.stream({
'data': data,
'stylesheets': stylesheets,
'javascripts': javascripts
}).dump(os.path.join('./dist', filepath), "utf-8")
@click.group()
def cli():
pass
@cli.command()
def clean():
shutil.rmtree('dist/', ignore_errors=True)
def setup():
mkdir_p('dist/assets')
def css(target='development'):
content = sass.compile(
filename='stylesheets/styles.scss',
output_style='nested' if target == 'development' else 'compressed',
include_paths=imports
)
if target == 'production':
version = hashlib.md5(content).hexdigest()
output_name = 'dist/assets/styles.{}.css'.format(version)
else:
output_name = 'dist/assets/styles.css'
with open(output_name, 'w') as fout:
fout.write(content)
return os.path.basename(output_name)
def js():
log = logging.getLogger('webassets')
log.addHandler(logging.StreamHandler())
log.setLevel(logging.WARNING)
cmdenv = CommandLineEnvironment(assets_env, log)
cmdenv.build()
def images():
os.system("rsync -az images/ dist/assets")
@cli.command()
@click.argument('directory', type=click.Path(file_okay=False), default='.')
def init(directory):
"""Initialize `kulfon` directory structure; default is `.`, i.e.
current directory.
"""
mkdir_p(join(directory, 'javascripts'))
mkdir_p(join(directory, 'stylesheets'))
mkdir_p(join(directory, 'images'))
mkdir_p(join(directory, 'views', 'layouts'))
mkdir_p(join(directory, 'views', 'partials'))
fill(join(directory, 'stylesheets', 'styles.scss'), STYLES_SCSS)
fill(join(directory, 'views', 'index.html'), INDEX_HTML)
fill(join(directory, 'views', 'layouts', 'base.html'), BASE_HTML)
fill(join(directory, 'data.yml'), DATA_YML)
touch(join(directory, 'javascripts', 'main.js'))
@cli.command()
@click.option('--target', type=click.Choice(['development', 'production']),
default='development', help='Build in development or production mode; default is `development`.')
def build(target):
click.echo("Building...", nl=False)
load_data()
setup()
stylesheets = css(target)
js()
images()
render(stylesheets=stylesheets, target=target)
click.echo(click.style("\tDone", fg='green'))
@cli.command()
def serve():
server = Server()
load_data()
server.watch('*.scss', css())
server.watch('*.js', js())
server.watch('*.html', render())
server.serve(root='dist/', port=PORT, debug=False)