+ fetcher = _CachingFetcher(session, timeout)
+ yield fetcher
+ if fetcher.request_count > 0:
+ percent = 100.0 * fetcher.cache_hit_count / fetcher.request_count
+ print(
+ f"Fetch cache hits: {fetcher.cache_hit_count} ({percent:.1f}%)",
+ file=report_stream)
+
+
+class FakeFetcher(Fetcher):
+
+ def __init__(self, resources: dict[str, bytes]) -> None:
+ self._resources = resources
+ self._fetch_count = 0
+
+ def fetch(self, url: str) -> bytes:
+ self._fetch_count += 1
+ if url not in self._resources:
+ raise requests.HTTPError("URL not found")
+ return self._resources[url]
+
+ def request_count(self) -> int:
+ return self._fetch_count