VideoBackgroundReplacer2 / utils /progress_tracker.py
MogensR's picture
Create progress_tracker.py
f56676e verified
#!/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}