diff --git a/yamlreaders/base.py b/yamlreaders/base.py new file mode 100644 index 0000000..1c2612f --- /dev/null +++ b/yamlreaders/base.py @@ -0,0 +1,36 @@ +from abc import ABC, abstractmethod +from pathlib import Path +from typing import Any + +import yaml + +from models import TestModel + + +class ParseError(Exception): + def __init__(self, message: str, line: int | None = None): + self.line = line + super().__init__(message) + + +class BaseReader(ABC): + @abstractmethod + def validate(self, source: str | Path) -> bool: + pass + + @abstractmethod + def parse(self, source: str | Path) -> TestModel: + pass + + def load_yaml(self, source: str | Path) -> dict[str, Any]: + path = Path(source) + if not path.exists(): + raise FileNotFoundError(f"File not found: {path}") + + with open(path, "r", encoding="utf-8") as f: + data = yaml.safe_load(f) + + if not isinstance(data, dict): + raise ParseError("YAML root must be a mapping") + + return data