James Kuszmaul | 55d9fc7 | 2020-05-10 18:58:08 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import argparse |
James Kuszmaul | 55d9fc7 | 2020-05-10 18:58:08 -0700 | [diff] [blame] | 4 | import json |
| 5 | import sys |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame] | 6 | from pathlib import Path |
James Kuszmaul | 55d9fc7 | 2020-05-10 18:58:08 -0700 | [diff] [blame] | 7 | |
| 8 | import jinja2 |
| 9 | |
| 10 | |
| 11 | def main(): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 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.") |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame] | 17 | parser.add_argument("template", |
| 18 | type=Path, |
| 19 | help="File to use for template.") |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 20 | parser.add_argument( |
Philipp Schrader | 4187e17 | 2024-04-03 11:45:19 -0700 | [diff] [blame^] | 21 | "--replacements_file", |
| 22 | type=Path, |
| 23 | help=("File containing a dictionary of parameters to replace " |
| 24 | "in the template. The behaviour is undefined if keys are " |
| 25 | "duplicated between this file and the `replacements` argument."), |
| 26 | ) |
| 27 | parser.add_argument( |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 28 | "replacements", |
| 29 | type=json.loads, |
| 30 | help="Dictionary of parameters to replace in the template.") |
James Kuszmaul | ba43b06 | 2024-01-14 17:29:21 -0800 | [diff] [blame] | 31 | parser.add_argument( |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame] | 32 | "--include_dir", |
| 33 | action="append", |
| 34 | type=Path, |
| 35 | default=[], |
| 36 | help="One or more search directories for {% include %} blocks.", |
| 37 | ) |
| 38 | parser.add_argument("output", type=Path, help="Output file to create.") |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 39 | args = parser.parse_args(sys.argv[1:]) |
James Kuszmaul | 55d9fc7 | 2020-05-10 18:58:08 -0700 | [diff] [blame] | 40 | |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame] | 41 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(args.include_dir)) |
| 42 | template = env.from_string(args.template.read_text()) |
James Kuszmaul | 55d9fc7 | 2020-05-10 18:58:08 -0700 | [diff] [blame] | 43 | |
Philipp Schrader | 4187e17 | 2024-04-03 11:45:19 -0700 | [diff] [blame^] | 44 | replacements = args.replacements.copy() |
| 45 | if args.replacements_file: |
| 46 | with args.replacements_file.open() as file: |
| 47 | replacements.update(json.load(file)) |
| 48 | |
| 49 | args.output.write_text(template.render(replacements)) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 50 | |
James Kuszmaul | 55d9fc7 | 2020-05-10 18:58:08 -0700 | [diff] [blame] | 51 | |
| 52 | if __name__ == '__main__': |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 53 | main() |