25 lines
577 B
Python
Raw Permalink Normal View History

2025-12-19 16:24:04 +08:00
# MIT License
# Copyright (c) 2024
"""DSL 的加载与保存。"""
from pathlib import Path
from typing import Any, Dict
import yaml
from .schema import DSLSpec
def save_dsl(spec: DSLSpec, path: Path) -> None:
"""保存 DSL 为 YAML。"""
with path.open("w", encoding="utf-8") as f:
yaml.safe_dump(spec.dict(), f, allow_unicode=True, sort_keys=False)
def load_dsl(path: Path) -> DSLSpec:
"""从 YAML 读取 DSL。"""
with path.open("r", encoding="utf-8") as f:
data: Dict[str, Any] = yaml.safe_load(f)
return DSLSpec.parse_obj(data)