State Persistence
pamiq_core.state_persistence.PersistentStateMixin ¶
Mixin class for objects with persistable state.
This mixin provides the ability to save and load state. Classes that
inherit from this mixin must implement save_state()
and
load_state()
.
save_state ¶
pamiq_core.state_persistence.StateStore ¶
Class to save and load multiple persistable objects at once.
This class saves the state of each registered object to the specified directory. It is also possible to restore the state from the directory.
PARAMETER | DESCRIPTION |
---|---|
states_dir
|
Root path to the directory where states are saved
TYPE:
|
state_name_format
|
Format for the subdirectory name (defaults to timestamp)
TYPE:
|
Source code in src/pamiq_core/state_persistence.py
register ¶
Register a persistable object with a unique name.
PARAMETER | DESCRIPTION |
---|---|
name
|
Unique name to identify the state
TYPE:
|
state
|
Object implementing PersistentStateMixin
TYPE:
|
RAISES | DESCRIPTION |
---|---|
KeyError
|
If |
Source code in src/pamiq_core/state_persistence.py
save_state ¶
Save the all states of registered objects.
RETURNS | DESCRIPTION |
---|---|
Path
|
Path to the directory where the states are saved
TYPE:
|
RAISES | DESCRIPTION |
---|---|
FileExistsError
|
If the directory ( |
Source code in src/pamiq_core/state_persistence.py
load_state ¶
Restores the state from the state_path
directory.
PARAMETER | DESCRIPTION |
---|---|
state_path
|
Path to the directory where the state is saved
TYPE:
|
RAISES | DESCRIPTION |
---|---|
FileNotFoundError
|
If the specified path does not exist |
Source code in src/pamiq_core/state_persistence.py
pamiq_core.state_persistence.LatestStatesKeeper ¶
LatestStatesKeeper(
states_dir: str | Path,
state_name_pattern: str = "*.state",
max_keep: int = 10,
cleanup_interval: float = 60.0,
)
Keeps a fixed number of state directories by removing older ones.
This class monitors and manages the number of state directories in a specified path, ensuring only the most recent ones (up to max_keep) are retained, preventing disk space exhaustion.
Initialize the state keeper.
PARAMETER | DESCRIPTION |
---|---|
states_dir
|
Directory where states are stored.
TYPE:
|
state_name_pattern
|
Pattern to match state directories.
TYPE:
|
max_keep
|
Maximum number of state directories to keep.
TYPE:
|
cleanup_interval
|
Interval for executing cleanup process.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If cleanup interval is negative. |
Source code in src/pamiq_core/state_persistence.py
start ¶
Start the background cleanup thread if not already running.
PARAMETER | DESCRIPTION |
---|---|
background
|
Whether to run in background thread.
TYPE:
|
Source code in src/pamiq_core/state_persistence.py
stop ¶
Stop the background cleanup thread if running.
Source code in src/pamiq_core/state_persistence.py
cleanup ¶
Clean up old state directories, keeping only the most recent ones.
RETURNS | DESCRIPTION |
---|---|
list[Path]
|
List of removed state directory paths. |