dataknobs-config Complete API Reference¶
Complete auto-generated API documentation from source code docstrings.
💡 Also see: - Curated Guide - Hand-crafted tutorials and examples - Package Overview - Introduction and getting started - Source Code - View on GitHub
dataknobs_config ¶
DataKnobs Config Package
A modular, reusable configuration system for composable settings.
Modules:
| Name | Description |
|---|---|
binding_resolver |
Configuration binding resolver for resource instantiation. |
builders |
Optional object construction and caching functionality. |
config |
Core Config class implementation. |
environment |
Environment variable override system. |
environment_aware |
Environment-aware configuration with late-binding resource resolution. |
environment_config |
Environment-specific configuration and resource bindings. |
examples |
Example classes for dataknobs-config. |
exceptions |
Custom exceptions for the config package. |
inheritance |
Configuration inheritance utilities. |
references |
String reference system for cross-referencing configurations. |
settings |
Global settings and defaults management. |
substitution |
Environment variable substitution for configuration values. |
template_vars |
Template variable substitution for nested configuration structures. |
Classes:
| Name | Description |
|---|---|
AsyncCallableFactory |
Factory that wraps an async callable. |
BindingResolverError |
Error during resource binding resolution. |
CallableFactory |
Factory that wraps a callable. |
ConfigBindingResolver |
Resolves logical resource bindings to concrete instances. |
FactoryNotFoundError |
Factory not registered for resource type. |
SimpleFactory |
Simple factory that creates instances of a class. |
ConfigurableBase |
Base class for objects that can be configured. |
FactoryBase |
Base class for factory objects. |
Config |
A modular configuration system for composable settings. |
EnvironmentAwareConfig |
Configuration with environment-aware resource resolution. |
EnvironmentAwareConfigError |
Error related to environment-aware configuration. |
EnvironmentConfig |
Environment-specific configuration and resource bindings. |
EnvironmentConfigError |
Error related to environment configuration. |
ResourceBinding |
A binding from logical name to concrete implementation. |
ResourceNotFoundError |
Resource not found in environment configuration. |
ConfigNotFoundError |
Raised when a requested configuration is not found. |
InvalidReferenceError |
Raised when a configuration reference is invalid. |
InheritableConfigLoader |
Configuration loader with inheritance support. |
InheritanceError |
Error during configuration inheritance resolution. |
VariableSubstitution |
Handles environment variable substitution in configuration values. |
Functions:
| Name | Description |
|---|---|
deep_merge |
Deep merge two dictionaries. |
load_config_with_inheritance |
Convenience function to load a config file with inheritance. |
substitute_env_vars |
Recursively substitute environment variables in configuration. |
substitute_template_vars |
Recursively substitute {{var}} placeholders in configuration data. |
Classes¶
AsyncCallableFactory ¶
Factory that wraps an async callable.
Example
Initialize with an async callable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Async callable that creates resources |
required |
**default_kwargs
|
Any
|
Default kwargs merged with config |
{}
|
Methods:
| Name | Description |
|---|---|
create_async |
Create an instance asynchronously. |
create |
Sync create is not supported for async factories. |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
Functions¶
create_async
async
¶
Create an instance asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**config
|
Any
|
Configuration merged with defaults |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Created instance |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
create ¶
Sync create is not supported for async factories.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Always, use create_async instead |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
BindingResolverError ¶
Bases: Exception
Error during resource binding resolution.
CallableFactory ¶
Factory that wraps a callable.
Example
Initialize with a callable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Callable that creates resources |
required |
**default_kwargs
|
Any
|
Default kwargs merged with config |
{}
|
Methods:
| Name | Description |
|---|---|
create |
Create an instance. |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
ConfigBindingResolver ¶
Resolves logical resource bindings to concrete instances.
Works with EnvironmentConfig to: 1. Look up resource configurations by logical name 2. Resolve environment variables in configurations 3. Instantiate resources using registered factories 4. Cache instances for reuse
Attributes:
| Name | Type | Description |
|---|---|---|
environment |
EnvironmentConfig
|
The EnvironmentConfig for resource lookup |
Initialize the binding resolver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
EnvironmentConfig
|
Environment configuration for resource lookup |
required |
resolve_env_vars
|
bool
|
Whether to resolve env vars before instantiation |
True
|
Methods:
| Name | Description |
|---|---|
register_factory |
Register a factory for a resource type. |
unregister_factory |
Unregister a factory for a resource type. |
has_factory |
Check if a factory is registered for a resource type. |
get_registered_types |
Get all registered resource types. |
resolve |
Resolve a logical resource reference to a concrete instance. |
resolve_async |
Async version of resolve for async factories. |
get_cached |
Get a cached instance if it exists. |
is_cached |
Check if an instance is cached. |
clear_cache |
Clear cached resource instances. |
cache_instance |
Manually cache an instance. |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
Attributes¶
Functions¶
register_factory ¶
Register a factory for a resource type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource (e.g., "databases", "vector_stores") |
required |
factory
|
ResourceFactory | Callable[..., Any]
|
Factory instance or callable that creates resources. Must have create(**config) method or be callable. |
required |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
unregister_factory ¶
Unregister a factory for a resource type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource to unregister |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If factory not registered |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
has_factory ¶
Check if a factory is registered for a resource type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if factory is registered |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
get_registered_types ¶
Get all registered resource types.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of resource type names with registered factories |
resolve ¶
Resolve a logical resource reference to a concrete instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource ("databases", "vector_stores", etc.) |
required |
logical_name
|
str
|
Logical name from app config |
required |
use_cache
|
bool
|
Whether to return cached instance if available |
True
|
**overrides
|
Any
|
Config overrides for this resolution |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Instantiated resource |
Raises:
| Type | Description |
|---|---|
FactoryNotFoundError
|
If no factory registered for resource type |
BindingResolverError
|
If resource creation fails |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
resolve_async
async
¶
resolve_async(
resource_type: str,
logical_name: str,
use_cache: bool = True,
**overrides: Any,
) -> Any
Async version of resolve for async factories.
If the factory has a create_async method, it will be used. Otherwise, falls back to synchronous create.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
logical_name
|
str
|
Logical name from app config |
required |
use_cache
|
bool
|
Whether to return cached instance |
True
|
**overrides
|
Any
|
Config overrides for this resolution |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Instantiated resource |
Raises:
| Type | Description |
|---|---|
FactoryNotFoundError
|
If no factory registered for resource type |
BindingResolverError
|
If resource creation fails |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
get_cached ¶
Get a cached instance if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
logical_name
|
str
|
Logical name |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
Cached instance or None |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
is_cached ¶
Check if an instance is cached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
logical_name
|
str
|
Logical name |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if cached |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
clear_cache ¶
Clear cached resource instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str | None
|
Specific resource type to clear, or None for all |
None
|
Source code in packages/config/src/dataknobs_config/binding_resolver.py
cache_instance ¶
Manually cache an instance.
Useful for pre-populating cache or caching externally created instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
logical_name
|
str
|
Logical name |
required |
instance
|
Any
|
Instance to cache |
required |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
FactoryNotFoundError ¶
Bases: BindingResolverError
Factory not registered for resource type.
SimpleFactory ¶
Simple factory that creates instances of a class.
Example
Initialize with a class to instantiate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
Class to instantiate |
required |
**default_kwargs
|
Any
|
Default kwargs merged with config |
{}
|
Methods:
| Name | Description |
|---|---|
create |
Create an instance. |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
ConfigurableBase ¶
Base class for objects that can be configured.
Classes that inherit from this can implement custom configuration loading logic.
Methods:
| Name | Description |
|---|---|
from_config |
Create an instance from a configuration dictionary. |
Functions¶
from_config
classmethod
¶
Create an instance from a configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration dictionary |
required |
Returns:
| Type | Description |
|---|---|
ConfigurableBase
|
Instance of the class |
Source code in packages/config/src/dataknobs_config/builders.py
FactoryBase ¶
Base class for factory objects.
Factories that inherit from this should implement the create method.
Methods:
| Name | Description |
|---|---|
create |
Create an object from configuration. |
Config ¶
A modular configuration system for composable settings.
Internally stores configurations as a dictionary of lists of atomic configuration dictionaries, organized by type.
Initialize a Config object from one or more sources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*sources
|
Union[str, Path, dict]
|
Variable number of sources (file paths or dictionaries) |
()
|
**kwargs
|
Any
|
Additional keyword arguments |
{}
|
Methods:
| Name | Description |
|---|---|
from_file |
Create a Config object from a file. |
from_dict |
Create a Config object from a dictionary. |
load |
Load configuration from a source. |
get_types |
Get all configuration types. |
get_count |
Get the count of configurations for a type. |
get_names |
Get all configuration names for a type. |
get |
Get a configuration by type and name/index. |
set |
Set a configuration by type and name/index. |
resolve_reference |
Resolve a string reference to a configuration. |
build_reference |
Build a string reference for a configuration. |
merge |
Merge another configuration into this one. |
to_dict |
Export configuration as a dictionary. |
to_file |
Save configuration to a file. |
build_object |
Build an object from a configuration reference. |
clear_object_cache |
Clear cached objects. |
get_factory |
Get a factory instance for a configuration. |
register_factory |
Register a factory instance for use in configurations. |
unregister_factory |
Unregister a factory. |
get_registered_factories |
Get all registered factories. |
get_instance |
Get an instance from a configuration. |
Source code in packages/config/src/dataknobs_config/config.py
Functions¶
from_file
classmethod
¶
Create a Config object from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to configuration file (YAML or JSON) |
required |
Returns:
| Type | Description |
|---|---|
Config
|
Config object |
from_dict
classmethod
¶
Create a Config object from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Configuration dictionary |
required |
Returns:
| Type | Description |
|---|---|
Config
|
Config object |
load ¶
Load configuration from a source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, Path, dict]
|
File path or dictionary |
required |
Source code in packages/config/src/dataknobs_config/config.py
get_types ¶
Get all configuration types.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of type names |
get_count ¶
Get the count of configurations for a type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Type name |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of configurations |
get_names ¶
Get all configuration names for a type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Type name |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List of configuration names |
Source code in packages/config/src/dataknobs_config/config.py
get ¶
Get a configuration by type and name/index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Type name |
required |
name_or_index
|
Union[str, int]
|
Configuration name or index |
0
|
Returns:
| Type | Description |
|---|---|
dict
|
Configuration dictionary |
Source code in packages/config/src/dataknobs_config/config.py
set ¶
Set a configuration by type and name/index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Type name |
required |
name_or_index
|
Union[str, int]
|
Configuration name or index |
required |
config
|
dict
|
Configuration dictionary |
required |
Source code in packages/config/src/dataknobs_config/config.py
resolve_reference ¶
Resolve a string reference to a configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str
|
String reference (e.g., "xref:foo[bar]") |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Referenced configuration dictionary |
Source code in packages/config/src/dataknobs_config/config.py
build_reference ¶
Build a string reference for a configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Type name |
required |
name_or_index
|
Union[str, int]
|
Configuration name or index |
required |
Returns:
| Type | Description |
|---|---|
str
|
String reference |
Source code in packages/config/src/dataknobs_config/config.py
merge ¶
Merge another configuration into this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Config
|
Configuration to merge |
required |
precedence
|
str
|
Precedence rule ("first" or "last") |
'first'
|
Source code in packages/config/src/dataknobs_config/config.py
to_dict ¶
Export configuration as a dictionary.
Returns:
| Type | Description |
|---|---|
dict
|
Configuration dictionary |
Source code in packages/config/src/dataknobs_config/config.py
to_file ¶
Save configuration to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Output file path |
required |
format
|
str | None
|
Output format ("yaml" or "json"), auto-detected if not specified |
None
|
Source code in packages/config/src/dataknobs_config/config.py
build_object ¶
Build an object from a configuration reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str
|
String reference to configuration |
required |
cache
|
bool
|
Whether to cache the built object |
True
|
**kwargs
|
Any
|
Additional keyword arguments for construction |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Built object instance |
Source code in packages/config/src/dataknobs_config/config.py
clear_object_cache ¶
Clear cached objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str | None
|
Specific reference to clear, or None to clear all |
None
|
get_factory ¶
Get a factory instance for a configuration.
This method lazily instantiates and caches factory instances defined in configurations with a 'factory' attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Type name |
required |
name_or_index
|
Union[str, int]
|
Configuration name or index |
0
|
Returns:
| Type | Description |
|---|---|
Any
|
Factory instance |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If no factory is defined for the configuration |
Source code in packages/config/src/dataknobs_config/config.py
register_factory ¶
Register a factory instance for use in configurations.
Registered factories take precedence over module paths. This allows: 1. Cleaner configuration files (no module paths needed) 2. Runtime factory substitution (useful for testing) 3. Pre-configured factory instances
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name to register the factory under |
required |
factory
|
Any
|
Factory instance or class |
required |
Example
Note
If a factory name matches both a registered factory and a module path, the registered factory takes precedence.
Source code in packages/config/src/dataknobs_config/config.py
unregister_factory ¶
Unregister a factory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the factory to unregister |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If factory is not registered |
Source code in packages/config/src/dataknobs_config/config.py
get_registered_factories ¶
Get all registered factories.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary mapping factory names to factory instances |
get_instance ¶
Get an instance from a configuration.
This is a convenience method that combines get() and build_object(). If the configuration has a 'class' or 'factory' attribute, it will build and return an instance. Otherwise, it returns the config dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Type name |
required |
name_or_index
|
Union[str, int]
|
Configuration name or index |
0
|
**kwargs
|
Any
|
Additional keyword arguments for construction |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Built instance or configuration dictionary |
Source code in packages/config/src/dataknobs_config/config.py
EnvironmentAwareConfig ¶
EnvironmentAwareConfig(
config: dict[str, Any],
environment: EnvironmentConfig | None = None,
app_name: str | None = None,
)
Configuration with environment-aware resource resolution.
Manages application configuration with support for: - Logical resource references that resolve per-environment - Late-binding of environment variables - Portable config storage (unresolved)
Attributes:
| Name | Type | Description |
|---|---|---|
environment |
EnvironmentConfig
|
The EnvironmentConfig for resource resolution |
app_name |
str | None
|
Name of the loaded application (if any) |
Initialize environment-aware configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Application configuration dictionary |
required |
environment
|
EnvironmentConfig | None
|
Environment configuration for resource resolution. If None, auto-detects and loads environment. |
None
|
app_name
|
str | None
|
Optional name for this application config |
None
|
Methods:
| Name | Description |
|---|---|
load_app |
Load an application configuration with environment bindings. |
from_dict |
Create from a configuration dictionary. |
get |
Get a value from the config. |
resolve_for_build |
Resolve configuration for object building. |
get_portable_config |
Get the portable (unresolved) configuration. |
to_dict |
Get the raw configuration dictionary. |
with_environment |
Create a new instance with a different environment. |
get_resource |
Get a resolved resource configuration. |
get_setting |
Get an environment setting. |
__repr__ |
String representation. |
Source code in packages/config/src/dataknobs_config/environment_aware.py
Attributes¶
Functions¶
load_app
classmethod
¶
load_app(
app_name: str,
app_dir: str | Path = "config/apps",
env_dir: str | Path = "config/environments",
environment: str | None = None,
) -> EnvironmentAwareConfig
Load an application configuration with environment bindings.
This is the primary entry point for loading configs in an environment-aware manner. Config files are loaded WITHOUT environment variable substitution (late binding).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
str
|
Application/bot name (without .yaml extension) |
required |
app_dir
|
str | Path
|
Directory containing app configs |
'config/apps'
|
env_dir
|
str | Path
|
Directory containing environment configs |
'config/environments'
|
environment
|
str | None
|
Environment name, or None to auto-detect |
None
|
Returns:
| Type | Description |
|---|---|
EnvironmentAwareConfig
|
EnvironmentAwareConfig with both app and environment loaded |
Raises:
| Type | Description |
|---|---|
EnvironmentAwareConfigError
|
If app config not found or invalid |
Source code in packages/config/src/dataknobs_config/environment_aware.py
from_dict
classmethod
¶
from_dict(
config: dict[str, Any],
environment: str | None = None,
env_dir: str | Path = "config/environments",
) -> EnvironmentAwareConfig
Create from a configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Application configuration dictionary |
required |
environment
|
str | None
|
Environment name, or None to auto-detect |
None
|
env_dir
|
str | Path
|
Directory containing environment configs |
'config/environments'
|
Returns:
| Type | Description |
|---|---|
EnvironmentAwareConfig
|
EnvironmentAwareConfig instance |
Source code in packages/config/src/dataknobs_config/environment_aware.py
get ¶
Get a value from the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key (supports dot notation for nested access) |
required |
default
|
Any
|
Default value if not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Configuration value |
Source code in packages/config/src/dataknobs_config/environment_aware.py
resolve_for_build ¶
resolve_for_build(
config_key: str | None = None,
resolve_resources: bool = True,
resolve_env_vars: bool = True,
) -> dict[str, Any]
Resolve configuration for object building.
This is the late-binding resolution point where: 1. Logical resource names are resolved to concrete configs 2. Environment variables are substituted 3. Final merged configuration is returned
Call this method immediately before instantiating objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_key
|
str | None
|
Specific config key to resolve, or None for root |
None
|
resolve_resources
|
bool
|
Whether to resolve logical resource refs |
True
|
resolve_env_vars
|
bool
|
Whether to substitute environment variables |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Fully resolved configuration dictionary |
Source code in packages/config/src/dataknobs_config/environment_aware.py
get_portable_config ¶
Get the portable (unresolved) configuration.
Returns the configuration with: - Logical resource references intact - Environment variables as placeholders
This is the config that should be stored in databases for cross-environment portability.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Unresolved configuration dictionary |
Source code in packages/config/src/dataknobs_config/environment_aware.py
to_dict ¶
Get the raw configuration dictionary.
Alias for get_portable_config().
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Configuration dictionary |
with_environment ¶
with_environment(
environment: str | EnvironmentConfig,
env_dir: str | Path = "config/environments",
) -> EnvironmentAwareConfig
Create a new instance with a different environment.
Useful for testing or multi-environment scenarios.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
str | EnvironmentConfig
|
Environment name or EnvironmentConfig instance |
required |
env_dir
|
str | Path
|
Directory containing environment configs (if name provided) |
'config/environments'
|
Returns:
| Type | Description |
|---|---|
EnvironmentAwareConfig
|
New EnvironmentAwareConfig with the specified environment |
Source code in packages/config/src/dataknobs_config/environment_aware.py
get_resource ¶
get_resource(
resource_type: str,
logical_name: str,
defaults: dict[str, Any] | None = None,
) -> dict[str, Any]
Get a resolved resource configuration.
Convenience method to directly access environment resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
logical_name
|
str
|
Logical name of resource |
required |
defaults
|
dict[str, Any] | None
|
Default values if resource not found |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Resolved resource configuration |
Source code in packages/config/src/dataknobs_config/environment_aware.py
get_setting ¶
Get an environment setting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Setting key |
required |
default
|
Any
|
Default value if not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Setting value |
Source code in packages/config/src/dataknobs_config/environment_aware.py
__repr__ ¶
EnvironmentAwareConfigError ¶
Bases: Exception
Error related to environment-aware configuration.
EnvironmentConfig
dataclass
¶
EnvironmentConfig(
name: str,
resources: dict[str, dict[str, dict[str, Any]]] = dict(),
settings: dict[str, Any] = dict(),
description: str = "",
)
Environment-specific configuration and resource bindings.
Manages the mapping from logical resource names to concrete implementations for a specific deployment environment.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Environment name (e.g., "development", "staging", "production") |
resources |
dict[str, dict[str, dict[str, Any]]]
|
Nested dict of {resource_type: {logical_name: config}} |
settings |
dict[str, Any]
|
Environment-wide settings (log levels, feature flags, etc.) |
description |
str
|
Optional description of the environment |
Methods:
| Name | Description |
|---|---|
detect_environment |
Detect current environment from env vars or indicators. |
load |
Load environment configuration from file. |
from_dict |
Create EnvironmentConfig from a dictionary. |
get_resource |
Get concrete config for a logical resource. |
has_resource |
Check if a resource exists. |
get_setting |
Get an environment-wide setting. |
get_resource_types |
Get all resource types in this environment. |
get_resource_names |
Get all resource names for a type. |
to_dict |
Convert to dictionary representation. |
merge |
Merge another environment config into this one. |
Functions¶
detect_environment
classmethod
¶
Detect current environment from env vars or indicators.
Checks in order: 1. DATAKNOBS_ENVIRONMENT env var 2. Common cloud indicators (AWS_EXECUTION_ENV, etc.) 3. Default to "development"
Returns:
| Type | Description |
|---|---|
str
|
Detected environment name (lowercase) |
Source code in packages/config/src/dataknobs_config/environment_config.py
load
classmethod
¶
load(
environment: str | None = None,
config_dir: str | Path = "config/environments",
) -> EnvironmentConfig
Load environment configuration from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
str | None
|
Environment name, or None to auto-detect |
None
|
config_dir
|
str | Path
|
Directory containing environment config files |
'config/environments'
|
Returns:
| Type | Description |
|---|---|
EnvironmentConfig
|
Loaded EnvironmentConfig instance |
Raises:
| Type | Description |
|---|---|
EnvironmentConfigError
|
If config file is invalid |
Source code in packages/config/src/dataknobs_config/environment_config.py
from_dict
classmethod
¶
Create EnvironmentConfig from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Configuration dictionary |
required |
Returns:
| Type | Description |
|---|---|
EnvironmentConfig
|
EnvironmentConfig instance |
Source code in packages/config/src/dataknobs_config/environment_config.py
get_resource ¶
get_resource(
resource_type: str,
logical_name: str,
defaults: dict[str, Any] | None = None,
) -> dict[str, Any]
Get concrete config for a logical resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource ("databases", "vector_stores", etc.) |
required |
logical_name
|
str
|
Logical name referenced in app config |
required |
defaults
|
dict[str, Any] | None
|
Default config values if resource not found |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Concrete configuration for the resource |
Raises:
| Type | Description |
|---|---|
ResourceNotFoundError
|
If resource not found and no defaults provided |
Source code in packages/config/src/dataknobs_config/environment_config.py
has_resource ¶
Check if a resource exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
logical_name
|
str
|
Logical name of resource |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if resource exists |
Source code in packages/config/src/dataknobs_config/environment_config.py
get_setting ¶
Get an environment-wide setting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Setting key |
required |
default
|
Any
|
Default value if not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Setting value |
Source code in packages/config/src/dataknobs_config/environment_config.py
get_resource_types ¶
Get all resource types in this environment.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of resource type names |
get_resource_names ¶
Get all resource names for a type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_type
|
str
|
Type of resource |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of logical resource names |
Source code in packages/config/src/dataknobs_config/environment_config.py
to_dict ¶
Convert to dictionary representation.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation of environment config |
Source code in packages/config/src/dataknobs_config/environment_config.py
merge ¶
Merge another environment config into this one.
The other config's values take precedence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
EnvironmentConfig
|
Environment config to merge |
required |
Returns:
| Type | Description |
|---|---|
EnvironmentConfig
|
New merged EnvironmentConfig |
Source code in packages/config/src/dataknobs_config/environment_config.py
EnvironmentConfigError ¶
Bases: Exception
Error related to environment configuration.
ResourceBinding
dataclass
¶
A binding from logical name to concrete implementation.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Logical name of the resource |
resource_type |
str
|
Type of resource (e.g., "databases", "vector_stores") |
config |
dict[str, Any]
|
Concrete configuration for the resource |
ResourceNotFoundError ¶
Bases: EnvironmentConfigError, KeyError
Resource not found in environment configuration.
ConfigNotFoundError ¶
ConfigNotFoundError(
message: str,
context: Dict[str, Any] | None = None,
details: Dict[str, Any] | None = None,
)
Bases: NotFoundError
Raised when a requested configuration is not found.
Source code in packages/common/src/dataknobs_common/exceptions.py
InvalidReferenceError ¶
InvalidReferenceError(
message: str,
context: Dict[str, Any] | None = None,
details: Dict[str, Any] | None = None,
)
Bases: ValidationError
Raised when a configuration reference is invalid.
Source code in packages/common/src/dataknobs_common/exceptions.py
InheritableConfigLoader ¶
Configuration loader with inheritance support.
Loads YAML/JSON configuration files with support for configuration
inheritance via an extends field. Child configurations override
parent values through deep merge.
Attributes:
| Name | Type | Description |
|---|---|---|
config_dir |
Directory containing configuration files |
|
cache |
Configuration cache for performance |
Initialize configuration loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_dir
|
str | Path | None
|
Directory containing configuration files. If None, uses ./configs |
None
|
Methods:
| Name | Description |
|---|---|
load |
Load and resolve configuration with inheritance. |
load_from_file |
Load configuration from a specific file path. |
clear_cache |
Clear configuration cache. |
list_available |
List all available configuration files. |
validate |
Validate a configuration file. |
Source code in packages/config/src/dataknobs_config/inheritance.py
Functions¶
load ¶
Load and resolve configuration with inheritance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name (without extension) |
required |
use_cache
|
bool
|
Whether to use cached configuration if available |
True
|
substitute_vars
|
bool
|
Whether to substitute environment variables |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Resolved configuration dictionary |
Raises:
| Type | Description |
|---|---|
InheritanceError
|
If config not found, cycle detected, or other error |
Source code in packages/config/src/dataknobs_config/inheritance.py
load_from_file ¶
Load configuration from a specific file path.
This method bypasses the config_dir and loads directly from the path. Inheritance is resolved relative to the file's directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to configuration file |
required |
substitute_vars
|
bool
|
Whether to substitute environment variables |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Resolved configuration dictionary |
Raises:
| Type | Description |
|---|---|
InheritanceError
|
If file not found or other error |
Source code in packages/config/src/dataknobs_config/inheritance.py
clear_cache ¶
Clear configuration cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Specific config to clear, or None to clear all |
None
|
Source code in packages/config/src/dataknobs_config/inheritance.py
list_available ¶
List all available configuration files.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of configuration names (without extensions) |
Source code in packages/config/src/dataknobs_config/inheritance.py
validate ¶
Validate a configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None]
|
Tuple of (is_valid, error_message) |
Source code in packages/config/src/dataknobs_config/inheritance.py
InheritanceError ¶
Bases: Exception
Error during configuration inheritance resolution.
VariableSubstitution ¶
Handles environment variable substitution in configuration values.
Supports patterns: - ${VAR} - Replace with environment variable VAR, error if not found - ${VAR:default} - Replace with VAR or use default if not found - ${VAR:-default} - Same as above (bash-style)
Methods:
| Name | Description |
|---|---|
substitute |
Recursively substitute environment variables in a value. |
has_variables |
Check if a value contains environment variable references. |
Functions¶
substitute ¶
Recursively substitute environment variables in a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to process (can be string, dict, list, or other) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Value with environment variables substituted |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a required environment variable is not found |
Source code in packages/config/src/dataknobs_config/substitution.py
has_variables ¶
Check if a value contains environment variable references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if value contains ${...} patterns |
Source code in packages/config/src/dataknobs_config/substitution.py
Functions¶
deep_merge ¶
Deep merge two dictionaries.
Recursively merges override into base, with override values taking precedence. Nested dictionaries are merged recursively; all other types are replaced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
dict[str, Any]
|
Base dictionary (values used when not overridden) |
required |
override
|
dict[str, Any]
|
Override dictionary (takes precedence) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
New merged dictionary |
Example
base = {"a": 1, "nested": {"x": 10, "y": 20}} override = {"a": 2, "nested": {"y": 25, "z": 30}} deep_merge(base, override) {'a': 2, 'nested': {'x': 10, 'y': 25, 'z': 30}}
Source code in packages/config/src/dataknobs_config/inheritance.py
load_config_with_inheritance ¶
load_config_with_inheritance(
filepath: str | Path, substitute_vars: bool = True
) -> dict[str, Any]
Convenience function to load a config file with inheritance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to configuration file |
required |
substitute_vars
|
bool
|
Whether to substitute environment variables |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Resolved configuration dictionary |
Source code in packages/config/src/dataknobs_config/inheritance.py
substitute_env_vars ¶
Recursively substitute environment variables in configuration.
Supports formats: - ${VAR_NAME}: Required variable, raises error if not set - ${VAR_NAME:default_value}: Optional with default
Substitution applies to both dict keys and values, list items, and top-level strings. Non-string dict keys (integers, booleans, etc.) pass through unchanged.
Also expands ~ in string keys and values after substitution using os.path.expanduser(), which leaves non-path strings (URLs, connection strings) intact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Configuration data (dict, list, string, or primitive) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Data with environment variables substituted |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required environment variable not set |
Example
os.environ["MY_VAR"] = "hello" substitute_env_vars({"key": "\({MY_VAR}", "default": "\))}"
Source code in packages/config/src/dataknobs_config/inheritance.py
substitute_template_vars ¶
substitute_template_vars(
data: Any,
variables: dict[str, Any],
*,
preserve_missing: bool = True,
type_cast: bool = True,
) -> Any
Recursively substitute {{var}} placeholders in configuration data.
Walks through nested dicts and lists, replacing {{var}} placeholders with values from the variables map. Supports type preservation for entire-value placeholders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Configuration data (dict, list, string, or primitive) |
required |
variables
|
dict[str, Any]
|
Dictionary of variable names to values |
required |
preserve_missing
|
bool
|
If True, leave {{var}} intact when var not in variables. If False, replace missing vars with empty string. |
True
|
type_cast
|
bool
|
If True, preserve Python types for entire-value placeholders. "{{count}}" with count=10 becomes int 10, not string "10". If False, always return strings for substituted values. |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
Data with template variables substituted |
Example
variables = {"name": "Math Tutor", "count": 10, "items": ["a", "b"]} data = {"title": "{{name}}", "max": "{{count}}", "desc": "Has {{count}} items"} substitute_template_vars(data, variables)
Type preservation for entire-value placeholders¶
substitute_template_vars({"list": "{{items}}"}, variables)
Missing variables preserved by default¶
substitute_template_vars("Hello {{name}}, {{unknown}}", {"name": "World"}) 'Hello World, {{unknown}}'