#!/usr/bin/env python3 """ Used as a tool by the Makefile to generate sources from the assets.toml file. """ import toml from mako.template import Template def update_file(fname, contents): """ Updates the given file with the given contents unless .fname.hash matches hash(contents). """ try: with open(f'.{fname}.hash', 'r') as fhash: if fhash.read() == str(contents): return except: pass with open(fname, 'w') as f: f.write(contents) def main(infile, outdir): """ Entry point: generates outputs based on data in input files. """ with open(infile, 'r') as f: data = toml.load(infile) for template_name in data: template = Template(filename=template_name) for output_name in data[template_name]: result = template.render(**data[template_name][output_name]) update_file(outdir + '/' + output_name, result)