AQIT
0.1.0
Toggle main menu visibility
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
8
from
__future__
import
annotations
9
10
import
json
11
from
pathlib
import
Path
12
from
typing
import
Any
13
14
from
aquin.recipe.schema
import
Recipe, RecipeError
15
16
17
def
_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
34
def
_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
43
def
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))
aquin.recipe.schema.RecipeError
Definition
schema.py:24
aquin.recipe.load.load_recipe
Recipe load_recipe(str|Path path)
Definition
load.py:47
aquin.recipe.load._resolve_path
Any _resolve_path(Path base, Any value)
Definition
load.py:38
aquin.recipe.load._parse
dict[str, Any] _parse(Path path)
Definition
load.py:21
aquin.recipe.schema
Definition
schema.py:1
aquin
recipe
load.py
AQIT · Aquin Labs Private Limited · Apache 2.0 · Generated by
1.18.0