Source code for globus_sdk.scopes.scope_definition
"""THIS IS A LEGACY MODULEThis module defines a legacy scope object and parser called `MutableScope`.It is maintained for backwards compatibility.For new code, use the `globus_sdk.Scope` object."""from__future__importannotationsimporttypingastimportwarningsift.TYPE_CHECKING:fromglobus_sdk._typesimportScopeCollectionType
[docs]classMutableScope:""" A scope object is a representation of a scope which allows modifications to be made. In particular, it supports handling scope dependencies via ``add_dependency``. `str(MutableScope(...))` produces a valid scope string for use in various methods. :param scope_string: The string which will be used as the basis for this Scope :param optional: The scope may be marked as optional. This means that the scope can be declined by the user without declining consent for other scopes """def__init__(self,scope_string:str,*,optional:bool=False,dependencies:list[MutableScope]|None=None,)->None:ifany(cinscope_stringforcin"[]* "):raiseValueError("MutableScope instances may not contain the special characters '[]* '.")self.scope_string=scope_stringself.optional=optionalself.dependencies:list[MutableScope]=([]ifdependenciesisNoneelsedependencies)defserialize(self)->str:base_scope=("*"ifself.optionalelse"")+self.scope_stringifnotself.dependencies:returnbase_scopereturn(base_scope+"["+" ".join(c.serialize()forcinself.dependencies)+"]")
[docs]defadd_dependency(self,scope:str|MutableScope,*,optional:bool|None=None,)->MutableScope:""" Add a scope dependency. The dependent scope relationship will be stored in the Scope and will be evident in its string representation. :param scope: The scope upon which the current scope depends :param optional: Mark the dependency an optional one. By default it is not. An optional scope dependency can be declined by the user without declining consent for the primary scope """ifoptionalisnotNone:ifisinstance(scope,MutableScope):raiseValueError("cannot use optional=... with a MutableScope object as the ""argument to add_dependency")warnings.warn("Passing 'optional' to add_dependency is deprecated. ""Construct an optional MutableScope object instead.",DeprecationWarning,stacklevel=2,)scopeobj=MutableScope(scope,optional=optional)else:ifisinstance(scope,str):scopeobj=MutableScope(scope)else:scopeobj=scopeself.dependencies.append(scopeobj)returnself
[docs]@staticmethoddefscopes2str(obj:ScopeCollectionType)->str:""" .. warning:: Deprecated. Prefer ``globus_sdk.scopes.scopes_to_str``. Given a scope string, a collection of scope strings, a MutableScope object, a collection of MutableScope objects, or a mixed collection of strings and Scopes, convert to a string which can be used in a request. :param obj: The object or collection to convert to a string """from._normalizeimportscopes_to_strreturnscopes_to_str(obj)