-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpreferences.py
More file actions
63 lines (50 loc) · 1.71 KB
/
preferences.py
File metadata and controls
63 lines (50 loc) · 1.71 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
"""Simple JSON-file preferences for global settings."""
import json
from pathlib import Path
PREFS_PATH = Path("preferences.json")
DEFAULTS = {
"default_vocabulary": "",
"speaker_profiles_enabled": True,
"hf_auth_token": "",
"openrouter_api_key": "",
}
# Keys that should never be sent to the frontend in full
_SECRET_KEYS = {"hf_auth_token", "openrouter_api_key"}
def load_preferences() -> dict:
if PREFS_PATH.exists():
try:
with open(PREFS_PATH) as f:
data = json.load(f)
# Merge with defaults for any missing keys
return {**DEFAULTS, **data}
except (json.JSONDecodeError, OSError):
pass
return dict(DEFAULTS)
def save_preferences(prefs: dict):
with open(PREFS_PATH, "w") as f:
json.dump(prefs, f, indent=2, ensure_ascii=False)
def get_public_preferences() -> dict:
"""Return preferences with secrets masked for the frontend."""
prefs = load_preferences()
result = {}
for k, v in prefs.items():
if k in _SECRET_KEYS:
result[k] = _mask(v) if v else ""
else:
result[k] = v
return result
def _mask(value: str) -> str:
"""Show first 4 and last 4 chars of a secret."""
if len(value) <= 10:
return "*" * len(value)
return value[:4] + "*" * (len(value) - 8) + value[-4:]
def get_secret(key: str) -> str:
"""Get a secret value, checking preferences then env vars."""
import os
# Environment variable takes precedence
env_val = os.environ.get(key.upper(), "")
if env_val and env_val != f"hf_your_token_here":
return env_val
# Fall back to preferences
prefs = load_preferences()
return prefs.get(key, "")