API Reference
Manage configuration with pydantic.
config
¶
The base configuration model class BaseConfig
.
ModelConfig
¶
Bases: SettingsConfigDict
Meta-configuration for configzen models.
Source code in configzen/config.py
45 46 47 48 49 50 |
|
BaseConfig
¶
Bases: BaseSettings
Base class for all configuration models.
Source code in configzen/config.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
config_root: BaseConfig
property
¶
Return the root configuration that was used to load the entire data.
config_source: ConfigSource[Any, Any] | None
property
¶
Return the configuration source that was used to load the configuration.
config_data: Data
property
¶
Return the configuration that was loaded from the configuration source.
config_processor: ConfigProcessor
property
¶
Current configuration processor.
Processor stores the initial data used when loading the configuration, resolves macros etc.
config_find_routes(subconfig: BaseConfig) -> set[Route]
¶
Locate all occurrences of a subconfiguration in the current configuration.
Return a set of routes to the located subconfiguration.
Source code in configzen/config.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
config_find_route(subconfig: BaseConfig) -> Route
¶
Locate exactly one (closest) route to the given subconfiguration.
Source code in configzen/config.py
225 226 227 228 229 230 231 |
|
config_load(source: object | None = None, *, processor_factory: Callable[..., ConfigProcessor] | None = None) -> Self
classmethod
¶
Load this configuration from a given source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
object | None
|
Where to load the configuration from. The argument passed is forwarded
to |
None
|
context |
The context to use during model validation.
See also [ |
required | |
processor_factory |
Callable[..., ConfigProcessor] | None
|
The state factory to use to parse the newly loaded configuration data. |
None
|
Returns:
Type | Description |
---|---|
self
|
|
Source code in configzen/config.py
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 |
|
config_load_async(source: object | None = None, *, processor_factory: Callable[..., ConfigProcessor] | None = None) -> Self
async
classmethod
¶
Do the same as config_load
, but asynchronously (no I/O blocking).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
object | None
|
Where to load the configuration from. The argument passed is forwarded
to |
None
|
processor_factory |
Callable[..., ConfigProcessor] | None
|
The state factory to use to parse the newly loaded configuration data. |
None
|
Returns:
Type | Description |
---|---|
self
|
|
Source code in configzen/config.py
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 |
|
config_reload() -> Self
¶
Reload the configuration from the same source.
Source code in configzen/config.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
config_reload_async() -> Self
async
¶
Do the same as config_reload
asynchronously (no I/O blocking).
Source code in configzen/config.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
|
config_save(destination: object | None = None) -> Self
¶
Save the configuration to a given destination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
destination |
object | None
|
Where to save the configuration to. The argument passed is forwarded
to |
None
|
Source code in configzen/config.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
config_save_async(destination: object | None = None) -> Self
async
¶
Do the same as config_save
, but asynchronously (no I/O blocking).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
destination |
object | None
|
Where to save the configuration to. The argument passed is forwarded
to |
None
|
Source code in configzen/config.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
|
config_at(*routes: RouteLike) -> Item
¶
Return a configuration item at the given set of routes.
Source code in configzen/config.py
536 537 538 |
|
config_dump() -> dict[str, object]
¶
Return a dictionary representation of the configuration.
Source code in configzen/config.py
540 541 542 |
|
__getitem__(routes: RouteLike | tuple[RouteLike, ...]) -> Item
¶
Return a configuration item at the given set of routes.
Source code in configzen/config.py
544 545 546 547 548 |
|
__setitem__(item: RouteLike, value: Any) -> None
¶
Set a configuration item at the given set of routes.
Source code in configzen/config.py
550 551 552 |
|
__init_subclass__(**kwargs: Unpack[ModelConfig]) -> None
¶
Initialize the configuration subclass.
Source code in configzen/config.py
554 555 556 |
|
config_step(owner: type[BaseConfig], _annotation: Any, step: Step[Any]) -> Any
¶
Return the value of a configuration attribute.
Source code in configzen/config.py
568 569 570 571 572 573 574 575 |
|
context
¶
Facilities for contextual processing.
isolated_context_function(func: Callable[_P, _T]) -> Callable[_P, _T]
¶
Copy the context automatically on function call.
Allows to isolate the library context from the user context.
Used as a decorator.
Source code in configzen/context.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
isolated_context_coroutine(func: Callable[_P, Coroutine[object, object, _T]]) -> Callable[_P, Coroutine[object, object, _T]]
¶
Copy the context automatically on coroutine execution.
Allows to isolate library context from the user context.
Used as a decorator.
Source code in configzen/context.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
run_isolated(func: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T
¶
Run a function in an isolated context.
Source code in configzen/context.py
66 67 68 69 |
|
async_run_isolated(func: Callable[_P, Coroutine[object, object, _T]], *args: _P.args, **kwargs: _P.kwargs) -> asyncio.Task[_T]
¶
Await a coroutine in an isolated context.
Source code in configzen/context.py
72 73 74 75 76 77 78 |
|
contrib
¶
data
¶
Interfaces for encapsulation of configuring and using data formats.
BinaryDataFormat = DataFormat[DataFormatOptionsType, bytes]
module-attribute
¶
Core interface for configuring and using binary data formats through within configzen.
Do not use this class directly. If you need to implement your own binary data format, implement a subclass of this class. Remember to ensure that your subclass is executed, so that it gets registered to the registry of data formats.
TextDataFormat = DataFormat[DataFormatOptionsType, str]
module-attribute
¶
Core interface for configuring and using text data formats through within configzen.
Do not use this class directly. If you need to implement your own text data format, implement a subclass of this class. Remember to ensure that your subclass is executed, so that it gets registered to the registry of data formats.
DataFormatOptions
¶
Bases: TypedDict
Base class for indicating possible options to configure a data format.
Source code in configzen/data.py
41 42 |
|
DataFormat
¶
Bases: Generic[DataFormatOptionsType, AnyStr]
Core interface for configuring and using any data format through within configzen.
Do not use this class directly. If you need to implement your own data format, implement a subclass of: - BinaryDataFormat, if it is a bitwise data format, or - TextDataFormat, if it is a text data format.
Source code in configzen/data.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
for_extension(extension_name: str, options: DataFormatOptionsType | None = None) -> DataFormat[Any, Any]
classmethod
¶
Create a data format instance for an extension.
Source code in configzen/data.py
68 69 70 71 72 73 74 75 |
|
is_binary() -> bool
¶
Return whether the data format is bitwise.
Source code in configzen/data.py
89 90 91 |
|
configure(**options: Unpack[DataFormatOptions]) -> None
¶
Configure the data format.
Every data format provides its own options, related to comments, indentation, and other format-specific features.
Source code in configzen/data.py
97 98 99 100 101 102 103 |
|
load(stream: IO[AnyStr]) -> Data
abstractmethod
¶
Load the data from a stream.
Return a mutable mapping representing the loaded data which is mutation-sensitive (for round-trip processing).
Every configuration source transforms the input data into a stream to be processed by the data format, because most data format libraries operate on streams.
This method is called by the config instance.
Source code in configzen/data.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
dump(data: Data, stream: IO[AnyStr]) -> None
abstractmethod
¶
Load the data from a stream.
Every configuration source transforms the input data into a stream to be processed by the data format, because most libraries implementing data formats operate on streams.
This method is called by the config instance.
Source code in configzen/data.py
120 121 122 123 124 125 126 127 128 129 130 |
|
register_file_extensions() -> None
classmethod
¶
Register the file extensions supported by this data format.
Source code in configzen/data.py
132 133 134 135 |
|
validate_source(source: ConfigSource[Any, AnyStr]) -> None
¶
Validate the config source.
Source code in configzen/data.py
137 138 139 140 141 142 143 144 |
|
roundtrip_update_mapping(roundtrip_data: Data, mergeable_data: MutableMapping[str, object]) -> None
¶
Update the loaded data in a round-trip manner.
Use values from the configuration altered programmatically in runtime, while keeping the structure and comments of the original data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
roundtrip_data |
Data
|
The data to be updated. Stores the original structure, comments etc. |
required |
mergeable_data |
MutableMapping[str, object]
|
The new values to be merged into the loaded data. |
required |
Source code in configzen/data.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
roundtrip_update_sequence(roundtrip_data: MutableSequence[object], mergeable_data: Sequence[object]) -> None
¶
Merge new data sequence without losing comments.
Source code in configzen/data.py
172 173 174 175 176 177 178 179 180 181 182 183 |
|
__init_subclass__(*, skip_hook: bool = False) -> None
¶
Subclass hook. Pass skip_hook=True to skip it.
Source code in configzen/data.py
185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
roundtrip_update_mapping(roundtrip_data: Data, mergeable_data: MutableMapping[str, object], *, _recursive_update_mapping: Callable[[Data, MutableMapping[str, object]], None] | None = None, _recursive_update_sequence: Callable[[MutableSequence[object], Sequence[object]], None] | None = None) -> None
¶
Update a mapping without losing recursively attached metadata.
Source code in configzen/data.py
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 |
|
roundtrip_update_sequence(roundtrip_data: MutableSequence[object], mergeable_data: Sequence[object], *, _recursive_update_mapping: Callable[[Data, MutableMapping[str, object]], None] | None = None, _recursive_update_sequence: Callable[[MutableSequence[object], Sequence[object]], None] | None = None) -> None
¶
Update a sequence without losing recursively attached metadata.
Source code in configzen/data.py
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 |
|
errors
¶
Specialized exceptions raised by configzen.
ConfigError
¶
Bases: Exception
Base class for all errors related to configzen.
Source code in configzen/errors.py
23 24 25 26 27 |
|
ConfigLoadError
¶
Bases: ConfigError
Raised when the configuration cannot be loaded.
Source code in configzen/errors.py
30 31 |
|
ConfigReloadError
¶
Bases: ConfigLoadError
Raised when the configuration cannot be reloaded.
Source code in configzen/errors.py
34 35 |
|
ConfigSaveError
¶
Bases: ConfigError
Raised when the configuration cannot be saved.
Source code in configzen/errors.py
38 39 |
|
NotAMappingError
¶
Bases: ConfigLoadError
, TypeError
Raised when the configuration being loaded is not a mapping.
Source code in configzen/errors.py
42 43 |
|
ConfigProcessorError
¶
Bases: ConfigError
Raised when a configuration replacement processor error occurs.
Source code in configzen/errors.py
46 47 |
|
BaseRouteError
¶
Bases: ConfigError
, ValueError
Raised when a configuration item route is invalid.
Source code in configzen/errors.py
50 51 |
|
RouteError
¶
Bases: BaseRouteError
Raised when a configuration item route is invalid at a specific index.
Source code in configzen/errors.py
54 55 56 57 58 59 60 61 62 63 64 |
|
__str__() -> str
¶
Return a string representation of the route error.
Source code in configzen/errors.py
62 63 64 |
|
LinkedRouteError
¶
Bases: BaseRouteError
Raised when a declared configuration item route is invalid.
Source code in configzen/errors.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
__str__() -> str
¶
Return a string representation of the route error.
Source code in configzen/errors.py
80 81 82 |
|
formats
¶
configzen.formats
: Data formats supported natively by configzen.
std_json
¶
configzen.formats.std_json
: The JSON data format.
JSONOptions
¶
Bases: DataFormatOptions
Prototype of the allowed options for the JSON data format.
Source code in configzen/formats/std_json.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
JSONDataFormat
¶
Bases: TextDataFormat[JSONOptions]
The JSON data format.
Source code in configzen/formats/std_json.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
configure(**options: Unpack[JSONOptions]) -> None
¶
For the documentation of the options, see the JSONOptions class.
Source code in configzen/formats/std_json.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
load(stream: IO[str]) -> Data
¶
Load the JSON data from the given stream.
Source code in configzen/formats/std_json.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
dump(data: Data, stream: IO[str]) -> None
¶
Dump the given JSON data to the given stream.
Source code in configzen/formats/std_json.py
108 109 110 111 112 113 114 |
|
std_plist
¶
configzen.formats.std_plist
: The Plist data format.
PlistOptions
¶
Bases: DataFormatOptions
Prototype of the allowed options for the Plist data format.
Source code in configzen/formats/std_plist.py
26 27 28 29 30 31 32 |
|
PlistDataFormat
¶
Bases: BinaryDataFormat[PlistOptions]
The Plist data format.
Source code in configzen/formats/std_plist.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
configure(**options: Unpack[PlistOptions]) -> None
¶
For the documentation of the options, see the PlistOptions class.
Source code in configzen/formats/std_plist.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
load(stream: IO[bytes]) -> Data
¶
Load the data from the given stream.
Source code in configzen/formats/std_plist.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
dump(data: Data, stream: IO[bytes]) -> None
¶
Dump the given data to the stream.
Source code in configzen/formats/std_plist.py
82 83 84 85 86 87 88 89 90 |
|
toml
¶
configzen.formats.toml
: The TOML data format.
TOMLOptions
¶
Bases: DataFormatOptions
Prototype of the allowed options for the TOML data format.
Source code in configzen/formats/toml.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
encoders: list[Encoder]
instance-attribute
¶
List of encoders to perform automatic tomlkit.register_encoder() calls on.
unregister_old_encoders: bool
instance-attribute
¶
Whether to unregister all previously registered encoders before registering the new ones.
sort_keys: bool
instance-attribute
¶
Whether to sort keys in the output.
TOMLDataFormat
¶
Bases: TextDataFormat[TOMLOptions]
The TOML data format.
Source code in configzen/formats/toml.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
configure(**options: Unpack[TOMLOptions]) -> None
¶
For the documentation of the options, see the TOMLOptions class.
Source code in configzen/formats/toml.py
55 56 57 58 59 60 61 62 63 64 65 66 |
|
load(stream: IO[str]) -> Data
¶
Load the data from the given stream.
Source code in configzen/formats/toml.py
68 69 70 |
|
dump(data: Data, stream: IO[str]) -> None
¶
Dump the data to the given stream.
Source code in configzen/formats/toml.py
72 73 74 75 76 77 78 |
|
yaml
¶
configzen.formats.yaml
: The YAML data format.
YAMLOptions
¶
Bases: DataFormatOptions
Prototype of the allowed options for the YAML data format.
For more information, see the documentation of the ruamel.yaml.YAML
class.
Source code in configzen/formats/yaml.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
classes: list[type]
instance-attribute
¶
List of classes to automatically call YAML.register_class() on.
version: str | tuple[int | str, ...]
instance-attribute
¶
The YAML version to use.
indent: int
instance-attribute
¶
Indentation width.
block_seq_indent: int
instance-attribute
¶
Indentation for nested block sequences.
YAMLDataFormat
¶
Bases: TextDataFormat[YAMLOptions]
The YAML data format.
Source code in configzen/formats/yaml.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
configure(**options: Unpack[YAMLOptions]) -> None
¶
For the documentation of the options, see the YAMLOptions class.
Source code in configzen/formats/yaml.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
load(stream: IO[str]) -> Data
¶
Load the data from a stream.
Return a mutable mapping representing the loaded data which is mutation-sensitive (for round-trip processing).
Every configuration source transforms the input data into a stream to be processed by the data format, because most data format libraries operate on streams.
This method is called by the configuration model.
Source code in configzen/formats/yaml.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
dump(data: Data, stream: IO[str]) -> None
¶
Load the data from a stream.
Every configuration source transforms the input data into a stream to be processed by the data format, because most data format libraries operate on streams.
This method is called by the configuration model.
Source code in configzen/formats/yaml.py
106 107 108 109 110 111 112 113 114 115 116 |
|
module_proxy
¶
Runtime modules with attribute type validation.
ModuleProxy
¶
Bases: ModuleType
, Generic[ConfigObject]
Proxy object that extends a runtime module with type validation.
Triggered via a config instance (initialization and assignment).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the module. |
required |
config |
ConfigObject
|
The configuration model to use for type validation. |
required |
module_namespace |
dict[str, Any] | None
|
The module namespace to wrap. |
None
|
Source code in configzen/module_proxy.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
__getattribute__(name: str) -> Any
¶
Get an attribute of the underlying model.
Source code in configzen/module_proxy.py
67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__setattr__(key: str, value: Any) -> None
¶
Set an attribute on the underlying model.
Source code in configzen/module_proxy.py
81 82 83 84 85 86 |
|
__repr__() -> str
¶
Get the string representation of the module proxy.
Inform the user that this is a configuration module.
Source code in configzen/module_proxy.py
88 89 90 91 92 93 94 |
|
get_config() -> ConfigObject
¶
Get the configuration model.
Source code in configzen/module_proxy.py
96 97 98 |
|
wrap_module(module_name: str, config_class: type[ConfigObject] | None = None, namespace: dict[str, Any] | None = None, /, **values: Any) -> ModuleProxy[ConfigObject]
classmethod
¶
Wrap a module to ensure type validation.
Every attribute of the wrapped module that is also a field of the config will be validated against it. The module will be extended with the config's attributes. Assignments on the module's attributes will be propagated to the configuration instance. It could be said that the module becomes a proxy for the configuration once wrapped.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name |
str
|
The name of the module to wrap. |
required |
config_class |
type[ConfigObject] | None
|
The config class to use for type validation. |
None
|
namespace |
dict[str, Any] | None
|
The namespace of the module to wrap. If not provided, it will be
retrieved from |
None
|
values |
Any
|
Values used to initialize the config. |
{}
|
Returns:
Type | Description |
---|---|
The wrapped module.
|
|
Source code in configzen/module_proxy.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
wrap_this_module(config_class: type[ConfigObject] | None = None, /, **values: Any) -> ModuleProxy[ConfigObject]
classmethod
¶
Wrap the module calling this function.
For more information on wrapping modules, see ModuleProxy.wrap_module()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_class |
type[ConfigObject] | None
|
The config class to use for type validation. |
None
|
values |
Any
|
Values used to initialize the config. |
{}
|
Source code in configzen/module_proxy.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
processor
¶
Replacement API processor for configuration data.
Allows to tweak the configuration data programmatically before it is given to the model config and revert the changes back to the original data structure when the configuration managed by that model is saved.
ProcessorOptions
¶
Bases: TypedDict
Prototype of the allowed options for the ConfigProcessor class.
Source code in configzen/processor.py
52 53 54 55 56 57 58 |
|
ConfigProcessor
¶
A class that takes in configuration data and processes it.
Recursively resolves & applies replacements in data magically.
Source code in configzen/processor.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 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 |
|
macros: MacroDict
property
¶
Get macros bound to this processor.
roundtrip_initial: Data
property
¶
The initial configuration data that the processor was given.
create_processor(data: Data) -> ConfigProcessor
¶
Create a new configuration processor with identical options.
Source code in configzen/processor.py
223 224 225 |
|
get_processed_data(*, force: bool = False) -> _ProcessedData
¶
Create the data with replacements or return the one already cached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force |
bool
|
Whether to forcibly parse the original data even if it was already parsed. Default is False. |
False
|
Source code in configzen/processor.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
__init_subclass__() -> None
¶
Merge macro registries on subclass.
Source code in configzen/processor.py
250 251 252 253 254 255 256 257 258 259 260 261 |
|
sanitize_macro_name(name: str) -> str
staticmethod
¶
Ensure a uniform name of every macro.
Source code in configzen/processor.py
263 264 265 266 |
|
macro(name: str, macro: MacroT) -> MacroT
classmethod
¶
Override a macro.
Source code in configzen/processor.py
268 269 270 271 272 273 |
|
ProcessorReplacement
dataclass
¶
A change that was made to the configuration data during processing.
Attributes:
Name | Type | Description |
---|---|---|
key |
str
|
The key of the item before alteration. |
value |
object
|
The value of the item before alteration. |
content |
Data
|
The value to unpack in place of the alteration key. |
Source code in configzen/processor.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
FileSystemAwareConfigProcessor
¶
Bases: ConfigProcessor
Config processor that is aware of the file system.
Can handle requests for transcluding other configuration files to achieve a sense of extendability.
Source code in configzen/processor.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
extend(sources: str | dict[str, str]) -> Data
¶
Transclude a config in this config.
Source code in configzen/processor.py
334 335 336 337 338 339 340 341 342 |
|
routes
¶
Routes creation and parsing.
Step
¶
Bases: Generic[_KT]
A configuration route step.
Do not use this class directly. Use GetAttr or GetItem instead.
Source code in configzen/routes.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
__eq__(other: object) -> bool
¶
Compare this step to another step.
Source code in configzen/routes.py
62 63 64 65 66 67 68 69 |
|
get(_: Any) -> object
¶
Perform a get operation.
Source code in configzen/routes.py
71 72 73 |
|
set(_: Any, __: object) -> None
¶
Perform a set operation.
Source code in configzen/routes.py
75 76 77 |
|
__call__(obj: Any) -> object
¶
Perform a get operation.
Source code in configzen/routes.py
79 80 81 |
|
__repr__() -> str
¶
Represent this step in a string.
Source code in configzen/routes.py
83 84 85 |
|
GetAttr
¶
A route step that gets an attribute from an object.
The argument is used as an attribute name.
Source code in configzen/routes.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
get(target: Any) -> object
¶
Get an attribute from an object.
Source code in configzen/routes.py
95 96 97 |
|
set(target: Any, value: object) -> None
¶
Set an attribute in an object.
Source code in configzen/routes.py
99 100 101 |
|
__str__() -> str
¶
Compose this step into a string.
Source code in configzen/routes.py
103 104 105 |
|
GetItem
¶
A route step that gets an item from an object.
If the argument is a string, it is used checked for being a digit. Unless explicitly escaped, if it is a digit, it is casted to an integer. Otherwise, it is used as is.
Source code in configzen/routes.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
get(target: Any) -> object
¶
Get an item from an object.
Source code in configzen/routes.py
126 127 128 |
|
set(target: Any, value: object) -> None
¶
Set an item in an object.
Source code in configzen/routes.py
130 131 132 |
|
__str__() -> str
¶
Compose this step into a string.
Source code in configzen/routes.py
134 135 136 137 138 139 140 141 |
|
Route
¶
Routes are, lists of steps that are used to access values in a configuration.
Each step is either a key or an index.
A route can be created from a string, a list of steps, or another route.
Examples:
>>> route = Route("a.b.c")
>>> route
<Route 'a.b.c'>
>>> route.steps
[GetAttr('a'), GetAttr('b'), GetAttr('c')]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
route |
RouteLike
|
A route to parse. |
required |
allow_empty |
bool
|
Whether to allow empty routes. |
False
|
Source code in configzen/routes.py
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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
steps: list[Step[Any]]
property
¶
Get all steps in this route.
__hash__() -> int
¶
Get a hash of this route.
Source code in configzen/routes.py
295 296 297 |
|
parse(route: RouteLike) -> list[Step[Any]]
classmethod
¶
Parse a route into steps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
route |
RouteLike
|
The route to parse. |
required |
Returns:
Type | Description |
---|---|
List of steps.
|
|
Source code in configzen/routes.py
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 |
|
decompose(route: str) -> list[Step[Any]]
classmethod
¶
Decompose a route into a list of steps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
route |
str
|
A route to decompose. |
required |
Returns:
Type | Description |
---|---|
List of steps.
|
|
Source code in configzen/routes.py
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 |
|
compose() -> str
¶
Compose this route into a string.
Source code in configzen/routes.py
365 366 367 368 369 370 371 372 373 374 375 |
|
enter(subroute: RouteLike) -> Route
¶
Enter a subroute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subroute |
RouteLike
|
A subroute to enter. |
required |
Source code in configzen/routes.py
377 378 379 380 381 382 383 384 385 386 387 |
|
get(obj: Any) -> object
¶
Get an object at the end of this route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
An object to dive in. |
required |
Returns:
Type | Description |
---|---|
The result of visiting the object.
|
|
Source code in configzen/routes.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
set(obj: Any, value: object) -> None
¶
Set an object pointed to by this route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
An object to dive in. |
required |
value |
object
|
A value to set. |
required |
Returns:
Type | Description |
---|---|
The result of visiting the object.
|
|
Source code in configzen/routes.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
__eq__(other: object) -> bool
¶
Compare this route to another route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object
|
Another route to compare to. |
required |
Source code in configzen/routes.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
__str__() -> str
¶
Compose this route into a string.
Source code in configzen/routes.py
447 448 449 |
|
__iter__() -> Iterator[Step[Any]]
¶
Yield all steps in this route.
Source code in configzen/routes.py
451 452 453 |
|
__repr__() -> str
¶
Represent this route in a string.
Source code in configzen/routes.py
455 456 457 |
|
advance_linked_route(_current_head: Type[object], _annotation: Any, _step: Step[object]) -> Any
¶
Move one step forward in a linked route.
Source code in configzen/routes.py
463 464 465 466 467 468 469 470 |
|
sources
¶
Sources and destinations that hold the configuration data.
ConfigSource
¶
Bases: Generic[SourceType, AnyStr]
Core interface for loading and saving configuration data.
If you need to implement your own configuration source class,
implement a subclass of this class and pass in to the .config_load()
method
of your configuration or its model_config.
Source code in configzen/sources.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
data_format: DataFormat[Any, AnyStr]
property
writable
¶
The current data format for a configuration source.
is_binary() -> bool
¶
Determine whether the configuration source is binary.
Source code in configzen/sources.py
132 133 134 |
|
load() -> Data
abstractmethod
¶
Load the configuration source.
Return its contents as a dictionary.
Source code in configzen/sources.py
136 137 138 139 140 141 142 143 |
|
load_async() -> Data
abstractmethod
async
¶
Load the configuration source asynchronously.
Return its contents as a dictionary.
Source code in configzen/sources.py
145 146 147 148 149 150 151 152 |
|
dump(data: Data) -> None
abstractmethod
¶
Dump the configuration source.
Source code in configzen/sources.py
154 155 156 157 |
|
dump_async(data: Data) -> int
abstractmethod
async
¶
Dump the configuration source asynchronously.
Source code in configzen/sources.py
159 160 161 162 |
|
StreamConfigSource
¶
Bases: Generic[AnyStr]
, ConfigSource[IO[Any], Any]
A configuration source that is a stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
IO[AnyStr]
|
The stream to the configuration source. |
required |
Source code in configzen/sources.py
189 190 191 192 193 194 195 196 197 198 199 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 |
|
load() -> Data
¶
Load the configuration source.
Return its contents as a dictionary.
Source code in configzen/sources.py
212 213 214 215 216 217 218 |
|
load_async() -> Never
¶
Unsupported.
Source code in configzen/sources.py
220 221 222 223 |
|
dump(data: Data) -> None
¶
Dump the configuration source.
Source code in configzen/sources.py
225 226 227 |
|
dump_async(_data: Data) -> Never
¶
Unsupported.
Source code in configzen/sources.py
229 230 231 232 |
|
FileConfigSource
¶
Bases: Generic[AnyStr]
, ConfigSource[Path, AnyStr]
A configuration source that is a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
str | bytes | PathLike[str] | PathLike[bytes]
|
The path to the configuration source file. |
required |
Source code in configzen/sources.py
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 412 413 414 415 416 417 418 419 |
|
paths: list[Path]
property
¶
List possible path variants basing on the processing context trace.
load() -> Data
¶
Load the configuration source file and return its contents as a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_format |
The data format to use when loading the data. |
required |
Source code in configzen/sources.py
305 306 307 308 309 310 311 312 313 314 315 |
|
load_async() -> Data
async
¶
Load the configuration source file asynchronously.
Return its contents as a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_format |
The data format to use when loading the data. |
required |
Source code in configzen/sources.py
317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
dump(data: Data) -> None
¶
Dump the configuration data to the source file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Data
|
The data to dump to the configuration source. |
required |
data_format |
The data format to use when dumping the data. |
required |
Source code in configzen/sources.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
dump_async(data: Data) -> int
async
¶
Load the configuration source file asynchronously.
Return its contents as a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Data
|
The data to dump to the configuration source. |
required |
data_format |
The data format to use when dumping the data. |
required |
Source code in configzen/sources.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
read() -> AnyStr
¶
Read the configuration source and return its contents.
Source code in configzen/sources.py
367 368 369 370 371 372 373 374 375 376 377 |
|
read_async() -> AnyStr
async
¶
Read the configuration source file asynchronously and return its contents.
Source code in configzen/sources.py
379 380 381 382 383 384 385 386 387 388 389 |
|
write(content: AnyStr) -> int
¶
Write the configuration source file and return the number of bytes written.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
AnyStr
|
The content to write to the configuration source. |
required |
Source code in configzen/sources.py
391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
write_async(content: AnyStr) -> int
async
¶
Write the configuration source file asynchronously.
Return the number of bytes written.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
AnyStr
|
The content to write to the configuration source. |
required |
Source code in configzen/sources.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
get_config_source(source: object, _data_format: DataFormat[Any, AnyStr] | None = None) -> ConfigSource[Any, Any]
¶
Get a dedicated interface for a configuration source.
Source code in configzen/sources.py
165 166 167 168 169 170 171 172 173 174 175 176 |
|
get_stream_config_source(source: IO[bytes] | IO[str], data_format: DataFormat[Any, Any]) -> StreamConfigSource[str] | StreamConfigSource[bytes]
¶
Get a dedicated interface for a configuration source stream.
Source code in configzen/sources.py
235 236 237 238 239 240 241 242 |
|
get_file_config_source(source: str | bytes | PathLike[str] | PathLike[bytes], data_format: DataFormat[Any, AnyStr] | None = None) -> FileConfigSource[str] | FileConfigSource[bytes]
¶
Get a dedicated interface for a configuration source file.
Source code in configzen/sources.py
422 423 424 425 426 427 428 429 430 |
|
typedefs
¶
Miscellaneous type definitions for configzen.