# steward/core/plugin_index.py """Remote plugin catalog: fetch, parse, and cache the plugin index.yaml.""" from __future__ import annotations import logging import time from typing import Any logger = logging.getLogger(__name__) _CACHE_TTL = 300 # seconds # Per-URL cache: url → (raw_data, fetched_at monotonic) _url_cache: dict[str, tuple[dict, float]] = {} class CatalogPlugin: """One entry from the remote plugin index.""" __slots__ = ( "name", "version", "description", "author", "min_app_version", "download_url", "checksum_sha256", "repository_url", "homepage", "license", "tags", ) def __init__(self, data: dict[str, Any]) -> None: self.name: str = data.get("name", "") self.version: str = data.get("version", "0.0.0") self.description: str = data.get("description", "") self.author: str = data.get("author", "") self.min_app_version: str | None = data.get("min_app_version") self.download_url: str = data.get("download_url", "") self.checksum_sha256: str = data.get("checksum_sha256", "") self.repository_url: str = data.get("repository_url", "") self.homepage: str = data.get("homepage", "") self.license: str = data.get("license", "") self.tags: list[str] = data.get("tags", []) async def _fetch_one(url: str, force: bool = False) -> list[CatalogPlugin]: """Fetch a single index URL, using per-URL cache.""" import httpx import yaml now = time.monotonic() if not force and url in _url_cache: raw, fetched_at = _url_cache[url] if now - fetched_at < _CACHE_TTL: return [CatalogPlugin(p) for p in raw.get("plugins", [])] try: async with httpx.AsyncClient(timeout=10) as client: resp = await client.get(url) resp.raise_for_status() raw = yaml.safe_load(resp.text) or {} _url_cache[url] = (raw, now) logger.info("Plugin catalog fetched from %s: %d plugin(s)", url, len(raw.get("plugins", []))) return [CatalogPlugin(p) for p in raw.get("plugins", [])] except Exception: logger.exception("Failed to fetch plugin catalog from %s", url) if url in _url_cache: raw, _ = _url_cache[url] logger.warning("Returning stale cached catalog for %s", url) return [CatalogPlugin(p) for p in raw.get("plugins", [])] return [] async def fetch_catalog( index_urls: str | list[str], force: bool = False, ) -> list[CatalogPlugin]: """Fetch and merge plugin catalogs from one or more index URLs. Results are cached per-URL for _CACHE_TTL seconds. Plugins with the same name across repos are deduplicated — the first occurrence (repo list order) wins. Pass force=True to bypass all caches. """ if isinstance(index_urls, str): index_urls = [index_urls] seen: dict[str, CatalogPlugin] = {} for url in index_urls: url = url.strip() if not url: continue for plugin in await _fetch_one(url, force=force): if plugin.name not in seen: seen[plugin.name] = plugin return list(seen.values()) def clear_catalog_cache() -> None: """Invalidate all in-process catalog caches.""" _url_cache.clear()