Skip to main content

Manages configuration coming from config files, environment variables, command line arguments, code defaults or other sources

Project description

https://badge.fury.io/py/layeredconfig.png https://travis-ci.org/staffanm/layeredconfig.png?branch=master https://ci.appveyor.com/api/projects/status/nnfqv9jhxh3afgn0/branch/master https://coveralls.io/repos/staffanm/layeredconfig/badge.png?branch=master Code Health https://pypip.in/d/layeredconfig/badge.png

Full documentation: https://layeredconfig.readthedocs.org/

LayeredConfig compiles configuration from files, environment variables, command line arguments, hard-coded default values, or other backends, and makes it available to your code in a simple way:

from layeredconfig import (LayeredConfig, Defaults, INIFile,
                           Environment, Commandline)

# This represents four different way of specifying the value of the
# configuration option "hello":

# 1. hard-coded defaults
defaults = {"hello": "is it me you're looking for?"}

# 2. INI configuration file
with open("myapp.ini", "w") as fp:
    fp.write("""
[__root__]
hello = kitty
""")

# 3. enironment variables
import os
os.environ['MYAPP_HELLO'] = 'goodbye'

# 4.command-line arguments
import sys
sys.argv = ['./myapp.py', '--hello=world']

# Create a config object that gets settings from these four
# sources.
config = LayeredConfig(Defaults(defaults),
                       INIFile("myapp.ini"),
                       Environment(prefix="MYAPP_"),
                       Commandline())

# Prints "Hello world!", i.e the value provided by command-line
# arguments. Latter sources take precedence over earlier sources.
print("Hello %s!" % config.hello)
  • A flexible system makes it possible to specify the sources of configuration information, including which source takes precedence. Implementations of many common sources are included and there is a API for writing new ones.

  • Included configuration sources for INI files, YAML files, JSON file, PList files, etcd stores (read-write), Python source files, hard-coded defaults, command line options, environment variables (read-only).

  • Configuration can include subsections (ie. config.downloading.refresh) and if a subsection does not contain a requested setting, it can optionally be fetched from the main configuration (if config.module.retry is missing, config.retry can be used instead).

  • Configuration settings can be changed by your code (i.e. to update a “lastmodified” setting or similar), and changes can be persisted (saved) to the backend of your choice.

  • Configuration settings are typed (ie. if a setting should contain a date, it’s made available to your code as a datetime.date object, not a str). If settings are fetched from backends that do not themselves provide typed data (ie. environment variables, which by themselves are strings only), a system for type coercion makes it possible to specify how data should be converted.

History

0.3.3 (2019-11-11)

  • INIFile has been extended to support nested subsections.

  • Fixed a problem with DictSource (and by extension the Defaults source) where nested sections could be erroneously handled.

  • The sectionsep parameter to the Environment constructor has been documented. Thanks to @brentmclark for the patch!

  • Using “_” in the name of a configuration setting was not supported, regardless of what sectionsep had been set to. Thanks to @dginzbourg for reporting this!

  • Support for python 3.3 and 2.6 was dropped.

0.3.2 (2016-09-26)

  • Fixed bug #9 (Custom section separators caused values to be retrieved as lists, not single values). Thanks to @numbnut for reporting this!

0.3.1 (2016-08-31)

  • Fixed bug #8 (layering a Commandline source over a YAMLFile with defined subsection resulted in crash in initialization. Thanks to @AnsonT for reporting this!

  • The default URI used for EtcdStore was changed to reflect that port 2379 should be used instead of 4001 (which was the default for etcd 1.*).

  • Support for Python 3.2 was dropped.

0.3.0 (2016-08-06)

  • New staticmethod dump, which returns the content of the passed config object as a dict. This is also used as a printable representation of a config object (through __repr__).

  • The intrinsic type of any typed setting may not be None any longer.

  • If you subclass LayeredConfig, any created subsection will be instances of your subclass, not the base LayeredConfig class

  • Layering multiple configuration files now works even when earlier files might lack subsections present in latter.

All of the above was done by @jedipi. Many thanks!

A number of unreported bugs, mostly concerning unicode handling and type conversion in various sources, was also fixed.

0.2.2 (2016-01-24)

  • Fixed a bug when using a class in a Default configuration for automatic coercion, where the type of the class isn’t type (as is the case with the “newint” and other classes from the future module).

  • Fixed a bug where loading configuration from multiple config files would crash if latter configs lacked subsections present in earlier. Thanks to @badkapitan!

0.2.1 (2014-11-26)

  • Made the Commandline source interact better with “partially configured” ArgumentParser objects (parsers that has been configured with some, but not all, possible arguments).

0.2.0 (2014-11-22)

  • Integration with argparse: The Commandline source now accepts an optional parse parameter, which should be a configured argparse.ArgumentParser object. Most features of argparse, such as specifying the type of arguments, and automatic help text

  • A new source, PyFile, for reading configuration from python source files.

  • Another new source, EtcdStore, for reading configuration from etcd stores.

0.1.0 (2014-11-03)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

layeredconfig-0.3.3.tar.gz (54.6 kB view details)

Uploaded Source

Built Distribution

layeredconfig-0.3.3-py2.py3-none-any.whl (27.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file layeredconfig-0.3.3.tar.gz.

File metadata

  • Download URL: layeredconfig-0.3.3.tar.gz
  • Upload date:
  • Size: 54.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.4.3 CPython/2.7.10 Darwin/18.7.0

File hashes

Hashes for layeredconfig-0.3.3.tar.gz
Algorithm Hash digest
SHA256 206cf5c6e3586d6479586b162d9d5c426a3e8d9cbfdd5c47bb2bdf379b917b3e
MD5 9cea835e4f0a7fcbacecb1b05efc702e
BLAKE2b-256 b031bcff528805c90944c99f666a2111467a8d51ac78587b233b41002d69a397

See more details on using hashes here.

File details

Details for the file layeredconfig-0.3.3-py2.py3-none-any.whl.

File metadata

  • Download URL: layeredconfig-0.3.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.4.3 CPython/2.7.10 Darwin/18.7.0

File hashes

Hashes for layeredconfig-0.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d542de6cbb16019367550d1f078984cd7d29b6bc77cc9c253d6f7dde4a9bdb66
MD5 95c321ef6902bfe1efce086483054251
BLAKE2b-256 9cd4a6e2f72b7f28039a2be0a1fe80aa2e552f901b0b81abe906dcdc186be4c7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page