Files
backend/scripts/swagger-drogon/swagger_drogon/contract.py
T

23 lines
671 B
Python

from __future__ import annotations
import json
from pathlib import Path
from typing import Any
def load_contract(path: Path) -> dict[str, Any]:
text = path.read_text(encoding="utf-8")
try:
return json.loads(text)
except json.JSONDecodeError:
try:
import yaml # type: ignore[import-untyped]
except ImportError as error:
raise ValueError(
f"{path}: install PyYAML or use JSON-compatible YAML syntax"
) from error
data = yaml.safe_load(text)
if not isinstance(data, dict):
raise ValueError(f"{path}: contract root must be an object")
return data