forked from sio/sysbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
65 lines (54 loc) · 1.75 KB
/
build.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
import os
import re
import yaml
import jinja2
DB_FILE = 'database.yml'
FIELDS_FILE = 'fields.yml'
TEMPLATE_DIR = 'templates/'
OUTPUT_FILE = 'index.html'
BOOTSTRAP_DEFAULT = {
'css': 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css',
'js': 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js',
}
CONVERTERS = {
'int': int,
}
def urlsafe(text, placeholder='-'):
'''Replace non-urlsafe characters with placeholder'''
if not text:
return
plain = re.sub(r'[^\w]', placeholder, text)
clean = re.sub(r'%s+' % placeholder, placeholder, plain)
return clean.lower()
def main():
'''Build static HTML page from YAML data and Jinja2 templates'''
with open(FIELDS_FILE) as f:
fields = yaml.safe_load(f)
with open(DB_FILE) as f:
data = yaml.safe_load(f)
for field_id, field in fields.items():
if not field.get('convert'):
continue
converter = CONVERTERS[field["convert"]]
for entry in data:
if field_id not in entry:
continue
entry[field_id] = converter(entry[field_id])
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_DIR))
jinja_env.filters['urlsafe'] = urlsafe
template = jinja_env.get_template('index.html.j2')
bootstrap = BOOTSTRAP_DEFAULT
for component in bootstrap:
override = os.environ.get(f'BOOTSTRAP_{component.upper()}')
if override:
bootstrap[component] = override
with open(OUTPUT_FILE, 'w') as output:
output.write(
template.render(
bootstrap=bootstrap,
data=data,
fields=fields,
)
)
if __name__ == '__main__':
main()