54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
# SPDX-FileCopyrightText: 2025 2025
|
|
# SPDX-FileContributor: Gergely Polonkai
|
|
#
|
|
# 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
|
|
|
|
|
|
def generate_type_classes(class_list: dict[str, Any]) -> dict[str, Type[Oracle]]:
|
|
"""Generate a dictionary of oracle type handlers"""
|
|
|
|
ret: dict[str, Type[Oracle]] = {}
|
|
|
|
for klass in class_list.values():
|
|
if not isinstance(klass, type) or klass == Oracle or not issubclass(klass, Oracle):
|
|
continue
|
|
|
|
if klass.TYPE_MARKER in ret:
|
|
raise KeyError(
|
|
f"{ret[klass.TYPE_MARKER].__name__} is already registered as a handler for {klass.TYPE_MARKER}"
|
|
)
|
|
|
|
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)
|