From 98e636ff25fef3c67ab4103861546b7e808b7ad2 Mon Sep 17 00:00:00 2001 From: Matthew Wildoer Date: Wed, 27 Dec 2023 17:02:29 -0800 Subject: [PATCH] Jinja templating --- .vscode/settings.json.j2 | 3 + ato.yaml | 6 -- ato.yaml.j2 | 6 ++ configure.py | 65 +++++++++++++++++++ ...ate123.kicad_pcb => __name__.kicad_pcb.j2} | 6 +- ...ate123.kicad_pro => __name__.kicad_pro.j2} | 2 +- ...ate123.kicad_sch => __name__.kicad_sch.j2} | 0 elec/src/__name__.ato.j2 | 2 + elec/src/template123.ato | 2 - 9 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 .vscode/settings.json.j2 delete mode 100644 ato.yaml create mode 100644 ato.yaml.j2 create mode 100644 configure.py rename elec/layout/default/{template123.kicad_pcb => __name__.kicad_pcb.j2} (99%) rename elec/layout/default/{template123.kicad_pro => __name__.kicad_pro.j2} (98%) rename elec/layout/default/{template123.kicad_sch => __name__.kicad_sch.j2} (100%) create mode 100644 elec/src/__name__.ato.j2 delete mode 100644 elec/src/template123.ato diff --git a/.vscode/settings.json.j2 b/.vscode/settings.json.j2 new file mode 100644 index 0000000..8e3d7f2 --- /dev/null +++ b/.vscode/settings.json.j2 @@ -0,0 +1,3 @@ +{ + "python.defaultInterpreterPath": "{{python_path}}" +} diff --git a/ato.yaml b/ato.yaml deleted file mode 100644 index 08314ed..0000000 --- a/ato.yaml +++ /dev/null @@ -1,6 +0,0 @@ -ato-version: ^0.1.2 -builds: - default: - entry: elec/src/template123.ato:template123 -dependencies: -- generics \ No newline at end of file diff --git a/ato.yaml.j2 b/ato.yaml.j2 new file mode 100644 index 0000000..f096cbf --- /dev/null +++ b/ato.yaml.j2 @@ -0,0 +1,6 @@ +ato-version: ^0.1.8 +builds: + default: + entry: elec/src/{{caseconverter.kebabcase(name)}}.ato:{{caseconverter.pascalcase(name)}} +dependencies: +- generics diff --git a/configure.py b/configure.py new file mode 100644 index 0000000..d6c39fd --- /dev/null +++ b/configure.py @@ -0,0 +1,65 @@ +import sys +from pathlib import Path + +import caseconverter +import click +from jinja2 import Environment, FileSystemLoader + + +MAGIC_WORD = "please" + + +@click.command() +@click.argument("name") +@click.argument("magic_word") +@click.option("--debug", is_flag=True, default=False, help="Enable debug mode.") +def main(name: str, magic_word: str, debug: bool): + """ + This is a script to configure the project. + + It's intended to be called by the `ato create` command. + If you're calling it manually, there's likely something + wrong and you should probably stop. + + This script is intended to be run in the same environment + as the ato CLI, so it's expecting to have access to the + same packages and tools; Jinja, etc... + """ + if magic_word != MAGIC_WORD: + raise click.BadArgumentUsage( + "This script is intended to be called by the ato CLI." + ) + + # Common variables + extended_globals = { + "name": name, + "caseconverter": caseconverter, + "cwd": Path.cwd(), + } + + # Load templates + env = Environment(loader=FileSystemLoader(".")) + + for template_path in Path(".").glob("**/*.j2"): + # Figure out the target path and variables and what not + target_path = template_path.parent / template_path.name.replace( + ".j2", "" + ).replace("__name__", caseconverter.kebabcase(name)) + + extended_globals["rel_path"] = target_path + extended_globals["python_path"] = sys.executable + + template = env.get_template(str(template_path), globals=extended_globals) + + # Make the noise! + with target_path.open("w") as f: + for chunk in template.generate(): + f.write(chunk) + + # Remove the template + if not debug: + template_path.unlink() + + +if __name__ == "__main__": + main() # pylint: disable=no-value-for-parameter diff --git a/elec/layout/default/template123.kicad_pcb b/elec/layout/default/__name__.kicad_pcb.j2 similarity index 99% rename from elec/layout/default/template123.kicad_pcb rename to elec/layout/default/__name__.kicad_pcb.j2 index f30030a..da62ba7 100644 --- a/elec/layout/default/template123.kicad_pcb +++ b/elec/layout/default/__name__.kicad_pcb.j2 @@ -1725,13 +1725,13 @@ (stroke (width 0.2) (type default)) (fill none) (layer "Edge.Cuts") (tstamp e1dec10d-8b7a-4e73-adf9-1aab96be1971)) (gr_line (start 225.62 117.79) (end 257.62 117.79) (stroke (width 0.1) (type solid)) (layer "Edge.Cuts") (tstamp e1f36007-0f9d-4dc5-ae43-6b056652526f)) - (gr_text "{{GITHASH}} " (at 152.4 139.7) (layer "F.SilkS") (tstamp 46268c7f-4a2b-440e-af15-46add3884cdf) + (gr_text "{{"{{GITHASH}}"}}" (at 152.4 139.7) (layer "F.SilkS") (tstamp 46268c7f-4a2b-440e-af15-46add3884cdf) (effects (font (size 2 2) (thickness 0.1)) (justify left bottom)) ) - (gr_text "{{GITHASH}} " (at 62.23 146.05) (layer "F.SilkS") (tstamp 47ada779-5919-4cca-9f25-e816e5b53339) + (gr_text "{{"{{GITHASH}}"}}" (at 62.23 146.05) (layer "F.SilkS") (tstamp 47ada779-5919-4cca-9f25-e816e5b53339) (effects (font (size 2 2) (thickness 0.1)) (justify left bottom)) ) - (gr_text "{{GITHASH}} " (at 232.508427 115.791573) (layer "F.SilkS") (tstamp d894e23f-c5ed-4336-947e-ac38e533f04c) + (gr_text "{{"{{GITHASH}}"}}" (at 232.508427 115.791573) (layer "F.SilkS") (tstamp d894e23f-c5ed-4336-947e-ac38e533f04c) (effects (font (size 2 2) (thickness 0.1)) (justify left bottom)) ) diff --git a/elec/layout/default/template123.kicad_pro b/elec/layout/default/__name__.kicad_pro.j2 similarity index 98% rename from elec/layout/default/template123.kicad_pro rename to elec/layout/default/__name__.kicad_pro.j2 index 84f9b7d..28e2b24 100644 --- a/elec/layout/default/template123.kicad_pro +++ b/elec/layout/default/__name__.kicad_pro.j2 @@ -215,7 +215,7 @@ "last_paths": { "gencad": "", "idf": "", - "netlist": "../../../build/default/.net", + "netlist": "../../../build/default/{{caseconverter.kebabcase(name)}}.net", "specctra_dsn": "", "step": "", "vrml": "" diff --git a/elec/layout/default/template123.kicad_sch b/elec/layout/default/__name__.kicad_sch.j2 similarity index 100% rename from elec/layout/default/template123.kicad_sch rename to elec/layout/default/__name__.kicad_sch.j2 diff --git a/elec/src/__name__.ato.j2 b/elec/src/__name__.ato.j2 new file mode 100644 index 0000000..bbf8031 --- /dev/null +++ b/elec/src/__name__.ato.j2 @@ -0,0 +1,2 @@ +module {{caseconverter.pascalcase(name)}}: + signal gnd diff --git a/elec/src/template123.ato b/elec/src/template123.ato deleted file mode 100644 index 9d80c70..0000000 --- a/elec/src/template123.ato +++ /dev/null @@ -1,2 +0,0 @@ -module template123: - signal gnd