Skip to main content

A library for building a configuration store from one or more layered configuration sources

Project description

CircleCI Docs

This is a Python library for building a configuration store from one or more layered configuration sources. These are most commonly files, with YAML, TOML and JSON support included and other formats easily added. The sources don’t have to be files, and support is included for both environment variables and command line options.

In addition to an easy to use interface, configuration information is also made available as nested, simple python data types so that you can validate the schema of your configuration using the tool of your choice.

Quickstart

To install the library, go for:

pip install configurator[yaml,toml]

Here’s how you would handle a layered set of defaults, system-wide config and then optional per-user config:

from configurator import Config

defaults = Config({
    'cache': {
        'location': '/tmp/my_app',
        'max_files': 100,
    },
    'banner': 'default banner',
    'threads': 1,
})
system = Config.from_path('/etc/my_app/config.yaml')
user = Config.from_path('~/.my_app.yaml', optional=True)
config = defaults + system + user

Now, if we wanted configuration from the environment and command line arguments to override those provided in configuration files, we could do so as follows:

import os
from argparse import ArgumentParser
from configurator import convert, target, required

config.merge(os.environ, {
    convert('MYAPP_THREADS', int): 'threads',
    required('MYAPP_CACHE_DIRECTORY'): 'cache.location',
})

parser = ArgumentParser()
parser.add_argument('--threads', type=int)
parser.add_argument('--max-files', type=int)
args = parser.parse_args()

config.merge(args, {
    'threads': 'threads',
    'max_files': 'cache.max_files',
})

To check the configuration we’ve accumulated is sensible we can use a data validation library such as Voluptuous:

from os.path import exists
from voluptuous import Schema, All, Required, PathExists

schema = Schema({
    'cache': {'location': All(str, PathExists()), 'max_files': int},
    'banner': Required(str),
    'threads': Required(int),
    })

schema(config.data)

So, with all of the above, we could use the following sources of configuration:

>>> import os, sys
>>> print(open('/etc/my_app/config.yaml').read())
cache:
  location: /var/my_app/
<BLANKLINE>
>>> os.environ['MYAPP_THREADS']
'2'
>>> os.environ['MYAPP_CACHE_DIRECTORY']
'/var/logs/myapp/'
>>> sys.argv
['myapp.py', '--threads', '3', '--max-files', '200']

With the above sources of configuration, we’d end up with a configuration store that we can use as follows:

>>> config.cache.location
'/var/logs/myapp/'
>>> config.cache.max_files
200
>>> config.banner
'default banner'
>>> config.threads
3

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

configurator-3.2.0.tar.gz (35.5 kB view details)

Uploaded Source

Built Distribution

configurator-3.2.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file configurator-3.2.0.tar.gz.

File metadata

  • Download URL: configurator-3.2.0.tar.gz
  • Upload date:
  • Size: 35.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for configurator-3.2.0.tar.gz
Algorithm Hash digest
SHA256 42650ea55e7df5e4601c1c3d137d1b0ea0bec332448947627c368e0254a36043
MD5 963b60ca7722d5542f29fd191b2c736a
BLAKE2b-256 62067c27b07622df52e2591ea581485afb84bbc3386493bb47a2f6992fe7b78c

See more details on using hashes here.

File details

Details for the file configurator-3.2.0-py3-none-any.whl.

File metadata

  • Download URL: configurator-3.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for configurator-3.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be39c84f9a9aafd09b3c34acdc267ca4a5804e51975bfdd01cb2d433e9856766
MD5 3f0c7059b9a3ce94490fcae405a99453
BLAKE2b-256 99f8dbc0d0b77eb72fbcfc94446d3b67049575d963a99280a3a41079cbce3cd7

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