This repository has been archived on 2019-12-12. You can view files and clone it, but cannot push or open issues/pull-requests.
tildetown-scripts/tildetown/mustache.py

31 lines
801 B
Python
Raw Normal View History

#!/usr/bin/env python3
import json
import sys
2015-10-05 23:41:42 +00:00
from pystache import render
2017-04-21 05:00:25 +00:00
def slurp(file_path):
contents = None
try:
with open(file_path, 'r', encoding="utf-8") as f:
contents = f.read()
except FileNotFoundError:
pass
except UnicodeDecodeError:
pass
return contents
2017-04-22 04:56:02 +00:00
# when you install scripts with entry_points in a setup.py, the resulting
# executable just calls main() and you have to look up sys.argv yourself. I like to explicitly take an argv in my actual main, hence the weird indirection. could probably be better.
def _main(argv):
template = slurp(argv[1])
data = json.loads(sys.stdin.read())
sys.stdout.write(render(template, data))
2015-10-05 23:41:42 +00:00
2017-04-22 04:56:02 +00:00
def main():
_main(sys.argv)
2015-10-05 23:41:42 +00:00
if __name__ == '__main__':
2017-04-22 04:56:02 +00:00
exit(_main(sys.argv))
2015-10-05 23:41:42 +00:00