feat: Create the load_oracle_from_yaml helper
This commit is contained in:
@@ -4,8 +4,11 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Oracle classes and related functions"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any, Type
|
||||
|
||||
import yaml
|
||||
|
||||
from .base import Oracle
|
||||
from .object_generator import ObjectGeneratorOracle
|
||||
from .random_choice import RandomChoiceOracle
|
||||
@@ -28,3 +31,23 @@ def generate_type_classes(class_list: dict[str, Any]) -> dict[str, Type[Oracle]]
|
||||
ret[klass.TYPE_MARKER] = klass
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
TYPE_CLASSES: dict[str, Type[Oracle]] = generate_type_classes(globals())
|
||||
|
||||
|
||||
def load_oracle_from_yaml(file_path: str | Path) -> Oracle:
|
||||
"""Create an Oracle from a YAML file"""
|
||||
|
||||
with open(file_path, "r", encoding="utf-8") as fhand:
|
||||
data = yaml.safe_load(fhand)
|
||||
|
||||
if not isinstance(data, dict):
|
||||
raise TypeError("Oracle data must be a YAML object")
|
||||
|
||||
if (generator_type := data.get("type")) not in TYPE_CLASSES:
|
||||
raise KeyError(f"No information on how to handle {generator_type} data")
|
||||
|
||||
handler_class = TYPE_CLASSES[generator_type]
|
||||
|
||||
return handler_class(data)
|
||||
|
Reference in New Issue
Block a user