Files
2026-04-21 16:24:11 +09:00

118 lines
4.0 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,
instrument_models: Optional[dict] = None,
audio_features: Optional[dict] = None,
preset_name: 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"),
"instrument_models": instrument_models if instrument_models is not None else existing.get("instrument_models", {}),
"audio_features": audio_features if audio_features is not None else existing.get("audio_features"),
"preset_name": preset_name if preset_name is not None else existing.get("preset_name", ""),
}
meta_path.write_text(
json.dumps(updated, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def patch_project_metadata_audio_features(project_dir: Path, audio_features: dict) -> None:
"""
Update (or add) the ``audio_features`` key in an existing project.json
without touching any other field. Safe to call even if the file does not
yet exist (silently no-ops in that case).
"""
meta_path = project_dir / _METADATA_FILENAME
if not meta_path.exists():
return
try:
existing = json.loads(meta_path.read_text(encoding="utf-8"))
existing["audio_features"] = audio_features
existing["updated_at"] = datetime.now(timezone.utc).isoformat()
meta_path.write_text(json.dumps(existing, indent=2, ensure_ascii=False), encoding="utf-8")
except Exception:
pass
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