44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import json
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from config import STATE_FILE
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_DEFAULTS = {
|
|
"status": "idle",
|
|
"last_check": None,
|
|
"available_updates": [],
|
|
"pending_restart": [],
|
|
"installed_history": [],
|
|
"errors": [],
|
|
}
|
|
|
|
|
|
def load() -> dict:
|
|
if not STATE_FILE.exists():
|
|
return dict(_DEFAULTS)
|
|
try:
|
|
with open(STATE_FILE) as f:
|
|
data = json.load(f)
|
|
for k, v in _DEFAULTS.items():
|
|
data.setdefault(k, v)
|
|
return data
|
|
except Exception as e:
|
|
logger.error(f"Impossible de charger l'état : {e}")
|
|
return dict(_DEFAULTS)
|
|
|
|
|
|
def save(state: dict) -> None:
|
|
STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
try:
|
|
tmp = STATE_FILE.with_suffix(".tmp")
|
|
with open(tmp, "w") as f:
|
|
json.dump(state, f, indent=2, default=str)
|
|
tmp.replace(STATE_FILE)
|
|
os.chmod(STATE_FILE, 0o644) # Lisible par tous pour l'interface utilisateur
|
|
except Exception as e:
|
|
logger.error(f"Impossible de sauvegarder l'état : {e}")
|