Config Package API Reference¶
📖 Also see: Auto-generated API Reference - Complete documentation from source code docstrings
dataknobs_config.config ¶
Core Config class implementation.
Classes:
| Name | Description |
|---|---|
Config |
A modular configuration system for composable settings. |
Classes¶
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 |
|---|---|
build_object |
Build an object from a configuration reference. |
build_reference |
Build a string reference for a configuration. |
clear_object_cache |
Clear cached objects. |
from_dict |
Create a Config object from a dictionary. |
from_file |
Create a Config object from a file. |
get |
Get a configuration by type and name/index. |
get_count |
Get the count of configurations for a type. |
get_factory |
Get a factory instance for a configuration. |
get_instance |
Get an instance from a configuration. |
get_names |
Get all configuration names for a type. |
get_registered_factories |
Get all registered factories. |
get_types |
Get all configuration types. |
load |
Load configuration from a source. |
merge |
Merge another configuration into this one. |
register_factory |
Register a factory instance for use in configurations. |
resolve_reference |
Resolve a string reference to a configuration. |
set |
Set a configuration by type and name/index. |
to_dict |
Export configuration as a dictionary. |
to_file |
Save configuration to a file. |
unregister_factory |
Unregister a factory. |
Source code in packages/config/src/dataknobs_config/config.py
Functions¶
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
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
clear_object_cache ¶
Clear cached objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str | None
|
Specific reference to clear, or None to clear all |
None
|
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 |
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 |
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
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_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
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
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_registered_factories ¶
Get all registered factories.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary mapping factory names to factory instances |
get_types ¶
Get all configuration types.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of type names |
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
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
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
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
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
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
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
dataknobs_config.references ¶
String reference system for cross-referencing configurations.
Classes:
| Name | Description |
|---|---|
ReferenceResolver |
Handles parsing and resolution of string references. |
Classes¶
ReferenceResolver ¶
Handles parsing and resolution of string references.
Reference format: xref:
Examples:
- xref:foo[bar] - Named reference
- xref:foo[0] - Index reference
- xref:foo[-1] - Last item reference
- xref:foo - First/only item reference
Initialize the reference resolver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_instance
|
Config
|
The Config instance to resolve references against |
required |
Methods:
| Name | Description |
|---|---|
build |
Build a string reference for a configuration. |
is_reference |
Check if a value is a string reference. |
parse_reference |
Parse a string reference into its components. |
resolve |
Resolve a string reference to a configuration. |
Source code in packages/config/src/dataknobs_config/references.py
Functions¶
build ¶
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/references.py
is_reference ¶
Check if a value is a string reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if value is a string reference |
Source code in packages/config/src/dataknobs_config/references.py
parse_reference ¶
Parse a string reference into its components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str
|
String reference |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, Union[str, int]]
|
Tuple of (type_name, name_or_index) |
Raises:
| Type | Description |
|---|---|
InvalidReferenceError
|
If reference format is invalid |
Source code in packages/config/src/dataknobs_config/references.py
resolve ¶
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 |
Raises:
| Type | Description |
|---|---|
InvalidReferenceError
|
If reference format is invalid |
ConfigNotFoundError
|
If referenced configuration doesn't exist |
Source code in packages/config/src/dataknobs_config/references.py
dataknobs_config.environment ¶
Environment variable override system.
Classes:
| Name | Description |
|---|---|
EnvironmentOverrides |
Handles environment variable overrides for configurations. |
Classes¶
EnvironmentOverrides ¶
Handles environment variable overrides for configurations.
Environment variable format:
DATAKNOBS_
Examples:
- DATAKNOBS_FOO__BAR__PARAM -> xref:foo[bar].param
- DATAKNOBS_FOO__0__PARAM -> xref:foo[0].param
Initialize the environment override handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str | None
|
Custom environment variable prefix (default: DATAKNOBS_) |
None
|
Methods:
| Name | Description |
|---|---|
get_overrides |
Get all environment variable overrides. |
parse_env_reference |
Parse a reference string with attribute. |
reference_to_env_var |
Convert a reference string to an environment variable name. |
Source code in packages/config/src/dataknobs_config/environment.py
Functions¶
get_overrides ¶
Get all environment variable overrides.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary mapping references to override values |
Source code in packages/config/src/dataknobs_config/environment.py
parse_env_reference ¶
Parse a reference string with attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str
|
Reference string with attribute (e.g., "xref:foo[bar].param") |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, Union[str, int], str | None]
|
Tuple of (type_name, name_or_index, attribute) |
Source code in packages/config/src/dataknobs_config/environment.py
reference_to_env_var ¶
Convert a reference string to an environment variable name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str
|
Reference string (e.g., "xref:foo[bar]") |
required |
attribute
|
str | None
|
Attribute name (if not included in ref) |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Environment variable name |
Source code in packages/config/src/dataknobs_config/environment.py
dataknobs_config.settings ¶
Global settings and defaults management.
Classes:
| Name | Description |
|---|---|
SettingsManager |
Manages global settings, defaults, and path resolution. |
Classes¶
SettingsManager ¶
Manages global settings, defaults, and path resolution.
Settings attributes
- config_root: Root directory for configuration files
- global_root: Global root directory for all types
.global_root: Type-specific root directory - path_resolution_attributes: List of attributes to resolve as paths
. : Type-specific defaults : Global defaults
Initialize the settings manager.
Methods:
| Name | Description |
|---|---|
apply_defaults |
Apply default values to a configuration. |
get_setting |
Get a setting value. |
load_settings |
Load settings from a dictionary. |
resolve_paths |
Resolve relative paths in configuration. |
set_setting |
Set a setting value. |
to_dict |
Export settings as a dictionary. |
Source code in packages/config/src/dataknobs_config/settings.py
Functions¶
apply_defaults ¶
Apply default values to a configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Atomic configuration dictionary |
required |
type_name
|
str
|
Type of the configuration |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Configuration with defaults applied |
Source code in packages/config/src/dataknobs_config/settings.py
get_setting ¶
Get a setting value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Setting key |
required |
default
|
Any
|
Default value if not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Setting value or default |
Source code in packages/config/src/dataknobs_config/settings.py
load_settings ¶
Load settings from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
dict
|
Settings dictionary |
required |
Source code in packages/config/src/dataknobs_config/settings.py
resolve_paths ¶
Resolve relative paths in configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Atomic configuration dictionary |
required |
type_name
|
str
|
Type of the configuration |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Configuration with resolved paths |
Source code in packages/config/src/dataknobs_config/settings.py
set_setting ¶
Set a setting value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Setting key |
required |
value
|
Any
|
Setting value |
required |
to_dict ¶
Export settings as a dictionary.
Returns:
| Type | Description |
|---|---|
dict
|
Settings dictionary |
dataknobs_config.builders ¶
Optional object construction and caching functionality.
Classes:
| Name | Description |
|---|---|
ObjectBuilder |
Handles object construction from configurations. |
ConfigurableBase |
Base class for objects that can be configured. |
FactoryBase |
Base class for factory objects. |
Classes¶
ObjectBuilder ¶
Handles object construction from configurations.
Supports
- Direct class instantiation via 'class' attribute
- Factory pattern via 'factory' attribute
- Object caching
Initialize the object builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_instance
|
Config
|
The Config instance to build objects from |
required |
Methods:
| Name | Description |
|---|---|
build |
Build an object from a configuration reference. |
clear_cache |
Clear cached objects. |
get_cached |
Get a cached object without building. |
Source code in packages/config/src/dataknobs_config/builders.py
Functions¶
build ¶
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 |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If object cannot be built |
Source code in packages/config/src/dataknobs_config/builders.py
clear_cache ¶
Clear cached objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str | None
|
Specific reference to clear, or None to clear all |
None
|
Source code in packages/config/src/dataknobs_config/builders.py
get_cached ¶
Get a cached object without building.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
str
|
String reference |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
Cached object or None |
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. |
dataknobs_config.exceptions ¶
Custom exceptions for the config package.
This module defines exception types for the config package, built on the common exception framework from dataknobs_common.
Classes:
| Name | Description |
|---|---|
ConfigNotFoundError |
Raised when a requested configuration is not found. |
InvalidReferenceError |
Raised when a configuration reference is invalid. |
FileNotFoundError |
Raised when a referenced configuration file is not found. |
Attributes:
| Name | Type | Description |
|---|---|---|
ConfigError |
|
|
ValidationError |
|
Attributes¶
Classes¶
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
FileNotFoundError ¶
FileNotFoundError(
message: str,
context: Dict[str, Any] | None = None,
details: Dict[str, Any] | None = None,
)
Bases: NotFoundError
Raised when a referenced configuration file is not found.
Source code in packages/common/src/dataknobs_common/exceptions.py
Environment-Aware Configuration¶
dataknobs_config.environment_config ¶
Environment-specific configuration and resource bindings.
This module provides environment-aware configuration management for deploying the same application across different environments (development, staging, production) where infrastructure differs.
Key features: - Environment detection (via DATAKNOBS_ENVIRONMENT or cloud indicators) - Resource bindings (logical names -> concrete implementations) - Environment-wide settings management
Example
# Auto-detect environment
env = EnvironmentConfig.load()
# Or specify explicitly
env = EnvironmentConfig.load("production", config_dir="config/environments")
# Get concrete config for a logical resource
db_config = env.get_resource("databases", "conversations")
# Returns: {"backend": "postgres", "connection_string": "..."}
Environment file format (config/environments/production.yaml):
name: production
description: AWS production environment
settings:
log_level: INFO
enable_metrics: true
resources:
databases:
default:
backend: postgres
connection_string: ${DATABASE_URL}
conversations:
backend: postgres
connection_string: ${DATABASE_URL}
table: conversations
vector_stores:
default:
backend: pgvector
connection_string: ${DATABASE_URL}
Classes:
| Name | Description |
|---|---|
EnvironmentConfig |
Environment-specific configuration and resource bindings. |
ResourceBinding |
A binding from logical name to concrete implementation. |
EnvironmentConfigError |
Error related to environment configuration. |
ResourceNotFoundError |
Resource not found in environment configuration. |
Classes¶
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. |
from_dict |
Create EnvironmentConfig from a dictionary. |
get_resource |
Get concrete config for a logical resource. |
get_resource_names |
Get all resource names for a type. |
get_resource_types |
Get all resource types in this environment. |
get_setting |
Get an environment-wide setting. |
has_resource |
Check if a resource exists. |
load |
Load environment configuration from file. |
merge |
Merge another environment config into this one. |
to_dict |
Convert to dictionary representation. |
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
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
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
get_resource_types ¶
Get all resource types in this environment.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of resource type names |
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
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
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
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
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
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 |
EnvironmentConfigError ¶
Bases: Exception
Error related to environment configuration.
ResourceNotFoundError ¶
Bases: EnvironmentConfigError, KeyError
Resource not found in environment configuration.
dataknobs_config.environment_aware ¶
Environment-aware configuration with late-binding resource resolution.
This module provides the EnvironmentAwareConfig class that supports: - Logical resource references resolved per-environment - Late-binding of environment variables (at instantiation time, not load time) - Separation of portable app config from infrastructure bindings
Example
# Load with auto-detected environment
config = EnvironmentAwareConfig.load_app(
"my-bot",
app_dir="config/apps",
env_dir="config/environments"
)
# Get resolved config for object building (late binding happens here)
resolved = config.resolve_for_build()
# Get portable config for storage (no env vars resolved)
portable = config.get_portable_config()
App config format (config/apps/my-bot.yaml):
name: my-bot
version: "1.0.0"
bot:
llm:
$resource: default
type: llm_providers
temperature: 0.7
conversation_storage:
$resource: conversations
type: databases
Classes:
| Name | Description |
|---|---|
EnvironmentAwareConfig |
Configuration with environment-aware resource resolution. |
EnvironmentAwareConfigError |
Error related to environment-aware configuration. |
Classes¶
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 |
|---|---|
__repr__ |
String representation. |
from_dict |
Create from a configuration dictionary. |
get |
Get a value from the config. |
get_portable_config |
Get the portable (unresolved) configuration. |
get_resource |
Get a resolved resource configuration. |
get_setting |
Get an environment setting. |
load_app |
Load an application configuration with environment bindings. |
resolve_for_build |
Resolve configuration for object building. |
to_dict |
Get the raw configuration dictionary. |
with_environment |
Create a new instance with a different environment. |
Source code in packages/config/src/dataknobs_config/environment_aware.py
Attributes¶
Functions¶
__repr__ ¶
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
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
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
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
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
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
EnvironmentAwareConfigError ¶
Bases: Exception
Error related to environment-aware configuration.
dataknobs_config.binding_resolver ¶
Configuration binding resolver for resource instantiation.
This module provides the ConfigBindingResolver class that resolves logical resource references to concrete instances using registered factories.
Example
from dataknobs_config import EnvironmentConfig, ConfigBindingResolver
# Load environment config
env = EnvironmentConfig.load("production")
# Create resolver
resolver = ConfigBindingResolver(env)
# Register factories for resource types
resolver.register_factory("databases", DatabaseFactory())
resolver.register_factory("vector_stores", VectorStoreFactory())
# Resolve a logical reference to a concrete instance
db = resolver.resolve("databases", "conversations")
# Or with async factories
vector_store = await resolver.resolve_async("vector_stores", "knowledge")
Classes:
| Name | Description |
|---|---|
ConfigBindingResolver |
Resolves logical resource bindings to concrete instances. |
SimpleFactory |
Simple factory that creates instances of a class. |
CallableFactory |
Factory that wraps a callable. |
AsyncCallableFactory |
Factory that wraps an async callable. |
BindingResolverError |
Error during resource binding resolution. |
FactoryNotFoundError |
Factory not registered for resource type. |
Classes¶
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 |
|---|---|
cache_instance |
Manually cache an instance. |
clear_cache |
Clear cached resource instances. |
get_cached |
Get a cached instance if it exists. |
get_registered_types |
Get all registered resource types. |
has_factory |
Check if a factory is registered for a resource type. |
is_cached |
Check if an instance is cached. |
register_factory |
Register a factory for a resource type. |
resolve |
Resolve a logical resource reference to a concrete instance. |
resolve_async |
Async version of resolve for async factories. |
unregister_factory |
Unregister a factory for a resource type. |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
Attributes¶
Functions¶
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
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
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
get_registered_types ¶
Get all registered resource types.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of resource type names with registered factories |
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
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
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
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
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
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
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
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 |
Sync create is not supported for async factories. |
create_async |
Create an instance asynchronously. |
Source code in packages/config/src/dataknobs_config/binding_resolver.py
Functions¶
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
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
BindingResolverError ¶
Bases: Exception
Error during resource binding resolution.
FactoryNotFoundError ¶
Bases: BindingResolverError
Factory not registered for resource type.