Config
plateforme.core.config
This module provides utilities for managing configuration classes within the Plateforme framework.
Config
module-attribute
Config = TypeVar('Config', bound='ConfigWrapper')
A type variable for configuration classes.
ConfigType
module-attribute
ConfigType = Type['ConfigWrapper']
A type alias for configuration classes.
ConfigFieldInfo
module-attribute
ConfigFieldInfo = FieldInfo['ConfigType']
A type alias for a configuration wrapper field information.
ConfigSource
module-attribute
A type alias for configuration dictionary sources.
ConfigWrapperMeta
Bases: type
A metaclass for configuration wrappers.
collect_config_fields
collect_config_fields(
bases: tuple[type, ...],
types_namespace: dict[str, Any] | None,
*sources: ConfigSource,
**defaults: Any,
) -> dict[str, ConfigFieldInfo]
Collect configuration fields for the configuration wrapper.
The configuration fields are collected from the wrapper class definition and the provided configuration dictionary sources. The configuration fields are checked for consistency in type across sources and for the presence of default values for non-required fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bases
|
tuple[type, ...]
|
The configuration wrapper base classes to collect the configuration fields from. |
required |
types_namespace
|
dict[str, Any] | None
|
The types namespace of the configuration wrapper to look for type hints. |
required |
*sources
|
ConfigSource
|
The configuration dictionary sources to collect the fields from. These dictionaries must be subclasses of a typed dictionary. |
()
|
**defaults
|
Any
|
Optional default values to provide in addition to those in the configuration wrapper namespace. It is typically set with keyword arguments in the configuration wrapper definition. |
{}
|
Returns:
Type | Description |
---|---|
dict[str, ConfigFieldInfo]
|
A dictionary containing the configuration fields collected from the |
dict[str, ConfigFieldInfo]
|
wrapper class definition and the configuration dictionary sources. |
Raises:
Type | Description |
---|---|
AttributeError
|
If a configuration field conflicts with a protected member attribute or is not present in the class namespace when setting a default value. |
TypeError
|
If a configuration dictionary source is not a valid subclass of a typed dictionary or if a configuration field has different annotations in the configuration dictionary sources. |
ValueError
|
If a not required configuration field does not have a default value. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
ConfigWrapper
ConfigWrapper(
__owner: Any | None = None,
__defaults: dict[str, Any] | None = None,
__partial_init: bool = False,
/,
**data: Any,
)
Bases: Representation
A base configuration wrapper class.
Initialize the configuration class with the given data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__owner
|
Any | None
|
The owner of the configuration instance. It can be any
object or type that uses the configuration instance.
Defaults to |
None
|
__defaults
|
dict[str, Any] | None
|
The default values to initialize the configuration
instance with. Defaults to |
None
|
__partial_init
|
bool
|
Flag indicating whether to partially initialize
the configuration instance. Defaults to |
False
|
**data
|
Any
|
The data as keyword arguments to initialize the configuration instance with. |
{}
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
create
classmethod
create(
owner: Any | None = None,
defaults: dict[str, Any] | None = None,
partial_init: bool = False,
*,
data: dict[str, Any] | None = None,
) -> Self
Create a new configuration instance.
This method is typically used internally to create a new configuration
class with a specific owner and partial initialization flag. It is an
alternative to the __init__
method for creating a new configuration
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
owner
|
Any | None
|
The owner of the configuration instance. |
None
|
defaults
|
dict[str, Any] | None
|
The default values to initialize the configuration
instance with. Defaults to |
None
|
partial_init
|
bool
|
Flag indicating whether to partially initialize the
configuration instance. Defaults to |
False
|
data
|
dict[str, Any] | None
|
The data to initialize the configuration instance with.
Defaults to |
None
|
Returns:
Type | Description |
---|---|
Self
|
The new configuration instance created. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
from_meta
classmethod
from_meta(
owner: type[Any],
bases: tuple[type, ...],
namespace: dict[str, Any],
/,
config_attr: str = "__config__",
partial_init: bool = False,
data: dict[str, Any] | None = None,
) -> Self
Create a new configuration instance from a class constructor.
This method is typically used internally to create a new configuration class from the meta configuration of a model, package, resource, or service. It merges the configuration of the given bases, the namespace, and the keyword arguments to create a new configuration class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
owner
|
type[Any]
|
The owner of the configuration instance. It should be the class that is being created from the meta configuration. |
required |
bases
|
tuple[type, ...]
|
The configurable base classes to merge. |
required |
namespace
|
dict[str, Any]
|
The configurable namespace to merge. |
required |
config_attr
|
str
|
The configurable attribute name used to extract the
configuration dictionary from the bases and the namespace of
the configurable class. Defaults to |
'__config__'
|
partial_init
|
bool
|
Flag indicating whether to partially initialize the
configuration instance. Defaults to |
False
|
data
|
dict[str, Any] | None
|
The data to initialize the configuration instance with.
Defaults to |
None
|
Returns:
Type | Description |
---|---|
Self
|
The new configuration instance created from the given meta |
Self
|
configuration. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
post_init
Post-initialization method for the configuration class.
Override this method to perform additional initialization steps after
the configuration instance has been created. This method is called
automatically after the configuration instance has been initialized,
unless the partial_init
flag is set to True
.
Note
See the validate
method for more information on the validation
process of the configuration instance.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
validate
Validate the configuration instance.
It post-initializes the configuration instance, checks for any missing required fields and validates the assignments of the configuration values based on the configuration fields information and the current validation context. This is performed automatically upon initialization of the configuration instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict
|
bool | None
|
Whether to enforce strict validation. Defaults to |
None
|
context
|
dict[str, Any] | None
|
The context to use for validation. Defaults to |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the configuration instance has undefined values for required fields. |
ValidationError
|
If the assignment of a value is invalid based on the configuration fields information and the current validation context. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
context
staticmethod
Context manager for the configuration instance.
If the frozen mutation flag is not specified, the current frozen
mutation flag is used if available, otherwise it defaults to False
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allow_mutation
|
bool | None
|
Flag indicating whether to allow frozen mutation
of the configuration instance. When set to |
None
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
clear
copy
copy() -> Self
Return a shallow copy of the configuration dictionary.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
merge
Merge the configuration with other configurations.
It merges the configuration with the provided configuration instances
or dictionaries. The precedence of the rightmost configuration is
higher than the leftmost configuration. This can be changed by setting
the setdefault
argument to True
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*configs
|
Self | dict[str, Any]
|
The configuration instances or dictionaries to merge with the target configuration dictionary. |
()
|
setdefault
|
bool
|
Flag indicating whether to set the default values for
the configuration keys if they are not already set.
This modifies the behavior of the merge operation, making the
precedence of the leftmost configuration higher than the
rightmost configuration.
Defaults to |
False
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
check
check(
key: str,
*,
scope: Literal["all", "default", "set"] = "all",
raise_errors: bool = True,
) -> bool
Check if the configuration key exists in the given scope.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The configuration key to check for. |
required |
scope
|
Literal['all', 'default', 'set']
|
The scope to check for the configuration key. It can be
either |
'all'
|
raise_errors
|
bool
|
Flag indicating whether to raise an error if the
configuration key is not defined for the configuration wrapper.
Defaults to |
True
|
Returns:
Type | Description |
---|---|
bool
|
A boolean indicating whether the configuration key exists in the |
bool
|
specified scope. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
entries
entries(
*,
scope: Literal["all", "default", "set"] = "all",
default_mode: Literal[
"preserve", "unwrap", "wrap"
] = "unwrap",
include_keys: Iterable[str] | None = None,
exclude_keys: Iterable[str] | None = None,
include_metadata: Iterable[Any] | None = None,
exclude_metadata: Iterable[Any] | None = None,
) -> dict[str, Any]
Return the configuration dictionary.
It returns the configuration dictionary based on the specified scope, and keys and extra information to filter the configuration dictionary entries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
Literal['all', 'default', 'set']
|
The scope of the configuration dictionary to return. It can
be either |
'all'
|
default_mode
|
Literal['preserve', 'unwrap', 'wrap']
|
The default mode to use when returning a default
entry from the configuration dictionary. It can be either
|
'unwrap'
|
include_keys
|
Iterable[str] | None
|
The keys to include from the configuration dictionary
entries. Defaults to |
None
|
exclude_keys
|
Iterable[str] | None
|
The keys to exclude from the configuration dictionary
entries. Defaults to |
None
|
include_metadata
|
Iterable[Any] | None
|
The metadata information to include from the
configuration dictionary entries. Defaults to |
None
|
exclude_metadata
|
Iterable[Any] | None
|
The metadata information to exclude from the
configuration dictionary entries. Defaults to |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary containing the configuration entries based on the |
dict[str, Any]
|
specified scope and extra information. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 |
|
keys
values
values() -> ValuesView[Any]
items
get
get(
key: str,
default: Any = Undefined,
*,
default_mode: Literal[
"preserve", "unwrap", "wrap"
] = "unwrap",
) -> Any
Get the value for the specified key if set otherwise the default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The configuration key to get the value for. |
required |
default
|
Any
|
The default value to return if the key is not set. |
Undefined
|
default_mode
|
Literal['preserve', 'unwrap', 'wrap']
|
The default mode to use when returning a default
value from the configuration dictionary. It can be either
|
'unwrap'
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
pop
pop(
key: str,
default: Any = Undefined,
*,
default_mode: Literal[
"preserve", "unwrap", "wrap"
] = "unwrap",
) -> Any
Pop the specified key if set and return its corresponding value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The configuration key to pop the value for. |
required |
default
|
Any
|
The default value to return if the key is not set. |
Undefined
|
default_mode
|
Literal['preserve', 'unwrap', 'wrap']
|
The default mode to use when returning a default
value from the configuration dictionary. It can be either
|
'unwrap'
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
getdefault
Get the default value for the specified key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The configuration key to get the default value for. |
required |
mode
|
Literal['preserve', 'unwrap', 'wrap']
|
The mode to use when returning the default value. It can be
either |
'unwrap'
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
setdefault
Set the default value for the specified key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The configuration key to set the default value for. |
required |
default
|
Any
|
The default value to set for the key. |
required |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
update
Update the config dictionary with new data.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
ConfigurableMeta
Bases: type
A metaclass for configurable classes.
collect_config_wrappers
Collect configuration wrappers from the given bases and namespace.
It collects the configuration wrappers from the given bases and namespace by extracting the configuration wrapper type from the original bases annotation if it is a generic subclass of the configurable class or metaclass, and from the configuration attribute if present in the class and bases namespace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bases
|
tuple[type, ...]
|
The class bases. |
required |
namespace
|
dict[str, Any]
|
The class namespace. |
required |
Returns:
Type | Description |
---|---|
list[ConfigType]
|
A list of configuration wrapper classes found in the given bases |
list[ConfigType]
|
and namespace. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 |
|
evaluate_config_field
evaluate_config_field(
config: ConfigWrapper, /, name: str, *, parents: Any
) -> Any
Evaluate a configuration field from the provided sources.
It evaluates the configuration field from the provided configuration sources, which can be any object that has a configuration wrapper or a configuration dictionary. The evaluation is done by taking the first defined value from the sources (non default), where the first parent has the highest precedence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the field to evaluate. |
required |
config
|
ConfigWrapper
|
The configuration object to evaluate the field from. |
required |
*parents
|
Any
|
The parents to evaluate the field from. The first parent has the highest precedence. |
required |
Returns:
Type | Description |
---|---|
Any
|
The evaluated configuration field. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/config.py
with_config
with_config(
type_: Literal["model"],
**kwargs: Unpack[ModelConfigDict],
) -> Callable[[type[Any]], ModelType]
with_config(
type_: Literal["resource"],
**kwargs: Unpack[ResourceConfigDict],
) -> Callable[[type[Any]], ResourceType]
with_config(
type_: Literal["service"],
**kwargs: Unpack[ServiceConfigDict],
) -> Callable[[type[Any]], ServiceType]
with_config(
type_: Literal["model", "resource", "service"],
**kwargs: Any,
) -> Callable[[type[Any]], type[Any]]
Decorator to apply configuration to a class.
It transforms the decorated class into the specified configuration class type and applies the provided configuration to the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_
|
Literal['model', 'resource', 'service']
|
The configuration type to apply. It can be one of: |
required |
**kwargs
|
Any
|
The keyword arguments to pass to the configuration instance. |
{}
|
Returns:
Type | Description |
---|---|
Callable[[type[Any]], type[Any]]
|
A decorator to apply the configuration to the class. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the configuration type is not supported. |