API Reference
Reading configuration
- confidence.load_name(*names, load_order=DEFAULT_LOAD_ORDER, extension='yaml', missing=Missing.SILENT)[source]
Read a
Configurationinstance by name, trying to read from files in increasing significance. The default load order issystem,user,application,environment.Multiple names are combined with multiple loaders using names as the ‘inner loop / selector’, loading
/etc/name1.yamland/etc/name2.yamlbefore./name1.yamland./name2.yaml.- Parameters:
names (str) – application or configuration set names, in increasing significance
load_order (Iterable[Union[str, Callable[[str, str], Configuration]]]) – ordered list of name templates or
callables, in increasing order of significanceextension (str) – file extension to be used
missing (Any) – policy to be used when a configured key is missing, either as a
Missinginstance or a default value
- Returns:
a
Configurationinstances providing values loaded from names in load_order ordering- Return type:
- confidence.loadf(*fnames, default=NoDefault, missing=Missing.SILENT)[source]
Read a
Configurationinstance from named files.- Parameters:
fnames (Union[str, PathLike]) – name of the files to
open()default (Any) –
dictorConfigurationto use when a file does not exist (default is to raise aFileNotFoundError)missing (Any) – policy to be used when a configured key is missing, either as a
Missinginstance or a default value
- Returns:
a
Configurationinstance providing values from fnames- Return type:
- confidence.loads(*strings, missing=Missing.SILENT)[source]
Read a
Configurationinstance from strings.- Parameters:
strings (str) – configuration contents
missing (Any) – policy to be used when a configured key is missing, either as a
Missinginstance or a default value
- Returns:
a
Configurationinstance providing values from strings- Return type:
- confidence.load(*fps, missing=Missing.SILENT)[source]
Read a
Configurationinstance from file-like objects.- Parameters:
fps (IO) – file-like objects (supporting
.read())missing (Any) – policy to be used when a configured key is missing, either as a
Missinginstance or a default value
- Returns:
a
Configurationinstance providing values from fps- Return type:
- confidence.dumpf(value, fname, encoding='utf-8')[source]
Serialize the configuration in value to a YAML-formatted file.
- Parameters:
value (Any) – the value (like a
Configurationobject) to dumpfname (Union[str, PathLike]) – name or path of the file to write to
encoding (str) – encoding to use
- Return type:
None
- confidence.dumps(value)[source]
Serialize the configuration in value as a YAML-formatted string.
- Parameters:
value (Any) – the value (like a
Configurationobject) to dump- Returns:
configuration, serialized as a
strin YAML format- Return type:
str
- confidence.dump(value, fp, encoding='utf-8')[source]
Serialize the configuration in value to YAML format, writing it to fp.
- Parameters:
value (Any) – the value (like a
Configurationobject) to dumpfp (IO) – a file-like object to write to
encoding (str) – encoding to use
- Return type:
None
The Configuration object
- class confidence.Configuration[source]
A collection of configured values, retrievable as either
dict-like items or attributes.- __init__(*sources, missing=Missing.SILENT)[source]
Create a new
Configuration, based on one or multiple source mappings.- Parameters:
sources (Mapping[str, Any]) – source mappings to base this
Configurationon, ordered from least to most significantmissing (Any) – policy to be used when a configured key is missing, either as a
Missinginstance or a default value
- get(path, default=NoDefault, *, as_type=None, resolve_references=True)[source]
Gets a value for the specified path.
- Parameters:
path (str) – the configuration key to fetch a value for, steps separated by a dot (
.)default (Any) – a value to return if no value is found for the supplied path (
Noneis allowed)as_type (Optional[Callable]) – an optional callable to apply to the value found for the supplied path (possibly raising exceptions of its own if the value can not be coerced to the expected type)
resolve_references (bool) – whether to resolve references in values
- Returns:
the value associated with the supplied configuration key, if available, or a supplied default value if the key was not found
- Raises:
NotConfiguredError – when no value was found for path and default was not provided
ConfiguredReferenceError – when a reference could not be resolved
- Return type:
Any
Controlling precedence / load order
- confidence.loaders(*specifiers)[source]
Generates loaders in the specified order.
Arguments can be
Localityinstances, producing the loader(s) available for that locality,strinstances (used as file path templates) orcallables. These can be mixed:# define a load order using predefined user-local locations, # an explicit path, a template and a user-defined function load_order = loaders(Locality.user, '/etc/defaults/hard-coded.yaml', '/path/to/{name}.{extension}', my_loader) # load configuration for name 'my-application' using the load order # defined above config = load_name('my-application', load_order=load_order)
- Parameters:
specifiers (Union[Locality, str, Callable[[str, str], Configuration]]) – loader specifiers, see description
- Yields:
configuration loaders in the specified order
- Return type:
Iterable[Union[str, Callable[[str, str], Configuration]]]
- class confidence.Locality[source]
Enumeration of localities defined by confidence, ranging from system-wide locations for configurations (e.g.
/etc/name.yaml) to environment variables.- SYSTEM = 0
system-wide configuration locations
- USER = 1
user-local configuration locations
- APPLICATION = 2
application-local configuration locations (dependent on the current working directory)
- ENVIRONMENT = 3
configuration from environment variables