Add jinja2 preprocessor stage
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:
parent
954c4f5f92
commit
4d9346a365
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",
|
||||
"pillow",
|
||||
"graphviz",
|
||||
"jinja2",
|
||||
],
|
||||
license="GPLv3",
|
||||
keywords="cable connector hardware harness wiring wiring-diagram wiring-harness",
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import jinja2
|
||||
|
||||
import click
|
||||
|
||||
@ -50,6 +51,13 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i
|
||||
type=Path,
|
||||
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(
|
||||
"-o",
|
||||
"--output-dir",
|
||||
@ -71,7 +79,7 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i
|
||||
default=False,
|
||||
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.
|
||||
"""
|
||||
@ -115,6 +123,15 @@ def wireviz(file, format, prepend, output_dir, output_name, version):
|
||||
else:
|
||||
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
|
||||
for file in filepaths:
|
||||
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}"
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
yaml_input = prepend_input + yaml_input
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user