Skip to content

Instantly share code, notes, and snippets.

Created October 21, 2016 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/feb4b6693f5394b392d63571799e5736 to your computer and use it in GitHub Desktop.
Save anonymous/feb4b6693f5394b392d63571799e5736 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import json
import pystache
import sys
ap = argparse.ArgumentParser()
ap.add_argument("--template", metavar="FILE", help="Mustache template file")
ap.add_argument("--data", metavar="FILE", help="JSON data file")
ap.add_argument("--schema", metavar="FILE", help="JSON schema file")
ap.add_argument("--output", metavar="FILE", help="Generated output file")
args = ap.parse_args()
if args.data:
with open(args.data, "r") as fh:
context = json.load(fh)
else:
raise ValueError("data file not specified")
if args.schema:
import jsonschema
with open(args.schema, "r") as fh:
schema = json.load(fh)
jsonschema.validate(context, schema)
if args.template:
with open(args.template, "r") as fh:
template = fh.read()
else:
template = sys.stdin.read()
output = pystache.render(template, context)
if args.output:
with open(args.output, "w") as fh:
fh.write(output)
else:
sys.stdout.write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment