forked from mtiller/ModelicaWebRef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jinjafy.py
executable file
·60 lines (46 loc) · 1.37 KB
/
jinjafy.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
#!/usr/bin/env python
"""
command-line wrapper for Jinja2
Usage:
% jinjafy.py [-o output_file] [-v vars_file] template_file
"""
import argparse
import json
import jinja2
import os
parser = argparse.ArgumentParser()
parser.add_argument("template_file",
help="File containing Jinja2 markup")
parser.add_argument("-o", "--output",
help="Name of output file")
parser.add_argument("-v", "--vars", default="vars.json",
help="Name of file containing JSON encoded dictionary")
args = parser.parse_args()
template_dir = os.path.dirname(os.path.abspath(args.template_file))
if args.output==None:
(output_file, ext) = os.path.splitext(os.path.abspath(args.template_file))
else:
output_file = args.output
loader = jinja2.FileSystemLoader(template_dir)
env = jinja2.Environment(loader=loader)
# Read in JSON data
if args.vars==None:
print "No vars file specified"
vars = {}
else:
try:
vf = open(args.vars, "r")
jsondata = vf.read()
vf.close()
vars = json.loads(jsondata)
except Exception as e:
print "Unable to open vars file %s: %s" % (args.vars, str(e))
vars = {}
#print "Variables:"
#for k in vars:
# print " '%s'='%s'" % (k, vars[k])
template = env.get_template(args.template_file)
result = template.render(**vars)
fp = open(output_file, "w")
fp.write(result)
fp.close()