- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinja-cli.py
More file actions
Latest commit
88 lines (74 loc) · 2.95 KB
/
Copy pathjinja-cli.py
File metadata and controls
88 lines (74 loc) · 2.95 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
# Script to run Jinja2 reading from template and parameter files
# - Note: Undefined Jinja variables will cause errors
# - Note: 'trim_blocks' and 'lstrip_block are forced to True
defrender_template(template_filename, parameters_filename, whitespace, unset_variables, verbose=0):
importos
importjson
importyaml
importsys
fromjinja2importTemplate, StrictUndefined
ifos.path.exists(parameters_filename):
filepath=parameters_filename
else:
workdir=os.getcwd()
filepath=os.path.join(workdir, parameters_filename)
try:
file=open(filepath)
exceptOSErrorasos_error:
print(f"{os_error}", file=sys.stderr)
sys.exit(1)
params=None
try:
ifverbose>0:
print(f"trying to JSON load, {file.name}")
params=json.load(file)
exceptjson.decoder.JSONDecodeErrorasjson_decode_error:
file.seek(0) # return to beginning of file
ifverbose>0:
print(f"trying to YAML load, {file.name}")
try:
params=yaml.safe_load(file)
exceptyaml.YAMLErrorasyaml_error:
print(f"Parameters file not valid JSON or YAML, {yaml_error}", file=sys.stderr)
finally:
file.close()
# Read the template file
ifos.path.exists(template_filename):
filepath=template_filename
else:
workdir=os.getcwd()
filepath=os.path.join(workdir, template_filename)
template=None
try:
file=open(filepath)
template=file.read()
exceptOSErrorasos_error:
print(f"{os_error}", file=sys.stderr)
sys.exit(1)
ifwhitespaceandunset_variables:
j2_template=Template(template)
elifwhitespace:
j2_template=Template(template, undefined=StrictUndefined)
elifunset_variables:
j2_template=Template(template, trim_blocks=True, lstrip_blocks=True)
else:
j2_template=Template(template, undefined=StrictUndefined, trim_blocks=True, lstrip_blocks=True)
print(j2_template.render(params))
if__name__=='__main__':
importargparse
arguments=None
parser=argparse.ArgumentParser(description='Simple Jinja2 CLI tool')
parser.add_argument('-t', '--template', type=str, default=None, help='template file', required=True)
parser.add_argument('-p', '--parameters', type=str, default=None, help='parameters file', required=True)
parser.add_argument('-w', '--whitespace', help='enable white-space controls', default=False, action='store_true')
parser.add_argument('-u', '--unset', help='allow unset variables', default=False, action='store_true')
parser.add_argument('-v', '--verbose', action='count', default=0)
args=parser.parse_args()
render_template(
template_filename=args.template,
parameters_filename=args.parameters,
whitespace=args.whitespace,
unset_variables=args.unset,
verbose=args.verbose
)
exit(0)