From f2e2de1d2e2f12c6576d6d65483b33d4c573cd79 Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 12 Oct 2023 01:31:44 -0700 Subject: [PATCH] add mkport() for safely making ports --- masque/ports.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/masque/ports.py b/masque/ports.py index fcd3ebf..6179f87 100644 --- a/masque/ports.py +++ b/masque/ports.py @@ -142,6 +142,30 @@ class PortList(metaclass=ABCMeta): # because it's weird on stuff like Pattern that contains other lists # and because you can just grab .ports and use that instead + def mkport( + self, + name: str, + value: Port, + ) -> Self: + """ + Create a port, raising a `PortError` if a port with the same name already exists. + + Args: + name: Name for the port. A port with this name should not already exist. + value: The `Port` object to which `name` will refer. + + Returns: + self + + Raises: + `PortError` if the name already exists. + """ + if name in self.ports: + raise PortError(f'Port {name} already exists.') + assert name not in self.ports + self.ports[name] = value + return self + def rename_ports( self, mapping: dict[str, str | None],