"""Read immutable bundled assets without inserting machine-specific database rows.""" import hashlib import json from functools import lru_cache from pathlib import Path from app.schemas.card_images import CardImageAsset, CardImageConfiguration, CardImageRule REPO_ROOT = Path(__file__).resolve().parents[3] MANIFEST_PATH = REPO_ROOT / "frontend/public/earth/assets/cards/manifest.json" def _asset_from_manifest(item: dict) -> CardImageAsset: pending = item.get("redistribution_status") == "permission_not_verified" return CardImageAsset( id=item["id"], title=item["label"], url=item["url"], width=item["width"], height=item["height"], credit=item["credit"], source_url=item.get("source_page") or "", license=item["license"], license_url=item.get("license_url") or "", kind=item["kind"], rights_status="permission_pending" if pending else "available", enabled=item.get("enabled", not pending), focus_x=item.get("focus_x", 50), focus_y=item.get("focus_y", 50), variants=item.get("variants", {}), ) @lru_cache(maxsize=1) def previous_bundled_assets() -> dict[str, list[CardImageAsset]]: manifest = json.loads(MANIFEST_PATH.read_bytes()) return { key: [CardImageAsset.model_validate(item) for item in value] for key, value in manifest.get("previous_defaults", {}).items() } @lru_cache(maxsize=1) def previous_bundled_rules() -> dict[str, CardImageRule]: manifest = json.loads(MANIFEST_PATH.read_bytes()) return { key: CardImageRule.model_validate(value) for key, value in manifest.get("previous_rules", {}).items() } @lru_cache(maxsize=1) def bundled_configuration() -> tuple[CardImageConfiguration, str]: content = MANIFEST_PATH.read_bytes() manifest = json.loads(content) assets, rules = [], [] for item in manifest["assets"]: asset = _asset_from_manifest(item) assets.append(asset) if not asset.enabled: continue hints = item.get("binding_hints", {}) for index, target in enumerate(hints.get("defaults", [])): rules.append( CardImageRule( id=target.get("id", f"builtin-{asset.id}-default-{index}"), entity_type=target["entity_type"], scope=target["scope"], value=target["value"], label=asset.title, asset_id=asset.id, ) ) for index, entity_id in enumerate(hints.get("entity_ids", [])): rules.append( CardImageRule( id=f"builtin-{asset.id}-entity-{index}", entity_type=item["entity_type"], scope="object", value=entity_id, label=asset.title, asset_id=asset.id, ) ) for norad_id in hints.get("norad_ids", []): rules.append( CardImageRule( id=f"builtin-{asset.id}-{norad_id}", entity_type="satellite", scope="object", value=str(norad_id), label=f"{asset.title} ยท NORAD {norad_id}", asset_id=asset.id, ) ) if group := hints.get("constellation_group"): rules.append( CardImageRule( id=f"builtin-{asset.id}-group", entity_type="satellite", scope="group", value=group, label=asset.title, asset_id=asset.id, ) ) return CardImageConfiguration(assets=assets, rules=rules), hashlib.sha256(content).hexdigest()