-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
45 lines (35 loc) · 1.05 KB
/
Copy pathsettings.py
File metadata and controls
45 lines (35 loc) · 1.05 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
"""
settings.py — persistent application settings
Saves to ~/.flashscan_settings.json
"""
from __future__ import annotations
import json
import logging
from pathlib import Path
log = logging.getLogger(__name__)
SETTINGS_FILE = Path.home() / ".flashscan_settings.json"
DEFAULTS: dict = {
"lastPath": "",
"lastOutput": "",
"lastFormats": ["MD"],
"inclHidden": False,
"inclSystem": False,
"customExts": [],
}
def load() -> dict:
try:
if SETTINGS_FILE.exists():
data = json.loads(SETTINGS_FILE.read_text(encoding="utf-8"))
return {**DEFAULTS, **data}
except Exception as e:
log.warning("Cannot load settings from %s: %s", SETTINGS_FILE, e)
return dict(DEFAULTS)
def save(settings: dict) -> None:
try:
merged = {**load(), **settings}
SETTINGS_FILE.write_text(
json.dumps(merged, ensure_ascii=False, indent=2),
encoding="utf-8",
)
except Exception as e:
log.warning("Cannot save settings to %s: %s", SETTINGS_FILE, e)