exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

taler-helper-auditor-render.py (1918B)


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