ConfigFramework.abstract package¶
Submodules¶
-
class
AbstractConfigLoader
(data: data_type, defaults: defaults_type, include_defaults_to_dumps: bool = False, *args, **kwargs)[source]¶ Bases:
abc.ABC
,collections.abc.MutableMapping
Base class for any of your loaders. Initialization can be made through load function, or, if author wants so, through __init__ function.
Creates new config first_loader with your variables.
- Parameters
data – data from loader.
defaults – default variables that will be used if not found in first_loader.
include_defaults_to_dumps – represents if default values should be dumped to that first_loader.
args – arguments that can be used for your custom loaders.
kwargs – keyword arguments that can be used for your custom loaders.
-
lookup_data
: Union[ChainMap, MutableMapping[Hashable, Any], Dict[Hashable, Any]]¶
-
abstract classmethod
load
(*args, **kwargs)[source]¶ Initializes creation of new ConfigLoader. You must provide your arguments that are needed for your first_loader.
- Parameters
args – arguments that can be used for your custom loaders.
kwargs – keyword arguments that can be used for your custom loaders.
-
abstract
dump
(include_defaults: bool = False) → None[source]¶ Dumps updated variables to loader.
- Parameters
include_defaults – specifies if you want to have default variables to be dumped.
- Returns
nothing.
-
dump_to
(other_loader: ConfigFramework.abstract.abc_loader.AbstractConfigLoader, include_defaults: bool = False) → None[source]¶ Dumps variables to other first_loader that been already initialized.
- Parameters
other_loader – other first_loader that already initialized and where you want to dump stuff.
include_defaults – include_defaults_to_dumps: specifies if you want to have default variables to be.
- Returns
nothing.
-
get
(key: Union[Hashable, AnyStr, pathlib.Path], default=None) → Any[source]¶ Returns an item under specified key or key as path and if it didn’t find it - returns default variable.
- Parameters
key – a key that points at where you want to grab variable.
default – default config_var.
- Returns
anything lying there.
-
static
key_to_path_cast
(key: Union[AnyStr, Hashable, pathlib.Path]) → Tuple[Union[str, Hashable], …][source]¶ Casts a key to tuple of keys, that should be applied one by one to get to where variable lies. Path pointing example: config_root/database/database_ip
- Parameters
key – string with path, pointing at out variable or hashable.
- Returns
tuple of sub keys.
-
get_to_variable_root
(keys: Union[Tuple[str, …], Tuple[Hashable, …]], lookup_at: Optional[Union[ChainMap[Hashable, Any], MutableMapping[Hashable, Any], Dict[Hashable, Any]]] = None) → Union[ChainMap[Hashable, Any], MutableMapping[Hashable, Any], Dict[Hashable, Any]][source]¶ Returns a dictionary with our variable.
- Parameters
keys – keys tuple we apply to get to root of variable.
lookup_at – the location we’re looking at. In case we need to lookup at specific part of our first_loader.
- Returns
a dictionary which layer should contain our variable.
-
class
AbstractConfigVar
(key: key_type, loader: AbstractConfigLoader, *, typehint: Optional[Union[Type, Any]] = typing.Any, caster: Optional[Callable[[Any], Var]] = None, dump_caster: Optional[Callable[[AbstractConfigVar], Any]] = None, validator: Optional[Callable[[Var], bool]] = None, default: Optional[Any] = None, constant: bool = False)[source]¶ Bases:
Generic
[ConfigFramework.abstract.abc_variable.Var
]Initializes variable for specified first_loader and key.
- Parameters
key – Any hashable, string or Path instance. Example of how to get such vars: config_root/database/database_ip. Warning: you must not start config paths with / or since it may cause unwanted errors because pathlib.Path is used to transform these to keys sequence.
loader – A first_loader that will be looked up to get vars config_var or to update values.
typehint – Typehint for __value field, that by default being returned.
caster – Callable that should return variable casted to specific type (in case you need custom types).
dump_caster – Callable that being called when config being dumped. Also can be instance of ConfigFramework.dump_caster.DumpCaster.
validator – Callable that validates config_var or defaults in case the original config_var is invalid.
default – Default config_var that will be set, if config_var is invalid.
constant – Sets if variable config_var can be set in runtime.
New in version 2.1.0: pathlib.Path support as variable key.
New in version 2.2.0: AbstractConfigVar can be used as a type hint.
Deprecated since version 2.2.0: typehint parameter is deprecated and will be deleted in 2.5.0. To use type hints use AbstractConfigVar[desired_type] instead.
-
key
: key_type¶
-
is_constant
: bool¶
-
loader
: AbstractConfigLoader¶ Abstract config variable with descriptors interface which is a base type for any of your variables classes.
-
caster
(value: Any) → Var[source]¶ Callable that should return variable casted to specific type (in case you need custom types).
- Parameters
value – config_var to be casted.
- Returns
value casted to whatever type you need.
-
dump_caster
(config_var: ConfigFramework.abstract.abc_variable.AbstractConfigVar) → Any[source]¶ Callable that being called when config being dumped. Nothing being passed as attribute since it should be executed after assigning the config_var to ConfigVar. Through self we can get to config variables value, but not when we use DumpCaster, which allows us to assign specific caster for exact ConfigLoader and so parameter config_var is used to pass config variable that is going to be casted for compatibility with DumpCaster.
- Parameters
config_var – Config variable that going to be casted.
- Returns
value casted to whatever type you need to save value.
-
validate
(value: Var) → bool[source]¶ Callable that validates config_var or defaults in case the original config_var is invalid.
- Parameters
value – config_var to be validated.
- Returns
bool value representing if variable has a valid value.
-
property
value
¶ Gives access to casted value.
- Returns
any value from config, that been casted and validated.