Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jinja2 preprocessor stage #382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/jinja/connectors.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions examples/jinja/top.yml
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"pyyaml",
"pillow",
"graphviz",
"jinja2",
],
license="GPLv3",
keywords="cable connector hardware harness wiring wiring-diagram wiring-harness",
Expand Down
25 changes: 23 additions & 2 deletions src/wireviz/wv_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
from pathlib import Path
import jinja2

import click

Expand Down Expand Up @@ -50,6 +51,13 @@
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",
Expand All @@ -71,7 +79,7 @@
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.
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down