API reference

class layeredconfig.LayeredConfig(*sources, **kwargs)[source]

Creates a config object from one or more sources and provides unified access to a nested set of configuration parameters. The source of these parameters a config file (using .ini-file style syntax), command line parameters, and default settings embedded in code. Command line parameters override configuration file parameters, which in turn override default settings in code (hence Layered Config).

Configuration parameters are accessed as regular object attributes, not dict-style key/value pairs. Configuration parameter names should therefore be regular python identifiers, and preferrably avoid upper-case and “_” as well (i.e. only consist of the characters a-z and 0-9)

Configuration parameter values can be typed (strings, integers, booleans, dates, lists...). Even though some sources lack typing information (eg in INI files, command-line parameters and enviroment variables, everything is a string), LayeredConfig will attempt to find typing information in other sources and convert data.

Parameters:
  • *sources – Initialized ConfigSource-derived objects
  • cascade (bool) – If an attempt to get a non-existing parameter on a sub (nested) configuration object should attempt to get the parameter on the parent config object. False by default,
  • writable (bool) – Whether configuration values should be mutable. True by default. This does not affect set().
static write(config)[source]

Commits any pending modifications, ie save a configuration file if it has been marked “dirty” as a result of an normal assignment. The modifications are written to the first writable source in this config object.

Note

This is a static method, ie not a method on any object instance. This is because all attribute access on a LayeredConfig object is meant to retrieve configuration settings.

Parameters:config (layeredconfig.LayeredConfig) – The configuration object to save
static set(config, key, value, sourceid='defaults')[source]

Sets a value in this config object without marking any source dirty, and with exact control of exactly where to set the value. This is mostly useful for low-level trickery with config objects.

Parameters:
  • config – The configuration object to set values on
  • key – The parameter name
  • value – The new value
  • sourceid – The identifier for the underlying source that the value should be set on.
static get(config, key, default=None)[source]

Gets a value from the config object, or return a default value if the parameter does not exist, like dict.get() does.

static dump(config)[source]

Returns the entire content of the config object in a way that can be easily examined, compared or dumped to a string or file.

Parameters:config – The configuration object to dump
Return type:dict
static datetimeconvert(value)[source]

Convert the string value to a datetime object. value is assumed to be on the form “YYYY-MM-DD HH:MM:SS” (optionally ending with fractions of a second).

static dateconvert(value)[source]

Convert the string value to a date object. value is assumed to be on the form “YYYY-MM-DD”.

static boolconvert(value)[source]

Convert the string value to a boolean. "True" is converted to True and "False" is converted to False.

Note

If value is neither “True” nor “False”, it’s returned unchanged.