Files
Black_Raven 6ae350b48c fix(yamlreaders): Improve format auto-detection in get_reader()
Now properly detects Postman and OpenAPI formats by parsing YAML content
2026-08-24 16:46:29 +03:00

36 lines
906 B
Python

from .base import BaseReader, ParseError
from .yaml_reader import YamlReader
from .postman_reader import PostmanReader
from .openapi_reader import OpenAPIReader
def get_reader(source: str) -> BaseReader:
from pathlib import Path
import yaml
path = Path(source)
if not path.exists():
raise FileNotFoundError(f"File not found: {path}")
with open(path, "r", encoding="utf-8") as f:
content = f.read(2048)
if "openapi:" in content:
try:
data = yaml.safe_load(content)
if isinstance(data, dict) and "openapi" in data:
return OpenAPIReader()
except Exception:
pass
try:
data = yaml.safe_load(content)
if isinstance(data, dict):
if "info" in data and "item" in data:
return PostmanReader()
except Exception:
pass
return YamlReader()