-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
72 lines (56 loc) · 1.82 KB
/
generator.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
#!/usr/bin/env python3
import click
import os
import yaml
import sys
from jinja2 import \
Environment, \
FileSystemLoader, \
StrictUndefined
from config import Config
@click.command()
@click.option('--config')
@click.option('--template')
@click.option('--output')
@click.option('--option', multiple=True)
def main(config, template, output, option):
assert config
assert template
assert output
with open(config) as fd:
content = fd.read()
data = yaml.load(content, Loader=yaml.SafeLoader)
config_obj = Config(**data)
template_data = dict(
config=config_obj,
options=option,
)
template_paths = [
template,
]
loader = FileSystemLoader(template_paths)
jinja_env = Environment(
loader=loader,
undefined=StrictUndefined,
)
# iterate through directory
walker = os.walk(template)
for path, directories, files in walker:
relative_path = path[len(template)+1:]
output_path = os.path.join(output, relative_path)
for directory in directories:
directory_path = os.path.join(output_path, directory)
directory_path = jinja_env.from_string(directory_path).render(**template_data)
os.makedirs(directory_path, exist_ok=True)
for filename in files:
if filename.endswith(".swp"):
continue
template_path = os.path.join(relative_path, filename)
file_path = os.path.join(output_path, filename)
file_path = jinja_env.from_string(file_path).render(**template_data)
print('Generating %s' % file_path)
content = jinja_env.get_template(template_path).render(**template_data)
with open(file_path, 'w') as fd:
fd.write(content)
if __name__ == '__main__':
main()