-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrender.py
More file actions
executable file
·55 lines (46 loc) · 1.34 KB
/
render.py
File metadata and controls
executable file
·55 lines (46 loc) · 1.34 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
#!/bin/env python
import argparse
import json
import os
import sys
import chevron
def parse_args():
def is_file_or_pipe(arg):
if not os.path.exists(arg) or os.path.isdir(arg):
parser.error("The file {0} does not exist!".format(arg))
else:
return arg
parser = argparse.ArgumentParser(description="Render templates")
parser.add_argument(
"-d",
"--data",
dest="data",
help="The json data file",
type=is_file_or_pipe,
default={},
)
parser.add_argument(
"-z",
"--zone",
dest="zone",
required=False,
help="The zone name",
default={},
)
parser.add_argument("template", metavar="template", help="The mustache file")
return parser.parse_args()
def main():
args = parse_args()
with open(args.data, "r") as j:
d = json.load(j)
if args.zone:
all_zones = d["hosts"] + d["announcers"] + d["party-zones"]
try:
d["zone"] = next(z for z in all_zones if z["name"] == args.zone)
except StopIteration:
print(f"No zone '{args.zone}' in config '{args.data}'", file=sys.stderr)
sys.exit(1)
with open(args.template, "r") as t:
print(chevron.render(t, d))
if __name__ == "__main__":
main()