Sanyam400 commited on
Commit
65b5b97
·
verified ·
1 Parent(s): 20f8605

Create config.py

Browse files
Files changed (1) hide show
  1. app/config.py +68 -0
app/config.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Persistent config — survives across requests within the same container session.
3
+ Falls back to env vars, then to saved JSON file.
4
+ """
5
+ import os, json
6
+ from pathlib import Path
7
+
8
+ CONFIG_FILE = Path(os.environ.get("HOME", "/home/user")) / ".praison_config.json"
9
+
10
+ _config: dict = {}
11
+
12
+ def _load():
13
+ global _config
14
+ # Start with env vars
15
+ _config = {
16
+ "longcat_api_key": os.environ.get("LONGCAT_API_KEY", ""),
17
+ "longcat_model": os.environ.get("LONGCAT_MODEL", "LongCat-Flash-Lite"),
18
+ "telegram_token": os.environ.get("TELEGRAM_BOT_TOKEN", ""),
19
+ }
20
+ # Override with saved file if it exists
21
+ if CONFIG_FILE.exists():
22
+ try:
23
+ saved = json.loads(CONFIG_FILE.read_text())
24
+ for k, v in saved.items():
25
+ if v: # only override if non-empty
26
+ _config[k] = v
27
+ except Exception:
28
+ pass
29
+
30
+ def _save():
31
+ try:
32
+ CONFIG_FILE.write_text(json.dumps(_config, indent=2))
33
+ except Exception:
34
+ pass
35
+
36
+ # Load on import
37
+ _load()
38
+
39
+ def get(key: str, default: str = "") -> str:
40
+ return _config.get(key, default) or default
41
+
42
+ def set(key: str, value: str):
43
+ _config[key] = value
44
+ _save()
45
+
46
+ def get_longcat_key() -> str:
47
+ return get("longcat_api_key")
48
+
49
+ def get_model() -> str:
50
+ return get("longcat_model", "LongCat-Flash-Lite")
51
+
52
+ def get_telegram_token() -> str:
53
+ return get("telegram_token")
54
+
55
+ def set_longcat_key(key: str):
56
+ set("longcat_api_key", key)
57
+ os.environ["LONGCAT_API_KEY"] = key
58
+
59
+ def set_telegram_token(token: str):
60
+ set("telegram_token", token)
61
+ os.environ["TELEGRAM_BOT_TOKEN"] = token
62
+
63
+ def set_model(model: str):
64
+ set("longcat_model", model)
65
+
66
+ def all_config() -> dict:
67
+ return {k: ("***" if "key" in k or "token" in k else v)
68
+ for k, v in _config.items()}