"""Read a dated public sample or explicitly request available live statements. No third-party Python packages are required. Store real account keys privately. """ import argparse import json import os from decimal import Decimal from pathlib import Path from urllib.error import HTTPError, URLError from urllib.request import Request, urlopen parser = argparse.ArgumentParser() mode = parser.add_mutually_exclusive_group(required=True) mode.add_argument("--input", type=Path, help="Downloaded public sample JSON") mode.add_argument("--live", action="store_true", help="Use account allowance") args = parser.parse_args() if args.live: key = os.environ.get("EUROFINDATA_API_KEY") if not key: raise SystemExit("Set EUROFINDATA_API_KEY privately before using --live.") url = ("https://api.eurofindata.com/v1/financials/income-statements" "?ticker=ASML&period=annual&limit=5") request = Request(url, headers={"Authorization": "Bearer " + key}) try: with urlopen(request, timeout=30) as response: payload = json.load(response) except HTTPError as error: raise SystemExit(f"Data request failed: HTTP {error.code}. Check access and allowance.") except URLError: raise SystemExit("Data request failed. Check connectivity and try later.") rows = payload.get("income_statements", []) print("LIVE REQUEST: verify source freshness, accounting basis and report versions.") else: payload = json.loads(args.input.read_text(encoding="utf-8")) if payload.get("is_live") is not False or payload.get("issuer") != "ASML": raise SystemExit("Expected the dated ASML public sample.") rows = payload.get("records", []) print("DATED SAMPLE:", payload["sample_as_of"], payload["accounting_basis"]) if not isinstance(rows, list) or not rows: raise SystemExit("No statements returned. An empty result is not zero revenue.") seen = set() for row in sorted(rows, key=lambda value: value.get("report_period", value.get("period_end", ""))): period = row.get("report_period", row.get("period_end")) currency = row.get("currency") identity = (period, currency) if not period or not currency or identity in seen: raise SystemExit("Missing period/currency or duplicate period. Select a report version first.") seen.add(identity) revenue, income = row.get("revenue"), row.get("operating_income") margin = None if revenue is not None and income is not None and Decimal(str(revenue)) != 0: margin = Decimal(str(income)) / Decimal(str(revenue)) * 100 display = "not available" if margin is None else f"{margin:.1f}%" print(period, currency, "revenue=", revenue, "operating_income=", income, "margin=", display, "source=", row.get("source", "not supplied"))