Skip to main content

Unified configuration management system for Python applications with multiple storage backends, type validation, and immutability control

Project description

PyConfBox 🎯

Python Configuration Management with Multiple Storage Backends

PyConfBox is a powerful Python configuration management library that provides unified management of environment variables, system variables, global variables, and more.

한국어 문서: README_ko.md | English Documentation: README.md (current)

✨ Key Features

  • 🏗️ Multiple Storage Backends: Memory, Environment, File (JSON/YAML/TOML), SQLite, Redis
  • 🎯 Scope System: Support for env, global, local, system, secret, django scopes
  • 🔒 Immutability Control: Per-configuration immutability and global release mode
  • 🔄 Automatic Type Conversion: Automatic string → int, float, bool, list, dict conversion
  • 🔌 Plugin Architecture: Extensible storage and plugin system
  • 📊 Metadata Management: Configuration statistics and state tracking

🚀 Quick Start

Installation

pip install pyconfbox

Basic Usage

from pyconfbox import Config, ConfigScope

# Create Config instance
config = Config(default_storage="memory", fallback_storage="environment")

# Basic configuration
config.set("app_name", "MyApp")
config.set("debug", True)

# Type conversion
config.set("port", "8080", data_type=int)
config.set("timeout", "30.5", data_type=float)
config.set("hosts", "localhost,127.0.0.1", data_type=list)

# Scope-based configuration
config.set("database_url", "sqlite:///app.db", scope=ConfigScope.LOCAL)
config.set("secret_key", "super-secret", scope=ConfigScope.SECRET, immutable=True)

# Retrieve configuration
app_name = config.get("app_name")
port = config.get("port")  # Automatically int type
hosts = config.get("hosts")  # Automatically list type

# Scope-based retrieval
global_configs = config.get_by_scope(ConfigScope.GLOBAL)
secret_configs = config.get_by_scope(ConfigScope.SECRET)

# Release mode (lock all configurations)
config.release()

File Storage Usage

from pyconfbox import Config, JSONStorage, YAMLStorage, TOMLStorage

# JSON file storage
json_storage = JSONStorage('config.json')
config = Config(default_storage=json_storage)

config.set('app_name', 'MyApp')
config.set('version', '1.0.0')
config.set('features', ['auth', 'cache', 'logging'])

# YAML file storage
yaml_storage = YAMLStorage('config.yaml')
config = Config(default_storage=yaml_storage)

config.set('database', {
    'host': 'localhost',
    'port': 5432,
    'name': 'myapp_db'
})

# TOML file storage 
toml_storage = TOMLStorage('config.toml')
config = Config(default_storage=toml_storage)

config.set('owner', {
    'name': 'John Doe',
    'email': 'john@example.com'
})

SQLite Storage Usage

from pyconfbox import Config, SQLiteStorage

# In-memory SQLite
memory_storage = SQLiteStorage()  # ":memory:"
config = Config(default_storage=memory_storage)

# File SQLite
file_storage = SQLiteStorage('config.db')
config = Config(default_storage=file_storage)

config.set('session_timeout', 3600)
config.set('max_connections', 100)

# Batch update
batch_data = {
    'env': 'production',
    'region': 'us-west-2',
    'replicas': 3
}
file_storage.update(batch_data)

📋 Configuration Scopes

Scope Description Use Cases
env Environment variables OS environment variables, process-specific settings
global Global variables Application-wide settings
local Local variables Module/class-specific local settings
system System variables System-level settings
secret Secret variables Sensitive settings requiring encryption
django Django settings Django-specific settings

🏗️ Storage Architecture

Built-in Storage Backends

  • Memory: In-memory storage (default)
  • Environment: Environment variable storage (read-only)
  • File: File-based storage (JSON, YAML, TOML)
  • Redis: Redis storage
  • SQLite: SQLite database storage

