From 37053a5d8e627f64c5f4917bb5888bbfa847c082 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 24 Aug 2026 16:03:13 +0300 Subject: [PATCH] feat(yamlreaders): Added BaseReader abstract class Base class for all parsers with: - validate(): checks if source file is valid - parse(): converts source to TestModel - load_yaml(): loads YAML file --- yamlreaders/base.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 yamlreaders/base.py 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