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
This commit is contained in:
Black_Raven committed 2026-08-24 16:03:13 +03:00
1 parent b16e3aa89a
commit 37053a5d8e
1 file changed
+36
+36
View File
@@ -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