AQIT 0.1.0
Loading...
Searching...
No Matches
load.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6"""Load Recipe YAML (or JSON) and resolve paths relative to the file."""
7
8from __future__ import annotations
9
10import json
11from pathlib import Path
12from typing import Any
13
14from aquin.recipe.schema import Recipe, RecipeError
15
16
17def _parse(path: Path) -> dict[str, Any]:
18 raw = path.read_text(encoding="utf-8")
19 if path.suffix.lower() == ".json":
20 data = json.loads(raw)
21 else:
22 try:
23 import yaml
24 except ImportError as exc:
25 raise RecipeError(
26 "YAML recipes need PyYAML (`pip install pyyaml`) or use a .json recipe."
27 ) from exc
28 data = yaml.safe_load(raw)
29 if not isinstance(data, dict):
30 raise RecipeError(f"{path} is not a mapping.")
31 return data
32
33
34def _resolve_path(base: Path, value: Any) -> Any:
35 if not isinstance(value, str) or not value:
36 return value
37 p = Path(value).expanduser()
38 if p.is_absolute():
39 return str(p)
40 return str((base / p).resolve())
41
42
43def load_recipe(path: str | Path) -> Recipe:
44 recipe_path = Path(path).expanduser().resolve()
45 if not recipe_path.is_file():
46 raise RecipeError(f"Recipe not found: {recipe_path}")
47 data = _parse(recipe_path)
48 base = recipe_path.parent
49 data_block = data.get("data")
50 if isinstance(data_block, dict) and data_block.get("path"):
51 data_block = dict(data_block)
52 data_block["path"] = _resolve_path(base, data_block["path"])
53 data["data"] = data_block
54 ev = data.get("eval")
55 if isinstance(ev, dict) and ev.get("probes"):
56 ev = dict(ev)
57 ev["probes"] = _resolve_path(base, ev["probes"])
58 data["eval"] = ev
59 return Recipe.from_dict(data, source=str(recipe_path))
Recipe load_recipe(str|Path path)
Definition load.py:47
Any _resolve_path(Path base, Any value)
Definition load.py:38
dict[str, Any] _parse(Path path)
Definition load.py:21