-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.py
More file actions
55 lines (41 loc) · 1.28 KB
/
settings.py
File metadata and controls
55 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Persistent app settings — stored in ~/.sampson/settings.json"""
import json
from pathlib import Path
_SETTINGS_DIR = Path.home() / ".sampson"
_SETTINGS_FILE = _SETTINGS_DIR / "settings.json"
_settings: dict = {}
_loaded = False
def _load():
global _settings, _loaded
if _loaded:
return
_loaded = True
try:
if _SETTINGS_FILE.exists():
_settings = json.loads(_SETTINGS_FILE.read_text(encoding="utf-8"))
except Exception:
_settings = {}
def _save():
try:
_SETTINGS_DIR.mkdir(parents=True, exist_ok=True)
_SETTINGS_FILE.write_text(json.dumps(_settings, indent=2), encoding="utf-8")
except Exception:
pass
def get_last_source() -> str | None:
"""Return last used source directory, or None if not set."""
_load()
return _settings.get("last_source")
def set_last_source(path: str) -> None:
"""Persist last used source directory."""
_load()
_settings["last_source"] = path
_save()
def get_last_dest() -> str | None:
"""Return last used destination directory, or None if not set."""
_load()
return _settings.get("last_dest")
def set_last_dest(path: str) -> None:
"""Persist last used destination directory."""
_load()
_settings["last_dest"] = path
_save()