"""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 us-treasury-events/1.0 algo@limex.com" DEFAULT_HF_REPO = "ZipLime/us-treasury-events" TREASURY_DIRECT = "https://www.treasurydirect.gov" HOME_TREASURY = "https://home.treasury.gov" # Auctions close and curves are struck in New York. The offset is never # computed: a February auction and a July auction 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 auction query reaches back to 1979 and the rate feeds to 1990. Both are # taken whole; `metadata/coverage.parquet` records what each subset spans and # from when each field is populated. FIRST_CURVE_YEAR = 1990 @dataclass(frozen=True, slots=True) class Settings: data_dir: Path = Path("data") cache_dir: Path = Path(".cache/treasury") user_agent: str = DEFAULT_USER_AGENT requests_per_second: float = 4.0 hf_repo: str = DEFAULT_HF_REPO first_curve_year: int = FIRST_CURVE_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("TREASURY_CACHE_DIR", ".cache/treasury") ), user_agent=os.environ.get("TREASURY_USER_AGENT", DEFAULT_USER_AGENT), requests_per_second=float(os.environ.get("TREASURY_REQUESTS_PER_SECOND", "4")), hf_repo=overrides.get("hf_repo") or os.environ.get("HF_DATASET_REPO", DEFAULT_HF_REPO), first_curve_year=int( overrides.get("first_curve_year") or os.environ.get("TREASURY_FIRST_CURVE_YEAR", FIRST_CURVE_YEAR) ), ) def validate_user_agent(value: str) -> str: cleaned = (value or "").strip() if "@" not in cleaned or len(cleaned) < 12: raise ValueError( f"the Treasury expects a User-Agent carrying a contact address, got {value!r}" ) return cleaned __all__ = [ "COUNTRY", "DEFAULT_HF_REPO", "DEFAULT_USER_AGENT", "EASTERN", "FIRST_CURVE_YEAR", "HOME_TREASURY", "RELEASE_TIMEZONE", "TREASURY_DIRECT", "Settings", "validate_user_agent", ]