State
provide.foundation.state
¶
TODO: Add module docstring.
Classes¶
ConfigManager
¶
Thread-safe manager for versioned configurations.
Provides atomic updates and change tracking for configurations.
Functions¶
add_change_listener
¶
add_change_listener(
name: str,
listener: Callable[
[ImmutableState, ImmutableState], None
],
) -> None
Add a change listener for a configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
listener
|
Callable[[ImmutableState, ImmutableState], None]
|
Function called when configuration changes |
required |
Source code in provide/foundation/state/config.py
clear_all
¶
get_config
¶
Get a configuration by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
Returns:
| Type | Description |
|---|---|
VersionedConfig | None
|
Configuration instance or None if not found |
Source code in provide/foundation/state/config.py
get_config_generation
¶
get_config_value
¶
Get a single configuration value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
key
|
str
|
Configuration key |
required |
default
|
Any
|
Default value if not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Configuration value or default |
Raises:
| Type | Description |
|---|---|
KeyError
|
If configuration not found |
Source code in provide/foundation/state/config.py
list_configs
¶
register_config
¶
Register a new configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
VersionedConfig
|
Configuration to register |
required |
Source code in provide/foundation/state/config.py
remove_change_listener
¶
remove_change_listener(
name: str,
listener: Callable[
[ImmutableState, ImmutableState], None
],
) -> None
Remove a change listener for a configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
listener
|
Callable[[ImmutableState, ImmutableState], None]
|
Listener function to remove |
required |
Source code in provide/foundation/state/config.py
reset_config
¶
Reset a configuration to its initial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
Source code in provide/foundation/state/config.py
set_config_value
¶
Set a single configuration value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
key
|
str
|
Configuration key |
required |
value
|
Any
|
Configuration value |
required |
Returns:
| Type | Description |
|---|---|
VersionedConfig
|
Updated configuration instance |
Raises:
| Type | Description |
|---|---|
KeyError
|
If configuration not found |
Source code in provide/foundation/state/config.py
update_config
¶
Update a configuration with new values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name |
required |
**updates
|
Any
|
Key-value pairs to update |
{}
|
Returns:
| Type | Description |
|---|---|
VersionedConfig
|
Updated configuration instance |
Raises:
| Type | Description |
|---|---|
KeyError
|
If configuration not found |
Source code in provide/foundation/state/config.py
ImmutableState
¶
Base class for immutable state objects.
All state in Foundation should inherit from this to ensure immutability and provide consistent state management.
Functions¶
with_changes
¶
Create a new state instance with the specified changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**changes
|
Any
|
Field updates to apply |
{}
|
Returns:
| Type | Description |
|---|---|
ImmutableState
|
New state instance with updated generation |
Source code in provide/foundation/state/base.py
StateMachine
¶
Bases: Generic[StateT, EventT], ABC
Abstract base class for state machines.
Provides thread-safe state transitions with guards and actions.
Source code in provide/foundation/state/base.py
Attributes¶
state_history
property
¶
Get the state transition history.
Functions¶
add_transition
¶
Add a state transition to the machine.
reset
abstractmethod
¶
transition
¶
Attempt to transition to a new state based on the event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
EventT
|
Event that triggers the transition |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if transition was successful, False otherwise |
Source code in provide/foundation/state/base.py
StateManager
¶
Thread-safe manager for immutable state objects.
Provides atomic updates and version tracking for state objects.
Attributes¶
Functions¶
add_observer
¶
Add a state change observer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Callable[[ImmutableState, ImmutableState], None]
|
Function called with (old_state, new_state) on changes |
required |
Source code in provide/foundation/state/base.py
remove_observer
¶
Remove a state change observer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
Callable[[ImmutableState, ImmutableState], None]
|
Observer function to remove |
required |
Source code in provide/foundation/state/base.py
replace_state
¶
Replace the entire state object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_state
|
ImmutableState
|
New state to set |
required |
Source code in provide/foundation/state/base.py
update_state
¶
Atomically update the state with the given changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**changes
|
Any
|
Field updates to apply |
{}
|
Returns:
| Type | Description |
|---|---|
ImmutableState
|
New state instance |
Source code in provide/foundation/state/base.py
VersionedConfig
¶
Bases: ImmutableState
Immutable configuration with generation tracking.
All Foundation configurations should inherit from this to ensure immutability and proper change tracking.
Functions¶
get
¶
merge
¶
Merge with another configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
VersionedConfig
|
Configuration to merge with |
required |
Returns:
| Type | Description |
|---|---|
VersionedConfig
|
New configuration instance with merged data |
Source code in provide/foundation/state/config.py
remove
¶
Create a new config with the specified key removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key to remove |
required |
Returns:
| Type | Description |
|---|---|
VersionedConfig
|
New configuration instance |
Source code in provide/foundation/state/config.py
set
¶
Create a new config with the specified key-value pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key |
required |
value
|
Any
|
Configuration value |
required |
Returns:
| Type | Description |
|---|---|
VersionedConfig
|
New configuration instance |
Source code in provide/foundation/state/config.py
update
¶
Create a new config with multiple updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
updates
|
dict[str, Any]
|
Dictionary of key-value pairs to update |
required |
Returns:
| Type | Description |
|---|---|
VersionedConfig
|
New configuration instance |
Source code in provide/foundation/state/config.py
with_changes
¶
Create a new state instance with the specified changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**changes
|
Any
|
Field updates to apply |
{}
|
Returns:
| Type | Description |
|---|---|
VersionedConfig
|
New state instance with updated generation |