Skip to main content

A lightweight configuration management library for Python applications with multiple sources, type validation, and automatic merging

Project description

Config-Lib

A lightweight configuration management library for Python applications that supports multiple sources (YAML, environment variables, CLI arguments) with type validation and automatic merging.

Features

  • Multiple Configuration Sources
    Load from YAML files, environment variables, and command-line arguments
  • Precedence Hierarchy
    CLI arguments > Environment variables > YAML config files > Default values
  • Type Annotations
    Built on msgspec for robust type validation and serialization
  • Nested Configurations
    Support for complex nested structures with dot/bracket notation
  • Error Handling
    Clear validation errors and graceful handling of source failures

Installation

pip install config-lib

Basic Usage

from config_lib.base import BaseConfig

class AppConfig(BaseConfig):
    """Main application configuration"""
    host: str = "localhost"
    port: int = 8080
    debug: bool = False

config = AppConfig.load()

Configuration Sources

YAML File

# config.yaml
host: "0.0.0.0"
port: 9000
os.environ["CFG_CONFIG"] = "config.yaml"
config = AppConfig.load()  # Loads from YAML

Environment Variables

export CFG_HOST="127.0.0.1"
export CFG_PORT=3000

CLI Arguments

python app.py --host 192.168.1.1 --port 4000

Nested Configurations

from msgspec import Struct

class DatabaseConfig(Struct):
    host: str = "localhost"
    port: int = 5432

class AppConfig(BaseConfig):
    db: DatabaseConfig = DatabaseConfig()
    cache_ttl: int = 300

Environment variables:
CFG_DB__HOST=postgres.example.com CFG_DB__PORT=6432

CLI arguments:
python app.py --db.host postgres.example.com --db.port 6432

Precedence Rules

  1. Command-line arguments (highest priority)
  2. Environment variables
  3. YAML configuration file (lowest priority)
  4. Class defaults (used if no other sources provide values)

Error Handling & Validation

The library provides:

  • Automatic type validation using msgspec
  • Clear error messages for missing required fields
  • Graceful handling of malformed configuration sources
  • Warning logging for source loading errors

Contributing

# Run tests
pytest tests/

# Check test coverage
pytest --cov=config_lib --cov-report=html

Contributions welcome! Please follow standard Python packaging practices and ensure all tests pass before submitting PRs.

License

MIT License. See LICENSE for details.

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

config_lib_msgspec-0.0.8.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

config_lib_msgspec-0.0.8-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file config_lib_msgspec-0.0.8.tar.gz.

File metadata

  • Download URL: config_lib_msgspec-0.0.8.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for config_lib_msgspec-0.0.8.tar.gz
Algorithm Hash digest
SHA256 d605b92d3808afacb0ce28ac92bbf7932e248757987f2f5d87e3735446331573
MD5 a7e818ac92801d71320ea8ffbc88bfdc
BLAKE2b-256 4c82596081f0b2d3ee0e38e3228b5f960526658925db0ee7a6b099fee2228ca3

See more details on using hashes here.

Provenance

The following attestation bundles were made for config_lib_msgspec-0.0.8.tar.gz:

Publisher: python-publish.yml on rastaclaus/config-lib-msgspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file config_lib_msgspec-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for config_lib_msgspec-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 cacf0c95550a63a1e14d0b8ff72b9101f56147bba3c4df72c5b99f634c2a286b
MD5 f95013aa76b2c4714e14f1c804bd8106
BLAKE2b-256 9eab0d0b57a5a5e296a6662b20bb264a67dcfa20db0728b4bd283791d23f62b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for config_lib_msgspec-0.0.8-py3-none-any.whl:

Publisher: python-publish.yml on rastaclaus/config-lib-msgspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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