-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbuilder.py
More file actions
107 lines (90 loc) · 2.27 KB
/
builder.py
File metadata and controls
107 lines (90 loc) · 2.27 KB
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
import os
from distutils.dir_util import copy_tree
from typing import Dict
import click
import yaml
from jinja2 import Environment, FileSystemLoader
TEMPLATE_DIRECTORY = "./template"
TEMPLATE_FILES_DIRECTORY = "./template_files"
def load_variable(
variable_file: str,
) -> Dict:
with open(variable_file, "r") as f:
variables = yaml.load(
f,
Loader=yaml.SafeLoader,
)
return variables
def format_path(
correspond_file_paths: Dict,
name: str,
) -> Dict:
formatted_correspond_file_paths: Dict = {}
for k, v in correspond_file_paths.items():
formatted_correspond_file_paths[k] = v.format(name)
return formatted_correspond_file_paths
def copy_directory(name: str):
copy_tree(TEMPLATE_DIRECTORY, name)
def build(
template_file_name: str,
output_file_path: str,
variables: Dict,
):
env = Environment(
loader=FileSystemLoader("./", encoding="utf8"),
)
tmpl = env.get_template(
os.path.join(
TEMPLATE_FILES_DIRECTORY,
template_file_name,
),
)
file = tmpl.render(**variables)
with open(output_file_path, mode="w") as f:
f.write(str(file))
@click.command(help="template pattern")
@click.option(
"--name",
type=str,
required=True,
help="name of project",
)
@click.option(
"--variable_file",
type=str,
default="vars.yaml",
required=True,
help="path to variable file yaml",
)
@click.option(
"--correspond_file_path",
type=str,
default="correspond_file_path.yaml",
required=True,
help="file defining corresponding file path",
)
def main(
name: str,
variable_file: str,
correspond_file_path: str,
):
variables = load_variable(
variable_file=variable_file,
)
correspond_file_paths = load_variable(
variable_file=correspond_file_path,
)
formatted_correspond_file_paths = format_path(
correspond_file_paths=correspond_file_paths,
name=name,
)
os.makedirs(name, exist_ok=True)
copy_directory(name=name)
for k, v in formatted_correspond_file_paths.items():
build(
template_file_name=k,
output_file_path=v,
variables=variables,
)
if __name__ == "__main__":
main()