blob: 82222c3632509ce29bfd2e6dfe6f0d13ccc7973e [file] [log] [blame]
James Kuszmaul55d9fc72020-05-10 18:58:08 -07001#!/usr/bin/python3
2
3import argparse
James Kuszmaul55d9fc72020-05-10 18:58:08 -07004import json
5import sys
6
7import jinja2
8
9
10def main():
Ravago Jones5127ccc2022-07-31 16:32:45 -070011 # Note: this is a pretty transparent interface to jinja2--there's no reason
12 # this script couldn't be renamed and then used to generate any config from
13 # a template.
14 parser = argparse.ArgumentParser(
15 description="Generates the raspberry pi configs from a template.")
16 parser.add_argument("template", type=str, help="File to use for template.")
17 parser.add_argument(
18 "replacements",
19 type=json.loads,
20 help="Dictionary of parameters to replace in the template.")
21 parser.add_argument("output", type=str, help="Output file to create.")
James Kuszmaulba43b062024-01-14 17:29:21 -080022 parser.add_argument(
23 "genfiles_dir",
24 type=str,
25 help="Directory where generated JSON files will be available.")
Ravago Jones5127ccc2022-07-31 16:32:45 -070026 args = parser.parse_args(sys.argv[1:])
James Kuszmaul55d9fc72020-05-10 18:58:08 -070027
Ravago Jones5127ccc2022-07-31 16:32:45 -070028 with open(args.template, 'r') as input_file:
James Kuszmaulba43b062024-01-14 17:29:21 -080029 template = jinja2.Environment(loader=jinja2.FileSystemLoader(
30 [".", args.genfiles_dir])).from_string(input_file.read())
James Kuszmaul55d9fc72020-05-10 18:58:08 -070031
Ravago Jones5127ccc2022-07-31 16:32:45 -070032 output = template.render(args.replacements)
33 with open(args.output, 'w') as config_file:
34 config_file.write(output)
35
James Kuszmaul55d9fc72020-05-10 18:58:08 -070036
37if __name__ == '__main__':
Ravago Jones5127ccc2022-07-31 16:32:45 -070038 main()