| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import dataclasses |
| 4 |
import re |
| 5 |
import typing as t |
| 6 |
import uuid |
| 7 |
|
| 8 |
_NORMALIZATION_PATTERN = re.compile(r"[_\- ]+") |
| 9 |
|
| 10 |
|
| 11 |
def _normalize_name(name: str) -> str: |
| 12 |
return _NORMALIZATION_PATTERN.sub("-", name.strip()).lower() |
| 13 |
|
| 14 |
|
| 15 |
@dataclasses.dataclass |
| 16 |
class GlobusConnectServerConnector: |
| 17 |
""" |
| 18 |
A container for Globus Connect Server Connector descriptions. |
| 19 |
Contains a ``name`` and a ``connector_id``. |
| 20 |
""" |
| 21 |
|
| 22 |
name: str |
| 23 |
connector_id: str |
| 24 |
|
| 25 |
|
| 26 |
class ConnectorTable: |
| 27 |
""" |
| 28 |
This class defines the known Globus Connect Server Connectors in a mapping |
| 29 |
structure. |
| 30 |
|
| 31 |
It supports access by attribute or via a helper method for doing lookups. |
| 32 |
For example, all of the following three usages retrieve the Azure Blob connector: |
| 33 |
|
| 34 |
.. code-block:: pycon |
| 35 |
|
| 36 |
>>> ConnectorTable.AZURE_BLOB |
| 37 |
>>> ConnectorTable.lookup("Azure Blob") |
| 38 |
>>> ConnectorTable.lookup("9436da0c-a444-11eb-af93-12704e0d6a4d") |
| 39 |
|
| 40 |
Given the results of such a lookup, you can retrieve the canonical name and ID for |
| 41 |
a connector like so: |
| 42 |
|
| 43 |
.. code-block:: pycon |
| 44 |
|
| 45 |
>>> connector = ConnectorTable.AZURE_BLOB |
| 46 |
>>> connector.name |
| 47 |
'Azure Blob' |
| 48 |
>>> connector.connector_id |
| 49 |
'9436da0c-a444-11eb-af93-12704e0d6a4d' |
| 50 |
""" |
| 51 |
|
| 52 |
_connectors: t.ClassVar[tuple[tuple[str, str, str], ...]] = ( |
| 53 |
("ACTIVESCALE", "ActiveScale", "7251f6c8-93c9-11eb-95ba-12704e0d6a4d"), |
| 54 |
("AZURE_BLOB", "Azure Blob", "9436da0c-a444-11eb-af93-12704e0d6a4d"), |
| 55 |
("BLACKPEARL", "BlackPearl", "7e3f3f5e-350c-4717-891a-2f451c24b0d4"), |
| 56 |
("BOX", "Box", "7c100eae-40fe-11e9-95a3-9cb6d0d9fd63"), |
| 57 |
("CEPH", "Ceph", "1b6374b0-f6a4-4cf7-a26f-f262d9c6ca72"), |
| 58 |
("DROPBOX", "Dropbox", "49b00fd6-63f1-48ae-b27f-d8af4589f876"), |
| 59 |
( |
| 60 |
"GOOGLE_CLOUD_STORAGE", |
| 61 |
"Google Cloud Storage", |
| 62 |
"56366b96-ac98-11e9-abac-9cb6d0d9fd63", |
| 63 |
), |
| 64 |
("GOOGLE_DRIVE", "Google Drive", "976cf0cf-78c3-4aab-82d2-7c16adbcc281"), |
| 65 |
("HPSS", "HPSS", "fb656a17-0f69-4e59-95ff-d0a62ca7bdf5"), |
| 66 |
("IRODS", "iRODS", "e47b6920-ff57-11ea-8aaa-000c297ab3c2"), |
| 67 |
("POSIX", "POSIX", "145812c8-decc-41f1-83cf-bb2a85a2a70b"), |
| 68 |
("POSIX_STAGING", "POSIX Staging", "052be037-7dda-4d20-b163-3077314dc3e6"), |
| 69 |
("ONEDRIVE", "OneDrive", "28ef55da-1f97-11eb-bdfd-12704e0d6a4d"), |
| 70 |
("S3", "S3", "7643e831-5f6c-4b47-a07f-8ee90f401d23"), |
| 71 |
) |
| 72 |
|
| 73 |
ACTIVESCALE: t.ClassVar[GlobusConnectServerConnector] |
| 74 |
AZURE_BLOB: t.ClassVar[GlobusConnectServerConnector] |
| 75 |
BLACKPEARL: t.ClassVar[GlobusConnectServerConnector] |
| 76 |
BOX: t.ClassVar[GlobusConnectServerConnector] |
| 77 |
CEPH: t.ClassVar[GlobusConnectServerConnector] |
| 78 |
DROPBOX: t.ClassVar[GlobusConnectServerConnector] |
| 79 |
GOOGLE_CLOUD_STORAGE: t.ClassVar[GlobusConnectServerConnector] |
| 80 |
GOOGLE_DRIVE: t.ClassVar[GlobusConnectServerConnector] |
| 81 |
HPSS: t.ClassVar[GlobusConnectServerConnector] |
| 82 |
IRODS: t.ClassVar[GlobusConnectServerConnector] |
| 83 |
ONEDRIVE: t.ClassVar[GlobusConnectServerConnector] |
| 84 |
POSIX: t.ClassVar[GlobusConnectServerConnector] |
| 85 |
POSIX_STAGING: t.ClassVar[GlobusConnectServerConnector] |
| 86 |
S3: t.ClassVar[GlobusConnectServerConnector] |
| 87 |
|
| 88 |
@classmethod |
| 89 |
def all_connectors(cls) -> t.Iterable[GlobusConnectServerConnector]: |
| 90 |
""" |
| 91 |
Return an iterator of all known connectors. |
| 92 |
""" |
| 93 |
for attribute, _, _ in cls._connectors: |
| 94 |
item: GlobusConnectServerConnector = getattr(cls, attribute) |
| 95 |
yield item |
| 96 |
|
| 97 |
@classmethod |
| 98 |
def lookup(cls, name_or_id: uuid.UUID | str) -> GlobusConnectServerConnector | None: |
| 99 |
""" |
| 100 |
Convert a name or ID into a connector object. |
| 101 |
Returns None if the name or ID is not recognized. |
| 102 |
|
| 103 |
Names are normalized before lookup so that they are case-insensitive and |
| 104 |
spaces, dashes, and underscores are all treated equivalently. For |
| 105 |
example, ``Google Drive``, ``google-drive``, and ``gOOgle_dRiVe`` are |
| 106 |
all equivalent. |
| 107 |
|
| 108 |
:param name_or_id: The name or ID of the connector |
| 109 |
""" |
| 110 |
normalized = _normalize_name(str(name_or_id)) |
| 111 |
for connector in cls.all_connectors(): |
| 112 |
if normalized == connector.connector_id or normalized == _normalize_name( |
| 113 |
connector.name |
| 114 |
): |
| 115 |
return connector |
| 116 |
return None |
| 117 |
|
| 118 |
@classmethod |
| 119 |
def extend( |
| 120 |
cls, |
| 121 |
*, |
| 122 |
connector_name: str, |
| 123 |
connector_id: uuid.UUID | str, |
| 124 |
attribute_name: str | None = None, |
| 125 |
) -> type[ConnectorTable]: |
| 126 |
""" |
| 127 |
Extend the ConnectorTable class with a new connector, returning a new |
| 128 |
ConnectorTable subclass. |
| 129 |
|
| 130 |
Usage example: |
| 131 |
|
| 132 |
.. code-block:: pycon |
| 133 |
|
| 134 |
>>> MyTable = ConnectorTable.extend( |
| 135 |
... connector_name="Star Trek Transporter", |
| 136 |
... connector_id="0b19772d-729a-4c8f-93e1-59d5778cecf3", |
| 137 |
... ) |
| 138 |
>>> obj = MyTable.STAR_TREK_TRANSPORTER |
| 139 |
>>> obj.connector_id |
| 140 |
'0b19772d-729a-4c8f-93e1-59d5778cecf3' |
| 141 |
>>> obj.name |
| 142 |
'Star Trek Transporter' |
| 143 |
|
| 144 |
:param connector_name: The name of the connector to add |
| 145 |
:param connector_id: The ID of the connector to add |
| 146 |
:param attribute_name: The attribute name with which the connector will be |
| 147 |
attached to the new subclass. Defaults to the connector name uppercased and |
| 148 |
with spaces converted to underscores. |
| 149 |
""" |
| 150 |
if attribute_name is None: |
| 151 |
attribute_name = connector_name.upper().replace(" ", "_") |
| 152 |
connector_id_str = str(connector_id) |
| 153 |
|
| 154 |
connectors = cls._connectors + ( |
| 155 |
(attribute_name, connector_name, connector_id_str), |
| 156 |
) |
| 157 |
connector_obj = GlobusConnectServerConnector( |
| 158 |
name=connector_name, connector_id=connector_id_str |
| 159 |
) |
| 160 |
return type( |
| 161 |
"ExtendedConnectorTable", |
| 162 |
(cls,), |
| 163 |
{"_connectors": connectors, attribute_name: connector_obj}, |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
# "render" the _connectors to live attributes of the ConnectorTable |
| 168 |
for _attribute, _name, _id in ConnectorTable._connectors: |
| 169 |
setattr( |
| 170 |
ConnectorTable, |
| 171 |
_attribute, |
| 172 |
GlobusConnectServerConnector(name=_name, connector_id=_id), |
| 173 |
) |
| 174 |
del _attribute, _name, _id |