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))