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.5.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

cfitall-1.0.5-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfitall-1.0.5.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cfitall-1.0.5.tar.gz
Algorithm Hash digest
SHA256 19442d9c53c573e6a8f6ae686e8bce45af604573df7416a1f2d40708dbd366fc
MD5 ca404310c70fa9e00413e8aa4702cd73
BLAKE2b-256 1068f0380d9c2431006b9e063c0b67a93db181bf4e84557f7b71ecd777b0f866

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cfitall-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for cfitall-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cdaf37846466d12a2d0f18e50123f5a232466f7601383803ee6c2b4ad73e4824
MD5 c522a256e1da5eceb0599c537d80eaa9
BLAKE2b-256 57c47ccfa9cd0f2285f35f627a1601e3321f941912119d85f3ae2561a389a176

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