Skip to main content

Dictionary-style configuration with path helpers and automatic environment variable handling

Project description

project-conf

PyPI version Python Support License: MIT

Dictionary-style configuration with path helpers and automatic environment variable handling.

A simple, powerful configuration system that behaves like a dictionary but provides convenient path helpers and automatic environment variable integration. Perfect for Python projects that need clean, flexible configuration management.

✨ Features

  • 🗂️ Dictionary Interface: Full dict compatibility - use config['key'], config.get(), config.update(), etc.
  • 📁 Path Helpers: Automatic directory creation with config.data_path(), config.logs_path(), etc.
  • 🌐 Environment Integration: Automatic override from environment variables and .env files
  • 🎯 Type-Safe Conversion: Smart type conversion based on your defaults
  • 🚀 Auto Project Detection: Finds your project root automatically
  • 🔧 Runtime Modification: Change configuration at runtime, visible globally
  • 📦 Zero Dependencies: Pure Python, no external dependencies

🚀 Quick Start

Installation

pip install project-conf

Basic Usage

from project_conf import setup, get_config

# 1. Define your configuration schema with defaults
setup({
    'database_url': 'sqlite:///app.db',
    'debug': False,
    'max_workers': 4,
    'api_key': '',
    'features': ['auth', 'cache']
})

# 2. Use anywhere in your application
config = get_config()

# Dictionary-style access
print(config['database_url'])  # sqlite:///app.db
config['debug'] = True         # Runtime modification
config.update({'timeout': 30}) # Bulk updates

# Path helpers (creates directories automatically)
db_path = config.data_path('app.db')      # /project/data/app.db
log_file = config.logs_path('server.log') # /project/logs/server.log
cache_dir = config.cache_path()           # /project/cache/

📖 Documentation

Environment Variable Override

Configuration values are automatically overridden by environment variables:

# config.py
setup({
    'database_url': 'sqlite:///dev.db',
    'debug': True,
    'max_workers': 2
})
# Environment variables (case-insensitive, converts types)
export DATABASE_URL="postgresql://localhost/prod"
export DEBUG=false
export MAX_WORKERS=8
config = get_config()
print(config['database_url'])  # postgresql://localhost/prod
print(config['debug'])         # False
print(config['max_workers'])   # 8

.env File Support

Create a .env file in your project root:

# .env
DATABASE_URL=postgresql://localhost/myapp
DEBUG=false
MAX_WORKERS=8
API_KEY=secret-key-123
FEATURES=["auth", "admin", "analytics"]

Values are automatically loaded and type-converted based on your defaults.

Path Helpers

Any method ending with _path becomes a path helper:

config = get_config()

# Directory paths (creates directories)
config.data_path()           # /project/data/
config.logs_path()           # /project/logs/
config.uploads_path()        # /project/uploads/
config.my_custom_path()      # /project/my_custom/

# File paths (creates parent directories)
config.data_path('app.db')           # /project/data/app.db
config.logs_path('server.log')       # /project/logs/server.log
config.uploads_path('image.jpg')     # /project/uploads/image.jpg
config.my_custom_path('file.txt')    # /project/my_custom/file.txt

Type Conversion

Environment variables are automatically converted to match your default types:

setup({
    'debug': False,        # bool: 'true'/'false', '1'/'0', 'yes'/'no'
    'workers': 4,          # int: '8' -> 8
    'timeout': 30.0,       # float: '45.5' -> 45.5
    'name': 'app',         # str: kept as string
    'features': ['auth']   # list: '["auth", "admin"]' -> ['auth', 'admin']
})

Project Root Detection

The project root is automatically detected by looking for:

  1. .git directory (most reliable)
  2. pyproject.toml
  3. setup.py
  4. requirements.txt
  5. poetry.lock
  6. Pipfile
  7. package.json
  8. .env

You can also override with PROJECT_ROOT environment variable or pass it directly:

setup(defaults, project_root='/custom/path')

🎯 Real-World Example

Clean, Simple Pattern - Handle everything in your config module:

# myproject/config.py - ONLY file that deals with project_conf
from project_conf import setup, get_config

# Single source of truth for configuration
DEFAULTS = {
    'database_url': 'sqlite:///myproject.db',
    'redis_url': 'redis://localhost:6379',
    'debug': False,
    'max_workers': 4,
    'timeout': 30.0,
    'api_key': '',
    'log_level': 'INFO',
    'features': {
        'auth': True,
        'admin': False,
        'analytics': True
    }
}

# Initialize configuration
setup(DEFAULTS)

# Export clean interface - no need for project_conf anywhere else!
config = get_config()
# myproject/database.py - Clean imports, no project_conf needed!
from .config import config

def create_connection():
    # Use as dictionary
    db_url = config['database_url']
    timeout = config.get('timeout', 30)

    # Use path helper for file location
    if 'sqlite' in db_url:
        db_file = config.data_path('myproject.db')
        return f"sqlite://{db_file}"

    return db_url
# myproject/api.py - Clean imports, no project_conf needed!
from .config import config

def setup_logging():
    # Runtime configuration changes
    if not config.get('api_key'):
        config['api_key'] = 'development-key'

    # Path helpers for log files
    log_file = config.logs_path('api.log')

    return setup_logger(
        level=config['log_level'],
        file=log_file
    )

That's it! No complex imports, no worrying about initialization order. Your config.py handles everything, and the rest of your code just imports from there.

✨ Why This Pattern Works

  • 🎯 Single Responsibility: Only config.py deals with project_conf
  • 🧹 Clean Imports: Rest of your code just imports from your own config module
  • 🔄 No Initialization Worries: Configuration is ready when you import it
  • 📦 Encapsulation: project_conf is an implementation detail, hidden from your app
  • 🚀 Simple Testing: Mock myproject.config.config in tests, not the underlying library

🧪 Testing

Run the test suite:

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=project_conf --cov-report=html

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🔗 Links

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

project_conf-0.1.2.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

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

project_conf-0.1.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file project_conf-0.1.2.tar.gz.

File metadata

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

File hashes

Hashes for project_conf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 771a4919d0cde6362fa2cd67328c6d1240f34584a4ae5b41fdd0030e061b17d7
MD5 2cc1e24aa5f3edc3576eb064fcbdc0a6
BLAKE2b-256 f015952b87fe1f6bb995e463d491f6de6302912556c00ff1a9b85c49449b482d

See more details on using hashes here.

File details

Details for the file project_conf-0.1.2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for project_conf-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 377dced8947a238c64b3ed4498b43a05c98def18059191906d5b81ed4c39b5c4
MD5 8f25c47d16734ab372f9531fce99fe7f
BLAKE2b-256 1074d363ee8c0d2c36422ad9790ee2d5f020b65b9628ab6bfe6b58ceb4f4ba12

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