API reference

class layeredconfig.LayeredConfig(*sources, **kwargs)

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)

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')

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)

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