]> git.scottworley.com Git - paperdoorknob/blobdiff - fetch.py
FakeFetcher: Show bad URLs in error messages
[paperdoorknob] / fetch.py
index 3c0c6a3aa9c47bc63999e0adde5cb750df8c85e6..b99938c055fe8a2fc31f951404ed497849e5dcff 100644 (file)
--- a/fetch.py
+++ b/fetch.py
@@ -7,7 +7,8 @@
 
 from abc import ABC, abstractmethod
 from contextlib import contextmanager
-from typing import Iterator
+from sys import stderr
+from typing import IO, Iterator
 
 import requests
 import requests_cache
@@ -31,6 +32,33 @@ class _SessionFetcher(Fetcher):
             return r.content
 
 
+class _CachingFetcher(Fetcher):
+
+    def __init__(
+            self,
+            session: requests_cache.CachedSession,
+            timeout: int) -> None:
+        self._session = session
+        self._timeout = timeout
+        self._request_count = 0
+        self._cache_hit_count = 0
+
+    def fetch(self, url: str) -> bytes:
+        with self._session.get(url, timeout=self._timeout) as r:
+            r.raise_for_status()
+            self._request_count += 1
+            self._cache_hit_count += int(r.from_cache)
+            return r.content
+
+    @property
+    def request_count(self) -> int:
+        return self._request_count
+
+    @property
+    def cache_hit_count(self) -> int:
+        return self._cache_hit_count
+
+
 @contextmanager
 def DirectFetcher(timeout: int) -> Iterator[_SessionFetcher]:
     with requests.session() as session:
@@ -38,6 +66,31 @@ def DirectFetcher(timeout: int) -> Iterator[_SessionFetcher]:
 
 
 @contextmanager
-def CachingFetcher(cache_path: str, timeout: int) -> Iterator[_SessionFetcher]:
+def CachingFetcher(
+        cache_path: str,
+        timeout: int,
+        report_stream: IO[str] = stderr) -> Iterator[_CachingFetcher]:
     with requests_cache.CachedSession(cache_path, cache_control=True) as session:
-        yield _SessionFetcher(session, timeout)
+        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", url)
+        return self._resources[url]
+
+    def request_count(self) -> int:
+        return self._fetch_count