From 28b65c84397e74128adc64829f8568968f779d04 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Fri, 2 Aug 2024 12:14:12 -0500 Subject: [PATCH] Add ConnectorTable.extend() method This method allows ConnectorTable consumers to extend the class with an additional connector (or, if they chain calls, multiple connectors). The new connector is written into the new subclass as a part of its `_connectors` tuple and as an attribute. The base ConnectorTable class is left unmodified, meaning that callers who extend the table cannot conflict. --- .../services/gcs/connector_table.py | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/globus_sdk/services/gcs/connector_table.py b/src/globus_sdk/services/gcs/connector_table.py index f5ab331fb..964071e88 100644 --- a/src/globus_sdk/services/gcs/connector_table.py +++ b/src/globus_sdk/services/gcs/connector_table.py @@ -96,7 +96,7 @@ def all_connectors(cls) -> t.Iterable[GlobusConnectServerConnector]: yield item @classmethod - def lookup(cls, name_or_id: str | UUIDLike) -> GlobusConnectServerConnector | None: + def lookup(cls, name_or_id: UUIDLike) -> GlobusConnectServerConnector | None: """ Convert a name or ID into a connector object. Returns None if the name or ID is not recognized. @@ -116,6 +116,42 @@ def lookup(cls, name_or_id: str | UUIDLike) -> GlobusConnectServerConnector | No return connector return None + @classmethod + def extend( + cls, + *, + connector_name: str, + connector_id: UUIDLike, + attribute_name: str | None = None, + ) -> type[ConnectorTable]: + """ + Extend the ConnectorTable class with a new connector, returning a new + ConnectorTable subclass. + + :param connector_name: The name of the connector to add + :param connector_id: The ID of the connector to add + :param attribute_name: The attribute name with which the connector will be + attached to the new subclass. Defaults to the connector name uppercased and + with spaces converted to underscores. + """ + if attribute_name is None: + attribute_name = connector_name.upper().replace(" ", "_") + connector_id_str = str(connector_id) + + connectors = cls._connectors + ( + attribute_name, + connector_name, + connector_id_str, + ) + connector_obj = GlobusConnectServerConnector( + name=connector_name, connector_id=connector_id_str + ) + return type( + "ExtendedConnectorTable", + (ConnectorTable,), + {"_connectors": connectors, attribute_name: connector_obj}, + ) + # "render" the _connectors to live attributes of the ConnectorTable for _attribute, _name, _id in ConnectorTable._connectors: