StateStorage API
StateStorage - Persistent state storage for Qtile.
This module provides a simple key-value storage that persists across Qtile config reloads and restarts. Data is stored as JSON in the Qtile cache directory.
Features: - Automatic loading/saving of state - Thread-safe operations - Support for primitive types (str, int, float, bool, list, dict) - Namespace support for organizing state - Hook integration for automatic save on config reload
- Usage:
from qtile_expanded.storage import StateStorage
# Create a storage instance storage = StateStorage(“my_app”)
# Set values storage.set(“counter”, 42) storage.set(“settings”, {“theme”: “dark”, “notifications”: True})
# Get values counter = storage.get(“counter”, default=0) settings = storage.get(“settings”, default={})
# Delete values storage.delete(“counter”)
# Save explicitly storage.save()
# Use as context manager (auto-saves) with storage:
storage.set(“temporary”, “value”)
- class qtile_expanded.storage.StateStorage(namespace: str, cache_dir: str | Path | None = None, auto_save: bool = True)[source]
Bases:
objectPersistent key-value storage for Qtile that survives config reloads.
Data is stored as JSON in ~/.cache/qtile/qtile_expanded/{namespace}.json
- Args:
namespace: Unique identifier for this storage (e.g., “my_widget”) cache_dir: Override the Qtile cache directory (default: ~/.cache/qtile) auto_save: Whether to automatically save after each write (default: True)
- Attributes:
data: The loaded state dictionary
- __enter__() StateStorage[source]
Context manager entry.
- __init__(namespace: str, cache_dir: str | Path | None = None, auto_save: bool = True)[source]
Initialize StateStorage with a namespace.
- decrement(key: str, amount: int = 1) int[source]
Decrement a numeric value in the storage.
- Args:
key: The key to decrement amount: The amount to decrement by (default: 1)
- Returns:
The new value
- delete(key: str) bool[source]
Delete a value from the storage.
- Args:
key: The key to delete
- Returns:
True if the key existed and was deleted, False otherwise
- get(key: str, default: Any | None = None) Any[source]
Get a value from the storage.
- Args:
key: The key to retrieve default: Default value if key doesn’t exist
- Returns:
The stored value, or default if not found
- classmethod get_instance(namespace: str, **kwargs) StateStorage[source]
Get or create a StateStorage instance with the given namespace.
This ensures only one instance per namespace exists.
- Args:
namespace: The storage namespace **kwargs: Additional arguments passed to StateStorage constructor
- Returns:
The StateStorage instance
- increment(key: str, amount: int = 1) int[source]
Increment a numeric value in the storage.
- Args:
key: The key to increment amount: The amount to increment by (default: 1)
- Returns:
The new value
- set(key: str, value: Any) None[source]
Set a value in the storage.
- Args:
key: The key to store the value under value: The value to store (must be JSON-serializable)
- toggle(key: str, default: bool = False) bool[source]
Toggle a boolean value in the storage.
- Args:
key: The key to toggle default: Default value if key doesn’t exist
- Returns:
The new value
- qtile_expanded.storage.setup_qtile_hooks(qtile) None[source]
Set up Qtile hooks to automatically save state before reload/shutdown.
This function should be called from your Qtile config:
from qtile_expanded.storage import setup_qtile_hooks
- def main(q):
setup_qtile_hooks(q) # … rest of your config
- Args:
qtile: The Qtile instance (usually ‘q’ in config.py)
Classes
StateStorage
- class qtile_expanded.storage.StateStorage(namespace: str, cache_dir: str | Path | None = None, auto_save: bool = True)[source]
Bases:
objectPersistent key-value storage for Qtile that survives config reloads.
Data is stored as JSON in ~/.cache/qtile/qtile_expanded/{namespace}.json
- Args:
namespace: Unique identifier for this storage (e.g., “my_widget”) cache_dir: Override the Qtile cache directory (default: ~/.cache/qtile) auto_save: Whether to automatically save after each write (default: True)
- Attributes:
data: The loaded state dictionary
- __enter__() StateStorage[source]
Context manager entry.
- __init__(namespace: str, cache_dir: str | Path | None = None, auto_save: bool = True)[source]
Initialize StateStorage with a namespace.
- decrement(key: str, amount: int = 1) int[source]
Decrement a numeric value in the storage.
- Args:
key: The key to decrement amount: The amount to decrement by (default: 1)
- Returns:
The new value
- delete(key: str) bool[source]
Delete a value from the storage.
- Args:
key: The key to delete
- Returns:
True if the key existed and was deleted, False otherwise
- get(key: str, default: Any | None = None) Any[source]
Get a value from the storage.
- Args:
key: The key to retrieve default: Default value if key doesn’t exist
- Returns:
The stored value, or default if not found
- classmethod get_instance(namespace: str, **kwargs) StateStorage[source]
Get or create a StateStorage instance with the given namespace.
This ensures only one instance per namespace exists.
- Args:
namespace: The storage namespace **kwargs: Additional arguments passed to StateStorage constructor
- Returns:
The StateStorage instance
- increment(key: str, amount: int = 1) int[source]
Increment a numeric value in the storage.
- Args:
key: The key to increment amount: The amount to increment by (default: 1)
- Returns:
The new value
- set(key: str, value: Any) None[source]
Set a value in the storage.
- Args:
key: The key to store the value under value: The value to store (must be JSON-serializable)
- toggle(key: str, default: bool = False) bool[source]
Toggle a boolean value in the storage.
- Args:
key: The key to toggle default: Default value if key doesn’t exist
- Returns:
The new value
Functions
- qtile_expanded.storage.setup_qtile_hooks(qtile) None[source]
Set up Qtile hooks to automatically save state before reload/shutdown.
This function should be called from your Qtile config:
from qtile_expanded.storage import setup_qtile_hooks
- def main(q):
setup_qtile_hooks(q) # … rest of your config
- Args:
qtile: The Qtile instance (usually ‘q’ in config.py)