Settings Manager¶
settings
¶
Settings manager — single source of truth for all application settings.
The :class:SettingsManager owns the authoritative in-memory settings dict
and is the only code that reads from / writes to the settings JSON file on
disk. Both the GUI settings page and every other module talk to this object;
they never touch the file directly.
Sync model¶
- GUI widget changes a value → calls :meth:
set/ :meth:set_nested. - :meth:
setupdates in-memory dict and calls :meth:save. - :meth:
savewrites JSON atomically with owner-only (0o600) permissions. - Any registered change listener is notified so other components
(e.g. :class:
~src.ai_assistant.AIAssistant) can react immediately.
The JSON file mirrors the same structure as config.yaml (all keys,
nested dicts). On load it is deep-merged over the compiled-in defaults
from :mod:src.config so missing keys always have sane values.
SettingsManager
¶
Thread-safe settings manager with JSON persistence and change listeners.
Parameters¶
path:
Path to the settings.json file. Created (with secure permissions)
if it does not exist. Pass None to disable persistence (useful in
tests).
defaults:
Base default values deep-merged before the file is loaded. Typically
the _DEFAULTS dict from :mod:src.config.
Source code in src/settings.py
save
¶
Persist current settings to JSON with owner-only permissions.
Writes are atomic (temp-file + rename) via :func:~src.security.secure_write.
Safe to call from any thread.
Source code in src/settings.py
reload
¶
Reload settings from disk, re-merging over defaults.
Useful when the config file has been edited externally.
get
¶
Return the value at dot-separated key_path, or default.
Example::
settings.get("ollama.model") # "llava"
settings.get("tools.unload_after_use") # False
Source code in src/settings.py
get_section
¶
Return a copy of a top-level section dict.
Returns an empty dict if the section does not exist.
Source code in src/settings.py
all
¶
set
¶
Set value at dot-separated key_path and optionally persist.
Notifies all registered change listeners synchronously before returning.
Parameters¶
key_path:
Dot-separated path, e.g. "ollama.model" or "tools.allowed".
value:
New value (any JSON-serialisable type).
save:
Write to disk immediately (default: True). Set to False
when making several changes in a batch and calling :meth:save
manually afterwards.
Source code in src/settings.py
set_many
¶
Apply multiple changes atomically and save once.
Parameters¶
changes: Dict mapping dot-separated key paths to new values.
Example::
settings.set_many({
"backend": "ollama",
"ollama.model": "llama3:8b-q4_K_M",
"tools.unload_after_use": True,
})
Source code in src/settings.py
update_section
¶
Deep-merge values into an existing top-level section and save.
Parameters¶
section:
Top-level key (e.g. "tools", "ollama").
values:
Dict of new values to merge in.
Source code in src/settings.py
add_listener
¶
Register listener to be called whenever a setting changes.
The listener receives (key_path: str, new_value: Any). It is
called from whatever thread called :meth:set, so listeners must be
thread-safe.
Source code in src/settings.py
remove_listener
¶
to_config
¶
Return a :class:~src.config.Config built from the current settings.
Useful for reconstructing the Config object after the user changes
settings in the GUI.