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.

It provides an easy interface for accessing configuration information sourced from overlaid config files or mapped in from environment variables or command line options.

Configuration information is also 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']

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
100
>>> 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-1.0.0.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

configurator-1.0.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: configurator-1.0.0.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for configurator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9db150f5c3fff2d04d7cf449cdaee823506b9e7b80a44f54dff49d05e3aea90f
MD5 735fa7d51e725ded42cadb1f48d17061
BLAKE2b-256 6e0d8e9be8425d06be690b383ecf9751a982b868d954a1d288434bfeb794c793

See more details on using hashes here.

File details

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

File metadata

  • Download URL: configurator-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for configurator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2e31a14d879bff543810aeed3d9bcaa22192566160862a6ca311c8b55ade48d
MD5 02622106afb010ad90d44edd3deeee37
BLAKE2b-256 4ed5a9b0ec2b31f33b5d3f93caaea2af2e3e30811fd353c702ea7876d928f7cb

See more details on using hashes here.

Supported by

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