Merge feature/jinja2-preprocessor: Jinja2 templating for YAML harness definitions
Resolves conflict in wv_cli.py by taking the Jinja2 template rendering approach, which subsumes plain file reading (upstream PR #382).
This commit is contained in:
commit
2faaa4dbf7
7
examples/jinja/connectors.yml
Normal file
7
examples/jinja/connectors.yml
Normal 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
21
examples/jinja/top.yml
Normal 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]
|
||||||
1
setup.py
1
setup.py
@ -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",
|
||||||
|
|||||||
@ -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 = file_read_text(file)
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user