Config API Reference¶
Multi-source configuration management.
Config Class¶
dspu.config.config.Config
¶
Main configuration loader with Pydantic validation.
Loads configuration from multiple sources with priority ordering (later sources override earlier ones) and validates using Pydantic models.
Example
from pydantic import BaseModel from dspu.config import Config, ConfigSource
class AppConfig(BaseModel): ... debug: bool = False ... port: int = 8000 ... config = Config.load( ... AppConfig, ... sources=[ ... ConfigSource.file("config.yaml"), ... ConfigSource.env(prefix="APP_"), ... ] ... ) config.port 8000
WatchedConfig¶
dspu.config.watched.WatchedConfig
¶
WatchedConfig(
config_class: type[T],
sources: Sequence[ConfigSource],
reload_interval: float = 5.0,
)
Bases: Generic[T]
Configuration that watches for file changes and automatically reloads.
This class wraps a configuration and monitors the source files for changes. When a change is detected, the configuration is automatically reloaded and registered callbacks are invoked.
Example
watched = WatchedConfig.load( ... AppConfig, ... source=FileSource("config.yaml"), ... reload_interval=5.0, ... )
Use current config¶
app = Application(watched.current)
Register callback¶
watched.on_change(lambda old, new: app.reconfigure(new))
When config.yaml changes, app.reconfigure() is called¶
Stop watching when done¶
watched.stop()
Note
- Only works with FileSource (needs file to watch)
- Callbacks are called in a background thread
- Always call stop() when done or use as context manager
Initialize watched configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_class
|
type[T]
|
Pydantic model class for validation. |
required |
sources
|
Sequence[ConfigSource]
|
Configuration sources to watch. |
required |
reload_interval
|
float
|
How often to check for changes (seconds). |
5.0
|
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If no file sources are provided. |
Source code in src/dspu/config/watched.py
Configuration Sources¶
dspu.config.sources.FileSource
¶
Bases: ConfigSource
Configuration source from a file.
Supports multiple formats: - JSON (.json) - YAML (.yaml, .yml) - TOML (.toml) - HOCON (.conf, .hocon) - ENV (.env)
Format is auto-detected from file extension or can be specified explicitly.
Example
source = FileSource("config.yaml") config = source.load()
Explicit format¶
source = FileSource("settings.txt", format="json") config = source.load()
Initialize file source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to configuration file. |
required |
format
|
str | None
|
Format override (json, yaml, toml, hocon, env). If None, auto-detect from extension. |
None
|
required
|
bool
|
If True, raise error if file doesn't exist. |
True
|
Source code in src/dspu/config/sources.py
Functions¶
load
¶
Load configuration from file.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Configuration dictionary. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If file cannot be read or parsed. |
Source code in src/dspu/config/sources.py
dspu.config.sources.EnvSource
¶
Bases: ConfigSource
Configuration source from environment variables.
Supports: - Optional prefix filtering - Nested keys using underscores or double underscores - Type conversion (numbers, booleans)
Example
With prefix: APP_DEBUG=true, APP_PORT=8000¶
source = EnvSource(prefix="APP_") config = source.load() config
With nested keys: APP__DATABASE__HOST=localhost¶
source = EnvSource(prefix="APP__", separator="__") config = source.load() config {'database': {'host': 'localhost'}}
Initialize environment source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Only load variables starting with this prefix. |
''
|
separator
|
str
|
Separator for nested keys (default: double underscore). |
'__'
|
lowercase
|
bool
|
Convert keys to lowercase. |
True
|
Source code in src/dspu/config/sources.py
Functions¶
load
¶
Load configuration from environment variables.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Configuration dictionary with nested structure if separator is used. |
Source code in src/dspu/config/sources.py
dspu.config.sources.DictSource
¶
Bases: ConfigSource
Configuration source from a Python dictionary.
Useful for testing and programmatic configuration.
Example
source = DictSource({"debug": True, "port": 8000}) config = source.load() config["debug"] True
Initialize dictionary source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Configuration dictionary. |
required |
Source code in src/dspu/config/sources.py
Usage¶
from dspu.config import Config, WatchedConfig
# Load from file
config = Config.from_file("config.yaml")
db_host = config.get("database.host")
# Merge from environment
config.merge_from_env(prefix="APP_")
# Watch for changes
watched = WatchedConfig.from_file("config.yaml")
watched.start_watching()