Plugin Storage Backends (Separate Packages)

  • pyconfbox-mysql: MySQL storage
  • pyconfbox-postgresql: PostgreSQL storage
  • pyconfbox-mongodb: MongoDB storage
  • pyconfbox-django: Django integration plugin

🔒 Immutability Management

# Set individual configuration as immutable
config.set("api_key", "secret", immutable=True)

# Attempt to change immutable configuration (raises exception)
try:
    config.set("api_key", "new_secret")
except ImmutableConfigError:
    print("Immutable configurations cannot be changed!")

# Lock all configurations (release mode)
config.release()

# Attempt to change configuration after release (raises exception)
try:
    config.set("new_key", "value")
except ReleasedConfigError:
    print("Released configurations cannot be changed!")

🔄 Automatic Type Conversion

# String → Integer
config.set("port", "8080", data_type=int)
assert config.get("port") == 8080

# String → Boolean
config.set("debug", "true", data_type=bool)
assert config.get("debug") is True

# String → List (comma-separated)
config.set("hosts", "localhost,127.0.0.1", data_type=list)
assert config.get("hosts") == ["localhost", "127.0.0.1"]

# String → Dictionary (JSON)
config.set("db_config", '{"host": "localhost", "port": 5432}', data_type=dict)
assert config.get("db_config") == {"host": "localhost", "port": 5432}

📊 Metadata and Statistics

metadata = config.get_metadata()

print(f"Total configurations: {metadata.total_configs}")
print(f"By scope: {metadata.scopes}")
print(f"By storage: {metadata.storages}")
print(f"Immutable count: {metadata.immutable_count}")
print(f"Is released: {metadata.is_released}")

🔌 Advanced Usage

Environment Variable Prefix

from pyconfbox import Config, EnvironmentStorage

# Use environment variables with prefix
env_storage = EnvironmentStorage(prefix="MYAPP_")
config = Config(default_storage=env_storage)

# Reads from MYAPP_DATABASE_URL environment variable
database_url = config.get("DATABASE_URL")

Custom Storage Backend

from pyconfbox.storage.base import BaseStorage
from pyconfbox.core.types import ConfigValue

class CustomStorage(BaseStorage):
    def get(self, key: str) -> ConfigValue | None:
        # Implementation
        pass
    
    def set(self, key: str, value: ConfigValue) -> None:
        # Implementation
        pass
    
    def delete(self, key: str) -> bool:
        # Implementation
        pass
    
    def list_keys(self) -> list[str]:
        # Implementation
        pass

# Use custom storage
custom_storage = CustomStorage()
config = Config(default_storage=custom_storage)

📖 Documentation

🔗 Related Packages

🤝 Contributing

Contributions are welcome! Please see our Contributing Guide for details.

📄 License

MIT License - See the LICENSE file for details.


Experience better configuration management with PyConfBox! 🚀

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

pyconfbox-0.1.1.tar.gz (71.1 kB view details)

Uploaded Source

Built Distribution

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

pyconfbox-0.1.1-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file pyconfbox-0.1.1.tar.gz.

File metadata

  • Download URL: pyconfbox-0.1.1.tar.gz
  • Upload date:
  • Size: 71.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pyconfbox-0.1.1.tar.gz
Algorithm Hash digest
SHA256 92c9a0bf662d42fefd748c69ae14e80102be20909fb5db2345cf15998aebb7d8
MD5 e2e531cea966c7dafbe8316401469e5d
BLAKE2b-256 e6059f92c35c2528dd70fb6bba64d0c4bf1cd1cb5de4c2fd2d133340ea9fcea3

See more details on using hashes here.

File details

Details for the file pyconfbox-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyconfbox-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pyconfbox-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a901e468ab909b09a4b9cbd0859ee37c6ba09824362400803cc7fdde958b963
MD5 2cf0c662abc01a6e7e02326f88a0e5b9
BLAKE2b-256 3e26df647c6b810329ee619b7213bc438e9c92a0b8dc67dbcea2c8bdfdad96b2

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