| 1 |
.. _scopes: |
| 2 |
|
| 3 |
.. currentmodule:: globus_sdk.scopes |
| 4 |
|
| 5 |
Scopes |
| 6 |
====== |
| 7 |
|
| 8 |
The SDK provides a ``Scope`` object which is the class model for a scope. |
| 9 |
``Scope``\s can be parsed from strings and serialized to strings, and support |
| 10 |
programmatic manipulations to describe dependent scopes. |
| 11 |
|
| 12 |
``Scope`` can be constructed using its initializer, via ``Scope.parse``, or via |
| 13 |
:meth:`ScopeParser.parse`. |
| 14 |
|
| 15 |
For example, one can create a ``Scope`` object for the OIDC ``openid`` scope: |
| 16 |
|
| 17 |
.. code-block:: python |
| 18 |
|
| 19 |
from globus_sdk.scopes import Scope |
| 20 |
|
| 21 |
openid_scope = Scope("openid") |
| 22 |
|
| 23 |
``Scope`` objects primarily provide three main pieces of functionality: |
| 24 |
|
| 25 |
* deserializing (parsing a single scope) |
| 26 |
* serializing (stringifying) |
| 27 |
* scope tree construction |
| 28 |
|
| 29 |
Tree Construction |
| 30 |
~~~~~~~~~~~~~~~~~ |
| 31 |
|
| 32 |
``Scope`` objects provide a tree-like interface for constructing scopes |
| 33 |
and their dependencies. |
| 34 |
Because ``Scope`` objects are immutable, trees are constructed by building new |
| 35 |
scopes. |
| 36 |
|
| 37 |
For example, the transfer scope dependent upon a collection scope may be |
| 38 |
constructed by means of ``Scope`` methods thusly: |
| 39 |
|
| 40 |
.. code-block:: python |
| 41 |
|
| 42 |
from globus_sdk.scopes import GCSCollectionScopeBuilder, TransferScopes, Scope |
| 43 |
|
| 44 |
MAPPED_COLLECTION_ID = "...ID HERE..." |
| 45 |
|
| 46 |
# create the scope object, and get the data_access_scope as a string |
| 47 |
data_access_scope = GCSCollectionScopeBuilder(MAPPED_COLLECTION_ID).data_access |
| 48 |
# add data_access as an optional dependency |
| 49 |
transfer_scope = TransferScopes.all.with_dependency(data_access_scope, optional=True) |
| 50 |
|
| 51 |
``Scope``\s can be used in most of the same locations where scope |
| 52 |
strings can be used, but you can also call ``str(scope)`` to get a |
| 53 |
stringified representation. |
| 54 |
|
| 55 |
Serializing Scopes |
| 56 |
~~~~~~~~~~~~~~~~~~ |
| 57 |
|
| 58 |
Whenever scopes are being sent to Globus services, they need to be encoded as |
| 59 |
strings. All scope objects support this by means of their defined |
| 60 |
``__str__`` method. For example, the following is an example of |
| 61 |
``str()`` and ``repr()`` usage: |
| 62 |
|
| 63 |
.. code-block:: pycon |
| 64 |
|
| 65 |
>>> from globus_sdk.scopes import Scope |
| 66 |
>>> foo = Scope("foo") |
| 67 |
>>> bar = Scope("bar") |
| 68 |
>>> bar = bar.with_dependency(Scope("baz")) |
| 69 |
>>> foo = foo.with_dependency(bar) |
| 70 |
>>> print(str(foo)) |
| 71 |
foo[bar[baz]] |
| 72 |
>>> print(str(bar)) |
| 73 |
bar[baz] |
| 74 |
>>> alpha = Scope("alpha") |
| 75 |
>>> alpha = alpha.with_dependency("beta", optional=True) |
| 76 |
>>> print(str(alpha)) |
| 77 |
alpha[*beta] |
| 78 |
>>> print(repr(alpha)) |
| 79 |
Scope("alpha", dependencies=[Scope("beta", optional=True)]) |
| 80 |
|
| 81 |
Reference |
| 82 |
~~~~~~~~~ |
| 83 |
|
| 84 |
.. autoclass:: Scope |
| 85 |
:members: |
| 86 |
:member-order: bysource |