-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.py
More file actions
executable file
·48 lines (36 loc) · 1.24 KB
/
template.py
File metadata and controls
executable file
·48 lines (36 loc) · 1.24 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
"""
template.py
Takes a jinja2 template and injects values into it from the command line.
Can operate with a template file or from stdin. Replacements can be supplied as an argument in JSON format or from env.
Usage:
template.py [<replacements>] [-f <path> | --file=<path>] [-e | --env]
template.py - [<replacements>] [-e | --env]
template.py (-h | --help)
Options:
-h --help Show this screen.
-f <path> --file=<path> Template from file at path.
-e --env Replace from environment variables instead of JSON argument.
"""
import os
import sys
import json
from docopt import docopt
from jinja2 import Template
def replace(template, replacements):
return Template(template).render(**replacements)
def main(args):
template = ""
if args["--file"]:
with open(args["--file"], "r") as template_file:
template = template_file.read()
else:
template = sys.stdin.read()
replacements = {}
if args["<replacements>"]:
replacements = json.loads(args["<replacements>"])
elif args["--env"]:
replacements = os.environ
sys.stdout.write(replace(template, replacements))
sys.stdout.close()
if __name__ == "__main__":
main(docopt(__doc__, version="1"))