Skip to main content

Simple hierarchic configuration manager for apps

Project description

modconfig – Simple hierarchic configuration manager for apps

Tests Status PYPI Version

Motivation

Applications (especially web services) often require certain configuration options to depend on the environment an application runs in (development, testing, production, etc.). For instance, a database address config option may default to a local database server during development, a mock database server during testing, and yet another database server during production. It may also need to be customizable via an environment variable. modconfig approaches scenarios like this and, allows to specify default configuration options for various environments and optionally override them by custom environment variables.

modconfig uses python modules for keep the configuration options. You are not locked by format (json, yaml, ini) restrictions and able to use any python statements/modules to tune your configuration as a pro. It keeps the flexability and make your configuration very declarative without any magic.

Requirements

  • python >= 3.6

Installation

modconfig should be installed using pip:

pip install modconfig

Usage

For example you have the structure in your app:

|- myapp/
|  |- __init__.py
|  |- config/
|  |  |- __init__.py
|  |  |- defaults.py
|  |  |- production.py
|  |  |- stage.py
|  |  |- tests.py
|  | ...

See https://github.com/klen/modconfig/tree/develop/example as a simple reference.

Initialize the config in your app and use it anywhere:

from modconfig import Config

cfg = Config('myapp.config.production', ANY_OPTION1="VALUE", ANY_OPTION2="VALUE")  # instead an import path it could be the module itself

assert cfg.DATABASE
assert cfg.ANY_OPTION1

Fallbacks

If you provide a several modules, modconfig will be using the first available:

from modconfig import Config

cfg = Config('myapp.config.local', 'myapp.config.production', ANY_OPTION1="VALUE")

assert cfg.DATABASE
assert cfg.ANY_OPTION1

Enviroments

The module path may be set as ENV variable:

import os
from modconfig import Config

# Let's define an env var
os.environ['MODCONFIG'] = 'myapp.config.production'

cfg = Config('env:MODCONFIG', 'myapp.config.local')
assert cfg.DATABASE

Custom Environment Variables

Any option may be redifened with ENV variables. By default the modconfig tries to parse value as a JSON which allows us to set complex values (dict, list, etc). If value is not JSON it would be parsed as str.

Any ENV variables which names are not contained in source module would be ignored.

See https://github.com/klen/modconfig/tree/develop/tests.py for more examples.

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/modconfig/issues

Contributing

Development of the project happens at: https://github.com/klen/modconfig

License

Licensed under a MIT license.

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

modconfig-1.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

modconfig-1.0.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modconfig-1.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for modconfig-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e9ea28326059b8755fd58e7b492d4cf7cc917d73da17006eed6b8e6b169beb3e
MD5 222e54b4f1c86fa1b37e2cf284b3e39f
BLAKE2b-256 1273beae2cf21c4380f2704d8b554004304a0dfdfb78619b3b742012eb0a6e35

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modconfig-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for modconfig-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b112de0c39c1b7a5947797b017eff19bf83c6f3aea792068497eaec973d4caa6
MD5 8c9628772acd97302ea4f3421a688166
BLAKE2b-256 2b186bb493c645e492d9ed47ff5574668027677baff692fe9b70e571bb0ee39d

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