94 lines
2.9 KiB
Python
Executable File
94 lines
2.9 KiB
Python
Executable File
"""
|
|
Project metadata file service.
|
|
|
|
Writes/reads a project.json snapshot in each project root folder.
|
|
This file mirrors the DB state and can be used for browsing, repair,
|
|
and offline project identification without a database query.
|
|
"""
|
|
import json
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
|
|
_METADATA_FILENAME = "project.json"
|
|
|
|
|
|
def write_project_metadata(
|
|
project_dir: Path,
|
|
*,
|
|
project_id: str,
|
|
name: str,
|
|
artist: str = "",
|
|
title: str = "",
|
|
model: str = "",
|
|
model_label: str = "",
|
|
format: str = "",
|
|
format_label: str = "",
|
|
source_file: str = "",
|
|
stems: dict,
|
|
sep_job_id: str = "",
|
|
trans_jobs: Optional[dict] = None,
|
|
abc_jobs: Optional[dict] = None,
|
|
lrc_jobs: Optional[dict] = None,
|
|
mix_job_id: Optional[str] = None,
|
|
created_at: Optional[str] = None,
|
|
) -> None:
|
|
"""
|
|
Write (or overwrite) project.json in *project_dir*.
|
|
|
|
If the file already exists its contents are merged so that keys not
|
|
supplied by the caller (e.g. trans_jobs added later) are preserved.
|
|
"""
|
|
meta_path = project_dir / _METADATA_FILENAME
|
|
|
|
# Load existing data so we can merge incremental updates
|
|
existing: dict = {}
|
|
if meta_path.exists():
|
|
try:
|
|
existing = json.loads(meta_path.read_text(encoding="utf-8"))
|
|
except Exception:
|
|
existing = {}
|
|
|
|
now = datetime.now(timezone.utc).isoformat()
|
|
|
|
updated = {
|
|
**existing,
|
|
"project_id": project_id,
|
|
"name": name,
|
|
"artist": artist,
|
|
"title": title,
|
|
"model": model,
|
|
"model_label": model_label,
|
|
"format": format,
|
|
"format_label": format_label,
|
|
"created_at": created_at or existing.get("created_at", now),
|
|
"updated_at": now,
|
|
"source_file": source_file or existing.get("source_file", ""),
|
|
"stems": stems if stems is not None else existing.get("stems", {}),
|
|
"sep_job_id": sep_job_id or existing.get("sep_job_id", ""),
|
|
"trans_jobs": trans_jobs if trans_jobs is not None else existing.get("trans_jobs", {}),
|
|
"abc_jobs": abc_jobs if abc_jobs is not None else existing.get("abc_jobs", {}),
|
|
"lrc_jobs": lrc_jobs if lrc_jobs is not None else existing.get("lrc_jobs", {}),
|
|
"mix_job_id": mix_job_id if mix_job_id is not None else existing.get("mix_job_id"),
|
|
}
|
|
|
|
meta_path.write_text(
|
|
json.dumps(updated, indent=2, ensure_ascii=False),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
|
|
def read_project_metadata(project_dir: Path) -> Optional[dict]:
|
|
"""
|
|
Read project.json from *project_dir*.
|
|
Returns None if the file does not exist or cannot be parsed.
|
|
"""
|
|
meta_path = project_dir / _METADATA_FILENAME
|
|
if not meta_path.exists():
|
|
return None
|
|
try:
|
|
return json.loads(meta_path.read_text(encoding="utf-8"))
|
|
except Exception:
|
|
return None
|