Exports all readers and provides get_reader() function that auto-detects file format and returns appropriate reader
25 lines
647 B
Python
25 lines
647 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
|
|
|
|
path = Path(source)
|
|
|
|
if not path.exists():
|
|
raise FileNotFoundError(f"File not found: {path}")
|
|
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
first_line = f.readline().strip()
|
|
|
|
if '"openapi"' in first_line or "openapi:" in first_line:
|
|
return OpenAPIReader()
|
|
|
|
if '"info"' in first_line and '"item"' in first_line:
|
|
return PostmanReader()
|
|
|
|
return YamlReader()
|