blob: 047a80534d5bc132e186af351ddec431aaa4a1fe [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
Philipp Schrader75f1c262024-04-03 11:32:58 -07006from pathlib import Path
James Kuszmaul55d9fc72020-05-10 18:58:08 -07007
8import jinja2
9
10
11def main():
Ravago Jones5127ccc2022-07-31 16:32:45 -070012 # 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.")
Philipp Schrader75f1c262024-04-03 11:32:58 -070017 parser.add_argument("template",
18 type=Path,
19 help="File to use for template.")
Ravago Jones5127ccc2022-07-31 16:32:45 -070020 parser.add_argument(
21 "replacements",
22 type=json.loads,
23 help="Dictionary of parameters to replace in the template.")
James Kuszmaulba43b062024-01-14 17:29:21 -080024 parser.add_argument(
Philipp Schrader75f1c262024-04-03 11:32:58 -070025 "--include_dir",
26 action="append",
27 type=Path,
28 default=[],
29 help="One or more search directories for {% include %} blocks.",
30 )
31 parser.add_argument("output", type=Path, help="Output file to create.")
Ravago Jones5127ccc2022-07-31 16:32:45 -070032 args = parser.parse_args(sys.argv[1:])
James Kuszmaul55d9fc72020-05-10 18:58:08 -070033
Philipp Schrader75f1c262024-04-03 11:32:58 -070034 env = jinja2.Environment(loader=jinja2.FileSystemLoader(args.include_dir))
35 template = env.from_string(args.template.read_text())
James Kuszmaul55d9fc72020-05-10 18:58:08 -070036
Philipp Schrader75f1c262024-04-03 11:32:58 -070037 args.output.write_text(template.render(args.replacements))
Ravago Jones5127ccc2022-07-31 16:32:45 -070038
James Kuszmaul55d9fc72020-05-10 18:58:08 -070039
40if __name__ == '__main__':
Ravago Jones5127ccc2022-07-31 16:32:45 -070041 main()