Async Configurationο
Async/await support for Confii.
This module provides async versions of Config and loaders for use in asynchronous Python applications.
- class confii.async_config.AsyncConfig(env: str | None = None, loaders: Sequence[AsyncLoader] | None = None, use_env_expander: bool = True, use_type_casting: bool = True, debug_mode: bool = False, deep_merge: bool = True, secret_resolver: SecretResolver | None = None)[source]ο
Bases:
objectAsync version of Config for use in async applications.
This class provides async methods for loading and managing configuration in asynchronous Python applications.
Example
>>> async def main(): ... loader = AsyncYamlLoader("config.yaml") ... config = await AsyncConfig.create(loaders=[loader]) ... value = await config.get_async("database.host")
- __init__(env: str | None = None, loaders: Sequence[AsyncLoader] | None = None, use_env_expander: bool = True, use_type_casting: bool = True, debug_mode: bool = False, deep_merge: bool = True, secret_resolver: SecretResolver | None = None) None[source]ο
Initialize AsyncConfig instance.
- Parameters:
env β Environment name
loaders β List of AsyncLoader instances
use_env_expander β Enable environment variable expansion
use_type_casting β Enable automatic type casting
debug_mode β Enable debug mode
deep_merge β Enable deep merging
secret_resolver β Optional secret resolver
- async classmethod create(env: str | None = None, loaders: Sequence[AsyncLoader] | None = None, **kwargs: Any) AsyncConfig[source]ο
Create and initialize AsyncConfig asynchronously.
This is the preferred way to create an AsyncConfig instance as it properly handles async initialization.
- Parameters:
env β Environment name
loaders β List of AsyncLoader instances
**kwargs β Additional arguments passed to __init__
- Returns:
Initialized AsyncConfig instance
Example
>>> config = await AsyncConfig.create( ... env="production", loaders=[AsyncYamlLoader("config.yaml")] ... )
- async get_async(key_path: str, default: Any = None) Any[source]ο
Get configuration value asynchronously.
- Parameters:
key_path β Dot-separated key path
default β Default value if key not found
- Returns:
Configuration value or default
- async load() None[source]ο
Load configuration from all async loaders.
This method must be called before accessing configuration values.
- Raises:
ConfigLoadError β If loading fails
- async reload() None[source]ο
Reload configuration asynchronously.
This method reloads all configurations from their sources.
- class confii.async_config.AsyncHTTPLoader(url: str, timeout: int = 30)[source]ο
Bases:
AsyncLoaderAsync HTTP configuration loader.
Fetches configuration from a remote HTTP/HTTPS endpoint using
aiohttp. The response format (JSON or YAML) is auto-detected from theContent-Typeheader or the URL file extension.Requires the
aiohttppackage (pip install aiohttp).Example
>>> loader = AsyncHTTPLoader("https://cfg.example.com/app.json", timeout=10) >>> config = await loader.load()
- class confii.async_config.AsyncLoader(source: str)[source]ο
Bases:
objectAbstract base class for async configuration loaders.
All async loaders must inherit from this class and implement the
load()method as a coroutine. Use async loaders when your configuration sources involve I/O that benefits from non-blocking execution (remote APIs, cloud storage, etc.).Example
>>> class AsyncRedisLoader(AsyncLoader): ... async def load(self): ... import aioredis ... ... r = await aioredis.from_url(self.source) ... return await r.hgetall("config")
- __init__(source: str) None[source]ο
Initialize the async loader.
- Parameters:
source β The source identifier (file path, URL, prefix, etc.)
- async load() Dict[str, Any] | None[source]ο
Load configuration from the source (async).
This method must be implemented by subclasses.
- Returns:
Dictionary containing the loaded configuration, or None if the source doesnβt exist or couldnβt be loaded.
- Raises:
ConfigLoadError β If loading fails due to an error
- class confii.async_config.AsyncYamlLoader(source: str)[source]ο
Bases:
AsyncLoaderAsync YAML configuration loader.
Reads a YAML file using a thread-pool executor so the event loop is never blocked by disk I/O. Suitable for
asyncio-based applications that load configuration at startup.Example
>>> loader = AsyncYamlLoader("config.yaml") >>> config = await loader.load() >>> print(config["database"]["host"])