Skip to main content

Load small app configurations from environment variables, command line arguments and config files, according to a pydantic model.

Project description

conflator logo

[!WARNING] This project is under development and not yet feature complete or tested.

[!WARNING] This project is BETA and will be experimental for the forseable future. Interfaces and functionality are likely to change, and the project itself may be scrapped. DO NOT use this software in any project/software that is operational.

Conflator is a configuration-handling library for Python. It is designed to simplify the handling of configuration from multiple sources, such as environment variables, command line arguments, and configuration files. As an application or library developer, you specify your configuration schema with a Pydantic model, and conflator will handle the rest.

Conflator loads configuration in the following order:

  1. Default values specified in the Pydantic model
  2. System-wide configuration in /etc/appname/config.json (and yaml)
  3. User configuration in ~/.appname.json (and yaml)
  4. Additional configuration files and values provided as command line args (e.g. -f filename or --set value.deeper=foo)
  5. Environment variables
  6. Command-line arguments
  7. Dictionaries passed to the load method

...and then validates the merged configuration against the Pydantic model.

Installation

Conflator is available on PyPI:

pip install conflator

Or using poetry:

poetry add conflator

Usage

  1. Define Your Configuration Model: Begin by defining your configuration schema using Pydantic models. Annotate your model fields with EnvVar and CLIArg for environment variable and command-line argument support, respectively.
from pydantic import Field
from conflator import EnvVar, CLIArg, ConfigModel
from annotated_types import Annotated

class AppConfig(ConfigModel):

    host: str = "localhost"
    port: Annotated[int, EnvVar("PORT"), CLIArg("--port")] = 5432
    user: Annotated[str, EnvVar("USER"), CLIArg("--user"), Field(description="Your username")] = "foo"
  1. Initialize Conflator: Create an instance of the Conflator class, passing your application's name and the configuration model.
from conflator import Conflator

config = Conflator(app_name="my_app", model=AppConfig).load()
  1. Access Configuration: Use the loaded configuration throughout your application, knowing that the configuration has been fully validated.
print(f"User: {config.user}")

Try setting MY_APP_USER in the environment to see the value change, or use the --user flag to override the value.

Advanced Usage

Configuration layering for different deployments

your-app -f ./config/base.yaml -f ./config/production.yaml

Nested config just works

from annotated_types import Annotated
from conflator import EnvVar, CLIArg, ConfigModel

class DeeperConfig(ConfigModel):
    nested: Annotated[str, EnvVar("NESTED"), CLIArg("--nested")] = "default"

class Config(ConfigModel):
    host: str = "localhost"
    port: int = 543
    deeper: DeeperConfig = DeeperConfig()

Generate the JSON schema for your configuration

config = Conflator(app_name="my_app", model=AppConfig).schema() # uses pydantic's schema method behind the scenes

Limitations

  • CLI arguments and environment variables are defined per model type, not per model instance. This means that you cannot have different CLI arguments or environment variables for different instances of the same model. Some support for this may be added in the future.

  • Including ConfigModel's from other packages, to nest their configuration with yours, is possible. However, there is no way to resolve conflicts between CLI arguments and environment variable naming between two different config schemas. Some support for this may be added in the future.

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

conflator-0.1.5.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

conflator-0.1.5-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file conflator-0.1.5.tar.gz.

File metadata

  • Download URL: conflator-0.1.5.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.8.18 Linux/6.5.0-1016-azure

File hashes

Hashes for conflator-0.1.5.tar.gz
Algorithm Hash digest
SHA256 80afbb8e9ff68aa0e50f1fbcf0541c6cebf0acce31e42a576d74aa07a9a9a8bc
MD5 8316da7a38dc211b45ae7624f8e8dd6e
BLAKE2b-256 7aa9e9502c5225a5e814765b048c2860f98c850c9a7c1adfaed6a08d183829fd

See more details on using hashes here.

File details

Details for the file conflator-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: conflator-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.8.18 Linux/6.5.0-1016-azure

File hashes

Hashes for conflator-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cae382e17e3a6362d7b8597040e28ea479e5a7ac89cebeeaa52f745c621487c9
MD5 65971094c40d7e089f2b528f6f007861
BLAKE2b-256 10301713cf59cd9293af0eec7a8af7b2765fe7795b438a5af0f26aab76eb94e5

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