taler-deployment

Deployment scripts and configuration files
Log | Files | Refs | README

render.py (1396B)


      1 #!/usr/bin/python3
      2 # This file is in the public domain.
      3 
      4 """Expand Jinja2 templates based on JSON input.
      5 
      6 The tool then reads the template from stdin and writes the expanded
      7 output to stdout.
      8 
      9 TODO: proper installation, man page, error handling, --help option.
     10 
     11 @author Christian Grothoff
     12 
     13 """
     14 
     15 import sys
     16 import json
     17 import jinja2
     18 from jinja2 import BaseLoader
     19 
     20 
     21 class StdinLoader(BaseLoader):
     22      def __init__ (self):
     23          self.path = '-'
     24      def get_source(self, environment, template):
     25               source = sys.stdin.read()
     26               return source, self.path, lambda: false
     27 
     28 
     29 jsonFile1 = open (sys.argv[1], 'r')
     30 jsonData1 = json.load(jsonFile1)
     31 
     32 jinjaEnv = jinja2.Environment(loader=StdinLoader(),
     33                               lstrip_blocks=True,
     34                               trim_blocks=True,
     35                               undefined=jinja2.StrictUndefined,
     36                               autoescape=False)
     37 tmpl = jinjaEnv.get_template('stdin');
     38 
     39 try:
     40      print(tmpl.render(data = jsonData1))
     41 except jinja2.TemplateSyntaxError as error:
     42      print("Template syntax error: {error.message} on line {error.lineno}.".format(error=error))
     43      exit(1)
     44 except jinja2.UndefinedError as error:
     45      print("Template undefined error: {error.message}.".format(error=error))
     46      exit(1)
     47 except TypeError as error:
     48      print("Template type error: {0}.".format(error.args[0]))
     49      exit(1)