]> git.scottworley.com Git - paperdoorknob/commitdiff
--quiet controls fetch cache hit rate report too
authorScott Worley <scottworley@scottworley.com>
Sat, 30 Dec 2023 00:33:39 +0000 (16:33 -0800)
committerScott Worley <scottworley@scottworley.com>
Sat, 30 Dec 2023 00:33:39 +0000 (16:33 -0800)
args.py
fetch.py
fetch_test.py

diff --git a/args.py b/args.py
index c5506729797f32ac2e8aaa9b050aae065da81082..67d8c3f84dbe485d14afd731475338115c269f56 100644 (file)
--- a/args.py
+++ b/args.py
@@ -94,7 +94,8 @@ def spec_from_commandline_args() -> Iterator[Spec]:
         layout = BesideIconLayout(texifier, args.image_size)
     else:
         raise ValueError(f'Unknown layout: {args.layout}')
         layout = BesideIconLayout(texifier, args.image_size)
     else:
         raise ValueError(f'Unknown layout: {args.layout}')
-    with CachingFetcher(args.cache_path, args.timeout) as fetcher:
+    log = (lambda _: None) if args.quiet else _print_status
+    with CachingFetcher(args.cache_path, args.timeout, log) as fetcher:
         with open(args.out + '.tex', 'wb') as texout:
             yield Spec(
                 args.url,
         with open(args.out + '.tex', 'wb') as texout:
             yield Spec(
                 args.url,
@@ -106,4 +107,4 @@ def spec_from_commandline_args() -> Iterator[Spec]:
                 layout,
                 args.geometry,
                 texout,
                 layout,
                 args.geometry,
                 texout,
-                (lambda _: None) if args.quiet else _print_status)
+                log)
index e998394445196fcf677766a78cd3805d6b241bd1..eb904a4f4b0e525756c977d442fb0e4144dc0825 100644 (file)
--- a/fetch.py
+++ b/fetch.py
@@ -8,7 +8,7 @@
 from abc import ABC, abstractmethod
 from contextlib import contextmanager
 from sys import stderr
 from abc import ABC, abstractmethod
 from contextlib import contextmanager
 from sys import stderr
-from typing import IO, Iterator
+from typing import Callable, Iterator
 
 import requests
 import requests_cache
 
 import requests
 import requests_cache
@@ -73,17 +73,16 @@ def DirectFetcher(timeout: int) -> Iterator[_SessionFetcher]:
 
 @contextmanager
 def CachingFetcher(
 
 @contextmanager
 def CachingFetcher(
-        cache_path: str,
-        timeout: int,
-        report_stream: IO[str] = stderr) -> Iterator[_CachingFetcher]:
+    cache_path: str,
+    timeout: int,
+    log: Callable[[str], None] = lambda x: print(x, file=stderr),
+) -> Iterator[_CachingFetcher]:
     with requests_cache.CachedSession(cache_path, cache_control=True) as session:
         fetcher = _CachingFetcher(session, timeout)
         yield fetcher
         if fetcher.request_count > 0:
             percent = 100.0 * fetcher.cache_hit_count / fetcher.request_count
     with requests_cache.CachedSession(cache_path, cache_control=True) as session:
         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)
+            log(f"Fetch cache hits: {fetcher.cache_hit_count} ({percent:.1f}%)")
 
 
 class FakeFetcher(Fetcher):
 
 
 class FakeFetcher(Fetcher):
index 5f21feee1c2d293e198d12a5851c38a1e52524ab..59a1514853d3f370fe233b2a883c5f8ad196ad88 100644 (file)
@@ -45,7 +45,10 @@ class TestFetch(unittest.TestCase):
 
     def testCacheHitRateReport(self) -> None:
         buf = StringIO()
 
     def testCacheHitRateReport(self) -> None:
         buf = StringIO()
-        with CachingFetcher("testcachehitratereportwithcl", TIMEOUT, buf) as f:
+
+        def log(msg: str) -> None:
+            print(msg, file=buf)
+        with CachingFetcher("testcachehitratereportwithcl", TIMEOUT, log) as f:
             for _ in range(7):
                 f.fetch(f"http://localhost:{self._port}")
         self.assertEqual("Fetch cache hits: 6 (85.7%)\n", buf.getvalue())
             for _ in range(7):
                 f.fetch(f"http://localhost:{self._port}")
         self.assertEqual("Fetch cache hits: 6 (85.7%)\n", buf.getvalue())