From 8f5b1aaf16d1d1d0a34b50b04e79c0575d8d8225 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Thu, 9 Jul 2020 19:21:53 +0200 Subject: [PATCH] Allow using connector pin names to define connections Pin names from pinout list can now be used in addition to pin numbers when defining connections, provided the identifier is unambiguous. Closes #71. --- src/wireviz/Harness.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 394640ac..bcc3d28c 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -26,6 +26,30 @@ def loop(self, connector_name, from_pin, to_pin): self.connectors[connector_name].loop(from_pin, to_pin) def connect(self, from_name, from_pin, via_name, via_pin, to_name, to_pin): + + for (name, pin) in zip([from_name, to_name], [from_pin, to_pin]): # check from and to connectors + if name is not None and name in self.connectors: + connector = self.connectors[name] + if pin in connector.pinnumbers and pin in connector.pinout: + if connector.pinnumbers.index(pin) == connector.pinout.index(pin): + # TODO: Maybe issue a warning? It's not worthy of an exception if it's unambiguous, but maybe risky? + pass + else: + raise Exception(f'{name}:{pin} is defined both in pinout and pinnumbers, for different pins.') + if pin in connector.pinout: + if connector.pinout.count(pin) > 1: + raise Exception(f'{name}:{pin} is defined more than once.') + else: + index = connector.pinout.index(pin) + pin = connector.pinnumbers[index] # map pin name to pin number + if name == from_name: + from_pin = pin + if name == to_name: + to_pin = pin + # TODO: what happens with loops? + if not pin in connector.pinnumbers: + raise Exception(f'{name}:{pin} not found.') + self.cables[via_name].connect(from_name, from_pin, via_pin, to_name, to_pin) if from_name in self.connectors: self.connectors[from_name].activate_pin(from_pin)