| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
import uuid |
| 5 |
|
| 6 |
from ..collection import ( |
| 7 |
DynamicScopeCollection, |
| 8 |
StaticScopeCollection, |
| 9 |
_url_scope, |
| 10 |
) |
| 11 |
from ..representation import Scope |
| 12 |
|
| 13 |
|
| 14 |
class _FlowsScopes(StaticScopeCollection): |
| 15 |
# The Flows service breaks the scopes/resource server convention: its |
| 16 |
# resource server is a domain name but its scopes are built around the |
| 17 |
# client ID. |
| 18 |
resource_server = "flows.globus.org" |
| 19 |
client_id = "eec9b274-0c81-4334-bdc2-54e90e689b9a" |
| 20 |
|
| 21 |
all = _url_scope(client_id, "all") |
| 22 |
manage_flows = _url_scope(client_id, "manage_flows") |
| 23 |
view_flows = _url_scope(client_id, "view_flows") |
| 24 |
run = _url_scope(client_id, "run") |
| 25 |
run_status = _url_scope(client_id, "run_status") |
| 26 |
run_manage = _url_scope(client_id, "run_manage") |
| 27 |
|
| 28 |
|
| 29 |
FlowsScopes = _FlowsScopes() |
| 30 |
|
| 31 |
|
| 32 |
class SpecificFlowScopes(DynamicScopeCollection): |
| 33 |
""" |
| 34 |
This defines the scopes for a single flow (as distinct from the Flows service). |
| 35 |
|
| 36 |
It primarily provides the `user` scope which is typically needed to start a run of |
| 37 |
a flow. |
| 38 |
|
| 39 |
Example usage: |
| 40 |
|
| 41 |
.. code-block:: python |
| 42 |
|
| 43 |
sc = SpecificFlowScopes("my-flow-id-here") |
| 44 |
flow_scope = sc.user |
| 45 |
""" |
| 46 |
|
| 47 |
_scope_names = ("user",) |
| 48 |
|
| 49 |
def __init__(self, flow_id: uuid.UUID | str) -> None: |
| 50 |
_flow_id = str(flow_id) |
| 51 |
super().__init__(_flow_id) |
| 52 |
|
| 53 |
self.user: Scope = _url_scope( |
| 54 |
_flow_id, f"flow_{_flow_id.replace('-', '_')}_user" |
| 55 |
) |
| 56 |
|
| 57 |
@classmethod |
| 58 |
def _build_class_stub(cls) -> SpecificFlowScopes: |
| 59 |
""" |
| 60 |
This internal helper builds a "stub" object so that |
| 61 |
``SpecificFlowClient.scopes`` is typed as ``SpecificFlowScopes`` but |
| 62 |
raises appropriate errors at runtime access via the class. |
| 63 |
""" |
| 64 |
return _SpecificFlowScopesClassStub() |
| 65 |
|
| 66 |
|
| 67 |
class _SpecificFlowScopesClassStub(SpecificFlowScopes): |
| 68 |
""" |
| 69 |
This stub object ensures that the type deductions for type checkers (e.g. mypy) on |
| 70 |
SpecificFlowClient.scopes are correct. |
| 71 |
|
| 72 |
Primarily, it should be possible to access the `scopes` attribute, the `user` |
| 73 |
scope, and the `resource_server`, but these usages should raise specific and |
| 74 |
informative runtime errors. |
| 75 |
|
| 76 |
Our types are therefore less accurate for class-var access, but more accurate for |
| 77 |
instance-var access. |
| 78 |
""" |
| 79 |
|
| 80 |
def __init__(self) -> None: |
| 81 |
super().__init__("<stub>") |
| 82 |
|
| 83 |
def __getattribute__(self, name: str) -> t.Any: |
| 84 |
if name == "user": |
| 85 |
_raise_attr_error("scopes") |
| 86 |
if name == "resource_server": |
| 87 |
_raise_attr_error("resource_server") |
| 88 |
return object.__getattribute__(self, name) |
| 89 |
|
| 90 |
|
| 91 |
def _raise_attr_error(name: str) -> t.NoReturn: |
| 92 |
raise AttributeError( |
| 93 |
f"It is not valid to attempt to access the '{name}' attribute of the " |
| 94 |
"SpecificFlowClient class. " |
| 95 |
f"Instead, instantiate a SpecificFlowClient and access the '{name}' attribute " |
| 96 |
"from that instance." |
| 97 |
) |