API Reference
Manage configuration with pydantic.
Modules:
-
config
–The base configuration model class
BaseConfig
. -
context
–Facilities for contextual processing.
-
contrib
–configzen.contrib: miscellaneous reusable configuration models from configzen.
-
data
–Interfaces for encapsulation of configuring and using data formats.
-
errors
–Specialized exceptions raised by configzen.
-
formats
–configzen.formats
: Data formats supported natively by configzen. -
module_proxy
–Runtime modules with attribute type validation.
-
processor
–Replacement API processor for configuration data.
-
routes
–Routes creation and parsing.
-
sources
–Sources and destinations that hold the configuration data.
-
typedefs
–Miscellaneous type definitions for configzen.
config
¶
The base configuration model class BaseConfig
.
Classes:
-
ModelConfig
–Meta-configuration for configzen models.
-
BaseConfig
–Base class for all configuration models.
Functions:
-
config_step
–Return the value of a configuration attribute.
ModelConfig
¶
Bases: SettingsConfigDict
Meta-configuration for configzen models.
BaseConfig(**data)
¶
Bases: BaseSettings
Base class for all configuration models.
Methods:
-
config_find_routes
–Locate all occurrences of a subconfiguration in the current configuration.
-
config_find_route
–Locate exactly one (closest) route to the given subconfiguration.
-
config_load
–Load this configuration from a given source.
-
config_load_async
–Do the same as
config_load
, but asynchronously (no I/O blocking). -
config_reload
–Reload the configuration from the same source.
-
config_reload_async
–Do the same as
config_reload
asynchronously (no I/O blocking). -
config_save
–Save the configuration to a given destination.
-
config_save_async
–Do the same as
config_save
, but asynchronously (no I/O blocking). -
config_at
–Return a configuration item at the given set of routes.
-
config_dump
–Return a dictionary representation of the configuration.
-
__getitem__
–Return a configuration item at the given set of routes.
-
__setitem__
–Set a configuration item at the given set of routes.
-
__init_subclass__
–Initialize the configuration subclass.
Attributes:
-
config_root
(BaseConfig
) –Return the root configuration that was used to load the entire data.
-
config_source
(ConfigSource[Any, Any] | None
) –Return the configuration source that was used to load the configuration.
-
config_data
(Data
) –Return the configuration that was loaded from the configuration source.
-
config_processor
(ConfigProcessor
) –Current configuration processor.
Source code in configzen/config.py
158 159 160 161 162 163 164 165 166 |
|
config_root: BaseConfig
¶
Return the root configuration that was used to load the entire data.
config_source: ConfigSource[Any, Any] | None
¶
Return the configuration source that was used to load the configuration.
config_data: Data
¶
Return the configuration that was loaded from the configuration source.
config_processor: ConfigProcessor
¶
Current configuration processor.
Processor stores the initial data used when loading the configuration, resolves macros etc.
config_find_routes(subconfig)
¶
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)
¶
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=None, *, processor_factory=None)
¶
Load this configuration from a given source.
Parameters:
-
source
¶object | None
, default:None
) –Where to load the configuration from. The argument passed is forwarded to
confizen.sources.get_config_source()
which will resolve the intended configuration source: for example, "abc.ini" will be resolved to a TOML text file source. Keep in mind, however, that for binary formats such as non-XML Plist you must specify its format type to binary, so in that case just createBinaryFileConfigSource("plist_file.plist")
. -
processor_factory
¶Callable[..., ConfigProcessor] | None
, default:None
) –The state factory to use to parse the newly loaded configuration data.
Returns:
-
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 |
|
config_load_async(source=None, *, processor_factory=None)
¶
Do the same as config_load
, but asynchronously (no I/O blocking).
Parameters:
-
source
¶object | None
, default:None
) –Where to load the configuration from. The argument passed is forwarded to
confizen.sources.get_config_source()
which will resolve the intended configuration source: for example, "abc.ini" will be resolved to a TOML text file source. Keep in mind, however, that for binary formats such as non-XML Plist you must specify its format type to binary, so in that case just createBinaryFileConfig"plist_file.plist")
. -
processor_factory
¶Callable[..., ConfigProcessor] | None
, default:None
) –The state factory to use to parse the newly loaded configuration data.
Returns:
-
self
–
Source code in configzen/config.py
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 |
|
config_reload()
¶
Reload the configuration from the same source.
Source code in configzen/config.py
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 |
|
config_reload_async()
¶
Do the same as config_reload
asynchronously (no I/O blocking).
Source code in configzen/config.py
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 |
|
config_save(destination=None)
¶
Save the configuration to a given destination.
Parameters:
-
destination
¶object | None
, default:None
) –Where to save the configuration to. The argument passed is forwarded to
confizen.sources.get_config_source()
which will resolve the intended configuration source: for example, "abc.ini" will be resolved to a TOML text file source. Keep in mind, however, that for binary formats such as non-XML Plist you must specify its format type to binary, so in that case just createBinaryFileConfigSource("plist_file.plist")
.
Source code in configzen/config.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
config_save_async(destination=None)
¶
Do the same as config_save
, but asynchronously (no I/O blocking).
Parameters:
-
destination
¶object | None
, default:None
) –Where to save the configuration to. The argument passed is forwarded to
confizen.sources.get_config_source()
which will resolve the intended configuration source: for example, "abc.ini" will be resolved to a TOML text file source. Keep in mind, however, that for binary formats such as non-XML Plist you must specify its format type to binary, so in that case just createBinaryFileConfigSource("plist_file.plist")
.
Source code in configzen/config.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
|
config_at(*routes)
¶
Return a configuration item at the given set of routes.
Source code in configzen/config.py
533 534 535 |
|
config_dump()
¶
Return a dictionary representation of the configuration.
Source code in configzen/config.py
537 538 539 |
|
__getitem__(routes)
¶
Return a configuration item at the given set of routes.
Source code in configzen/config.py
541 542 543 544 545 |
|
__setitem__(item, value)
¶
Set a configuration item at the given set of routes.
Source code in configzen/config.py
547 548 549 |
|
__init_subclass__(**kwargs)
¶
Initialize the configuration subclass.
Source code in configzen/config.py
551 552 553 |
|
config_step(owner, _annotation, step)
¶
Return the value of a configuration attribute.
Source code in configzen/config.py
565 566 567 568 569 570 571 572 |
|
context
¶
Facilities for contextual processing.
Functions:
-
isolated_context_function
–Copy the context automatically on function call.
-
isolated_context_coroutine
–Copy the context automatically on coroutine execution.
-
run_isolated
–Run a function in an isolated context.
-
async_run_isolated
–Await a coroutine in an isolated context.
isolated_context_function(func)
¶
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)
¶
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, *args, **kwargs)
¶
Run a function in an isolated context.
Source code in configzen/context.py
66 67 68 69 |
|
async_run_isolated(func, *args, **kwargs)
¶
Await a coroutine in an isolated context.
Source code in configzen/context.py
72 73 74 75 76 77 78 |
|
contrib
¶
configzen.contrib: miscellaneous reusable configuration models from configzen.
Modules:
data
¶
Interfaces for encapsulation of configuring and using data formats.
Classes:
-
DataFormatOptions
–Base class for indicating possible options to configure a data format.
-
DataFormat
–Core interface for configuring and using any data format through within configzen.
Functions:
-
roundtrip_update_mapping
–Update a mapping without losing recursively attached metadata.
-
roundtrip_update_sequence
–Update a sequence without losing recursively attached metadata.
Attributes:
-
BinaryDataFormat
–Core interface for configuring and using binary data formats through
-
TextDataFormat
–Core interface for configuring and using text data formats through
BinaryDataFormat = DataFormat[DataFormatOptionsType, bytes]
¶
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]
¶
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.
DataFormat(options=None)
¶
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.
Methods:
-
for_extension
–Create a data format instance for an extension.
-
is_binary
–Return whether the data format is bitwise.
-
configure
–Configure the data format.
-
load
–Load the data from a stream.
-
dump
–Load the data from a stream.
-
register_file_extensions
–Register the file extensions supported by this data format.
-
validate_source
–Validate the config source.
-
roundtrip_update_mapping
–Update the loaded data in a round-trip manner.
-
roundtrip_update_sequence
–Merge new data sequence without losing comments.
-
__init_subclass__
–Subclass hook. Pass skip_hook=True to skip it.
Source code in configzen/data.py
65 66 |
|
for_extension(extension_name, options=None)
¶
Create a data format instance for an extension.
Source code in configzen/data.py
68 69 70 71 72 73 74 75 |
|
is_binary()
¶
is_binary() -> Literal[True]
is_binary() -> Literal[False]
Return whether the data format is bitwise.
Source code in configzen/data.py
89 90 91 |
|
configure(**options)
¶
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)
¶
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, stream)
¶
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()
¶
Register the file extensions supported by this data format.
Source code in configzen/data.py
132 133 134 135 |
|
validate_source(source)
¶
Validate the config source.
Source code in configzen/data.py
137 138 139 140 141 142 143 144 |
|
roundtrip_update_mapping(roundtrip_data, mergeable_data)
¶
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:
-
roundtrip_data
¶Data
) –The data to be updated. Stores the original structure, comments etc.
-
mergeable_data
¶MutableMapping[str, object]
) –The new values to be merged into the loaded data.
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, mergeable_data)
¶
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=False)
¶
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, mergeable_data, *, _recursive_update_mapping=None, _recursive_update_sequence=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, mergeable_data, *, _recursive_update_mapping=None, _recursive_update_sequence=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.
Classes:
-
ConfigError
–Base class for all errors related to configzen.
-
ConfigLoadError
–Raised when the configuration cannot be loaded.
-
ConfigReloadError
–Raised when the configuration cannot be reloaded.
-
ConfigSaveError
–Raised when the configuration cannot be saved.
-
NotAMappingError
–Raised when the configuration being loaded is not a mapping.
-
ConfigProcessorError
–Raised when a configuration replacement processor error occurs.
-
BaseRouteError
–Raised when a configuration item route is invalid.
-
RouteError
–Raised when a configuration item route is invalid at a specific index.
-
LinkedRouteError
–Raised when a declared configuration item route is invalid.
ConfigError(message)
¶
Bases: Exception
Base class for all errors related to configzen.
Source code in configzen/errors.py
26 27 |
|
ConfigLoadError(message)
¶
Bases: ConfigError
Raised when the configuration cannot be loaded.
Source code in configzen/errors.py
26 27 |
|
ConfigReloadError(message)
¶
Bases: ConfigLoadError
Raised when the configuration cannot be reloaded.
Source code in configzen/errors.py
26 27 |
|
ConfigSaveError(message)
¶
Bases: ConfigError
Raised when the configuration cannot be saved.
Source code in configzen/errors.py
26 27 |
|
NotAMappingError(message)
¶
Bases: ConfigLoadError
, TypeError
Raised when the configuration being loaded is not a mapping.
Source code in configzen/errors.py
26 27 |
|
ConfigProcessorError(message)
¶
Bases: ConfigError
Raised when a configuration replacement processor error occurs.
Source code in configzen/errors.py
26 27 |
|
BaseRouteError(message)
¶
Bases: ConfigError
, ValueError
Raised when a configuration item route is invalid.
Source code in configzen/errors.py
26 27 |
|
RouteError(message, route, index)
¶
Bases: BaseRouteError
Raised when a configuration item route is invalid at a specific index.
Methods:
-
__str__
–Return a string representation of the route error.
Source code in configzen/errors.py
57 58 59 60 |
|
__str__()
¶
Return a string representation of the route error.
Source code in configzen/errors.py
62 63 64 |
|
LinkedRouteError(message, route, config_class)
¶
Bases: BaseRouteError
Raised when a declared configuration item route is invalid.
Methods:
-
__str__
–Return a string representation of the route error.
Source code in configzen/errors.py
70 71 72 73 74 75 76 77 78 |
|
__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.
Modules:
-
std_json
–configzen.formats.std_json
: The JSON data format. -
std_plist
–configzen.formats.std_plist
: The Plist data format. -
toml
–configzen.formats.toml
: The TOML data format. -
yaml
–configzen.formats.yaml
: The YAML data format.
std_json
¶
configzen.formats.std_json
: The JSON data format.
Classes:
-
JSONOptions
–Prototype of the allowed options for the JSON data format.
-
JSONDataFormat
–The JSON data format.
JSONOptions
¶
JSONDataFormat
¶
Bases: TextDataFormat[JSONOptions]
The JSON data format.
Methods:
-
configure
–For the documentation of the options, see the JSONOptions class.
-
load
–Load the JSON data from the given stream.
-
dump
–Dump the given JSON data to the given stream.
configure(**options)
¶
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)
¶
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, stream)
¶
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.
Classes:
-
PlistOptions
–Prototype of the allowed options for the Plist data format.
-
PlistDataFormat
–The Plist data format.
PlistOptions
¶
PlistDataFormat
¶
Bases: BinaryDataFormat[PlistOptions]
The Plist data format.
Methods:
-
configure
–For the documentation of the options, see the PlistOptions class.
-
load
–Load the data from the given stream.
-
dump
–Dump the given data to the stream.
configure(**options)
¶
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)
¶
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, stream)
¶
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.
Classes:
-
TOMLOptions
–Prototype of the allowed options for the TOML data format.
-
TOMLDataFormat
–The TOML data format.
TOMLOptions
¶
TOMLDataFormat
¶
Bases: TextDataFormat[TOMLOptions]
The TOML data format.
Methods:
-
configure
–For the documentation of the options, see the TOMLOptions class.
-
load
–Load the data from the given stream.
-
dump
–Dump the data to the given stream.
configure(**options)
¶
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)
¶
Load the data from the given stream.
Source code in configzen/formats/toml.py
68 69 70 |
|
dump(data, stream)
¶
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.
Classes:
-
YAMLOptions
–Prototype of the allowed options for the YAML data format.
-
YAMLDataFormat
–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.
Attributes:
YAMLDataFormat
¶
Bases: TextDataFormat[YAMLOptions]
The YAML data format.
Methods:
-
configure
–For the documentation of the options, see the YAMLOptions class.
-
load
–Load the data from a stream.
-
dump
–Load the data from a stream.
configure(**options)
¶
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)
¶
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, stream)
¶
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.
Classes:
-
ModuleProxy
–Proxy object that extends a runtime module with type validation.
ModuleProxy(name, config, module_namespace=None, doc=None)
¶
Bases: ModuleType
, Generic[ConfigObject]
Proxy object that extends a runtime module with type validation.
Triggered via a config instance (initialization and assignment).
Parameters:
-
name
¶str
) –The name of the module.
-
config
¶ConfigObject
) –The configuration model to use for type validation.
-
module_namespace
¶dict[str, Any] | None
, default:None
) –The module namespace to wrap.
Methods:
-
__getattribute__
–Get an attribute of the underlying model.
-
__setattr__
–Set an attribute on the underlying model.
-
__repr__
–Get the string representation of the module proxy.
-
get_config
–Get the configuration model.
-
wrap_module
–Wrap a module to ensure type validation.
-
wrap_this_module
–Wrap the module calling this function.
Source code in configzen/module_proxy.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
__getattribute__(name)
¶
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, value)
¶
Set an attribute on the underlying model.
Source code in configzen/module_proxy.py
81 82 83 84 85 86 |
|
__repr__()
¶
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()
¶
Get the configuration model.
Source code in configzen/module_proxy.py
96 97 98 |
|
wrap_module(module_name, config_class=None, namespace=None, /, **values)
¶
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:
-
module_name
¶str
) –The name of the module to wrap.
-
config_class
¶type[ConfigObject] | None
, default:None
) –The config class to use for type validation.
-
namespace
¶dict[str, Any] | None
, default:None
) –The namespace of the module to wrap. If not provided, it will be retrieved from
sys.modules
. -
values
¶Any
, default:{}
) –Values used to initialize the config.
Returns:
-
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=None, /, **values)
¶
Wrap the module calling this function.
For more information on wrapping modules, see ModuleProxy.wrap_module()
.
Parameters:
-
config_class
¶type[ConfigObject] | None
, default:None
) –The config class to use for type validation.
-
values
¶Any
, default:{}
) –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.
Classes:
-
ProcessorOptions
–Prototype of the allowed options for the ConfigProcessor class.
-
ConfigProcessor
–A class that takes in configuration data and processes it.
-
ProcessorReplacement
–A change that was made to the configuration data during processing.
-
FileSystemAwareConfigProcessor
–Config processor that is aware of the file system.
ConfigProcessor(initial, *, macro_prefix=Char('^'), update_prefix=Char('+'), macros_on_top=False, lenient=True)
¶
A class that takes in configuration data and processes it.
Recursively resolves & applies replacements in data magically.
Methods:
-
create_processor
–Create a new configuration processor with identical options.
-
get_processed_data
–Create the data with replacements or return the one already cached.
-
__init_subclass__
–Merge macro registries on subclass.
-
sanitize_macro_name
–Ensure a uniform name of every macro.
-
macro
–Override a macro.
Attributes:
-
macros
(MacroDict
) –Get macros bound to this processor.
-
roundtrip_initial
(Data
) –The initial configuration data that the processor was given.
Source code in configzen/processor.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
macros: MacroDict
¶
Get macros bound to this processor.
roundtrip_initial: Data
¶
The initial configuration data that the processor was given.
create_processor(data)
¶
Create a new configuration processor with identical options.
Source code in configzen/processor.py
223 224 225 |
|
get_processed_data(*, force=False)
¶
Create the data with replacements or return the one already cached.
Parameters:
-
force
¶bool
, default:False
) –Whether to forcibly parse the original data even if it was already parsed. Default is 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__()
¶
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)
¶
Ensure a uniform name of every macro.
Source code in configzen/processor.py
263 264 265 266 |
|
macro(name, macro)
¶
Override a macro.
Source code in configzen/processor.py
268 269 270 271 272 273 |
|
ProcessorReplacement(key, value, content)
¶
FileSystemAwareConfigProcessor(initial, *, macro_prefix=Char('^'), update_prefix=Char('+'), macros_on_top=False, lenient=True)
¶
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.
Methods:
-
extend
–Transclude a config in this config.
Source code in configzen/processor.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
extend(sources)
¶
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.
Classes:
-
Step
–A configuration route step.
-
GetAttr
–A route step that gets an attribute from an object.
-
GetItem
–A route step that gets an item from an object.
-
Route
–Routes are, lists of steps that are used to access values in a configuration.
Functions:
-
advance_linked_route
–Move one step forward in a linked route.
Step(key)
¶
Bases: Generic[_KT]
A configuration route step.
Do not use this class directly. Use GetAttr or GetItem instead.
Methods:
-
__eq__
–Compare this step to another step.
-
get
–Perform a get operation.
-
set
–Perform a set operation.
-
__call__
–Perform a get operation.
-
__repr__
–Represent this step in a string.
Source code in configzen/routes.py
59 60 |
|
__eq__(other)
¶
Compare this step to another step.
Source code in configzen/routes.py
62 63 64 65 66 67 68 69 |
|
get(_)
¶
Perform a get operation.
Source code in configzen/routes.py
71 72 73 |
|
set(_, __)
¶
Perform a set operation.
Source code in configzen/routes.py
75 76 77 |
|
__call__(obj)
¶
Perform a get operation.
Source code in configzen/routes.py
79 80 81 |
|
__repr__()
¶
Represent this step in a string.
Source code in configzen/routes.py
83 84 85 |
|
GetAttr(key)
¶
A route step that gets an attribute from an object.
The argument is used as an attribute name.
Methods:
-
get
–Get an attribute from an object.
-
set
–Set an attribute in an object.
-
__str__
–Compose this step into a string.
Source code in configzen/routes.py
59 60 |
|
get(target)
¶
Get an attribute from an object.
Source code in configzen/routes.py
95 96 97 |
|
set(target, value)
¶
Set an attribute in an object.
Source code in configzen/routes.py
99 100 101 |
|
__str__()
¶
Compose this step into a string.
Source code in configzen/routes.py
103 104 105 |
|
GetItem(key, /, *, ignore_digit=False)
¶
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.
Methods:
-
get
–Get an item from an object.
-
set
–Set an item in an object.
-
__str__
–Compose this step into a string.
Source code in configzen/routes.py
117 118 119 120 121 122 123 124 |
|
get(target)
¶
Get an item from an object.
Source code in configzen/routes.py
126 127 128 |
|
set(target, value)
¶
Set an item in an object.
Source code in configzen/routes.py
130 131 132 |
|
__str__()
¶
Compose this step into a string.
Source code in configzen/routes.py
134 135 136 137 138 139 140 141 |
|
Route(route, *, allow_empty=False)
¶
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:
-
route
¶RouteLike
) –A route to parse.
-
allow_empty
¶bool
, default:False
) –Whether to allow empty routes.
Methods:
-
__hash__
–Get a hash of this route.
-
parse
–Parse a route into steps.
-
decompose
–Decompose a route into a list of steps.
-
compose
–Compose this route into a string.
-
enter
–Enter a subroute.
-
get
–Get an object at the end of this route.
-
set
–Set an object pointed to by this route.
-
__eq__
–Compare this route to another route.
-
__str__
–Compose this route into a string.
-
__iter__
–Yield all steps in this route.
-
__repr__
–Represent this route in a string.
Attributes:
Source code in configzen/routes.py
278 279 280 281 282 283 284 285 286 287 288 |
|
steps: list[Step[Any]]
¶
Get all steps in this route.
__hash__()
¶
Get a hash of this route.
Source code in configzen/routes.py
295 296 297 |
|
parse(route)
¶
Parse a route into steps.
Parameters:
-
route
¶RouteLike
) –The route to parse.
Returns:
-
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)
¶
Decompose a route into a list of steps.
Parameters:
Returns:
-
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()
¶
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)
¶
Enter a subroute.
Parameters:
-
subroute
¶RouteLike
) –A subroute to enter.
Source code in configzen/routes.py
377 378 379 380 381 382 383 384 385 386 387 |
|
get(obj)
¶
Get an object at the end of this route.
Parameters:
Returns:
-
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, value)
¶
Set an object pointed to by this route.
Parameters:
Returns:
-
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)
¶
Compare this route to another route.
Parameters:
Source code in configzen/routes.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
__str__()
¶
Compose this route into a string.
Source code in configzen/routes.py
447 448 449 |
|
__iter__()
¶
Yield all steps in this route.
Source code in configzen/routes.py
451 452 453 |
|
__repr__()
¶
Represent this route in a string.
Source code in configzen/routes.py
455 456 457 |
|
advance_linked_route(_current_head, _annotation, _step)
¶
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.
Classes:
-
ConfigSource
–Core interface for loading and saving configuration data.
-
StreamConfigSource
–A configuration source that is a stream.
-
FileConfigSource
–A configuration source that is a file.
Functions:
-
get_config_source
–Get a dedicated interface for a configuration source.
-
get_stream_config_source
–Get a dedicated interface for a configuration source stream.
-
get_file_config_source
–Get a dedicated interface for a configuration source file.
ConfigSource(source, data_format=None, **options)
¶
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.
Methods:
-
is_binary
–Determine whether the configuration source is binary.
-
load
–Load the configuration source.
-
load_async
–Load the configuration source asynchronously.
-
dump
–Dump the configuration source.
-
dump_async
–Dump the configuration source asynchronously.
Attributes:
-
data_format
(DataFormat[Any, AnyStr]
) –The current data format for a configuration source.
Source code in configzen/sources.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
data_format: DataFormat[Any, AnyStr]
¶
The current data format for a configuration source.
is_binary()
¶
is_binary() -> Literal[False]
is_binary() -> Literal[True]
Determine whether the configuration source is binary.
Source code in configzen/sources.py
132 133 134 |
|
load()
¶
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()
¶
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)
¶
Dump the configuration source.
Source code in configzen/sources.py
154 155 156 157 |
|
dump_async(data)
¶
Dump the configuration source asynchronously.
Source code in configzen/sources.py
159 160 161 162 |
|
StreamConfigSource(source, data_format, **options)
¶
Bases: Generic[AnyStr]
, ConfigSource[IO[Any], Any]
A configuration source that is a stream.
Parameters:
Methods:
-
load
–Load the configuration source.
-
load_async
–Unsupported.
-
dump
–Dump the configuration source.
-
dump_async
–Unsupported.
Source code in configzen/sources.py
204 205 206 207 208 209 210 |
|
load()
¶
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()
¶
Unsupported.
Source code in configzen/sources.py
220 221 222 223 |
|
dump(data)
¶
Dump the configuration source.
Source code in configzen/sources.py
225 226 227 |
|
dump_async(_data)
¶
Unsupported.
Source code in configzen/sources.py
229 230 231 232 |
|
FileConfigSource(source, data_format=None, *, use_processing_trace=True, **options)
¶
Bases: Generic[AnyStr]
, ConfigSource[Path, AnyStr]
A configuration source that is a file.
Parameters:
-
source
¶str | bytes | PathLike[str] | PathLike[bytes]
) –The path to the configuration source file.
Methods:
-
load
–Load the configuration source file.
-
load_async
–Load the configuration source file asynchronously.
-
dump
–Dump the configuration data to the source file.
-
dump_async
–Load the configuration source file asynchronously.
-
read
–Read the configuration source and return its contents.
-
read_async
–Read the configuration source file asynchronously and return its contents.
-
write
–Write the configuration source file and return the number of bytes written.
-
write_async
–Write the configuration source file asynchronously.
Attributes:
Source code in configzen/sources.py
260 261 262 263 264 265 266 267 268 269 |
|
paths: list[Path]
¶
List possible path variants basing on the processing context trace.
load()
¶
Load the configuration source file.
Return its contents as a dictionary.
Source code in configzen/sources.py
312 313 314 315 316 317 318 319 320 |
|
load_async()
¶
Load the configuration source file asynchronously.
Return its contents as a dictionary.
Source code in configzen/sources.py
322 323 324 325 326 327 328 329 330 |
|
dump(data)
¶
Dump the configuration data to the source file.
Parameters:
-
data
¶Data
) –The data to dump to the configuration source.
Source code in configzen/sources.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
dump_async(data)
¶
Load the configuration source file asynchronously.
Return its contents as a dictionary.
Parameters:
-
data
¶Data
) –The data to dump to the configuration source.
Source code in configzen/sources.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
read()
¶
Read the configuration source and return its contents.
Source code in configzen/sources.py
364 365 366 367 368 369 370 371 372 373 374 |
|
read_async()
¶
Read the configuration source file asynchronously and return its contents.
Source code in configzen/sources.py
376 377 378 379 380 381 382 383 384 385 386 |
|
write(content)
¶
Write the configuration source file and return the number of bytes written.
Parameters:
Source code in configzen/sources.py
388 389 390 391 392 393 394 395 396 397 398 399 400 |
|
write_async(content)
¶
Write the configuration source file asynchronously.
Return the number of bytes written.
Parameters:
Source code in configzen/sources.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|
get_config_source(source, _data_format=None)
¶
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, data_format)
¶
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, data_format=None)
¶
Get a dedicated interface for a configuration source file.
Source code in configzen/sources.py
419 420 421 422 423 424 425 426 427 |
|
typedefs
¶
Miscellaneous type definitions for configzen.