Coder Social home page Coder Social logo

configurator's Introduction

configurator

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.