"""Runtime configuration, and the constants the whole recipe agrees on.""" from __future__ import annotations import os from dataclasses import dataclass from pathlib import Path from zoneinfo import ZoneInfo DEFAULT_USER_AGENT = "ZipLime fomc-events/1.0 algo@limex.com" DEFAULT_HF_REPO = "ZipLime/fomc-events" HOST = "https://www.federalreserve.gov" # Everything the Committee publishes is embargoed in Washington time. The # offset is never computed -- a March statement and a June statement are on # opposite sides of daylight saving, and which is which is a question for the # timezone database. RELEASE_TIMEZONE = "America/New_York" EASTERN = ZoneInfo(RELEASE_TIMEZONE) COUNTRY = "US" # The Summary of Economic Projections begins in 2007 and the dot plot in 2012. # Statements reach back to 1994 and the historical pages to 1936; starting at # the SEP keeps the subsets comparable, and metadata/coverage.parquet records # what each one actually spans. BACKFILL_FROM_YEAR = 2007 @dataclass(frozen=True, slots=True) class Settings: data_dir: Path = Path("data") cache_dir: Path = Path(".cache/fomc") user_agent: str = DEFAULT_USER_AGENT requests_per_second: float = 3.0 hf_repo: str = DEFAULT_HF_REPO backfill_from_year: int = BACKFILL_FROM_YEAR @classmethod def from_env(cls, **overrides) -> Settings: return cls( data_dir=Path(overrides.get("data_dir") or os.environ.get("DATA_DIR", "data")), cache_dir=Path( overrides.get("cache_dir") or os.environ.get("FOMC_CACHE_DIR", ".cache/fomc") ), user_agent=os.environ.get("FOMC_USER_AGENT", DEFAULT_USER_AGENT), requests_per_second=float(os.environ.get("FOMC_REQUESTS_PER_SECOND", "3")), hf_repo=overrides.get("hf_repo") or os.environ.get("HF_DATASET_REPO", DEFAULT_HF_REPO), backfill_from_year=int( overrides.get("backfill_from_year") or os.environ.get("FOMC_BACKFILL_FROM_YEAR", BACKFILL_FROM_YEAR) ), ) def validate_user_agent(value: str) -> str: cleaned = (value or "").strip() if "@" not in cleaned or len(cleaned) < 12: raise ValueError( f"the Federal Reserve expects a User-Agent carrying a contact address, got {value!r}" ) return cleaned