xwrf.config.Config#

class xwrf.config.Config(name, defaults=None, paths=None, env=None, env_var=None, root_env_var=None, env_prefix=None, deprecations=None)[source]#
__init__(name, defaults=None, paths=None, env=None, env_var=None, root_env_var=None, env_prefix=None, deprecations=None)[source]#

Methods

__init__(name[, defaults, paths, env, ...])

clear()

Clear all existing configuration.

collect([paths, env])

Collect configuration from paths and environment variables

ensure_file(source[, destination, comment])

Copy file to default location if it does not already exist

expand_environment_variables()

Expand any environment variables in this configuration in-place.

get(key[, default])

Get elements from global config

merge(*dicts)

Merge this configuration with multiple dictionaries.

pprint(**kwargs)

refresh(**kwargs)

Update configuration by re-reading yaml files and env variables.

rename(aliases)

Rename old keys to new keys

serialize()

Serialize conifg data into a string.

set([arg])

Set configuration values within a context manager.

to_dict()

Return dictionary copy of configuration.

update(new[, priority])

Update the internal configuration dictionary with new.

update_defaults(new)

Add a new set of defaults to the configuration

clear()[source]#

Clear all existing configuration.

collect(paths=None, env=None)[source]#

Collect configuration from paths and environment variables

Parameters:
  • paths (list[str]) – A list of paths to search for yaml config files. Defaults to the paths passed when creating this object.

  • env (Mapping[str, str]) – The system environment variables to search through. Defaults to the environment dictionary passed when creating this object.

Returns:

config

Return type:

dict

See also

donfig.Config.refresh

collect configuration and update into primary config

ensure_file(source, destination=None, comment=True)[source]#

Copy file to default location if it does not already exist

This tries to move a default configuration file to a default location if if does not already exist. It also comments out that file by default.

This is to be used by downstream modules that may have default configuration files that they wish to include in the default configuration path.

Parameters:
  • source (string, filename) – Source configuration file, typically within a source directory.

  • destination (string, directory) – Destination directory. Configurable by <CONFIG NAME>_CONFIG environment variable, falling back to ~/.config/<config name>.

  • comment (bool, True by default) – Whether or not to comment out the config file when copying.

expand_environment_variables()[source]#

Expand any environment variables in this configuration in-place.

See expand_environment_variables() for more information.

get(key, default='__no_default__')[source]#

Get elements from global config

Use ‘.’ for nested access

Examples

>>> from donfig import Config
>>> config = Config('mypkg')
>>> config.get('foo')  
{'x': 1, 'y': 2}
>>> config.get('foo.x')  
1
>>> config.get('foo.x.y', default=123)  
123

See also

donfig.Config.set

merge(*dicts)[source]#

Merge this configuration with multiple dictionaries.

See merge() for more information.

pprint(**kwargs)[source]#
refresh(**kwargs)[source]#

Update configuration by re-reading yaml files and env variables.

This goes through the following stages:

  1. Clearing out all old configuration

  2. Updating from the stored defaults from downstream libraries (see update_defaults)

  3. Updating from yaml files and environment variables

Note that some functionality only checks configuration once at startup and may not change behavior, even if configuration changes. It is recommended to restart your python process if convenient to ensure that new configuration changes take place.

See also

donfig.Config.collect

for parameters

donfig.Config.update_defaults

rename(aliases)[source]#

Rename old keys to new keys

This helps migrate older configuration versions over time

serialize()[source]#

Serialize conifg data into a string.

See serialize() for more information.

set(arg=None, **kwargs)[source]#

Set configuration values within a context manager.

Parameters:
  • arg (mapping or None, optional) – A mapping of configuration key-value pairs to set.

  • **kwargs – Additional key-value pairs to set. If arg is provided, values set in arg will be applied before those in kwargs. Double-underscores (__) in keyword arguments will be replaced with ., allowing nested values to be easily set.

Examples

>>> from donfig import Config
>>> config = Config('mypkg')

Set 'foo.bar' in a context, by providing a mapping.

>>> with config.set({'foo.bar': 123}):
...     pass

Set 'foo.bar' in a context, by providing a keyword argument.

>>> with config.set(foo__bar=123):
...     pass

Set 'foo.bar' globally.

>>> config.set(foo__bar=123)  

See also

donfig.Config.get

to_dict()[source]#

Return dictionary copy of configuration.

Warning

This will copy all keys and values. This includes values that may cause unwanted side effects depending on what values exist in the current configuration.

update(new, priority='new')[source]#

Update the internal configuration dictionary with new.

See update() for more information.

update_defaults(new)[source]#

Add a new set of defaults to the configuration

It does two things:

  1. Add the defaults to a collection to be used by refresh() later

  2. Updates the global config with the new configuration. Old values are prioritized over new ones, unless the current value is the old default, in which case it’s updated to the new default.