Skip to main content

A smart configuration manager with auto-save and type validation

Project description

Configium 🔧

A smart, type-safe configuration manager for Python with auto-save and auto-reload capabilities.

Features

Auto-save: Changes are automatically persisted to disk
🔄 Auto-reload: Detects external file modifications and reloads
🛡️ Type-safe: Pydantic integration for robust validation
📝 Multiple formats: Support for JSON, YAML, and TOML
🔒 Thread-safe: Safe for concurrent access
High performance: Efficient file change detection

Installation

pip install configium

Quick Start

from configium import ConfigManager

# Create or load configuration
config = ConfigManager("config.yaml")

# Use it like a dictionary
config["database"] = {
    "host": "localhost",
    "port": 5432
}

# Changes are automatically saved!
print(config["database"]["host"])  # localhost

# Nested access
config["api"]["key"] = "secret"

Advanced Usage

Type Validation with Pydantic

from pydantic import BaseModel
from configium import ConfigManager

class DatabaseConfig(BaseModel):
    host: str
    port: int
    username: str

config = ConfigManager("config.yaml", validation_model=DatabaseConfig)

# This works
config.update({"host": "localhost", "port": 5432, "username": "admin"})

# This raises ValidationError
config["port"] = "invalid"  # ❌ Must be int

Different Formats

# JSON
config = ConfigManager("config.json", format="json")

# TOML
config = ConfigManager("config.toml", format="toml")

# Auto-detect from extension
config = ConfigManager("config.yaml")  # Automatically uses YAML

Manual Control

# Disable auto-save
config = ConfigManager("config.yaml", auto_save=False)
config["key"] = "value"
config.save()  # Manually save

# Disable auto-reload
config = ConfigManager("config.yaml", auto_reload=False)
config.reload()  # Manually reload

Thread-Safe Operations

from threading import Thread

config = ConfigManager("config.yaml")

def update_config(key, value):
    config[key] = value

# Safe concurrent access
threads = [Thread(target=update_config, args=(f"key{i}", i)) for i in range(10)]
for t in threads:
    t.start()
for t in threads:
    t.join()

API Reference

ConfigManager

ConfigManager(
    file_path: str | Path,
    format: str = "yaml",
    auto_save: bool = True,
    auto_reload: bool = True,
    validation_model: Optional[Type[BaseModel]] = None,
    create_if_missing: bool = True,
)

Parameters:

  • file_path: Path to configuration file
  • format: File format (json, yaml, toml)
  • auto_save: Enable automatic saving
  • auto_reload: Enable automatic reloading
  • validation_model: Pydantic model for validation
  • create_if_missing: Create file if it doesn't exist

Methods:

  • reload(): Manually reload from file
  • save(): Manually save to file
  • update(dict): Update multiple values
  • to_dict(): Get configuration as dictionary
  • clear(): Clear all configuration

Performance

Configium is designed for high performance:

  • Smart change detection: Uses mtime + hash for accurate detection
  • Atomic writes: Prevents corruption during saves
  • Minimal overhead: Dict proxy with negligible performance impact
  • Efficient locking: Fine-grained locks for thread safety

Security

  • ✅ Uses yaml.safe_load() to prevent code execution
  • ✅ Atomic file writes prevent corruption
  • ✅ Type validation prevents injection attacks
  • ✅ No eval() or exec() usage

Contributing

Contributions welcome! Please check out our GitHub repository.

License

MIT License - see LICENSE file 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

configium-0.1.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

configium-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file configium-0.1.0.tar.gz.

File metadata

  • Download URL: configium-0.1.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for configium-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b1ce4faff0f7efcc52b46a77b9595f61d0ab26ba76068edff87f68661a9f4119
MD5 c315236a292c4968c00ba1e4c2aa5c9a
BLAKE2b-256 ddf76b601d08000cb1a4ba2721c2c8bbe8d3f726a9c8a01b64a0d0f4908c562f

See more details on using hashes here.

File details

Details for the file configium-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: configium-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for configium-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0900acbd1595d99f1bf817f90e27a33f6bea1c3fdc22a3362bf97c6e02d4c574
MD5 ebc2623e7231895e45952774e5cf0f92
BLAKE2b-256 baaeb62f5ed244c94336a5d2336d80d78b959d00fe7cddabebaa206076c10e14

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