[docs]defscopes_to_str(scopes:ScopeCollectionType)->str:""" Normalize a scope collection to a space-separated scope string. :param scopes: A scope string or object, or an iterable of scope strings or objects. :returns: A space-separated scope string. Example usage: .. code-block:: pycon >>> scopes_to_str(Scope("foo")) 'foo' >>> scopes_to_str(Scope("foo"), "bar", Scope("qux")) 'foo bar qux' """scope_iter=_iter_scope_collection(scopes,split_root_scopes=False)return" ".join(str(scope)forscopeinscope_iter)
[docs]defscopes_to_scope_list(scopes:ScopeCollectionType)->list[Scope]:""" Normalize a scope collection to a list of Scope objects. :param scopes: A scope string or object, or an iterable of scope strings or objects. :returns: A list of Scope objects. Example usage: .. code-block:: pycon >>> scopes_to_scope_list(Scope("foo")) [Scope('foo')] >>> scopes_to_scope_list(Scope("foo"), "bar baz", Scope("qux")) [Scope('foo'), Scope('bar'), Scope('baz'), Scope('qux')] """scope_list:list[Scope]=[]forscopein_iter_scope_collection(scopes):ifisinstance(scope,str):scope_list.extend(ScopeParser.parse(scope))else:scope_list.append(scope)returnscope_list
def_iter_scope_collection(obj:ScopeCollectionType,*,split_root_scopes:bool=True,)->t.Iterator[str|Scope]:""" Provide an iterator over a scope collection type, flattening nested scope collections as encountered. Collections of scope representations are yielded one at a time. Individual scope representations are yielded as-is. :param obj: A scope collection or scope representation. :param iter_scope_strings: If True, scope strings with multiple root scopes are split. This flag allows a caller to optimize, skipping a bfs operation if merging will be done later purely with strings. Example usage: .. code-block:: pycon >>> list(_iter_scope_collection("foo")) ['foo'] >>> list(_iter_scope_collection(Scope.parse("foo bar"), "baz qux")) [Scope('foo'), Scope('bar'), 'baz', 'qux'] >>> list(_iter_scope_collection("foo bar[baz qux]")) ['foo', 'bar[baz qux]'] >>> list(_iter_scope_collection("foo bar[baz qux]", split_root_scopes=False)) 'foo bar[baz qux]' """ifisinstance(obj,str):yield from_iter_scope_string(obj,split_root_scopes)elifisinstance(obj,Scope):yieldobjelse:foriteminobj:yield from_iter_scope_collection(item,split_root_scopes=split_root_scopes)def_iter_scope_string(scope_str:str,split_root_scopes:bool)->t.Iterator[str]:ifnotsplit_root_scopesor" "notinscope_str:yieldscope_strelif"["notinscope_str:yield fromscope_str.split(" ")else:forscope_objinScopeParser.parse(scope_str):yieldstr(scope_obj)