auxlib.configuration¶
A specialized map implementation to manage configuration and context information.
- Features:
- Uses YAML configuration files
- Use environment variables to override configs
- Can pass a list of required parameters at initialization
- Works with encrypted files
- Accepts multiple config files
- Can query information from consul
- Does type coercion on strings
- Composable configs
- Ordered merge: downstream configs will override upstream
- Reload from sources on SIGHUP
- Available source types:
- Environment variables
- Yaml file on file system
- Yaml file in S3
- Consul
- Hashicorp Vault
Notes
- Keys are case-insensitive.
-
class
auxlib.configuration.
Configuration
(appname, config_sources=None, required_parameters=None, package=None)[source]¶ A map implementation to manage configuration and context information. Values can be accessed (read, not assigned) as either a dict lookup (e.g. config[key]) or as an attribute (e.g. config.key).
This class makes the foundational assumption of a yaml configuration file, as values in yaml are typed.
This class allows overriding configuration keys with environment variables. Given an app name foo and a config parameter bar: 15, setting a FOO_BAR environment variable to 22 will override the value of bar. The type of 22 remains int because the underlying value of 15 is used to infer the type of the FOO_BAR environment variable. When an underlying parameter does not exist in a config file, the type is intelligently guessed.
Parameters: - app_name (str) –
- config_sources (str or list, optional) –
- required_parameters (iter, optional) –
Raises: InitializationError
– on instantiation, when required_parameters are not foundwarns
– on instantiation, when a given config_file cannot be readNotFoundError
– when requesting a key that does not exist
Examples
>>> for (key, value) in [('FOO_BAR', 22), ('FOO_BAZ', 'yes'), ('FOO_BANG', 'monkey')]: ... os.environ[key] = str(value)
>>> context = Configuration('foo') >>> context.bar, context.bar.__class__ (22, <... 'int'>) >>> context['baz'], type(context['baz']) (True, <... 'bool'>) >>> context.bang, type(context.bang) ('monkey', <... 'str'>)
-
set_env
(key, value)[source]¶ Sets environment variables by prepending the app_name to key. Also registers the environment variable with the instance object preventing an otherwise-required call to reload().
-
class
auxlib.configuration.
EnvironmentMappedSource
(envvar, sourcemap)[source]¶ Load a full Source object given the value of an environment variable.