Available sources

Hardcoded defaults

class layeredconfig.Defaults(defaults=None, **kwargs)[source]

This source is initialized with a dict.

Parameters:defaults (dict) – A dict with configuration keys and values. If any values are dicts, these are turned into nested config objects.

Environment variables

class layeredconfig.Environment(environ=None, prefix=None, lower=True, sectionsep='_', **kwargs)[source]

Loads settings from environment variables. If prefix is set to MYAPP_, the value of the environment variable MYAPP_HOME will be available as the configuration setting home.

Parameters:
  • environ (dict) – Environment variables, in dict form like os.environ. If not provided, uses the real os.environ.
  • prefix (str) – Since the entire environment is not suitable to use as a configuration, only variables starting with this prefix are used.
  • lower (True) – If true, lowercase the name of environment variables (since these typically uses uppercase)

Command-line parameters

class layeredconfig.Commandline(commandline=None, parser=None, sectionsep='-', add_help=True, **kwargs)[source]

Load configuration from command line options. Any long-style parameters are turned into configuration values, and parameters containing the section separator (by default "-") are turned into nested config objects (i.e. --module-parameter=foo results in self.module.parameter == "foo".

If an initialized ArgumentParser object is provided, the defined parameters in that object is used for supporting short form options (eg. '-f' instead of '--force'), typing information and help text. The standards argparse feature of printing a helpful message when the ‘-h’ option is given is retained.

Parameters:
  • commandline (list) – Command line arguments, in list form like sys.argv. If not provided, uses the real sys.argv.
  • parser (argparse.ArgumentParser) – An initialized/configured argparse object
  • sectionsep (str) – An alternate section separator instead of -.
  • add_help (bool) – Same as for ArgumentParser()
rest = []

The remainder of the command line, containing all parameters that couldn’t be turned into configuration settings.

INI files

class layeredconfig.INIFile(inifilename=None, rootsection='__root__', writable=True, **kwargs)[source]

Loads and optionally saves configuration files in INI format, as handled by configparser.

Parameters:
  • inifile (str) – The name of a ini-style configuration file. The file should have a top-level section, by default named __root__, whose keys are turned into top-level configuration parameters. Any other sections in this file are turned into nested config objects.
  • rootsection (str) – An alternative name for the top-level section. See note below.
  • writable (bool) – Whether changes to the LayeredConfig object that has this INIFile object amongst its sources should be saved in the INI file.

Note

Since this source uses configparser, and since that module handles sections named [DEFAULT] differently, this module will have a sort-of automatic cascading feature for subsections if DEFAULT is used as rootsection

JSON files

class layeredconfig.JSONFile(jsonfilename=None, writable=True, **kwargs)[source]

Loads and optionally saves configuration files in JSON format. Since JSON has some support for typed values (supports numbers, lists, bools, but not dates or datetimes), data from this source are sometimes typed, sometimes only available as strings.

Parameters:
  • jsonfile (str) – The name of a JSON file, whose root element should be a JSON object (python dict). Nested objects are turned into nested config objects.
  • writable (bool) – Whether changes to the LayeredConfig object that has this JSONFile object amongst its sources should be saved in the JSON file.

YAML files

class layeredconfig.YAMLFile(yamlfilename=None, writable=True, **kwargs)[source]

Loads and optionally saves configuration files in YAML format. Since YAML (and the library implementing the support, PyYAML) has automatic support for typed values, data from this source are typed.

Parameters:
  • yamlfile (str) – The name of a YAML file. Nested sections are turned into nested config objects.
  • writable (bool) – Whether changes to the LayeredConfig object that has this YAMLFile object amongst its sources should be saved in the YAML file.

PList files

class layeredconfig.PListFile(plistfilename=None, writable=True, **kwargs)[source]

Loads and optionally saves configuration files in PList format. Since PList has some support for typed values (supports numbers, lists, bools, datetimes but not dates), data from this source are sometimes typed, sometimes only available as strings.

Parameters:
  • plistfile (str) – The name of a PList file. Nested sections are turned into nested config objects.
  • writable (bool) – Whether changes to the LayeredConfig object that has this PListFile object amongst its sources should be saved in the PList file.

Python files

class layeredconfig.PyFile(pyfilename=None, **kwargs)[source]

Loads configuration from a python source file. Any variables defined in that file will be interpreted as configuration keys. The class Subsection is automatically imported into the context when the file is executed, and represents a subsection of the configuration. Any attribute set on such an object is treated as a configuration parameter on that subsection.

Note

The python source file is loaded and interpreted once, when creating the PyFile object. If a value is set by eg. calling a function, that function will only be called at load time, not when accessing the parameter.

Parameters:pyfile (str) – The name of a file containing valid python code.

etcd stores

class layeredconfig.EtcdStore(baseurl='http://127.0.0.1:2379/v2/', **kwargs)[source]

Loads configuration from a etcd store.

Parameters:baseurl – The main endpoint of the etcd store

etcd has no concept of typed values, so all data from this source are returned as strings.