Skip to main content

Easy configuration management in python

Project description

ConfMe: Configuration Made Easy 💖

image image image image

ConfMe is a simple to use, production ready application configuration management library, which takes into consideration the following three thoughts:

  1. Access to configuration values must be safe at runtime. No myconfig['value1']['subvalue'] anymore!
  2. The configuration must be checked for consistency at startup e.g. type check, range check, ...
  3. Secrets shall be injectable from environment variables

ConfMe makes all these features possible with just a few type annotations on plain Python objects.

Installation

ConfMe can be installed from the official python package repository pypi

pip install confme

Or, if you're using pipenv:

pipenv install confme

Or, if you're using poetry:

poetry add confme

Basic Usage of confme

Define your config structure as plain python objects with type annotations:

from confme import BaseConfig

class DatabaseConfig(BaseConfig):
    host: str
    port: int
    user: str

class MyConfig(BaseConfig):
    name: int
    database: DatabaseConfig

Create a configuration yaml file with the same structure as your configuration classes have:

name: "Database Application"
database:
    host: "localhost"
    port: 5000
    user: "any-db-user"

Load the yaml file into your Python object structure and access it in a secure manner:

my_config = MyConfig.load('config.yaml')

print(f'Using database connection {my_config.database.host} '
      f'on port {my_config.database.port}')

In the background the yaml file is parsed and mapped to the defined object structure. While mapping the values to object properties, type checks are performed. If a value is not available or is not of the correct type, an error is generated already when the configuration is loaded.

Supported Annotations

ConfMe is based on pydantic and supports all annotations provided by pydantic. The most important annotations are listed and explain bellow. For the whole list, please checkout Field Types:

Secret

With the Secret annotation you can inject secrets from environment variables directly into your configuration structure. This is especially handy when you're deploying applications by using docker. Therefore, let's extend the previous example with a Secret annotation:

...
from confme import BaseConfig
from confme.annotation import Secret

class DatabaseConfig(BaseConfig):
    ...
    password: str = Secret('highSecurePassword')

Now set the password to the defined environment variable:

export highSecurePassword="This is my password"

Load your config and check for the injected password.

my_config = MyConfig.load('config.yaml')
print(f'My password is: {my_config.database.password}')

Range

ConfME supports OpenRange, ClosedRange and MixedRange values. The terms open and close are similar to open and closed intervals in mathematics. This means, if you want to include the lower and upper range use ClosedRange otherwise OpenRange:

  • ClosedRange(2, 3) will include 2 and 3
  • OpenRange(2, 3) will not include 2 and 3

If you want to have a mixture of both, e.g. include 2 but exclude 3 use MixedRange:

  • MixedRange(ge=2, lt=3) will include 2 but exclude 3
...
from confme import BaseConfig
from confme.annotation import ClosedRange

class DatabaseConfig(BaseConfig):
    ...
    password: int = ClosedRange(2, 3)

Enum

from enum import Enum

class DatabaseConnection(Enum):
    TCP = 'tcp'
    UDP = 'udp'

class DatabaseConfig(BaseConfig):
    ...
    connection_type: DatabaseConnection

LICENSE

ConfMe is released under the 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.

Files for confme, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size confme-0.0.4-py3-none-any.whl (12.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size confme-0.0.4.tar.gz (13.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page