granular_configuration_language
.yaml.decorators
- class granular_configuration_language.yaml.decorators.TagDecoratorBase( )[source]
-
Base class for Tag Decorator factories.
You must implement the
user_friendly_type
property and define the generic type.Example:
class string_tag(TagDecoratorBase[str]): Type: typing.TypeAlias = str @property def user_friendly_type(self) -> str: return "str"
You must override at least one of
scalar_node_type_check()
,sequence_node_type_check()
,ormapping_node_type_check()
.For
scalar_node_type_check()
to be called the YAML has already be tested to be astr
.For
sequence_node_type_check()
to be called the YAML has already be tested to be aSequence
.For
mapping_node_type_check()
to be called the YAML has already be tested to be aMapping
.
If these are enough, then you may just return
True
in the override method. Otherwise, implement the override as aTypeGuard
.If the value needs to be altered before being passed to Tag functions, override
scalar_node_transformer()
,scalar_node_transformer()
, ormapping_node_transformer()
, as needed.The transformer is called if the associated node type check passes, just before the value is passed to tag function.
- final __init__( ) None [source]
- Parameters:
tag (Tag) – Value of Tag. Expected to be constructed inline using
Tag
(e.g.Tag("!Tag")
). Must start with!
.category (str, optional) – Category of Tag. Used by available_tags and available_plugins to organize tags, defaults to
General
.sort_as (str, optional) – Alternative Tag string. Used for sorting tags different to its explicit value. Used by available_tags and available_plugins.
- mapping_node_transformer(
- value: Any,
Defaults to an identity operation. Override if the value needs to be altered before being passed to Tag functions.
Only called if
mapping_node_type_check()
returnTrue
.
- mapping_node_type_check(
- value: Mapping,
Defaults to
False
. Override to enable Mapping Node support.
- scalar_node_transformer(
- value: Any,
Defaults to an identity operation. Override if the value needs to be altered before being passed to Tag functions.
Only called if
scalar_node_type_check()
returnTrue
.As an example, a float tag could be supported by:
def scalar_node_type_check(self, value: str) -> TypeGuard[float]: try: float(value) return True except ValueError: return False def scalar_node_transformer(self, value: Any) -> float: return float(value)
- scalar_node_type_check(
- value: str,
Defaults to
False
. Override to enable Scalar Node support.
- sequence_node_transformer(
- value: Any,
Defaults to an identity operation. Override if the value needs to be altered before being passed to Tag functions.
Only called if
sequence_node_type_check()
returnTrue
.
- class granular_configuration_language.yaml.decorators.mapping_of_any_tag( )[source]
Bases:
TagDecoratorBase
[Configuration
]A decorator factory for Tags that take a YAML mapping as argument.
- Example:
@mapping_of_any_tag(Tag("!Tag")) @as_lazy def tag(value: Configuration) -> Any: ...
- Type
alias of
Configuration
- __init__( ) None
- Parameters:
tag (Tag) – Value of Tag. Expected to be constructed inline using
Tag
(e.g.Tag("!Tag")
). Must start with!
.category (str, optional) – Category of Tag. Used by available_tags and available_plugins to organize tags, defaults to
General
.sort_as (str, optional) – Alternative Tag string. Used for sorting tags different to its explicit value. Used by available_tags and available_plugins.
- class granular_configuration_language.yaml.decorators.sequence_of_any_tag( )[source]
Bases:
TagDecoratorBase
[Sequence
[Any
]]A decorator factory for Tags that take a YAML sequence as argument.
- Example:
@sequence_of_any_tag(Tag("!Tag")) @as_lazy def tag(value: Sequence[Any]) -> Any: ...
- __init__( ) None
- Parameters:
tag (Tag) – Value of Tag. Expected to be constructed inline using
Tag
(e.g.Tag("!Tag")
). Must start with!
.category (str, optional) – Category of Tag. Used by available_tags and available_plugins to organize tags, defaults to
General
.sort_as (str, optional) – Alternative Tag string. Used for sorting tags different to its explicit value. Used by available_tags and available_plugins.
- class granular_configuration_language.yaml.decorators.string_or_twople_tag( )[source]
Bases:
TagDecoratorBase
[str
|tuple
[str
,Any
]]A decorator factory for Tags that take a YAML string or tuple of a YAML strings and YAML object as argument.
- Example:
@string_or_twople_tag(Tag("!Tag")) @as_lazy def tag(value: string_or_twople_tag.Type) -> Any: ...
- __init__( ) None
- Parameters:
tag (Tag) – Value of Tag. Expected to be constructed inline using
Tag
(e.g.Tag("!Tag")
). Must start with!
.category (str, optional) – Category of Tag. Used by available_tags and available_plugins to organize tags, defaults to
General
.sort_as (str, optional) – Alternative Tag string. Used for sorting tags different to its explicit value. Used by available_tags and available_plugins.
- class granular_configuration_language.yaml.decorators.string_tag( )[source]
Bases:
TagDecoratorBase
[str
]A decorator factory for Tags that take a YAML string as argument.
- Example:
@string_tag(Tag("!Tag")) @as_lazy def tag(value: str) -> Any: ...
- __init__( ) None
- Parameters:
tag (Tag) – Value of Tag. Expected to be constructed inline using
Tag
(e.g.Tag("!Tag")
). Must start with!
.category (str, optional) – Category of Tag. Used by available_tags and available_plugins to organize tags, defaults to
General
.sort_as (str, optional) – Alternative Tag string. Used for sorting tags different to its explicit value. Used by available_tags and available_plugins.
- granular_configuration_language.yaml.decorators.as_lazy( ) Callable[[Tag, T, StateHolder], LazyEval[RT]] [source]
Wraps the “Tag” function in a
LazyEval
, so that the function being wrapped is run just-in-time.Positional Parameters for “Tag” function
(
T
) - YAML value
- granular_configuration_language.yaml.decorators.as_lazy_with_load_options(
- func: Callable[[T, LoadOptions], RT],
Wraps the “Tag” function in a
LazyEval
, so that the function being wrapped is run just-in-time.Positional Parameters for “Tag” function
(
T
) - YAML value(
LoadOptions
) - ALoadOptions
instance
- Parameters:
func (Callable[[T, LoadOptions], RT]) – Function to be wrapped
- Returns:
Wrapped Function
- Return type:
- Example:
@string_tag(Tag("!Tag")) @as_lazy_with_load_options def tag(value: str, options: LoadOptions) -> Any: ...
- granular_configuration_language.yaml.decorators.as_lazy_with_root(
- func: Callable[[T, Root], RT] | None = None,
- /,
- *,
- needs_root_condition: Callable[[T], bool] | None = None,
Wraps the “Tag” function in a
LazyEval
, so that the function being wrapped is run just-in-time.Documentation Issue
sphinx.ext.autodoc
isn’t exposing thetyping.overload()
. See the example for a clearer type signaturesTyping Stub
# Decorator # Uses as: ``@as_lazy_with_root`` @overload def as_lazy_with_root( func: Callable[[T, Root], RT], ) -> Callable[[Tag, T, StateHolder], LazyEval[RT]]: ... # Decorator Factory # Uses as: ``@as_lazy_with_root(needs_root_condition=condition)`` @overload def as_lazy_with_root( *, needs_root_condition: Callable[[T], bool] ) -> Callable[ [Callable[[T, Root], RT]], Callable[[Tag, T, StateHolder], LazyEval[RT]], ]: ...
- Parameters:
- Returns:
Wrapped Function
- Return type:
- Example:
# Typical usage @string_tag(Tag("!Tag")) @as_lazy_with_root def tag(value: str, root: Root) -> Any: ... # Using `needs_root_condition` @string_tag(Tag("!Tag")) @as_lazy_with_root(needs_root_condition=interpolation_needs_ref_condition) @interpolate_value_with_ref def tag(value: str, root: Root) -> Any: ...
- granular_configuration_language.yaml.decorators.as_lazy_with_root_and_load_options(
- func: Callable[[T, Root, LoadOptions], RT],
- /,
Wraps the “Tag” function in a
LazyEval
, so that the function being wrapped is run just-in-time.Positional Parameters for “Tag” function
(
T
) – YAML value(
Root
) – Configuration root(
LoadOptions
) – ALoadOptions
instance
- Parameters:
func (Callable[[T, Root, LoadOptions], RT]) – Function to be wrapped
- Returns:
Wrapped Function
- Return type:
- Example:
@string_tag(Tag("!Tag")) @as_lazy_with_root_and_load_options def tag(value: str, root: Root, options: LoadOptions) -> Any: ...
- granular_configuration_language.yaml.decorators.as_not_lazy( ) Callable[[Tag, T, StateHolder], RT] [source]
Wraps the “Tag” function, but does not make it lazy. The function being wrapped is run at load time.
Positional Parameters for “Tag” function
(
T
) - YAML value
- granular_configuration_language.yaml.decorators.interpolate_value_with_ref( ) Callable[[Concatenate[str, Root, P]], RT] [source]
Replaces the YAML string value with the interpolated value before calling the tag function
“with_ref” does full interpolation, supporting references (e.g.
${$.value}
and${/value}
).Compatible Laziness Decorators
- Parameters:
func (Callable[Concatenate[str, Root, P], RT]) – Function to be wrapped
- Returns:
Wrapped Function
- Return type:
- Example:
@string_tag(Tag("!Tag")) @as_lazy_with_root @interpolate_value_with_ref def tag(value: str, root: Root) -> Any: ... @string_tag(Tag("!Tag")) @as_lazy_with_root_and_load_options @interpolate_value_with_ref def tag_with_options(value: str, root: Root, options: LoadOptions) -> Any: ...
- granular_configuration_language.yaml.decorators.interpolate_value_without_ref(
- func: Callable[[Concatenate[str, P]], RT],
- /,
Replaces the YAML string value with the interpolated value before calling the tag function
“without_ref” does a limited interpolation that does not support references (e.g.
${$.value}
and${/value}
)Compatible Laziness Decorators
- Parameters:
func (Callable[Concatenate[str, P], RT]) – Function to be wrapped
- Returns:
Wrapped Function
- Return type:
Callable[Concatenate[str, P], RT]
- Example:
@string_tag(Tag("!Tag")) @as_lazy @interpolate_value_with_ref def tag(value: str) -> Any: ... @string_tag(Tag("!Tag")) @as_lazy_with_load_options @interpolate_value_with_ref def tag_with_options(value: str, options: LoadOptions) -> Any: ...