-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
73 lines (52 loc) · 1.94 KB
/
Copy pathconfig.py
File metadata and controls
73 lines (52 loc) · 1.94 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
config.py
Manages persistent app configuration stored in config.json.
The archive password is kept in memory only — never written to disk.
"""
import json
import os
CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
_defaults = {
"watch_folders": [os.path.expanduser("~/Downloads")],
"archive_path": "", # path to the user's existing encrypted ZIP
}
_config: dict = {}
# Archive password is intentionally NOT persisted to disk
_archive_password: str = ""
def load():
print(f"[config] Loading config from {CONFIG_FILE}...")
"""Load config from disk. Falls back to defaults if file doesn't exist."""
global _config
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
_config = json.load(f)
except Exception:
_config = dict(_defaults)
else:
_config = dict(_defaults)
def save():
"""Persist config to disk (password is never included)."""
try:
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(_config, f, indent=2)
except Exception as e:
print(f"[config] Could not save config: {e}")
def get(key: str, default=None):
return _config.get(key, _defaults.get(key, default))
def get_watch_folders() -> list:
"""Return the list of watch folders, migrating legacy single-folder config if needed."""
# Migrate old single watch_folder key to list
if "watch_folder" in _config and "watch_folders" not in _config:
_config["watch_folders"] = [_config.pop("watch_folder")]
folders = _config.get("watch_folders", _defaults["watch_folders"])
return folders if isinstance(folders, list) else [folders]
def set(key: str, value):
_config[key] = value
def get_archive_password() -> str:
return _archive_password
def set_archive_password(pw: str):
global _archive_password
_archive_password = pw
# Load on import
load()