Add jinja2 preprocessor stage
Some checks failed
Create Examples / build (3.7) (push) Has been cancelled
Create Examples / build (3.8) (push) Has been cancelled

This allows users to leverage jinja to include other files but also
loops, conditions, and other templating features.

Signed-off-by: Liam Beguin <liambeguin@gmail.com>
This commit is contained in:
Liam Beguin 2024-06-09 17:51:28 -04:00
parent 954c4f5f92
commit 4d9346a365
4 changed files with 52 additions and 2 deletions

View File

@ -0,0 +1,7 @@
connectors:
{%- for i in range(1, 5) %}
X{{i}}:
type: Molex KK 254
subtype: female
pinlabels: [GND, RX, TX]
{%- endfor %}

21
examples/jinja/top.yml Normal file
View File

@ -0,0 +1,21 @@
{% include 'connectors.yml' %}
cables:
{%- for i in range(1, 3) %}
W{{i}}:
gauge: 0.25 mm2
length: 0.2
color_code: DIN
wirecount: 3
shield: true
{%- endfor %}
connections:
-
- X1: [1,2,3]
- W1: [1,2,3]
- X2: [1,2,3]
-
- X3: [1,2,3]
- W2: [1,2,3]
- X4: [1,2,3]

View File

@ -22,6 +22,7 @@ setup(
"pyyaml", "pyyaml",
"pillow", "pillow",
"graphviz", "graphviz",
"jinja2",
], ],
license="GPLv3", license="GPLv3",
keywords="cable connector hardware harness wiring wiring-diagram wiring-harness", keywords="cable connector hardware harness wiring wiring-diagram wiring-harness",

View File

@ -3,6 +3,7 @@
import os import os
import sys import sys
from pathlib import Path from pathlib import Path
import jinja2
import click import click
@ -50,6 +51,13 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i
type=Path, type=Path,
help="YAML file to prepend to the input file (optional).", help="YAML file to prepend to the input file (optional).",
) )
@click.option(
"-I",
"--include-path",
default=None,
type=Path,
help="Include path used for Jinja2 templates",
)
@click.option( @click.option(
"-o", "-o",
"--output-dir", "--output-dir",
@ -71,7 +79,7 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i
default=False, default=False,
help=f"Output {APP_NAME} version and exit.", help=f"Output {APP_NAME} version and exit.",
) )
def wireviz(file, format, prepend, output_dir, output_name, version): def wireviz(file, format, prepend, include_path, output_dir, output_name, version):
""" """
Parses the provided FILE and generates the specified outputs. Parses the provided FILE and generates the specified outputs.
""" """
@ -115,6 +123,15 @@ def wireviz(file, format, prepend, output_dir, output_name, version):
else: else:
prepend_input = "" prepend_input = ""
searchpath = [Path(f).parent for f in filepaths]
if include_path is not None:
searchpath.append(include_path)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(searchpath=searchpath),
)
# run WireVIz on each input file # run WireVIz on each input file
for file in filepaths: for file in filepaths:
file = Path(file) file = Path(file)
@ -130,7 +147,11 @@ def wireviz(file, format, prepend, output_dir, output_name, version):
"Output file: ", f"{Path(_output_dir / _output_name)}.{output_formats_str}" "Output file: ", f"{Path(_output_dir / _output_name)}.{output_formats_str}"
) )
yaml_input = open_file_read(file).read() template = env.get_template(file.name)
yaml_input = template.render()
with open(Path(_output_dir / (_output_name + '.rendered.yml')), 'w') as f:
f.write(yaml_input)
file_dir = file.parent file_dir = file.parent
yaml_input = prepend_input + yaml_input yaml_input = prepend_input + yaml_input