Skip to content

Commit 3564614

Browse files
committed
Add optional tweak per node with name placeholder
Implements feature suggested in #349
1 parent a3281b0 commit 3564614

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

docs/syntax.md

+11
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ tweak: # optional tweaking of .gv output
8585
# loops
8686
loops: <List> # every list item is itself a list of exactly two pins
8787
# on the connector that are to be shorted
88+
89+
# optional tweaking of .gv output executed for each instance of this connector
90+
tweak: # see below
91+
8892
```
8993

9094
## Cable attributes
@@ -148,6 +152,9 @@ tweak: # optional tweaking of .gv output
148152
show_wirecount: <bool> # defaults to true
149153
show_wirenumbers: <bool> # defaults to true for cables; false for bundles
150154

155+
# optional tweaking of .gv output executed for each instance of this cable
156+
tweak: # see below
157+
151158
```
152159

153160
## Connection sets
@@ -446,6 +453,10 @@ Alternatively items can be added to just the BOM by putting them in the section
446453
# This feature is experimental and might change
447454
# or be removed in future versions.
448455
456+
placeholder: <str> # Substring to be replaced with name of connector or
457+
# cable in all strings below for tweak in any of those
458+
# sections. Default value is the global placeholder.
459+
449460
override: # dict of .gv entries to override
450461
# Each entry is identified by its leading string
451462
# in lines beginning with a TAB character.

src/wireviz/DataClasses.py

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def __post_init__(self):
7272

7373
@dataclass
7474
class Tweak:
75+
placeholder: Optional[PlainText] = None
7576
override: Optional[Dict[Designator, Dict[str, Optional[str]]]] = None
7677
append: Union[str, List[str], None] = None
7778

@@ -164,11 +165,14 @@ class Connector:
164165
loops: List[List[Pin]] = field(default_factory=list)
165166
ignore_in_bom: bool = False
166167
additional_components: List[AdditionalComponent] = field(default_factory=list)
168+
tweak: Optional[Tweak] = None
167169

168170
def __post_init__(self) -> None:
169171

170172
if isinstance(self.image, dict):
171173
self.image = Image(**self.image)
174+
if self.tweak is not None:
175+
self.tweak = Tweak(**self.tweak)
172176

173177
self.ports_left = False
174178
self.ports_right = False
@@ -274,11 +278,14 @@ class Cable:
274278
show_wirenumbers: Optional[bool] = None
275279
ignore_in_bom: bool = False
276280
additional_components: List[AdditionalComponent] = field(default_factory=list)
281+
tweak: Optional[Tweak] = None
277282

278283
def __post_init__(self) -> None:
279284

280285
if isinstance(self.image, dict):
281286
self.image = Image(**self.image)
287+
if self.tweak is not None:
288+
self.tweak = Tweak(**self.tweak)
282289

283290
if isinstance(self.gauge, str): # gauge and unit specified
284291
try:

src/wireviz/Harness.py

+30
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
component_table_entry,
3030
generate_bom,
3131
get_additional_component_table,
32+
make_list,
3233
pn_info_string,
3334
)
3435
from wireviz.wv_colors import get_color_hex, translate_color
@@ -78,12 +79,41 @@ def __post_init__(self):
7879
self._bom = [] # Internal Cache for generated bom
7980
self.additional_bom_items = []
8081

82+
def join_tweak(self, node: Union[Connector, Cable]) -> None:
83+
"""Join node.tweak with self.tweak after replacing placeholders."""
84+
if node.tweak:
85+
ph = node.tweak.placeholder
86+
if ph is None:
87+
ph = self.tweak.placeholder
88+
# Create function rph() to replace placeholder with node name.
89+
rph = (lambda s: s.replace(ph, node.name)) if ph else lambda s: s
90+
n_override = node.tweak.override or {}
91+
s_override = self.tweak.override or {}
92+
for id, n_dict in n_override.items():
93+
id = rph(id)
94+
s_dict = s_override.get(id, {})
95+
for k, v in n_dict.items():
96+
k = rph(k)
97+
v = rph(v)
98+
if k in s_dict and v != s_dict[k]:
99+
ValueError(
100+
f"{node.name}.tweak.override.{id}.{k} conflicts with another"
101+
)
102+
s_dict[k] = v
103+
s_override[id] = s_dict
104+
self.tweak.override = s_override
105+
self.tweak.append = make_list(self.tweak.append) + [
106+
rph(v) for v in make_list(node.tweak.append)
107+
]
108+
81109
def add_connector(self, name: str, *args, **kwargs) -> None:
82110
check_old(f"Connector '{name}'", OLD_CONNECTOR_ATTR, kwargs)
83111
self.connectors[name] = Connector(name, *args, **kwargs)
112+
self.join_tweak(self.connectors[name])
84113

85114
def add_cable(self, name: str, *args, **kwargs) -> None:
86115
self.cables[name] = Cable(name, *args, **kwargs)
116+
self.join_tweak(self.cables[name])
87117

88118
def add_mate_pin(self, from_name, from_pin, to_name, to_pin, arrow_type) -> None:
89119
self.mates.append(MatePin(from_name, from_pin, to_name, to_pin, arrow_type))

0 commit comments

Comments
 (0)