-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
66 lines (50 loc) · 1.71 KB
/
Copy pathconfig_manager.py
File metadata and controls
66 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
64
65
66
"""
Gestión de configuraciones guardadas e historial de videos.
"""
import json
from datetime import datetime
from pathlib import Path
STORE = Path.home() / ".pv_videogen"
CONFIGS_DIR = STORE / "configs"
HISTORY_FILE = STORE / "history.json"
MAX_HISTORY = 20
def _ensure():
CONFIGS_DIR.mkdir(parents=True, exist_ok=True)
def save_config(name: str, settings: dict) -> None:
_ensure()
data = {"name": name, "saved_at": datetime.now().isoformat(), "settings": settings}
(CONFIGS_DIR / f"{name}.json").write_text(
json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8"
)
def load_config(name: str) -> dict:
path = CONFIGS_DIR / f"{name}.json"
if not path.exists():
return {}
return json.loads(path.read_text(encoding="utf-8")).get("settings", {})
def list_configs() -> list:
_ensure()
out = []
for p in sorted(CONFIGS_DIR.glob("*.json")):
try:
d = json.loads(p.read_text(encoding="utf-8"))
out.append({"name": d.get("name", p.stem), "saved_at": d.get("saved_at", ""), "stem": p.stem})
except Exception:
pass
return out
def delete_config(name: str) -> None:
(CONFIGS_DIR / f"{name}.json").unlink(missing_ok=True)
def add_to_history(entry: dict) -> None:
_ensure()
history = load_history()
history.insert(0, {**entry, "timestamp": datetime.now().isoformat()})
HISTORY_FILE.write_text(
json.dumps(history[:MAX_HISTORY], indent=2, ensure_ascii=False), encoding="utf-8"
)
def load_history() -> list:
_ensure()
if not HISTORY_FILE.exists():
return []
try:
return json.loads(HISTORY_FILE.read_text(encoding="utf-8"))
except Exception:
return []