From bf90fe4937aa9b46854971d3b63321ac28c0474b Mon Sep 17 00:00:00 2001 From: KV Date: Wed, 22 May 2024 02:22:42 +0200 Subject: [PATCH 1/2] Look-up mated connectors before mate processing (#358) Symptom reported in #355: Unable to connect an arrow (mate) to pins higher than 1 without failing: ValueError: X is not in list Bug: The code processing mates used a mix of repeated connector look-ups and local connector variables, and one variable was used before it was assigned the correct value. Fix: The local connector variables are now both assigned initially before processing each mate, and used when processing instead of repeated connector look-ups. --- src/wireviz/Harness.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 30468a6a..51287a2f 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -595,6 +595,7 @@ def typecheck(name: str, value: Any, expect: type) -> None: typecheck("tweak.append", self.tweak.append, str) dot.body.append(self.tweak.append) + # TODO: All code below until "return dot" must be moved above all tweak processing! for mate in self.mates: if mate.shape[0] == "<" and mate.shape[-1] == ">": dir = "both" @@ -613,29 +614,18 @@ def typecheck(name: str, value: Any, expect: type) -> None: raise Exception(f"{mate} is an unknown mate") from_connector = self.connectors[mate.from_name] - if ( - isinstance(mate, MatePin) - and self.connectors[mate.from_name].style != "simple" - ): + to_connector = self.connectors[mate.to_name] + if isinstance(mate, MatePin) and from_connector.style != "simple": from_pin_index = from_connector.pins.index(mate.from_pin) from_port_str = f":p{from_pin_index+1}r" else: # MateComponent or style == 'simple' from_port_str = "" - if ( - isinstance(mate, MatePin) - and self.connectors[mate.to_name].style != "simple" - ): + if isinstance(mate, MatePin) and to_connector.style != "simple": to_pin_index = to_connector.pins.index(mate.to_pin) - to_port_str = ( - f":p{to_pin_index+1}l" - if isinstance(mate, MatePin) - and self.connectors[mate.to_name].style != "simple" - else "" - ) + to_port_str = f":p{to_pin_index+1}l" else: # MateComponent or style == 'simple' to_port_str = "" code_from = f"{mate.from_name}{from_port_str}:e" - to_connector = self.connectors[mate.to_name] code_to = f"{mate.to_name}{to_port_str}:w" dot.attr("edge", color=color, style="dashed", dir=dir) From bae4d603e609cfdde872ec79f1d6d3eedb2ddc62 Mon Sep 17 00:00:00 2001 From: KV Date: Wed, 29 May 2024 22:49:08 +0200 Subject: [PATCH 2/2] Move mates processing above tweak processing (#358) Bug: Not all generated dot output could be changed by tweak entries. Seen in https://github.com/wireviz/WireViz/issues/325#issuecomment-2116395221 Tweak processing must be the very last dot producing code to enable tweaking any dot output. Fix: Move all other dot producing code above Tweak processing. --- src/wireviz/Harness.py | 70 +++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 51287a2f..5ef7ad02 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -530,6 +530,38 @@ def create_graph(self) -> Graph: fillcolor=translate_color(bgcolor, "HEX"), ) + # mates + for mate in self.mates: + if mate.shape[-1] == ">": + dir = "both" if mate.shape[0] == "<" else "forward" + else: + dir = "back" if mate.shape[0] == "<" else "none" + + if isinstance(mate, MatePin): + color = "#000000" + elif isinstance(mate, MateComponent): + color = "#000000:#000000" + else: + raise Exception(f"{mate} is an unknown mate") + + from_connector = self.connectors[mate.from_name] + to_connector = self.connectors[mate.to_name] + if isinstance(mate, MatePin) and from_connector.style != "simple": + from_pin_index = from_connector.pins.index(mate.from_pin) + from_port_str = f":p{from_pin_index+1}r" + else: # MateComponent or style == 'simple' + from_port_str = "" + if isinstance(mate, MatePin) and to_connector.style != "simple": + to_pin_index = to_connector.pins.index(mate.to_pin) + to_port_str = f":p{to_pin_index+1}l" + else: # MateComponent or style == 'simple' + to_port_str = "" + code_from = f"{mate.from_name}{from_port_str}:e" + code_to = f"{mate.to_name}{to_port_str}:w" + + dot.attr("edge", color=color, style="dashed", dir=dir) + dot.edge(code_from, code_to) + def typecheck(name: str, value: Any, expect: type) -> None: if not isinstance(value, expect): raise Exception( @@ -595,41 +627,9 @@ def typecheck(name: str, value: Any, expect: type) -> None: typecheck("tweak.append", self.tweak.append, str) dot.body.append(self.tweak.append) - # TODO: All code below until "return dot" must be moved above all tweak processing! - for mate in self.mates: - if mate.shape[0] == "<" and mate.shape[-1] == ">": - dir = "both" - elif mate.shape[0] == "<": - dir = "back" - elif mate.shape[-1] == ">": - dir = "forward" - else: - dir = "none" - - if isinstance(mate, MatePin): - color = "#000000" - elif isinstance(mate, MateComponent): - color = "#000000:#000000" - else: - raise Exception(f"{mate} is an unknown mate") - - from_connector = self.connectors[mate.from_name] - to_connector = self.connectors[mate.to_name] - if isinstance(mate, MatePin) and from_connector.style != "simple": - from_pin_index = from_connector.pins.index(mate.from_pin) - from_port_str = f":p{from_pin_index+1}r" - else: # MateComponent or style == 'simple' - from_port_str = "" - if isinstance(mate, MatePin) and to_connector.style != "simple": - to_pin_index = to_connector.pins.index(mate.to_pin) - to_port_str = f":p{to_pin_index+1}l" - else: # MateComponent or style == 'simple' - to_port_str = "" - code_from = f"{mate.from_name}{from_port_str}:e" - code_to = f"{mate.to_name}{to_port_str}:w" - - dot.attr("edge", color=color, style="dashed", dir=dir) - dot.edge(code_from, code_to) + # Tweak processing above must be the last before returning dot. + # Please don't insert any code that might change the dot contents + # after tweak processing. return dot