-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.py
executable file
·96 lines (73 loc) · 2.67 KB
/
builder.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
#!/usr/bin/env python3
from datetime import date
import logging
import os
import shutil
import subprocess
import sys
import jinja2
from markdown import markdown
import yaml
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
out_dir = 'out'
public_dir = 'public'
def build(data_file_name):
"""Loads the data in the file given by `data_file_name` and builds
the CV."""
clean_output()
os.makedirs(out_dir, exist_ok=True)
with open(data_file_name) as f:
data = yaml.load(f, Loader=yaml.Loader)
data['last_updated'] = get_current_date()
build_html(data)
build_css(['style.less'])
copy_public_files()
logger.info('Successfully built CV')
def clean_output():
for root_path, dirs, files in os.walk(out_dir):
for d in dirs:
shutil.rmtree(os.path.join(root_path, d))
for f in files:
os.unlink(os.path.join(root_path, f))
def build_html(data):
"""Renders templates with the data and writes the output HTML file."""
rendered = render_template('index.html', **data)
out_path = os.path.join(out_dir, 'index.html')
with open(out_path, 'w') as out_file:
out_file.write(rendered)
def build_css(file_names):
"""Compiles all LESS files in `file_names` into CSS files with
the same name."""
for file_name in file_names:
css_file_name = '.'.join(file_name.split('.')[:-1]) + '.css'
css_file_path = os.path.join(out_dir, css_file_name)
which_ret = subprocess.run(['which', 'lessc'], capture_output=True)
lessc_path = which_ret.stdout.rstrip(b'\n')
subprocess.run([lessc_path, file_name, css_file_path])
def copy_public_files():
for root_path, dirs, files in os.walk(public_dir):
for f in files:
shutil.copyfile(os.path.join(root_path, f), os.path.join(out_dir, f))
def render_template(file_name, *args, **kwargs):
"""Renders the template at `file_name` given the arguments as
data. Returns the rendered template."""
loader = jinja2.FileSystemLoader(searchpath='.')
env = jinja2.Environment(loader=loader)
env.globals = {'render_text': render_text}
template = env.get_template(file_name)
return template.render(*args, **kwargs)
def render_text(text):
"""A helper function for rendering blocks of text (like job
descriptions) using Markdown."""
return markdown(text)
def get_current_date():
return date.today().strftime('%Y-%m-%d')
if __name__ == '__main__':
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
data_file_name = sys.argv[1]
build(data_file_name)