Files
Black_Raven 37053a5d8e 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
2026-08-24 16:03:13 +03:00

37 lines
875 B
Python

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