File size: 2,263 Bytes
f56676e |
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 74 75 |
#!/usr/bin/env python3
"""
Progress tracking via file-based status updates
Allows background processing while UI stays responsive
"""
import json
import os
from pathlib import Path
from typing import Optional
PROGRESS_FILE = "/tmp/processing_status.json"
def init_progress():
"""Initialize progress tracking"""
status = {
"active": False,
"message": "",
"progress": 0,
"stage": "",
"error": None,
"complete": False
}
with open(PROGRESS_FILE, "w") as f:
json.dump(status, f)
def update_progress(message: str, progress: int = None, stage: str = None):
"""Update progress status"""
try:
if os.path.exists(PROGRESS_FILE):
with open(PROGRESS_FILE, "r") as f:
status = json.load(f)
else:
status = {"active": True, "message": "", "progress": 0, "stage": "", "error": None, "complete": False}
status["message"] = message
status["active"] = True
if progress is not None:
status["progress"] = progress
if stage is not None:
status["stage"] = stage
with open(PROGRESS_FILE, "w") as f:
json.dump(status, f)
except Exception:
pass # Fail silently to not interrupt processing
def mark_complete(success: bool = True, error: str = None):
"""Mark processing as complete"""
try:
if os.path.exists(PROGRESS_FILE):
with open(PROGRESS_FILE, "r") as f:
status = json.load(f)
else:
status = {}
status["active"] = False
status["complete"] = True
status["progress"] = 100 if success else status.get("progress", 0)
if error:
status["error"] = error
with open(PROGRESS_FILE, "w") as f:
json.dump(status, f)
except Exception:
pass
def get_progress() -> dict:
"""Read current progress status"""
try:
if os.path.exists(PROGRESS_FILE):
with open(PROGRESS_FILE, "r") as f:
return json.load(f)
except Exception:
pass
return {"active": False, "message": "", "progress": 0, "stage": "", "error": None, "complete": False} |