Skip to main content

a configuration manager for python apps

Project description

https://travis-ci.org/wryfi/cfitall.svg?branch=develop

cfitall (configure it all) is a configuration management library for python applications. It’s inspired by and loosely modeled on the excellent viper library for go, though it doesn’t have quite as many features (yet).

It does cover the basics of configuring your application from a combination of sources, with a predictable inheritance hierarchy. It does this by creating a configuration registry for your application, merging data from the following sources to retrieve a requested value:

  • default values provided by the developer

  • YAML or JSON configuration file (values override defaults)

  • environment variables (override configuration file values & defaults)

  • set() calls made by the developer (override everything)

(Support for command-line and k/v store data sources is intended for the future; pull requests welcome.)

Install

pip install cfitall should do the trick for most users. cfitall requires python3 but otherwise has minimal dependencies.

To build a package for debian/ubuntu, stdeb works well:

apt install python3-all python3-stdeb python3-pbr
setup.py --command-packages=stdeb.command sdist_dsc --debian-version bionic1 bdist_deb

Example

This example is for a contrived application called myapp.

First, set up a config module for myapp. Notice that we name our config object myapp.

# myapp/config.py

from cfitall.config import ConfigManager

# create a configuration registry for myapp
config = ConfigManager('myapp')

# set some default configuration values
config.set_default('global.name', 'my fancy application')
config.values['defaults']['global']['foo'] = 'bar'
config.set_default('network.listen', '127.0.0.1')

# add a path to search for configuration files
config.add_config_path('/Users/wryfi/.config/myapp')

# read data from first config file found (myapp.json, myapp.yaml, or myapp.yml)
config.read_config()

Since we named our config object myapp, environment variables beginning with MYAPP__ are searched for values by cfitall. Environment variables containing commas are interpreted as comma-delimited lists. Export some environment variables to see this in action:

export MYAPP__GLOBAL__NAME="my app from bash"
export MYAPP__GLOBAL__THINGS="four,five,six"
export MYAPP__NETWORK__PORT=8080

Again, since we chose myapp as our config object name, our configuration file is also named myapp.(json|yaml|yml). Create a configuration file in YAML or JSON and put it in one of the paths you added to your config registry:

# ~/.config/myapp/myapp.yml
global:
  bar: foo
  things:
    - one
    - two
    - three
  person:
    name: joe
    hair: brown
network:
  port: 9000
  listen: '*'

Now you can use your config object to get the configuration data you need. You can access the merged configuration data by its configuration key (dotted path notation), or you can just grab the entire merged dictionary via the dict property.

# myapp/logic.py

from config import config

# prints $MYAPP__GLOBAL__THINGS because env var overrides config file
print(config.get('global.things', list))

# prints $MYAPP__NETWORK__PORT because env var overrides config file
print(config.get('network.port', int))

# prints '*' from myapp.yml because config file overrides default
print(config.get('network.listen', str))

# prints 'joe' from myapp.yml because it is only defined there
print(config.get('global.person.name', str))

# alternate way to print joe through the config dict property
print(config.dict['global']['person']['name'])

# prints the entire assembled config as dictionary
print(config.dict)

Running logic.py should go something like this:

$ python logic.py
['four', 'five', 'six']
8080
*
joe
joe
{'global': {'name': 'my app from bash', 'foo': 'bar', 'bar': 'foo', 'things': ['four', 'five', 'six'], 'person': {'name': 'joe', 'hair': 'brown'}}, 'network': {'listen': '*', 'port': '8080'}}

Notes

  • Avoid using __ (double-underscore) in your configuration variable keys (names), as cfitall uses __ as a hierarchical delimiter when parsing environment variables.

    • If you must use __ in variable keys, you can pass an env_separator argument with a different string to the ConfigManager constructor, e.g. config = ConfigManager(env_separator='____').

  • Environment variables matching the pattern MYAPP__.* are automatically read into the configuration, where MYAPP refers to the uppercase name given to your ConfigManager at creation.

    • You can customize this behavior by passing an env_prefix value and/or env_separator as kwargs to the ConfigManager constructor.

Development

cfitall uses modern python tooling with the pipenv dependency/environment manager and pbr packaging system.

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

cfitall-1.0.3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

cfitall-1.0.3-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file cfitall-1.0.3.tar.gz.

File metadata

  • Download URL: cfitall-1.0.3.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for cfitall-1.0.3.tar.gz
Algorithm Hash digest
SHA256 e2e5ef12a36265f9e98e8b4196895524e9cad387e5741aaa5777290d5ef44324
MD5 4594a53302e6b14307c6babb0a152823
BLAKE2b-256 f4698d1fefb6706efc95e4f072ec966fadb92de13c12ce6cea07bbd5a3dc3370

See more details on using hashes here.

File details

Details for the file cfitall-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: cfitall-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for cfitall-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c5cfe194cc9a787ac1a8935fa6692569f5d34b71908bdaa1013b4b5c3cccfe3a
MD5 4ec99b0aa8b9e1739c817ca4cc2abdb5
BLAKE2b-256 2c5aa647f29d6d7d72b83be4fc83f254520d340f5c2dd338984c54dc4e019117

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