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.

  • 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.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.2.0.tar.gz (44.7 kB view hashes)

Uploaded Source

Built Distribution

layeredconfig-0.2.0-py2.py3-none-any.whl (24.0 kB view hashes)

Uploaded Python 2 Python 3

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