aboutsummaryrefslogtreecommitdiff
path: root/contrib/render.py
blob: d31c7f9bdf76ac5697c1d821309cc90616841d8b (plain)
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
#!/usr/bin/python
# This file is in the public domain.

"""
Expand Jinja2 templates based on JSON input.

First command-line argument must be the JSON input.
The tool reads the template from stdin and writes
the expanded output to stdout.

@author Christian Grothoff
"""

import sys
import json
import jinja2
from jinja2 import BaseLoader


class StdinLoader(BaseLoader):
     def __init__ (self):
         self.path = '-'
     def get_source(self, environment, template):
              source = sys.stdin.read().decode('utf-8')
              return source, self.path, lambda: false
  

jsonFile = open (sys.argv[1], 'r')
jsonData = json.load(jsonFile)

jinjaEnv = jinja2.Environment(loader=StdinLoader(),
                              lstrip_blocks=True,
                              trim_blocks=True,
                              undefined=jinja2.StrictUndefined,
                              autoescape=False)
tmpl = jinjaEnv.get_template('stdin');

print(tmpl.render(data = jsonData))