Skip to main content

Hierarchical configuration manager powered by Pydantic

Project description

confiddle

Hierarchical configuration for Python that's inspired by .NET and powered by Pydantic.

What is it?

It's a hierarchical configuration loader that should be familiar to folks in dotnet-land, but adapted for Python and leveraging Pydantic to build and validate the configuration data. Confiddle fiddles with the configuration so you don't have to!

Configurations can be loaded dynamically from a variety of sources:

  • JSON configuration files (ex: config.json)
  • Environment-specific JSON configuration files (ex: config.dev.json or config.prod.json)
  • Environment variables
  • Argparse
  • Click
  • Dictionaries

Those configurations are then merged together (shallowly, with later configurations overriding earlier ones), and used to build up the provided Pydantic model.

This is all customizable, too! With some easy tweaks, you can configure which configuration sources are loaded, what order they're loaded in, and the environment that your program is targeting. Confiddle will even bootstrap itself, so you can configure Confiddle's behavior the same way you'd load any other set of configurations.

Installation

pip install confiddle (preferably inside your project's venv 🙂)

Example

A web service that layers three configuration sources - base JSON defaults, a production-specific JSON override, and a runtime environment variable.

// config.json - shared across all environments
{
    "app_name": "my-web-service",
    "workers": 4,
    "admin_username": "admin"
}
// config.dev.json - local development overrides
{
    "host": "localhost",
    "port": 8080,
    "debug": true
}
// config.prod.json - production overrides
{
    "host": "1.2.3.4",
    "port": 80,
    "debug": false
}
## Injected by the platform's secrets manager
export MYAPP:ADMIN_PASSWORD="hunter2"
## app_config.py
from pydantic import BaseModel, Field

class AppConfig(BaseModel):
    app_name: str
    workers: int
    host: str
    port: int
    debug: bool
    admin_username: str
    admin_password: str = Field(default="", exclude=True)
## main.py
from confiddle import (
    Confiddle,
    ConfiddleConfigModel,
    ProviderConfigModel,
    ConfigEnvironment,
    JsonProviderConfig,
    EnvVarProviderConfig,
)
from app_config import AppConfig

confiddle = Confiddle(
    ConfiddleConfigModel(
        app=ProviderConfigModel(
            ## `directory_path="."`  searches for config JSON files at the current working directory
            json_file_provider=JsonProviderConfig(directory_path="."),
            ## `prefix="MYAPP"` filters out any environment variable that doesn't have the "MYAPP" prefix
            env_var_provider=EnvVarProviderConfig(prefix="MYAPP"),
        ),
        ## This tells it to only load production configurations, like config.prod.json above.
        ## Note that it'll still load the base configurations regardless, like config.json above.
        environment=ConfigEnvironment.PROD,
    )
)

config = confiddle.load_config(AppConfig)

## State of `config` after loading:
## config.app_name       -> "my-web-service"  (from config.json)
## config.workers        -> 4                 (from config.json)
## config.admin_username -> "admin"           (from config.json)
## config.host           -> "1.2.3.4"         (from config.prod.json)
## config.port           -> 80                (from config.prod.json)
## config.debug          -> False             (from config.prod.json)
## config.admin_password -> "hunter2"         (from MYAPP:ADMIN_PASSWORD env var)

And that's it! Confiddle uses the configuration files and Pydantic models that you're already using, but formalizes the ingestion process, making all the magic happen in just a few lines of code.

Documentation

  • API Reference - API docs, hierarchy behavior, environments, and bootstrapping
  • Examples - common usage patterns and recipes

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

confiddle-0.2.0.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

confiddle-0.2.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file confiddle-0.2.0.tar.gz.

File metadata

  • Download URL: confiddle-0.2.0.tar.gz
  • Upload date:
  • Size: 36.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for confiddle-0.2.0.tar.gz
Algorithm Hash digest
SHA256 de08119e85e3deec16f6eeb01cd2f5f0e7c1e4e8cf1a37e43e78d1487c6c4177
MD5 2fabe1ea5df187d04377a424c63d9c9d
BLAKE2b-256 1b04d82d3131a20d1dbbe5656fbc9fa02830f4f4073e02b3a7501f851be6b102

See more details on using hashes here.

File details

Details for the file confiddle-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: confiddle-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for confiddle-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 491a4e917d8a26cf4a3ba0cc3b84a63957fde64c8958e0d777badca462df5404
MD5 8dd19e54f2fb5396606708e3480eae62
BLAKE2b-256 cbfe7521356fb60634c29a67b3e7a41462fd54d7699fbfdbc421af54225b30fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page