Files
2026-04-14 10:36:33 +09:00

98 lines
2.7 KiB
Python

"""Git-managed Auto Pipeline preset catalog loading and merge helpers."""
from __future__ import annotations
import json
import logging
import os
from pathlib import Path
from typing import Any
log = logging.getLogger(__name__)
_DEFAULT_CATALOG_PATH = (
Path(__file__).resolve().parents[1]
/ "presets"
/ "auto_pipeline"
/ "default_presets.json"
)
def _normalize_preset(raw: Any) -> dict[str, Any] | None:
if not isinstance(raw, dict):
return None
name = str(raw.get("name") or "").strip()
model = str(raw.get("model") or "").strip()
fmt = str(raw.get("format") or "").strip()
if not name or not model or not fmt:
return None
inst_raw = raw.get("instrumentModels")
instrument_models: dict[str, str] = {}
if isinstance(inst_raw, dict):
for k, v in inst_raw.items():
if isinstance(k, str) and isinstance(v, str) and v.strip():
instrument_models[k] = v.strip()
steps_raw = raw.get("steps")
steps: list[dict[str, Any]] = []
if isinstance(steps_raw, list):
for step in steps_raw:
if isinstance(step, dict) and step.get("type"):
steps.append(step)
return {
"name": name,
"model": model,
"format": fmt,
"instrumentModels": instrument_models,
"steps": steps,
}
def normalize_auto_presets(raw: Any) -> list[dict[str, Any]]:
if not isinstance(raw, list):
return []
out: list[dict[str, Any]] = []
for item in raw:
preset = _normalize_preset(item)
if preset:
out.append(preset)
return out
def load_builtin_auto_presets() -> list[dict[str, Any]]:
path_raw = os.environ.get("AUTO_PRESETS_CATALOG_PATH", "").strip()
path = Path(path_raw) if path_raw else _DEFAULT_CATALOG_PATH
if not path.exists():
log.info("Auto preset catalog not found at %s; continuing with no built-ins", path)
return []
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except Exception as exc:
log.warning("Failed to parse auto preset catalog %s: %s", path, exc)
return []
presets = normalize_auto_presets(payload)
if not presets:
log.warning("Auto preset catalog %s loaded but produced no valid presets", path)
return presets
def merge_auto_presets(
builtin_presets: list[dict[str, Any]],
user_presets: list[dict[str, Any]],
) -> list[dict[str, Any]]:
merged: dict[str, dict[str, Any]] = {}
for preset in normalize_auto_presets(builtin_presets):
merged[preset["name"]] = preset
for preset in normalize_auto_presets(user_presets):
merged[preset["name"]] = preset
return list(merged.values())