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:
1 parent
b16e3aa89a
commit
37053a5d8e
1 file changed
+36
@@ -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
|
||||||
Reference in new issue
Block a user