Created
October 21, 2016 10:29
-
-
Save anonymous/feb4b6693f5394b392d63571799e5736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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