Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConnectorTable.extend() method #1021

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/globus_sdk/services/gcs/connector_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
Loading