From e5874f2d9fb008f67f09789a8b3871f12c87beab Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 30 Nov 2026 11:33:17 +0100 Subject: [PATCH] Key a finished picture by the credit it draws A finished picture's key named its route, its options and its layers' keys, but not the credit it draws from a pick. A pick whose author or licence changed while its file stayed the same kept its photo layer's key, so the stored picture was served again with the old credit drawn on it, while X-Credits already said the new one (seen locally for 1967, whose author lost Commons' "upload by" appendix). finished_key now also takes every text the picture draws from a pick: the credit of each layer made from a pick's photo (Layer.from_pick, season and layout picks alike). A map's credit follows its source, which its layer key already names. A picture drawn from no pick adds nothing to the digest, so its key is the one it had: no kept race, session or pick-less season picture is drawn again. Seen locally with the old store in place: the race poster, the session thumbnail and the season poster with bg=none came from the store under their old keys; the 1967 and 1989 season posters were drawn once under new keys. No release of master ever held a photo pick (neither seasons/photos.json nor circuits/photos.json), so the running service keeps no picture whose key changes. The keys test: the same photo with another credit makes another key (on the old code finished_key takes no credit; with the credit ignored the keys are equal), and a picture drawn from no pick keeps the digest of render version, route, options and layer keys it had. Builds: docs/posters.md at f6b9f937d09a3b79b2312af5bfd290c7408b20fb (Finished images: the key names the credit of each layer drawn from a pick; Principles, 2: a changed input makes a new key) Claude-Session: https://claude.ai/code/session_01JG73KFhbaNj3tyJ8tKmE3W --- poster_options.py | 10 +++++++--- posters_service.py | 11 +++++++---- tests/test_posters.py | 9 +++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/poster_options.py b/poster_options.py index 4cf11be..c7c487e 100644 --- a/poster_options.py +++ b/poster_options.py @@ -187,10 +187,14 @@ def outline_layer_key(trace: str, frame: str, zoom: int) -> str: return f"outline/{trace}-{frame}-z{zoom}" -def finished_key(route: str, options: Options, layer_keys: Iterable[str]) -> str: - """A finished picture's name in the store: its route, its options and the keys of the layers it was made from.""" +def finished_key(route: str, options: Options, layer_keys: Iterable[str], picked: Iterable[str] = ()) -> str: + """A finished picture's name in the store: its route, its options, the keys of the layers it was made from, and + every text it draws from a pick (a photo's credit), so a changed credit makes a new key (docs/posters.md, + principle 2). A picture drawn from no pick keeps the key it had before: nothing is added for it.""" made_of = "|".join(sorted(layer_keys)) - digest = hashlib.sha256(f"{RENDER_VERSION}\n{route}\n{options.canonical()}\n{made_of}".encode()).hexdigest() + texts = "\n".join(text for text in picked if text) + drawn = f"\n{texts}" if texts else "" + digest = hashlib.sha256(f"{RENDER_VERSION}\n{route}\n{options.canonical()}\n{made_of}{drawn}".encode()).hexdigest() return f"{digest[:40]}.{options.format}" diff --git a/posters_service.py b/posters_service.py index a7859b6..59bf4b6 100644 --- a/posters_service.py +++ b/posters_service.py @@ -421,11 +421,13 @@ class Rendered: @dataclass class Layer: """One layer a picture is made from: its key, its credit, what its source asks of the picture (share-alike or - not), and how to get it (from the store, or made now).""" + not), how to get it (from the store, or made now), and whether it is a pick's photo, whose credit the finished + picture's key names (a map's credit follows its source, which its key already names).""" key: str credit: str share: Optional[str] get: Callable[[], Optional[Image.Image]] + from_pick: bool = False class PostersService: @@ -534,7 +536,7 @@ class PostersService: credit, share = ps.pick_credit(pick), ps.pick_share(pick) info = self.commons.info(pick["file"]) if info is None: # no key without its SHA-1: served without the photo, and asked again next time - return Layer(f"photo/{pick['file']}?", credit, share, lambda: None) + return Layer(f"photo/{pick['file']}?", credit, share, lambda: None, from_pick=True) key = po.photo_layer_key(pick["file"], info["sha1"], frame) def make(): @@ -547,7 +549,7 @@ class PostersService: logger.warning(f"Commons file {pick['file']!r} unreadable: {e}") return None return cover(img, po.FRAMES[frame]) # kept untreated: the treatment is drawn on assembly - return Layer(key, credit, share, lambda: self._kept_or_made(key, "jpg", make)) + return Layer(key, credit, share, lambda: self._kept_or_made(key, "jpg", make), from_pick=True) def _locator_layer(self, geo: Geo) -> Layer: lat, lon = round(geo.lat, 2), round(geo.lon, 2) @@ -714,7 +716,8 @@ class PostersService: A stored picture was made from every layer it names, so its credit line and its licence are theirs.""" layers = [layer for layer in (background, locator) if layer] ps.picture_licence([layer.share for layer in layers]) # a clash of share-alike terms is refused, not drawn - name = po.finished_key(route, options, [layer.key for layer in layers] + ([outline] if outline else [])) + name = po.finished_key(route, options, [layer.key for layer in layers] + ([outline] if outline else []), + [layer.credit for layer in layers if layer.from_pick]) media_type = po.FORMATS[options.format] stored = self.store.get(name) if stored is not None: diff --git a/tests/test_posters.py b/tests/test_posters.py index 38ac28e..202fc07 100644 --- a/tests/test_posters.py +++ b/tests/test_posters.py @@ -2,6 +2,7 @@ Standard library only (no PIL, no numpy), so the merge check runs it as it is. """ +import hashlib import json import os import tempfile @@ -211,6 +212,14 @@ class OptionsTest(unittest.TestCase): ["map/eox-2016/x-poster-z15-r3"])) self.assertTrue(key.endswith(".png")) self.assertTrue(po.finished_key("race/Bahrain Grand Prix/2024", self.parse("format=webp"), []).endswith(".webp")) + # A changed credit with the same photo makes a new key; a picture drawn from no pick keeps the key it had + season, photo = self.parse("", "season"), ["photo/a1b2c3-sha1-poster-r3"] + credited = po.finished_key("season/1967", season, photo, ["Photo: A, CC BY 2.0, via Wikimedia Commons"]) + self.assertNotEqual(credited, po.finished_key("season/1967", season, photo, + ["Photo: B, CC BY 2.0, via Wikimedia Commons"])) + before = hashlib.sha256(f"{po.RENDER_VERSION}\nrace/Bahrain Grand Prix/2024\n{options.canonical()}\n" + "map/eox-2016/x-poster-z15-r3".encode()).hexdigest() + self.assertEqual(key, f"{before[:40]}.png") self.assertEqual(po.locator_layer_key(26.0325, 50.5106), f"locator/26.03_50.51-{po.RENDER_VERSION}") self.assertIn(po.RENDER_VERSION, po.map_layer_key("eox-2025", po.trace_hash([[1, 2]]), "thumb", 15))