media: cache the output of `get_video_codec`
- ID
7f2d7a8- date
2026-11-21 08:15:21+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
5038865- message
media: cache the output of `get_video_codec` This makes my static site tests substantially faster, and will save me repeatedly re-reading video files that haven't changed.- changed files
Changed files
CHANGELOG.md (10082 → 10231)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 22596f2..2c4466d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
# CHANGELOG
+## v64 - 2026-11-21
+
+Add caching for `media.get_video_codec`, keyed by file path/mtime/size, to speed up static site tests that check video codecs.
+
## v63 - 2026-11-21
In `StaticSiteTestSuite`, add `pyproject.toml` to the list of files which can be saved in the top level of a static site.
dev_requirements.txt (834 → 1181)
diff --git a/dev_requirements.txt b/dev_requirements.txt
index 5cc2c4d..a9c95f5 100644
--- a/dev_requirements.txt
+++ b/dev_requirements.txt
@@ -2,10 +2,14 @@
# uv pip compile dev_requirements.in --output-file=dev_requirements.txt --exclude-newer=P7D
-e file:.
# via -r dev_requirements.in
+annotated-types==0.8.0
+ # via pydantic
certifi==2026.10.23
# via alexwlchan-chives
coverage==7.17.0
# via -r dev_requirements.in
+funkycache==1.0.2
+ # via alexwlchan-chives
greenlet==3.5.6
# via playwright
iniconfig==2.3.1
@@ -18,6 +22,10 @@ playwright==1.65.2
# via alexwlchan-chives
pluggy==1.7.0
# via pytest
+pydantic==2.14.3
+ # via funkycache
+pydantic-core==2.50.4
+ # via pydantic
pygments==2.21.0
# via pytest
pytest==9.2.0
@@ -30,7 +38,16 @@ ruff==0.17.5
# via -r dev_requirements.in
ty==0.0.42
# via -r dev_requirements.in
+typing-extensions==4.16.0
+ # via
+ # pydantic
+ # pydantic-core
+ # typing-inspection
+typing-inspection==0.4.5
+ # via pydantic
vcrpy==9.4.2
# via -r dev_requirements.in
wrapt==2.5.2
# via vcrpy
+xxhash==4.1.0
+ # via funkycache
javascript/chives-image.js (6918 → 6918)
diff --git a/javascript/chives-image.js b/javascript/chives-image.js
index cfaa956..192dbbc 100644
--- a/javascript/chives-image.js
+++ b/javascript/chives-image.js
@@ -1,5 +1,5 @@
// javascript/chives-image.js
-// chives version: 63
+// chives version: 64
// ChivesImage provides a <chives-image> component that renders
// an ImageEntity on a web page.
javascript/chives-pagination.js (4929 → 4929)
diff --git a/javascript/chives-pagination.js b/javascript/chives-pagination.js
index 9468f4f..a283779 100644
--- a/javascript/chives-pagination.js
+++ b/javascript/chives-pagination.js
@@ -1,5 +1,5 @@
// javascript/chives-pagination.js
-// chives version: 63
+// chives version: 64
// ChivesPagination manages the pagination of a collection of items.
//
javascript/chives-sorting.js (5034 → 5034)
diff --git a/javascript/chives-sorting.js b/javascript/chives-sorting.js
index 83b91d9..5a1bc0d 100644
--- a/javascript/chives-sorting.js
+++ b/javascript/chives-sorting.js
@@ -1,5 +1,5 @@
// javascript/chives-sorting.js
-// chives version: 63
+// chives version: 64
// ChivesSorting manages the sorting of a collection of items.
//
javascript/chives-video.js (5521 → 5521)
diff --git a/javascript/chives-video.js b/javascript/chives-video.js
index b655950..b6a9abb 100644
--- a/javascript/chives-video.js
+++ b/javascript/chives-video.js
@@ -1,5 +1,5 @@
// javascript/chives-video.js
-// chives version: 63
+// chives version: 64
// ChivesVideo provides a <chives-video> component that renders
// a VideoEntity on a web page.
pyproject.toml (2228 → 2256)
diff --git a/pyproject.toml b/pyproject.toml
index fca631c..145e2e9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -34,8 +34,8 @@ license = "MIT"
[project.optional-dependencies]
browser_fixtures = ["playwright", "pytest"]
fetch = ["certifi"]
-media = ["Pillow"]
-static_site_tests = ["pytest"]
+media = ["funkycache", "Pillow"]
+static_site_tests = ["funkycache", "pytest"]
urls = ["certifi"]
[project.scripts]
python/chives/__init__.py (391 → 391)
diff --git a/python/chives/__init__.py b/python/chives/__init__.py
index df789c3..6c729c3 100644
--- a/python/chives/__init__.py
+++ b/python/chives/__init__.py
@@ -11,4 +11,4 @@ I share across multiple sites.
"""
-__version__ = "63"
+__version__ = "64"
python/chives/media.py (11899 → 12487)
diff --git a/python/chives/media.py b/python/chives/media.py
index d6a4283..0d11c86 100644
--- a/python/chives/media.py
+++ b/python/chives/media.py
@@ -16,10 +16,13 @@ References:
from fractions import Fraction
import json
+import os
from pathlib import Path
import subprocess
from typing import Literal, NotRequired, TypedDict, TYPE_CHECKING
+from funkycache import FunkyCache
+
if TYPE_CHECKING:
import PIL.Image
@@ -37,10 +40,28 @@ __all__ = [
]
+if os.environ.get("CHIVES_TEST") == "true":
+ cache = FunkyCache(storage="memory")
+else: # pragma: no cover
+ cache = FunkyCache(storage="sqlite")
+
+
def get_video_codec(path: str | Path) -> str:
"""
Return a video's codec. For example h264, av1, vp9.
"""
+ path = Path(path)
+ return _get_video_codec_cached(
+ path, mtime=path.stat().st_mtime, size=path.stat().st_size
+ )
+
+
+@cache.register
+def _get_video_codec_cached(path: Path, mtime: float, size: float) -> str:
+ """
+ Return a video's codec, cached by modification time and file size,
+ which serve as a good proxy for "is this the file we checked previously".
+ """
# fmt: off
cmd = [
"ffprobe",
python/chives/static_site_tests.py (9011 → 8752)
diff --git a/python/chives/static_site_tests.py b/python/chives/static_site_tests.py
index a86cabc..2aa4169 100644
--- a/python/chives/static_site_tests.py
+++ b/python/chives/static_site_tests.py
@@ -5,7 +5,6 @@ Defines a set of common tests and test helpers used for all my static sites.
from abc import ABC, abstractmethod
import collections
from collections.abc import Iterator
-import concurrent.futures
from difflib import SequenceMatcher
import itertools
import os
@@ -102,7 +101,7 @@ class StaticSiteTestSuite[M](ABC):
else:
if top_level_folder in {
".git",
- ".mypy_cache",
+ ".funkycache",
".pytest_cache",
".ruff_cache",
".venv",
@@ -194,16 +193,11 @@ class StaticSiteTestSuite[M](ABC):
p for p in site_root.rglob("*") if p.suffix in {".mp4", ".webm"}
]
- with concurrent.futures.ThreadPoolExecutor() as executor:
- futures = {
- executor.submit(get_video_codec, site_root / p): p
- for p in affected_paths
- }
- concurrent.futures.wait(futures)
-
- bad_videos = {
- p for fut, p in futures.items() if fut.result() in disallowed_codecs
- }
+ bad_videos = {
+ p
+ for p in affected_paths
+ if get_video_codec(site_root / p) in disallowed_codecs
+ }
assert bad_videos == set(), f"Found videos with disallowed codecs: {bad_videos}"