Storage#
A persistent, dict-like key-value store for application preferences, backed by a JSON file and written through on every change.
For a task-oriented introduction — creating a store, autosave, JSON-only values, remembering app state — see the Preferences Store guide.
- class bootstack.store.Store(name='settings', *, path=None, app_name=None, autosave=True)#
Bases:
objectA persistent, dict-like key-value store for app preferences.
Construct one with a logical name and it lives at
<config>/<app>/<name>.json(the per-platform config directory —Library/Application Supporton macOS,%APPDATA%on Windows,$XDG_CONFIG_HOMEon Linux), or pass an explicitpath. Read withget()orstore[key], write withset()orstore[key] = value; by default every change is saved immediately with an atomic write, so a crash cannot corrupt the file.Values must be JSON-serializable (scalars, lists, dicts); anything else raises
SerializationError. Keys must be strings. A missing or unreadable file simply starts the store empty rather than raising.Storedoes not require a runningApp— it is plain file I/O. When noapp_nameis given it uses the active App’s name if there is one, otherwise'bootstack'.- Parameters:
name (str) – Logical store name; the file is
<name>.jsonunder the per-app config directory. Ignored whenpathis given.path (str | PathLike[str] | None) – An explicit file path, overriding
name/app_name.app_name (str | None) – Override the per-app config sub-directory. Defaults to the active App’s name, or
'bootstack'.autosave (bool) – When True (default), persist on every change. When False, changes stay in memory until
save()is called.
Example
`python store = Store("settings") app = bs.App(theme=store.get("theme", "light")) # later, when the user switches theme: store.set("theme", "bootstrap-dark") `- as_dict()#
Return a deep copy of the store’s contents as a plain dict.
- clear()#
Remove every key, persisting the now-empty store.
- delete(key)#
Remove
keyif present; a no-op when it is missing.
- get(key, default=None)#
Return the value for
key, ordefaultif it is not present.
- items()#
Return the
(key, value)pairs, as a list snapshot.
- keys()#
Return the keys, as a list snapshot.
- reload()#
Discard in-memory state and re-read the store from disk.
- save()#
Write the store to disk now, atomically.
Useful when
autosave=False, or to force a flush. Writes to a temporary file and renames it over the target so a partial write never replaces good data.- Raises:
OSError – If the directory cannot be created or the file written.
- set(key, value)#
Set
keytovalueand (by default) persist immediately.- Parameters:
- Raises:
TypeError – If
keyis not a string.SerializationError – If
valueis not JSON-serializable.
- setdefault(key, default=None)#
Return
key’s value, insertingdefaultfirst if it is missing.
- update(values=None, **kwargs)#
Merge values in, persisting once at the end.
Accepts a mapping, keyword arguments, or both (mirroring
dict.update), sostore.update(theme="dark")andstore.update({"theme": "dark"})are equivalent.
- values()#
Return the values, as a list snapshot.