from __future__ import annotations from datetime import date from pathlib import Path import yaml from src.data.exceptions import UnknownInstrumentError from src.data.models import Instrument def load_instruments(config_path: Path) -> dict[str, Instrument]: with config_path.open("r", encoding="utf-8") as handle: payload = yaml.safe_load(handle) or {} instruments: dict[str, Instrument] = {} for key, raw in (payload.get("instruments") or {}).items(): instruments[key] = Instrument( key=key, name=raw["name"], index_code=str(raw["index_code"]), provider_symbol=str(raw["provider_symbol"]), exchange=str(raw["exchange"]), price_type=str(raw["price_type"]), bootstrap_start=date.fromisoformat(str(raw["bootstrap_start"])), ) return instruments def get_instrument(instruments: dict[str, Instrument], key: str) -> Instrument: try: return instruments[key] except KeyError as exc: raise UnknownInstrumentError(f"Unknown instrument: {key}") from exc