34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
"""registry.yaml 读写。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from skiff import yaml_io
|
|
from skiff.paths import REGISTRY_FILE
|
|
|
|
|
|
def load_registry(path: Path | None = None) -> dict[str, dict[str, Any]]:
|
|
path = path or REGISTRY_FILE
|
|
if not path.is_file():
|
|
return {}
|
|
data = yaml_io.safe_load(path.read_text(encoding="utf-8")) or {}
|
|
if not isinstance(data, dict):
|
|
raise SystemExit(f"registry 格式错误: {path}")
|
|
return {k: v for k, v in data.items() if isinstance(v, dict) and not k.startswith("#")}
|
|
|
|
|
|
def save_registry(data: dict[str, dict[str, Any]], path: Path | None = None) -> None:
|
|
path = path or REGISTRY_FILE
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(yaml_io.safe_dump(data, allow_unicode=True, sort_keys=False), encoding="utf-8")
|
|
|
|
|
|
def external_skill_path(name: str, entry: dict[str, Any] | None = None) -> Path:
|
|
from skiff.paths import EXTERNALS_DIR
|
|
|
|
entry = entry or load_registry().get(name, {})
|
|
subpath = entry.get("path", ".") or "."
|
|
return (EXTERNALS_DIR / name / subpath).resolve()
|