James Kuszmaul | 55d9fc7 | 2020-05-10 18:58:08 -0700 | [diff] [blame^] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import argparse |
| 4 | from pathlib import Path |
| 5 | import json |
| 6 | import sys |
| 7 | |
| 8 | import jinja2 |
| 9 | |
| 10 | |
| 11 | def main(): |
| 12 | # Note: this is a pretty transparent interface to jinja2--there's no reason |
| 13 | # this script couldn't be renamed and then used to generate any config from |
| 14 | # a template. |
| 15 | parser = argparse.ArgumentParser( |
| 16 | description="Generates the raspberry pi configs from a template.") |
| 17 | parser.add_argument("template", type=str, help="File to use for template.") |
| 18 | parser.add_argument("replacements", type=json.loads, help="Dictionary of parameters to replace in the template.") |
| 19 | parser.add_argument("output", type=str, help="Output file to create.") |
| 20 | args = parser.parse_args(sys.argv[1:]) |
| 21 | |
| 22 | with open(args.template, 'r') as input_file: |
| 23 | template = jinja2.Template(input_file.read()) |
| 24 | |
| 25 | output = template.render(args.replacements) |
| 26 | with open(args.output, 'w') as config_file: |
| 27 | config_file.write(output) |
| 28 | |
| 29 | if __name__ == '__main__': |
| 30 | main() |