From 57522fedcfbe6a164be2d68981efc3a67147c5d7 Mon Sep 17 00:00:00 2001 From: Rahul Parundekar Date: Tue, 3 Nov 2026 12:02:27 -0800 Subject: [PATCH 1/3] fix(create-chapter): render each chapter's Web Banner from its own deck Web Banner.png has the city drawn into the image, so the text rebrand never reached it and every clone kept TemplateCity's "San Francisco" banner; renamed chapters kept their old names. render_web_banner.py renders slide 4 of the chapter's own Square Logo deck through Google's renderer instead, refuses a slide that does not name the chapter, and stamps the banner with the deck's checksum so a re-run skips current banners and picks up renamed decks. create_chapter and rename_chapter call it after a write. The rename reference now lists the other surfaces a text rename misses (badges, Luma links, the feed's Summary prose). Co-Authored-By: Claude Opus 5.5 --- skills/aaif-create-chapter/SKILL.md | 13 + .../references/rename-chapter.md | 12 + .../scripts/create_chapter.py | 13 + .../scripts/rename_chapter.py | 12 + .../scripts/render_web_banner.py | 224 ++++++++++++++++++ .../scripts/test_create_chapter.py | 31 ++- .../scripts/test_render_web_banner.py | 189 +++++++++++++++ 7 files changed, 491 insertions(+), 3 deletions(-) create mode 100755 skills/aaif-create-chapter/scripts/render_web_banner.py create mode 100755 skills/aaif-create-chapter/scripts/test_render_web_banner.py diff --git a/skills/aaif-create-chapter/SKILL.md b/skills/aaif-create-chapter/SKILL.md index 0c36b70..18bfa3c 100644 --- a/skills/aaif-create-chapter/SKILL.md +++ b/skills/aaif-create-chapter/SKILL.md @@ -172,6 +172,8 @@ template or the rebrand engine is broken. Fix that — don't resume. render_slide_png('', '$(mktemp -d)/slide5.png', slide_index=4) " ``` +- [ ] The run printed **`Rendered Web Banner.png`**. If it warned instead, the + banner still says the template city — run the command it names. - [ ] If the Luma page wasn't live, the user has been reminded to create it. - [ ] Unit tests still pass after any engine edit: ```bash @@ -192,6 +194,17 @@ template or the rebrand engine is broken. Fix that — don't resume. - **A chapter is not born with its own agent art.** Cloning TemplateCity would hand it TemplateCity's agent. That is `upload_agents.py`'s job — see `references/design-system-sweep.md`. +- **`Web Banner.png` is rendered, not cloned.** The city is drawn into the + image, so no text rebrand reaches it, and until 2026-11 every new chapter + kept TemplateCity's "San Francisco" banner. `render_web_banner.py` renders it + from slide 4 of the chapter's own `Banners (…)/Square Logo.pptx` through + Google's renderer. `create_chapter` and `rename_chapter` call it after a + write; it refuses a slide that does not name the chapter, and skips a banner + stamped with the deck's current checksum, so a full run is cheap: + ```bash + python3 /scripts/render_web_banner.py # report + python3 /scripts/render_web_banner.py --write + ``` - **`create_chapter` writes no Chapters List row**, so its 100-chapter cap check is an early warning, not a guarantee: three runs at 99 live chapters each read 99, each pass, and the next sync refuses all three rows together. diff --git a/skills/aaif-create-chapter/references/rename-chapter.md b/skills/aaif-create-chapter/references/rename-chapter.md index 08c3cbb..857409f 100644 --- a/skills/aaif-create-chapter/references/rename-chapter.md +++ b/skills/aaif-create-chapter/references/rename-chapter.md @@ -65,6 +65,18 @@ Also deliberate: run and a re-run would double-apply it. - **A mid-run upload failure says exactly what landed** — which files were uploaded, that no names were changed, and where the originals are. +- **What the text pass cannot see, checked 2026-11-03.** The Japan and New + Zealand renames verified clean while four things still said the old city: + - **`Web Banner.png`** has the name drawn in. A successful `--write` now + re-renders it from the rewritten `Square Logo.pptx` (`render_web_banner.py`) + and exits `1` if that fails. + - **The `Badges/` set** is named and lettered with a *lowercase* slug and UPPER + SVG text, which the Title/UPPER name pass does not match. Regenerate it with + `aaif-sync-badges` (`--chapter `), then trash the old-slug files. + - **Luma links** stay until you pass `--slug-from`/`--slug-to`. Both pages had + moved, so the links pointed to redirects. + - **The Chapters List `Summary`** prose ("AAIF Tokyo connects…") is website + copy, not a Drive file. - **The Chapters List row, the intake's city cells and Slack channels are NOT touched.** The first two are `aaif-sync-chapters`' surfaces and must be updated for the engines to keep matching the chapter to its folder; a channel rename diff --git a/skills/aaif-create-chapter/scripts/create_chapter.py b/skills/aaif-create-chapter/scripts/create_chapter.py index 38673cf..1103ce5 100755 --- a/skills/aaif-create-chapter/scripts/create_chapter.py +++ b/skills/aaif-create-chapter/scripts/create_chapter.py @@ -804,6 +804,19 @@ def main(): "workbooks; delete it by hand." % ctx["tmp"], file=sys.stderr) print("\nDone. New chapter folder id: %s" % new_id) print("https://drive.google.com/drive/folders/%s" % new_id) + # The clone copied TemplateCity's Web Banner.png, an image that says "San + # Francisco" and that the text rebrand cannot reach. Re-render it from this + # chapter's own (rebranded) Square Logo deck. A failure here is not a + # failed chapter: say so and name the command that finishes the job. + import render_web_banner # lazy: it imports this module + err = render_web_banner.sync_one(new_id, name) + if err: + print("WARNING: %s was NOT re-rendered (%s) — it still shows the template " + "city. Run: render_web_banner.py --chapter %r --write" + % (render_web_banner.BANNER, err, name), file=sys.stderr) + else: + print("Rendered %s from this chapter's %s." % (render_web_banner.BANNER, + render_web_banner.DECK)) if status == "absent": print("REMINDER: create the Luma page at https://luma.com/aaif-%s (it is not live yet)." % slug) elif status == "unknown": diff --git a/skills/aaif-create-chapter/scripts/rename_chapter.py b/skills/aaif-create-chapter/scripts/rename_chapter.py index 0e46866..c5c702d 100755 --- a/skills/aaif-create-chapter/scripts/rename_chapter.py +++ b/skills/aaif-create-chapter/scripts/rename_chapter.py @@ -444,6 +444,18 @@ def main(): % ("/".join(OFFICE), a.old, " (native Google files and member data were not inspected)" if (native or skipped) else "")) + # Web Banner.png has the city drawn into the image, which no text pass + # reaches. It is rendered from the Square Logo deck just rewritten, and + # the deck's checksum moved, so this re-renders it (or is a no-op). + import render_web_banner + err = render_web_banner.sync_one(folder_id, a.new) + if err: + print("WARNING: %s still shows the old name (%s). Run: " + "render_web_banner.py --chapter %r --write" + % (render_web_banner.BANNER, err, a.new)) + return 1 + print("%s rendered from the renamed %s." % (render_web_banner.BANNER, + render_web_banner.DECK)) return 0 finally: # Member data on disk, cleaned up. The report path used to leave every diff --git a/skills/aaif-create-chapter/scripts/render_web_banner.py b/skills/aaif-create-chapter/scripts/render_web_banner.py new file mode 100755 index 0000000..9c6a506 --- /dev/null +++ b/skills/aaif-create-chapter/scripts/render_web_banner.py @@ -0,0 +1,224 @@ +#!/usr/bin/env python3 +"""Render each chapter's `Web Banner.png` from its own Square Logo deck. + +`Web Banner.png` sits at the root of every chapter folder: the AAIF mark over +"AAIF Community Events / ". It is an image, so `create_chapter.py`'s +rebrand — which rewrites OOXML text — cannot reach it, and the clone copied +TemplateCity's PNG byte for byte. Found 2026-11-03: dozens of chapters' banners +said "San Francisco", and every renamed chapter's still showed its old name. + +The same composition already exists as text, which the rebrand does reach: +slide 4 of `Banners (…)/Square Logo.pptx`. So the banner is not drawn here at +all; it is that slide, rendered by Google's own Slides renderer +(`aaif_events.slides_export.render_slide_png`, never LibreOffice), and so it is +correct whenever the deck is. + +Two guards: + + * **The slide must name the chapter.** Rendering a deck that still says + another city would publish the wrong name with a fresh timestamp on it, so + a slide whose text lacks the chapter's name is reported and skipped. + * **A banner records the deck it came from.** The upload stamps + `appProperties.aaifBannerDeck` with the deck's `md5Checksum`. A re-run + skips a chapter whose stamp matches, so this is cheap to re-run, and a deck + that `rename_chapter.py` has rewritten is picked up automatically because + its checksum moved. (Renders are not byte-stable, so comparing PNG bytes + would re-upload every banner on every run.) + +Read-only by default. + +Usage: + python3 render_web_banner.py # report + python3 render_web_banner.py --write + python3 render_web_banner.py --chapter Aarhus --write +""" +import argparse +import os +import re +import sys +import tempfile +import zipfile +from concurrent.futures import ThreadPoolExecutor +from typing import NamedTuple, Optional + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import create_chapter as cc # noqa: E402 (puts lib on the path) +from aaif_events import gws as gwsmod # noqa: E402 +from aaif_events import slides_export # noqa: E402 + +BANNER = "Web Banner.png" +DECK = "Square Logo.pptx" +#: The banners subfolder is "Banners (Chapter Specific, Changed Rarely)"; match +#: the stem so a reworded parenthetical does not hide it. +BANNERS_DIR = re.compile(r"^Banners\b") +#: 0-based: slide 4 is the "AAIF Community Events / " square. +SLIDE_INDEX = 3 +HEADLINE = "AAIF Community Events" +STAMP = "aaifBannerDeck" +#: The template's deck says the city it is cloned FROM, by design. +DECK_NAME = {"TemplateCity": "San Francisco"} +PNG = "image/png" + + +class Result(NamedTuple): + name: str + #: "current", "render" (would/did render), "absent" (no banner and not + #: asked to create one), or None when `error` is set. + action: Optional[str] = None + error: Optional[str] = None + + +def _norm(s): + return re.sub(r"\s+", " ", s).strip().casefold() + + +def slide_text(pptx_path, index=SLIDE_INDEX): + """Every text run on slide `index` (0-based), joined by spaces.""" + with zipfile.ZipFile(pptx_path) as z: + xml = z.read("ppt/slides/slide%d.xml" % (index + 1)).decode("utf-8", "replace") + return " ".join(re.findall(r"]*)?>([^<]*)", xml)) + + +def names_chapter(text, name): + """True when the slide is the community-events square and names `name`.""" + t = _norm(text) + return _norm(HEADLINE) in t and _norm(name) in t + + +def chapters(): + """[(id, name)] for every chapter folder under Chapters, sorted.""" + return sorted(((c["id"], c["name"]) for c in cc.list_children(cc.CHAPTERS_PARENT) + if c["mimeType"] == cc.FOLDER), key=lambda t: t[1]) + + +def _find(children, pred): + hits = [c for c in children if pred(c)] + if len(hits) > 1: + raise RuntimeError("%d matches for %s" % (len(hits), hits[0]["name"])) + return hits[0] if hits else None + + +def sync_chapter(chapter_id, name, write, tmpdir, create=False): + """Bring one chapter's banner up to date. Returns a `Result`, never raises: + this runs under a thread pool, and one chapter's failure must not end the + others. + + A chapter with no banner at all is created one only when `create` is set + (the create/rename hooks). A sweep reports it instead: the folders without + one predate the template that carries it (the Tatooine test chapter), and + a sweep has no business adding files to them.""" + try: + kids = cc.list_children(chapter_id) + bdir = _find(kids, lambda c: c["mimeType"] == cc.FOLDER + and BANNERS_DIR.match(c["name"])) + if bdir is None: + return Result(name, error="no Banners folder") + deck = _find(cc.list_children(bdir["id"]), lambda c: c["name"] == DECK) + if deck is None: + return Result(name, error="no %s in %s" % (DECK, bdir["name"])) + meta = cc.gws_json("drive", "files", "get", params={ + "fileId": deck["id"], "supportsAllDrives": True, "fields": "md5Checksum"}) + md5 = meta.get("md5Checksum") + if not md5: + return Result(name, error="%s has no checksum (not a stored .pptx?)" % DECK) + + banner = _find(kids, lambda c: c["name"] == BANNER) + if banner is None and not create: + return Result(name, "absent") + if banner is not None: + bmeta = cc.gws_json("drive", "files", "get", params={ + "fileId": banner["id"], "supportsAllDrives": True, + "fields": "appProperties"}) + if (bmeta.get("appProperties") or {}).get(STAMP) == md5: + return Result(name, "current") + + local = os.path.join(tmpdir, "%s.pptx" % chapter_id) + cc.gws_download(deck["id"], local) + try: + text = slide_text(local) + finally: + os.remove(local) + want = DECK_NAME.get(name, name) + if not names_chapter(text, want): + return Result(name, error="slide %d of %s does not say %r with %r — fix " + "the deck first" % (SLIDE_INDEX + 1, DECK, HEADLINE, want)) + if not write: + return Result(name, "render") + + png = os.path.join(tmpdir, "%s.png" % chapter_id) + slides_export.render_slide_png(deck["id"], png, slide_index=SLIDE_INDEX) + try: + if banner is None: + # NO_RETRY: a create that landed but answered like a timeout + # would, re-sent, leave two banners in the folder. + banner = cc.gws_json( + "drive", "files", "create", retries=gwsmod.NO_RETRY, + params={"supportsAllDrives": True, "fields": "id"}, + body={"name": BANNER, "parents": [chapter_id], "mimeType": PNG}) + cc.gws_upload(banner["id"], png, PNG) + # Stamp last: a banner whose upload failed must not be marked current. + cc.gws_json("drive", "files", "update", + params={"fileId": banner["id"], "supportsAllDrives": True}, + body={"appProperties": {STAMP: md5}}) + finally: + if os.path.exists(png): + os.remove(png) + return Result(name, "render") + except Exception as e: + return Result(name, error="%s: %s" % (type(e).__name__, str(e)[:200])) + + +def sync_one(chapter_id, name): + """Render one chapter's banner now (used by create/rename after a write). + Returns an error string, or None on success.""" + with tempfile.TemporaryDirectory() as tmp: + r = sync_chapter(chapter_id, name, True, tmp, create=True) + return r.error + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--write", action="store_true", + help="render and upload (default: report only)") + ap.add_argument("--chapter", help="only this chapter (exact name, case-insensitive)") + ap.add_argument("--jobs", type=int, default=4) + a = ap.parse_args() + + todo = chapters() + if a.chapter: + todo = [(i, n) for i, n in todo if n.lower() == a.chapter.lower()] + if not todo: + print("no chapters matched.") + return 1 + print("%d chapter(s).%s\n" % (len(todo), "" if a.write + else " REPORT ONLY — nothing will be written.")) + pending, current, failed, absent = [], 0, [], [] + with tempfile.TemporaryDirectory() as tmp, \ + ThreadPoolExecutor(max_workers=a.jobs) as pool: + for r in pool.map(lambda t: sync_chapter(t[0], t[1], a.write, tmp), todo): + if r.error is not None: + failed.append(r) + print(" FAILED %-26s %s" % (r.name, r.error)) + elif r.action == "absent": + absent.append(r.name) + elif r.action == "render": + pending.append(r.name) + print(" %s %s" % ("rendered " if a.write else "would render", r.name)) + else: + current += 1 + print("\n%d %s, %d already current, %d failed." + % (len(pending), "rendered" if a.write else "to render", current, len(failed))) + if absent: + print("No %s at all (left alone; a sweep never adds one): %s" + % (BANNER, ", ".join(sorted(absent)))) + if failed: + return 1 + if pending and not a.write: + print("Re-run with --write to apply.") + return 2 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/skills/aaif-create-chapter/scripts/test_create_chapter.py b/skills/aaif-create-chapter/scripts/test_create_chapter.py index 3616d1e..fb6db89 100644 --- a/skills/aaif-create-chapter/scripts/test_create_chapter.py +++ b/skills/aaif-create-chapter/scripts/test_create_chapter.py @@ -11,7 +11,8 @@ from unittest import mock sys.path.insert(0, os.path.dirname(__file__)) -import create_chapter as cc # noqa: E402 +import create_chapter as cc +import render_web_banner # noqa: E402 from aaif_events import gws as gwsmod # noqa: E402 A = 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"' @@ -722,7 +723,7 @@ class TestMainGuards(unittest.TestCase): """Argument checks in main(), the plan-by-default / tempdir contract of a --write run, and which invocations consult the 100-chapter cap.""" - def run_main(self, argv, clone=None, cap_calls=None): + def run_main(self, argv, clone=None, cap_calls=None, banner=None): """Drive main() with every external call stubbed. `assert_under_cap` joined that list when the 100-chapter cap landed: it @@ -739,9 +740,33 @@ def run_main(self, argv, clone=None, cap_calls=None): mock.patch.object(cc, "resolve_latlon", lambda *a: (1.0, 2.0)), \ mock.patch.object(cc._chapters, "assert_under_cap", lambda what="", **k: calls.append(what)), \ - mock.patch.object(cc, "clone_and_rebrand", clone): + mock.patch.object(cc, "clone_and_rebrand", clone), \ + mock.patch.object(render_web_banner, "sync_one", + banner or (lambda fid, name: None)): cc.main() + def test_a_write_re_renders_the_new_chapters_banner(self): + """The clone copies TemplateCity's Web Banner.png, which says "San + Francisco"; the text rebrand cannot reach an image.""" + seen = [] + with mock.patch("sys.stdout"): + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=lambda fid, name: seen.append((fid, name))) + self.assertEqual(seen, [("new-id", "Zed")]) + + def test_a_failed_banner_render_warns_with_the_command(self): + import io + err = io.StringIO() + with mock.patch("sys.stdout"), mock.patch("sys.stderr", err): + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=lambda fid, name: "slide 4 says the wrong city") + self.assertIn("NOT re-rendered", err.getvalue()) + self.assertIn("render_web_banner.py --chapter 'Zed' --write", err.getvalue()) + + def test_a_plan_run_renders_no_banner(self): + self.run_main(["--city", "Zed"], + banner=lambda *a: self.fail("plan must not render")) + def test_plan_runs_never_consult_the_cap(self): """Seeing what a chapter WOULD look like is how someone decides which existing one to retire, so refusing the plan would remove the tool they diff --git a/skills/aaif-create-chapter/scripts/test_render_web_banner.py b/skills/aaif-create-chapter/scripts/test_render_web_banner.py new file mode 100755 index 0000000..461435a --- /dev/null +++ b/skills/aaif-create-chapter/scripts/test_render_web_banner.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python3 +"""Unit tests for rendering each chapter's Web Banner from its Square Logo deck. + +The properties pinned here are the ones that fail quietly: + + (a) **a deck that names another city is never rendered.** Rendering it would + publish the wrong name with a fresh date on it, which is worse than the + stale banner it replaces. + (b) **a banner stamped with the deck's checksum is skipped**, and one whose + deck moved is not — otherwise a re-run re-renders the whole estate, or a + rename never reaches the banner. + (c) **the stamp is written only after the upload**, so a failed upload is + not recorded as current. + (d) **a chapter with no banner gets one created exactly once** (NO_RETRY). + +Nothing touches Drive: `create_chapter`'s Drive calls and the renderer are +replaced with a fake folder tree. + +Run: python3 skills/aaif-create-chapter/scripts/test_render_web_banner.py +""" +import io +import os +import sys +import tempfile +import unittest +import zipfile +from unittest import mock + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import render_web_banner as rwb # noqa: E402 +import create_chapter as cc # noqa: E402 +from aaif_events import gws as gwsmod # noqa: E402 + +BANNERS = "Banners (Chapter Specific, Changed Rarely)" + + +def _pptx(slide4_runs): + """Bytes of a minimal .pptx whose slide 4 holds `slide4_runs`.""" + buf = io.BytesIO() + with zipfile.ZipFile(buf, "w") as z: + for i in range(1, 6): + runs = slide4_runs if i == 4 else ["AAIF"] + body = "".join('%s' % r + if n % 2 else "%s" % r + for n, r in enumerate(runs)) + z.writestr("ppt/slides/slide%d.xml" % i, "%s" % body) + return buf.getvalue() + + +class FakeDrive: + """One chapter folder, `boston`, with a Banners folder and a deck.""" + + def __init__(self, runs, banner_stamp=None, banner=True, md5="m1"): + self.deck = _pptx(runs) + self.md5 = md5 + self.stamp = banner_stamp + self.banner = banner + self.calls = [] # (verb, fileId-or-None) + self.renders = 0 + self.fail_upload = False + + def list_children(self, fid): + if fid == "boston": + kids = [{"id": "bdir", "name": BANNERS, "mimeType": cc.FOLDER}] + if self.banner: + kids.append({"id": "wb", "name": rwb.BANNER, "mimeType": "image/png"}) + return kids + if fid == "bdir": + return [{"id": "deck", "name": rwb.DECK, "mimeType": "x"}] + return [] + + def gws_json(self, *verb, params=None, body=None, retries=None): + fid = (params or {}).get("fileId") + self.calls.append((verb[-1], fid, retries, body)) + if verb[-1] == "get" and fid == "deck": + return {"md5Checksum": self.md5} + if verb[-1] == "get" and fid == "wb": + return {"appProperties": {rwb.STAMP: self.stamp} if self.stamp else {}} + if verb[-1] == "create": + self.banner = True + return {"id": "wb"} + if verb[-1] == "update": + self.stamp = body["appProperties"][rwb.STAMP] + return {} + raise AssertionError("unexpected call %r %r" % (verb, params)) + + def download(self, fid, out): + assert fid == "deck" + with open(out, "wb") as fh: + fh.write(self.deck) + + def upload(self, fid, path, mime): + if self.fail_upload: + raise RuntimeError("upload blew up") + self.calls.append(("upload", fid, None, None)) + + def render(self, fid, out, slide_index=0): + assert (fid, slide_index) == ("deck", rwb.SLIDE_INDEX) + self.renders += 1 + with open(out, "wb") as fh: + fh.write(b"\x89PNG fake") + return out + + +def run(fake, write=True, name="Boston", create=False): + with mock.patch.object(cc, "list_children", fake.list_children), \ + mock.patch.object(cc, "gws_json", fake.gws_json), \ + mock.patch.object(cc, "gws_download", fake.download), \ + mock.patch.object(cc, "gws_upload", fake.upload), \ + mock.patch.object(rwb.slides_export, "render_slide_png", fake.render), \ + tempfile.TemporaryDirectory() as tmp: + return rwb.sync_chapter("boston", name, write, tmp, create=create) + + +class TestSlideText(unittest.TestCase): + def test_runs_with_and_without_attributes_are_read(self): + with tempfile.NamedTemporaryFile(suffix=".pptx", delete=False) as fh: + fh.write(_pptx(["AAIF Community Events", "New York"])) + self.addCleanup(os.remove, fh.name) + self.assertEqual(rwb.slide_text(fh.name), "AAIF Community Events New York") + + def test_names_chapter_is_case_and_space_blind(self): + self.assertTrue(rwb.names_chapter("AAIF Community Events new york", "New York")) + + def test_another_city_is_refused(self): + self.assertFalse(rwb.names_chapter("AAIF Community Events San Francisco", "Boston")) + + def test_a_slide_without_the_headline_is_refused(self): + self.assertFalse(rwb.names_chapter("Boston", "Boston")) + + +class TestSync(unittest.TestCase): + def test_a_deck_naming_another_city_is_never_rendered(self): + fake = FakeDrive(["AAIF Community Events", "San Francisco"]) + r = run(fake) + self.assertIsNotNone(r.error) + self.assertEqual(fake.renders, 0) + self.assertNotIn("upload", [c[0] for c in fake.calls]) + + def test_a_banner_stamped_with_the_current_deck_is_skipped(self): + fake = FakeDrive(["AAIF Community Events", "Boston"], banner_stamp="m1") + self.assertEqual(run(fake), rwb.Result("Boston", "current")) + self.assertEqual(fake.renders, 0) + + def test_a_moved_deck_is_re_rendered_and_restamped(self): + fake = FakeDrive(["AAIF Community Events", "Boston"], banner_stamp="old", md5="new") + self.assertEqual(run(fake).action, "render") + self.assertEqual(fake.renders, 1) + self.assertEqual(fake.stamp, "new") + + def test_report_mode_renders_and_writes_nothing(self): + fake = FakeDrive(["AAIF Community Events", "Boston"]) + self.assertEqual(run(fake, write=False).action, "render") + self.assertEqual(fake.renders, 0) + self.assertEqual([c[0] for c in fake.calls], ["get", "get"]) + + def test_a_failed_upload_is_not_stamped_current(self): + fake = FakeDrive(["AAIF Community Events", "Boston"], banner_stamp="old") + fake.fail_upload = True + r = run(fake) + self.assertIn("upload blew up", r.error) + self.assertEqual(fake.stamp, "old") + + def test_a_sweep_never_creates_a_missing_banner(self): + fake = FakeDrive(["AAIF Community Events", "Boston"], banner=False) + self.assertEqual(run(fake).action, "absent") + self.assertNotIn("create", [c[0] for c in fake.calls]) + self.assertEqual(fake.renders, 0) + + def test_templatecity_is_checked_against_the_city_it_clones_from(self): + fake = FakeDrive(["AAIF Community Events", "San Francisco"]) + self.assertEqual(run(fake, name="TemplateCity").action, "render") + + def test_a_missing_banner_is_created_once_without_retries(self): + fake = FakeDrive(["AAIF Community Events", "Boston"], banner=False) + self.assertEqual(run(fake, create=True).action, "render") + creates = [c for c in fake.calls if c[0] == "create"] + self.assertEqual(len(creates), 1) + self.assertIs(creates[0][2], gwsmod.NO_RETRY) + self.assertEqual(creates[0][3]["parents"], ["boston"]) + + def test_errors_are_returned_not_raised(self): + fake = FakeDrive(["AAIF Community Events", "Boston"]) + fake.list_children = lambda fid: (_ for _ in ()).throw(OSError("drive down")) + self.assertIn("drive down", run(fake).error) + + +if __name__ == "__main__": + unittest.main() From 6b85073ac496d16839020a1c99f9781dd897bab9 Mon Sep 17 00:00:00 2001 From: Rahul Parundekar Date: Tue, 3 Nov 2026 12:12:46 -0800 Subject: [PATCH 2/3] fix: address self-review findings - Stamp the deck checksum and the chapter name, so a renamed folder with an unchanged deck is checked again instead of reported current. - Check the slide the renderer draws: resolve slide 4 by presentation order in the deck, and re-check the rendered page's own text in render_slide_png. - Match the slide text exactly (headline plus name), unescape XML entities. - render_slide_png: refuse a non-PNG download, name a short deck, and with strict_cleanup fail on an untrashed temp copy. A sweep skips TemplateCity unless it is named. - The hooks get the Result: "already current" is no longer printed as "Rendered". Create exits 2 on a banner failure and skips a dirty deck. Rename never creates a banner, warns on stderr, and says so on a failed verify. Fallback commands print the script's absolute path. - Tests: upload and stamp land on the banner, missing checksum, duplicate ids, main() exit codes, the rename hook, slide order and the renderer guards. Co-Authored-By: Claude Opus 5.5 --- lib/aaif_events/slides_export.py | 62 +++- lib/aaif_events/tests/test_slides_export.py | 77 ++++- skills/aaif-create-chapter/SKILL.md | 20 +- .../references/rename-chapter.md | 7 +- .../scripts/create_chapter.py | 35 ++- .../scripts/rename_chapter.py | 39 ++- .../scripts/render_web_banner.py | 250 ++++++++++----- .../scripts/test_create_chapter.py | 43 ++- .../scripts/test_rename_chapter.py | 33 ++ .../scripts/test_render_web_banner.py | 293 ++++++++++++++---- 10 files changed, 664 insertions(+), 195 deletions(-) diff --git a/lib/aaif_events/slides_export.py b/lib/aaif_events/slides_export.py index 8b118f9..2733efb 100644 --- a/lib/aaif_events/slides_export.py +++ b/lib/aaif_events/slides_export.py @@ -26,6 +26,25 @@ _gws_json = _gws_mod.json_out +_PNG_MAGIC = b"\x89PNG\r\n\x1a\n" + + +def _page_text(page): + """Every text run on a Slides API page, top-level shapes and groups, joined + by spaces.""" + out = [] + + def walk(elements): + for el in elements or (): + for text_el in ((el.get("shape") or {}).get("text") or {}).get("textElements", ()): + run = (text_el.get("textRun") or {}).get("content") + if run: + out.append(run) + walk((el.get("elementGroup") or {}).get("children")) + walk(page.get("pageElements")) + return " ".join(out) + + def _download(url, out_path, timeout=30): """Fetch `url` to `out_path` with a bounded timeout. The bytes land in a temp file that is renamed over `out_path` only after the too-small sanity @@ -39,18 +58,34 @@ def _download(url, out_path, timeout=30): fh.write(r.read()) if os.path.getsize(tmp) < 1000: raise RuntimeError("rendered thumbnail suspiciously small (%s)" % out_path) + # Size alone let an HTML interstitial or error page through as a + # "PNG", and a caller that stamps what it uploads would then mark that + # page current and never look at it again. + with open(tmp, "rb") as fh: + if fh.read(8) != _PNG_MAGIC: + raise RuntimeError("rendered thumbnail is not a PNG (%s)" % out_path) os.replace(tmp, out_path) finally: if os.path.exists(tmp): # gone on success (os.replace consumed it) os.remove(tmp) -def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000_PX"): +def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000_PX", + check_text=None, strict_cleanup=False): """Export slide `slide_index` (0-based) of the Drive pptx `file_id` to a PNG at `out_path`. Makes a throwaway Google Slides copy to render from (Slides thumbnails only work on native Slides files, not stored .pptx blobs) and - trashes it afterward — the source file is never modified.""" + trashes it afterward — the source file is never modified. + + `check_text(text) -> error or None` is run on the text of the page that is + about to be rendered, the same page object, so a caller can refuse a slide + without trusting that `slideN.xml` numbering follows presentation order. + `strict_cleanup` turns a failure to trash the throwaway copy into an + exception (naming its id) instead of a stderr warning, for callers that + report per-file success: a stray copy beside TemplateCity's decks is + cloned into every later chapter.""" presentation_id = None + cleanup_error = None try: # NO_RETRY: a `files.copy` that succeeded server-side but answered like # a timeout would, on a retry, make a SECOND throwaway copy — and the @@ -63,16 +98,27 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 body={"name": "TEMP - render_slide_png", "mimeType": _SLIDES_MIME}) presentation_id = copy["id"] + fields = ("slides(objectId,pageElements(shape/text/textElements/textRun/content," + "elementGroup))") if check_text else "slides.objectId" presentation = _gws_json("slides", "presentations", "get", - params={"presentationId": presentation_id, "fields": "slides.objectId"}) - page_object_id = presentation["slides"][slide_index]["objectId"] + params={"presentationId": presentation_id, "fields": fields}) + slides = presentation.get("slides") or [] + if slide_index >= len(slides): + raise RuntimeError("deck %s has %d slide(s); slide %d was requested" + % (file_id, len(slides), slide_index + 1)) + page_object_id = slides[slide_index]["objectId"] + if check_text: + err = check_text(_page_text(slides[slide_index])) + if err: + raise RuntimeError(err) thumb = _gws_json("slides", "presentations", "pages", "getThumbnail", params={"presentationId": presentation_id, "pageObjectId": page_object_id, "thumbnailProperties.thumbnailSize": thumbnail_size}) os.makedirs(os.path.dirname(out_path) or ".", exist_ok=True) + if not thumb.get("contentUrl"): + raise RuntimeError("no thumbnail URL for slide %d of %s" % (slide_index + 1, file_id)) _download(thumb["contentUrl"], out_path) - return out_path finally: # The throwaway copy has no `parents` set, so it lands in the SAME # folder as the source file - not an out-of-sight scratch location. @@ -90,3 +136,9 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 except Exception as e: print("WARNING: could not trash temp Slides copy %s: %s" % (presentation_id, e), file=sys.stderr) + cleanup_error = e + if cleanup_error is not None and strict_cleanup: + raise RuntimeError("rendered, but the temp Slides copy %s beside the source " + "could not be trashed (%s); trash it by hand" + % (presentation_id, cleanup_error)) + return out_path diff --git a/lib/aaif_events/tests/test_slides_export.py b/lib/aaif_events/tests/test_slides_export.py index ddac027..ee6424a 100644 --- a/lib/aaif_events/tests/test_slides_export.py +++ b/lib/aaif_events/tests/test_slides_export.py @@ -197,6 +197,68 @@ def test_successful_render_returns_out_path_and_trashes_copy(self): self.assertEqual(out, "/tmp/out.png") +class TestRenderGuards(unittest.TestCase): + """The page-text check, the slide count and strict cleanup.""" + + PAGE = {"objectId": "p4", "pageElements": [ + {"shape": {"text": {"textElements": [{"textRun": {"content": "AAIF Community Events"}}]}}}, + {"elementGroup": {"children": [ + {"shape": {"text": {"textElements": [{"textRun": {"content": "Boston"}}]}}}]}}]} + + def _run(self, slides, trash_fails=False, **kw): + calls = [] + + def fake(*args, **kwargs): + calls.append((args, kwargs)) + if args == ("drive", "files", "copy"): + return {"id": "pres1"} + if args == ("slides", "presentations", "get"): + return {"slides": slides} + if args[-1] == "getThumbnail": + return {"contentUrl": "https://example.invalid/x.png"} + if args == ("drive", "files", "update"): + if trash_fails: + raise RuntimeError("trash failed") + return {} + raise AssertionError(args) + with mock.patch.object(se, "_gws_json", side_effect=fake), \ + mock.patch.object(se, "_download") as dl, \ + mock.patch("sys.stderr"): + try: + return se.render_slide_png("file1", "/tmp/out.png", **kw), calls, dl + except RuntimeError as e: + return e, calls, dl + + def test_check_text_sees_the_page_being_rendered_including_groups(self): + seen = [] + out, calls, dl = self._run([{"objectId": "p1"}, self.PAGE], slide_index=1, + check_text=lambda t: seen.append(t)) + self.assertEqual(out, "/tmp/out.png") + self.assertEqual(seen, ["AAIF Community Events Boston"]) + thumb = [k for a, k in calls if a[-1] == "getThumbnail"][0] + self.assertEqual(thumb["params"]["pageObjectId"], "p4") + + def test_a_refused_page_is_never_rendered_and_the_copy_is_trashed(self): + err, calls, dl = self._run([self.PAGE], check_text=lambda t: "wrong city") + self.assertIn("wrong city", str(err)) + self.assertFalse(dl.called) + self.assertNotIn("getThumbnail", [a[-1] for a, _ in calls]) + self.assertIn(("drive", "files", "update"), [a for a, _ in calls]) + + def test_a_short_deck_names_the_slide_and_the_count(self): + err, _c, _d = self._run([{"objectId": "p1"}], slide_index=3) + self.assertIn("has 1 slide(s); slide 4", str(err)) + + def test_strict_cleanup_raises_with_the_orphan_id(self): + err, _c, _d = self._run([self.PAGE], trash_fails=True, strict_cleanup=True) + self.assertIn("pres1", str(err)) + self.assertIn("could not be trashed", str(err)) + + def test_default_cleanup_failure_still_returns(self): + out, _c, _d = self._run([self.PAGE], trash_fails=True) + self.assertEqual(out, "/tmp/out.png") + + class TestDownload(unittest.TestCase): """_download must never leave a partial PNG behind, whatever failed.""" @@ -210,9 +272,9 @@ def test_success_writes_the_file_with_a_timeout(self): with tempfile.TemporaryDirectory() as d: out = os.path.join(d, "x.png") with mock.patch("urllib.request.urlopen", - return_value=self._resp(b"p" * 2000)) as uo: + return_value=self._resp(se._PNG_MAGIC + b"p" * 2000)) as uo: self.assertIsNone(se._download("https://example.invalid/x.png", out)) - self.assertEqual(os.path.getsize(out), 2000) + self.assertEqual(os.path.getsize(out), 2008) self.assertEqual(uo.call_args.kwargs.get("timeout"), 30) def test_a_too_small_render_is_deleted_not_left_behind(self): @@ -224,6 +286,17 @@ def test_a_too_small_render_is_deleted_not_left_behind(self): self.assertIn("suspiciously small", str(cm.exception)) self.assertFalse(os.path.exists(out)) + def test_a_non_png_body_is_refused_and_deleted(self): + """An HTML interstitial is large enough to pass the size check.""" + with tempfile.TemporaryDirectory() as d: + out = os.path.join(d, "x.png") + with mock.patch("urllib.request.urlopen", + return_value=self._resp(b"" + b"x" * 2000)): + with self.assertRaises(RuntimeError) as cm: + se._download("https://example.invalid/x.png", out) + self.assertIn("not a PNG", str(cm.exception)) + self.assertEqual(os.listdir(d), []) + def test_a_read_failure_deletes_the_partial_file(self): r = self._resp(b"") r.read.side_effect = TimeoutError("timed out") diff --git a/skills/aaif-create-chapter/SKILL.md b/skills/aaif-create-chapter/SKILL.md index 18bfa3c..db24a59 100644 --- a/skills/aaif-create-chapter/SKILL.md +++ b/skills/aaif-create-chapter/SKILL.md @@ -195,12 +195,20 @@ template or the rebrand engine is broken. Fix that — don't resume. hand it TemplateCity's agent. That is `upload_agents.py`'s job — see `references/design-system-sweep.md`. - **`Web Banner.png` is rendered, not cloned.** The city is drawn into the - image, so no text rebrand reaches it, and until 2026-11 every new chapter - kept TemplateCity's "San Francisco" banner. `render_web_banner.py` renders it - from slide 4 of the chapter's own `Banners (…)/Square Logo.pptx` through - Google's renderer. `create_chapter` and `rename_chapter` call it after a - write; it refuses a slide that does not name the chapter, and skips a banner - stamped with the deck's current checksum, so a full run is cheap: + image, so no text rebrand reaches it, and until 2026-11 every chapter cloned + from TemplateCity kept its "San Francisco" banner. `render_web_banner.py` + renders it from the fourth slide of the chapter's own + `Banners (…)/Square Logo.pptx` through Google's renderer. + - `create_chapter` calls it after a clean write (exit `2` if the banner + failed). `rename_chapter` calls it only after the rename verifies (exit `1` + if it failed). + - It refuses a slide that does not say exactly "AAIF Community Events" plus + the chapter's name. + - It skips a banner stamped with the current deck and name, so a re-run is + cheap. + - A sweep leaves TemplateCity alone unless named with `--chapter`, and never + adds a banner to a folder that has none. + - Exit codes: `2` report-only with banners to render, `1` any failure. ```bash python3 /scripts/render_web_banner.py # report python3 /scripts/render_web_banner.py --write diff --git a/skills/aaif-create-chapter/references/rename-chapter.md b/skills/aaif-create-chapter/references/rename-chapter.md index 857409f..4edd63d 100644 --- a/skills/aaif-create-chapter/references/rename-chapter.md +++ b/skills/aaif-create-chapter/references/rename-chapter.md @@ -67,9 +67,10 @@ Also deliberate: uploaded, that no names were changed, and where the originals are. - **What the text pass cannot see, checked 2026-11-03.** The Japan and New Zealand renames verified clean while four things still said the old city: - - **`Web Banner.png`** has the name drawn in. A successful `--write` now - re-renders it from the rewritten `Square Logo.pptx` (`render_web_banner.py`) - and exits `1` if that fails. + - **`Web Banner.png`** has the name drawn in. A `--write` that verifies now + re-renders it from `Square Logo.pptx` (`render_web_banner.py`) and exits + `1` if that fails. A failed verify skips it and prints the command to run + after the fix. A folder with no banner is not given one. - **The `Badges/` set** is named and lettered with a *lowercase* slug and UPPER SVG text, which the Title/UPPER name pass does not match. Regenerate it with `aaif-sync-badges` (`--chapter `), then trash the old-slug files. diff --git a/skills/aaif-create-chapter/scripts/create_chapter.py b/skills/aaif-create-chapter/scripts/create_chapter.py index 1103ce5..9b7bdcc 100755 --- a/skills/aaif-create-chapter/scripts/create_chapter.py +++ b/skills/aaif-create-chapter/scripts/create_chapter.py @@ -802,21 +802,32 @@ def main(): # survivor is a PII copy on disk, so say so rather than hide it. print("WARNING: could not remove temp dir %s — it holds downloaded " "workbooks; delete it by hand." % ctx["tmp"], file=sys.stderr) - print("\nDone. New chapter folder id: %s" % new_id) - print("https://drive.google.com/drive/folders/%s" % new_id) # The clone copied TemplateCity's Web Banner.png, an image that says "San # Francisco" and that the text rebrand cannot reach. Re-render it from this - # chapter's own (rebranded) Square Logo deck. A failure here is not a - # failed chapter: say so and name the command that finishes the job. + # chapter's own (rebranded) Square Logo deck — unless a fresh file still + # carries source tokens, in which case the deck may too, and the exit-1 + # path below is the real problem. A banner failure is not a failed clone: + # it is "done, one follow-up", exit 2, with the command that finishes it. import render_web_banner # lazy: it imports this module - err = render_web_banner.sync_one(new_id, name) - if err: - print("WARNING: %s was NOT re-rendered (%s) — it still shows the template " - "city. Run: render_web_banner.py --chapter %r --write" - % (render_web_banner.BANNER, err, name), file=sys.stderr) + banner_failed = False + if ctx["residuals"]: + print("\n%s not rendered: fresh files still carry source tokens (below)." + % render_web_banner.BANNER) else: - print("Rendered %s from this chapter's %s." % (render_web_banner.BANNER, - render_web_banner.DECK)) + r = render_web_banner.sync_one(new_id, name, create=True) + if r.error is not None: + banner_failed = True + print("WARNING: %s was NOT re-rendered (%s) — it still shows the template " + "city. Run: %s" % (render_web_banner.BANNER, r.error, + render_web_banner.fallback_command(name)), + file=sys.stderr) + elif r.action == render_web_banner.CURRENT: + print("%s already current." % render_web_banner.BANNER) + else: + print("Rendered %s from this chapter's %s." % (render_web_banner.BANNER, + render_web_banner.DECK)) + print("\nDone. New chapter folder id: %s" % new_id) + print("https://drive.google.com/drive/folders/%s" % new_id) if status == "absent": print("REMINDER: create the Luma page at https://luma.com/aaif-%s (it is not live yet)." % slug) elif status == "unknown": @@ -842,6 +853,8 @@ def main(): "--resume --repair-existing for design assets, fix CRM/Tracker by hand" % len(ctx["existing_residuals"]), file=sys.stderr) sys.exit(2) + if banner_failed: + sys.exit(2) if __name__ == "__main__": main() diff --git a/skills/aaif-create-chapter/scripts/rename_chapter.py b/skills/aaif-create-chapter/scripts/rename_chapter.py index c5c702d..99fc326 100755 --- a/skills/aaif-create-chapter/scripts/rename_chapter.py +++ b/skills/aaif-create-chapter/scripts/rename_chapter.py @@ -439,24 +439,15 @@ def main(): print("VERIFY FAILED — still carrying the old name, or not inspected:") for x in left: print(" " + x) + import render_web_banner + print("%s was NOT re-rendered either. After fixing the files: %s" + % (render_web_banner.BANNER, render_web_banner.fallback_command(a.new))) return 1 print("Verified: no file name, and no text in any %s, still says %r%s." % ("/".join(OFFICE), a.old, " (native Google files and member data were not inspected)" if (native or skipped) else "")) - # Web Banner.png has the city drawn into the image, which no text pass - # reaches. It is rendered from the Square Logo deck just rewritten, and - # the deck's checksum moved, so this re-renders it (or is a no-op). - import render_web_banner - err = render_web_banner.sync_one(folder_id, a.new) - if err: - print("WARNING: %s still shows the old name (%s). Run: " - "render_web_banner.py --chapter %r --write" - % (render_web_banner.BANNER, err, a.new)) - return 1 - print("%s rendered from the renamed %s." % (render_web_banner.BANNER, - render_web_banner.DECK)) - return 0 + return rerender_banner(folder_id, a.new) finally: # Member data on disk, cleaned up. The report path used to leave every # chapter workbook in a predictable world-readable /tmp directory. @@ -464,5 +455,27 @@ def main(): shutil.rmtree(scan, ignore_errors=True) +def rerender_banner(folder_id, new_name): + """Re-render Web Banner.png after a verified rename; returns the exit code. + + The banner has the city drawn into the image, which no text pass reaches. + It is rendered from the Square Logo deck, and its stamp carries the + chapter name, so a new name re-renders it even when the deck itself did + not change. A folder with no banner is left without one, as in a sweep.""" + import render_web_banner + r = render_web_banner.sync_one(folder_id, new_name, create=False) + if r.error is not None: + print("WARNING: %s still shows the old name (%s). Run: %s" + % (render_web_banner.BANNER, r.error, + render_web_banner.fallback_command(new_name)), file=sys.stderr) + return 1 + print({render_web_banner.RENDER: "%s rendered from the renamed %s." + % (render_web_banner.BANNER, render_web_banner.DECK), + render_web_banner.CURRENT: "%s already current." % render_web_banner.BANNER, + render_web_banner.ABSENT: "This chapter has no %s; none was added." + % render_web_banner.BANNER}[r.action]) + return 0 + + if __name__ == "__main__": sys.exit(main()) diff --git a/skills/aaif-create-chapter/scripts/render_web_banner.py b/skills/aaif-create-chapter/scripts/render_web_banner.py index 9c6a506..d6d3012 100755 --- a/skills/aaif-create-chapter/scripts/render_web_banner.py +++ b/skills/aaif-create-chapter/scripts/render_web_banner.py @@ -4,28 +4,45 @@ `Web Banner.png` sits at the root of every chapter folder: the AAIF mark over "AAIF Community Events / ". It is an image, so `create_chapter.py`'s rebrand — which rewrites OOXML text — cannot reach it, and the clone copied -TemplateCity's PNG byte for byte. Found 2026-11-03: dozens of chapters' banners -said "San Francisco", and every renamed chapter's still showed its old name. +TemplateCity's PNG byte for byte. Found 2026-11-03: every chapter cloned before +then (37 of them) had a "San Francisco" banner, and every renamed chapter's +still showed its old name. The same composition already exists as text, which the rebrand does reach: -slide 4 of `Banners (…)/Square Logo.pptx`. So the banner is not drawn here at -all; it is that slide, rendered by Google's own Slides renderer -(`aaif_events.slides_export.render_slide_png`, never LibreOffice), and so it is -correct whenever the deck is. - -Two guards: - - * **The slide must name the chapter.** Rendering a deck that still says - another city would publish the wrong name with a fresh timestamp on it, so - a slide whose text lacks the chapter's name is reported and skipped. - * **A banner records the deck it came from.** The upload stamps - `appProperties.aaifBannerDeck` with the deck's `md5Checksum`. A re-run - skips a chapter whose stamp matches, so this is cheap to re-run, and a deck - that `rename_chapter.py` has rewritten is picked up automatically because - its checksum moved. (Renders are not byte-stable, so comparing PNG bytes - would re-upload every banner on every run.) - -Read-only by default. +slide 4 of `Banners (…)/Square Logo.pptx`, a 2000 x 2000 square like the PNG it +replaces. So the banner is not drawn here at all; it is that slide, rendered by +Google's own Slides renderer (`aaif_events.slides_export.render_slide_png`, +never LibreOffice), and so it is correct whenever the deck is. + +Guards: + + * **The slide must say exactly "AAIF Community Events" and the chapter's + name.** Rendering a deck that names another city would publish the wrong + name with a fresh timestamp on it. The check is exact, not a substring + test, so "Portland, OR" does not pass for Portland and a slide still + carrying a leftover city fails. It runs twice: on the deck's own slide 4 + (resolved through `presentation.xml`'s slide order, not the `slideN.xml` + part name) before anything is written, and again by the renderer on the + page it is about to draw. + * **A banner records what it was rendered from.** The upload stamps + `appProperties.aaifBannerDeck` with the deck's `md5Checksum` AND the + chapter name. A re-run skips a chapter whose stamp matches, so it is cheap + to re-run; a deck that `rename_chapter.py` rewrote, or a folder renamed by + hand, no longer matches and is checked again. (Renders are not byte-stable, + so comparing PNG bytes would re-upload every banner on every run.) Drive + scopes `appProperties` to the OAuth client that wrote them, so an operator + on a different `gws` client sees no stamps and re-renders everything once: + slower, never wrong. + * **A stray temp copy is a failure.** The renderer's throwaway Slides copy + lands beside the deck; if it cannot be trashed, that chapter FAILS and the + copy's id is printed. + +A sweep skips TemplateCity (its deck says San Francisco by design, and a stray +temp copy there would be cloned into every later chapter) unless it is named +with `--chapter`, and never creates a banner in a folder that has none. + +Read-only by default. Exit codes: 0 all current/rendered, 2 report-only with +banners to render, 1 any failure. Usage: python3 render_web_banner.py # report @@ -33,7 +50,9 @@ python3 render_web_banner.py --chapter Aarhus --write """ import argparse +import html import os +import posixpath import re import sys import tempfile @@ -51,19 +70,27 @@ #: The banners subfolder is "Banners (Chapter Specific, Changed Rarely)"; match #: the stem so a reworded parenthetical does not hide it. BANNERS_DIR = re.compile(r"^Banners\b") -#: 0-based: slide 4 is the "AAIF Community Events / " square. +#: 0-based: the fourth slide in presentation order is the "AAIF Community +#: Events / " square. SLIDE_INDEX = 3 HEADLINE = "AAIF Community Events" STAMP = "aaifBannerDeck" -#: The template's deck says the city it is cloned FROM, by design. -DECK_NAME = {"TemplateCity": "San Francisco"} PNG = "image/png" +TEMPLATE = "TemplateCity" +#: Folder name -> the name its deck is expected to show, where they differ. +#: TemplateCity's deck carries the rebrand source city by design. +DECK_NAME = {TEMPLATE: cc.SOURCE_NAME} +#: Absolute, so the printed fallback command works from an installed plugin. +SCRIPT = os.path.abspath(__file__) + +CURRENT, RENDER, ABSENT = "current", "render", "absent" class Result(NamedTuple): + """One chapter's outcome. Exactly one of `action` / `error` is set.""" name: str - #: "current", "render" (would/did render), "absent" (no banner and not - #: asked to create one), or None when `error` is set. + #: CURRENT (stamp matched, nothing done), RENDER (would render / rendered), + #: or ABSENT (no banner, and not asked to create one). action: Optional[str] = None error: Optional[str] = None @@ -72,86 +99,139 @@ def _norm(s): return re.sub(r"\s+", " ", s).strip().casefold() -def slide_text(pptx_path, index=SLIDE_INDEX): - """Every text run on slide `index` (0-based), joined by spaces.""" - with zipfile.ZipFile(pptx_path) as z: - xml = z.read("ppt/slides/slide%d.xml" % (index + 1)).decode("utf-8", "replace") - return " ".join(re.findall(r"]*)?>([^<]*)", xml)) +def fallback_command(name): + return "python3 %s --chapter %r --write" % (SCRIPT, name) + + +def expected_name(folder): + return DECK_NAME.get(folder, folder) + + +def stamp_value(md5, want): + return "%s|%s" % (md5, _norm(want)) + + +def name_mismatch(text, want): + """None when `text` is exactly the headline plus `want`, else the reason. + Exact rather than "contains": a slide saying "Portland, OR" must not pass + for Portland, nor one still carrying the template city beside the new one. + Trailing punctuation is ignored (the decks' display style puts a full stop + after the city in some layouts).""" + t, head = _norm(text), _norm(HEADLINE) + if not t.startswith(head): + return "slide %d does not start with %r (it says %r)" % (SLIDE_INDEX + 1, HEADLINE, text[:80]) + rest = re.sub(r"^[\W_]+|[\W_]+$", "", t[len(head):]) + if rest != _norm(want): + return ("slide %d says %r, expected %r — fix the deck, or map the folder in " + "DECK_NAME if the deck is right" % (SLIDE_INDEX + 1, rest, want)) + return None -def names_chapter(text, name): - """True when the slide is the community-events square and names `name`.""" - t = _norm(text) - return _norm(HEADLINE) in t and _norm(name) in t +def slide_part(z, index=SLIDE_INDEX): + """The zip part of the `index`-th slide (0-based) in presentation order. -def chapters(): + Order lives in `presentation.xml`'s ``, not in the part names: + a reordered deck keeps `slide4.xml` wherever the slide now sits, while the + renderer draws the fourth slide in order.""" + pres_xml = z.read("ppt/presentation.xml").decode("utf-8", "replace") + rids = re.findall(r']*\br:id="([^"]+)"', pres_xml) + if index >= len(rids): + raise RuntimeError("deck has %d slide(s); slide %d is needed" % (len(rids), index + 1)) + rels = z.read("ppt/_rels/presentation.xml.rels").decode("utf-8", "replace") + for rel in re.findall(r"]*>", rels): + if re.search(r'\bId="%s"' % re.escape(rids[index]), rel): + target = re.search(r'\bTarget="([^"]+)"', rel).group(1) + if target.startswith("/"): # package-absolute + return target[1:] + return posixpath.normpath(posixpath.join("ppt", target)) + raise RuntimeError("slide %d's relationship %s is missing" % (index + 1, rids[index])) + + +def slide_text(pptx_path, index=SLIDE_INDEX): + """Every text run on the `index`-th slide (presentation order), unescaped + and joined by spaces.""" + with zipfile.ZipFile(pptx_path) as z: + xml = z.read(slide_part(z, index)).decode("utf-8", "replace") + return " ".join(html.unescape(r) + for r in re.findall(r"]*)?>([^<]*)", xml)) + + +def chapters(include_template=False): """[(id, name)] for every chapter folder under Chapters, sorted.""" return sorted(((c["id"], c["name"]) for c in cc.list_children(cc.CHAPTERS_PARENT) - if c["mimeType"] == cc.FOLDER), key=lambda t: t[1]) + if c["mimeType"] == cc.FOLDER + and (include_template or c["name"] != TEMPLATE)), + key=lambda t: t[1]) -def _find(children, pred): +def _find(children, pred, what): hits = [c for c in children if pred(c)] if len(hits) > 1: - raise RuntimeError("%d matches for %s" % (len(hits), hits[0]["name"])) + raise RuntimeError("%d copies of %s (ids %s) — trash the extra one(s)" + % (len(hits), what, ", ".join(h["id"] for h in hits))) return hits[0] if hits else None def sync_chapter(chapter_id, name, write, tmpdir, create=False): - """Bring one chapter's banner up to date. Returns a `Result`, never raises: - this runs under a thread pool, and one chapter's failure must not end the - others. - - A chapter with no banner at all is created one only when `create` is set - (the create/rename hooks). A sweep reports it instead: the folders without - one predate the template that carries it (the Tatooine test chapter), and - a sweep has no business adding files to them.""" + """Bring one chapter's banner up to date. Returns a `Result`; never raises + an Exception, because this runs under a thread pool and one chapter's + failure must not end the others. + + A banner is created in a folder that has none only when `create` is set + (the create hook). A sweep reports such a folder instead: the ones without + a banner predate the template that carries it (the Tatooine test chapter), + and a sweep has no business adding files to them.""" + created = None try: + want = expected_name(name) kids = cc.list_children(chapter_id) bdir = _find(kids, lambda c: c["mimeType"] == cc.FOLDER - and BANNERS_DIR.match(c["name"])) + and BANNERS_DIR.match(c["name"]), "the Banners folder") if bdir is None: return Result(name, error="no Banners folder") - deck = _find(cc.list_children(bdir["id"]), lambda c: c["name"] == DECK) + deck = _find(cc.list_children(bdir["id"]), lambda c: c["name"] == DECK, DECK) if deck is None: return Result(name, error="no %s in %s" % (DECK, bdir["name"])) - meta = cc.gws_json("drive", "files", "get", params={ - "fileId": deck["id"], "supportsAllDrives": True, "fields": "md5Checksum"}) - md5 = meta.get("md5Checksum") + md5 = cc.gws_json("drive", "files", "get", params={ + "fileId": deck["id"], "supportsAllDrives": True, + "fields": "md5Checksum"}).get("md5Checksum") if not md5: + # Without this, a None checksum would compare equal to an unstamped + # banner's missing stamp and report it current forever. return Result(name, error="%s has no checksum (not a stored .pptx?)" % DECK) + stamp = stamp_value(md5, want) - banner = _find(kids, lambda c: c["name"] == BANNER) + banner = _find(kids, lambda c: c["name"] == BANNER, BANNER) if banner is None and not create: - return Result(name, "absent") + return Result(name, ABSENT) if banner is not None: - bmeta = cc.gws_json("drive", "files", "get", params={ + props = cc.gws_json("drive", "files", "get", params={ "fileId": banner["id"], "supportsAllDrives": True, - "fields": "appProperties"}) - if (bmeta.get("appProperties") or {}).get(STAMP) == md5: - return Result(name, "current") + "fields": "appProperties"}).get("appProperties") or {} + if props.get(STAMP) == stamp: + return Result(name, CURRENT) local = os.path.join(tmpdir, "%s.pptx" % chapter_id) cc.gws_download(deck["id"], local) try: - text = slide_text(local) + why = name_mismatch(slide_text(local), want) finally: os.remove(local) - want = DECK_NAME.get(name, name) - if not names_chapter(text, want): - return Result(name, error="slide %d of %s does not say %r with %r — fix " - "the deck first" % (SLIDE_INDEX + 1, DECK, HEADLINE, want)) + if why: + return Result(name, error=why) if not write: - return Result(name, "render") + return Result(name, RENDER) png = os.path.join(tmpdir, "%s.png" % chapter_id) - slides_export.render_slide_png(deck["id"], png, slide_index=SLIDE_INDEX) try: + slides_export.render_slide_png( + deck["id"], png, slide_index=SLIDE_INDEX, strict_cleanup=True, + check_text=lambda text: name_mismatch(text, want)) if banner is None: # NO_RETRY: a create that landed but answered like a timeout # would, re-sent, leave two banners in the folder. - banner = cc.gws_json( + banner = created = cc.gws_json( "drive", "files", "create", retries=gwsmod.NO_RETRY, params={"supportsAllDrives": True, "fields": "id"}, body={"name": BANNER, "parents": [chapter_id], "mimeType": PNG}) @@ -159,33 +239,37 @@ def sync_chapter(chapter_id, name, write, tmpdir, create=False): # Stamp last: a banner whose upload failed must not be marked current. cc.gws_json("drive", "files", "update", params={"fileId": banner["id"], "supportsAllDrives": True}, - body={"appProperties": {STAMP: md5}}) + body={"appProperties": {STAMP: stamp}}) finally: if os.path.exists(png): os.remove(png) - return Result(name, "render") + return Result(name, RENDER) except Exception as e: - return Result(name, error="%s: %s" % (type(e).__name__, str(e)[:200])) + msg = "%s: %s" % (type(e).__name__, str(e)[:400]) + if created is not None: + msg = ("created an EMPTY %s (id %s) but did not finish: %s — re-run to " + "fill it" % (BANNER, created["id"], msg)) + return Result(name, error=msg) -def sync_one(chapter_id, name): - """Render one chapter's banner now (used by create/rename after a write). - Returns an error string, or None on success.""" +def sync_one(chapter_id, name, create): + """Render one chapter's banner now (the create/rename hooks). Returns the + `Result`, so a caller can tell a real render from an already-current one.""" with tempfile.TemporaryDirectory() as tmp: - r = sync_chapter(chapter_id, name, True, tmp, create=True) - return r.error + return sync_chapter(chapter_id, name, True, tmp, create=create) -def main(): +def main(argv=None): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--write", action="store_true", help="render and upload (default: report only)") - ap.add_argument("--chapter", help="only this chapter (exact name, case-insensitive)") + ap.add_argument("--chapter", help="only this chapter (exact name, case-insensitive; " + "the only way to include TemplateCity)") ap.add_argument("--jobs", type=int, default=4) - a = ap.parse_args() + a = ap.parse_args(argv) - todo = chapters() + todo = chapters(include_template=bool(a.chapter)) if a.chapter: todo = [(i, n) for i, n in todo if n.lower() == a.chapter.lower()] if not todo: @@ -200,13 +284,17 @@ def main(): if r.error is not None: failed.append(r) print(" FAILED %-26s %s" % (r.name, r.error)) - elif r.action == "absent": - absent.append(r.name) - elif r.action == "render": + elif r.action == RENDER: pending.append(r.name) print(" %s %s" % ("rendered " if a.write else "would render", r.name)) - else: + elif r.action == ABSENT: + absent.append(r.name) + elif r.action == CURRENT: current += 1 + else: + # An outcome this loop does not know is not "clean". + failed.append(r) + print(" FAILED %-26s unknown outcome %r" % (r.name, r.action)) print("\n%d %s, %d already current, %d failed." % (len(pending), "rendered" if a.write else "to render", current, len(failed))) if absent: diff --git a/skills/aaif-create-chapter/scripts/test_create_chapter.py b/skills/aaif-create-chapter/scripts/test_create_chapter.py index fb6db89..0603c3f 100644 --- a/skills/aaif-create-chapter/scripts/test_create_chapter.py +++ b/skills/aaif-create-chapter/scripts/test_create_chapter.py @@ -11,7 +11,7 @@ from unittest import mock sys.path.insert(0, os.path.dirname(__file__)) -import create_chapter as cc +import create_chapter as cc # noqa: E402 import render_web_banner # noqa: E402 from aaif_events import gws as gwsmod # noqa: E402 @@ -742,27 +742,56 @@ def run_main(self, argv, clone=None, cap_calls=None, banner=None): lambda what="", **k: calls.append(what)), \ mock.patch.object(cc, "clone_and_rebrand", clone), \ mock.patch.object(render_web_banner, "sync_one", - banner or (lambda fid, name: None)): + banner or (lambda fid, name, create: + render_web_banner.Result(name, "render"))): cc.main() def test_a_write_re_renders_the_new_chapters_banner(self): """The clone copies TemplateCity's Web Banner.png, which says "San Francisco"; the text rebrand cannot reach an image.""" seen = [] + + def banner(fid, name, create): + seen.append((fid, name, create)) + return render_web_banner.Result(name, "render") with mock.patch("sys.stdout"): self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", - banner=lambda fid, name: seen.append((fid, name))) - self.assertEqual(seen, [("new-id", "Zed")]) + banner=banner) + self.assertEqual(seen, [("new-id", "Zed", True)]) - def test_a_failed_banner_render_warns_with_the_command(self): + def test_a_failed_banner_render_warns_and_exits_2(self): + """Done with one follow-up, like the existing-residuals path: a wrapper + checking the exit code must not read a San Francisco banner as success.""" import io err = io.StringIO() - with mock.patch("sys.stdout"), mock.patch("sys.stderr", err): + with mock.patch("sys.stdout"), mock.patch("sys.stderr", err), \ + self.assertRaises(SystemExit) as cm: self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", - banner=lambda fid, name: "slide 4 says the wrong city") + banner=lambda fid, name, create: render_web_banner.Result( + name, error="slide 4 says the wrong city")) + self.assertEqual(cm.exception.code, 2) self.assertIn("NOT re-rendered", err.getvalue()) self.assertIn("render_web_banner.py --chapter 'Zed' --write", err.getvalue()) + def test_an_already_current_banner_does_not_claim_a_render(self): + import io + out = io.StringIO() + with mock.patch("sys.stdout", out): + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=lambda fid, name, create: render_web_banner.Result( + name, "current")) + self.assertIn("already current", out.getvalue()) + self.assertNotIn("Rendered", out.getvalue()) + + def test_dirty_fresh_files_skip_the_banner(self): + """A deck still carrying source tokens could be rendered as-is.""" + def dirty_clone(_tpl, _parent, _name, ctx, existing_id=None): + ctx["residuals"].append(("Square Logo.pptx", ["San Francisco"])) + return "new-id" + with mock.patch("sys.stdout"), self.assertRaises(SystemExit): + self.run_main(["--city", "Zed", "--write"], clone=dirty_clone, + banner=lambda *a: self.fail("must not render a dirty deck")) + def test_a_plan_run_renders_no_banner(self): self.run_main(["--city", "Zed"], banner=lambda *a: self.fail("plan must not render")) diff --git a/skills/aaif-create-chapter/scripts/test_rename_chapter.py b/skills/aaif-create-chapter/scripts/test_rename_chapter.py index 62844f2..eef2cf5 100755 --- a/skills/aaif-create-chapter/scripts/test_rename_chapter.py +++ b/skills/aaif-create-chapter/scripts/test_rename_chapter.py @@ -225,6 +225,39 @@ def zipped(parts): finally: _redact.REDACT = False +# --- the banner hook after a verified rename -------------------------------- +import io as _io # noqa: E402 +from unittest import mock as _mock # noqa: E402 +import render_web_banner as _rwb # noqa: E402 + + +def _rerender(result): + seen, err, out = [], _io.StringIO(), _io.StringIO() + + def fake(fid, name, create): + seen.append((fid, name, create)) + return result + with _mock.patch.object(_rwb, "sync_one", fake), \ + _mock.patch("sys.stderr", err), _mock.patch("sys.stdout", out): + code = rc.rerender_banner("folder-1", "Edinburgh") + return code, seen, err.getvalue(), out.getvalue() + + +code, seen, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.RENDER)) +check("banner hook gets the NEW name and never creates a banner", + seen, [("folder-1", "Edinburgh", False)]) +check("...a render exits 0", code, 0) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh", error="slide 4 says Scotland")) +check("a failed banner render exits 1", code, 1) +check("...with the command on stderr", + "--chapter 'Edinburgh' --write" in err and "slide 4 says Scotland" in err, True) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.CURRENT)) +check("an already-current banner exits 0 and does not claim a render", + (code, "already current" in out, "rendered" in out), (0, True, False)) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.ABSENT)) +check("a folder with no banner exits 0 and says none was added", + (code, "none was added" in out), (0, True)) + print() print("FAILED %d check(s)" % len(FAILS) if FAILS else "rename_chapter: all checks passed.") diff --git a/skills/aaif-create-chapter/scripts/test_render_web_banner.py b/skills/aaif-create-chapter/scripts/test_render_web_banner.py index 461435a..2845ba6 100755 --- a/skills/aaif-create-chapter/scripts/test_render_web_banner.py +++ b/skills/aaif-create-chapter/scripts/test_render_web_banner.py @@ -3,15 +3,21 @@ The properties pinned here are the ones that fail quietly: - (a) **a deck that names another city is never rendered.** Rendering it would - publish the wrong name with a fresh date on it, which is worse than the - stale banner it replaces. - (b) **a banner stamped with the deck's checksum is skipped**, and one whose - deck moved is not — otherwise a re-run re-renders the whole estate, or a - rename never reaches the banner. - (c) **the stamp is written only after the upload**, so a failed upload is - not recorded as current. - (d) **a chapter with no banner gets one created exactly once** (NO_RETRY). + (a) **a slide that does not say exactly the chapter's name is never + rendered** — another city, a qualified spelling, a leftover template city + beside the new one. Rendering it would publish the wrong name with a + fresh date on it. + (b) **the slide checked is the slide rendered**: the fourth in presentation + order, not the `slide4.xml` part. + (c) **a banner stamped with this deck AND this name is skipped**, and a moved + deck or a new name is not — otherwise a re-run re-renders the estate, or + a rename never reaches the banner. + (d) **the upload and the stamp land on the banner, never the deck**, and the + stamp only after the upload. + (e) **a chapter with no banner gets one created exactly once** (NO_RETRY), + and only when asked; a sweep never adds one. + (f) `main()`'s exit codes: 1 on any failure, 2 for report-only with work, 0 + otherwise; ABSENT is not a failure, an unknown outcome is. Nothing touches Drive: `create_chapter`'s Drive calls and the renderer are replaced with a fake folder tree. @@ -34,36 +40,54 @@ BANNERS = "Banners (Chapter Specific, Changed Rarely)" -def _pptx(slide4_runs): - """Bytes of a minimal .pptx whose slide 4 holds `slide4_runs`.""" +def _pptx(fourth_runs, order=(1, 2, 3, 4, 5), part_runs=None): + """Bytes of a minimal .pptx. `order` lists slide part numbers in + presentation order; the part shown FOURTH gets `fourth_runs`, unless + `part_runs` maps part numbers to runs explicitly.""" + if part_runs is None: + part_runs = {order[3]: fourth_runs} buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as z: - for i in range(1, 6): - runs = slide4_runs if i == 4 else ["AAIF"] + ids = "".join('' % (255 + n, n) for n in order) + z.writestr("ppt/presentation.xml", + "%s" % ids) + rels = "".join('' + % (n, n) for n in order) + z.writestr("ppt/_rels/presentation.xml.rels", "%s" % rels) + for n in order: + runs = part_runs.get(n, ["AAIF"]) body = "".join('%s' % r - if n % 2 else "%s" % r - for n, r in enumerate(runs)) - z.writestr("ppt/slides/slide%d.xml" % i, "%s" % body) + if i % 2 else "%s" % r + for i, r in enumerate(runs)) + z.writestr("ppt/slides/slide%d.xml" % n, "%s" % body) return buf.getvalue() +def _path(data): + fh = tempfile.NamedTemporaryFile(suffix=".pptx", delete=False) + fh.write(data) + fh.close() + return fh.name + + class FakeDrive: """One chapter folder, `boston`, with a Banners folder and a deck.""" - def __init__(self, runs, banner_stamp=None, banner=True, md5="m1"): + def __init__(self, runs, stamp=None, banner=True, md5="m1", banners=1): self.deck = _pptx(runs) self.md5 = md5 - self.stamp = banner_stamp - self.banner = banner - self.calls = [] # (verb, fileId-or-None) - self.renders = 0 + self.stamp = stamp + self.banners = banners if banner else 0 + self.calls = [] # (verb, fileId, retries, body) + self.renders = [] + self.render_check = None self.fail_upload = False def list_children(self, fid): if fid == "boston": kids = [{"id": "bdir", "name": BANNERS, "mimeType": cc.FOLDER}] - if self.banner: - kids.append({"id": "wb", "name": rwb.BANNER, "mimeType": "image/png"}) + kids += [{"id": "wb" if i == 0 else "wb%d" % i, "name": rwb.BANNER, + "mimeType": "image/png"} for i in range(self.banners)] return kids if fid == "bdir": return [{"id": "deck", "name": rwb.DECK, "mimeType": "x"}] @@ -73,13 +97,15 @@ def gws_json(self, *verb, params=None, body=None, retries=None): fid = (params or {}).get("fileId") self.calls.append((verb[-1], fid, retries, body)) if verb[-1] == "get" and fid == "deck": - return {"md5Checksum": self.md5} + return {"md5Checksum": self.md5} if self.md5 else {} if verb[-1] == "get" and fid == "wb": return {"appProperties": {rwb.STAMP: self.stamp} if self.stamp else {}} if verb[-1] == "create": - self.banner = True + self.banners = 1 return {"id": "wb"} if verb[-1] == "update": + if fid != "wb": + raise AssertionError("stamp written to %r, not the banner" % fid) self.stamp = body["appProperties"][rwb.STAMP] return {} raise AssertionError("unexpected call %r %r" % (verb, params)) @@ -90,17 +116,23 @@ def download(self, fid, out): fh.write(self.deck) def upload(self, fid, path, mime): + if fid != "wb": + raise AssertionError("PNG uploaded over %r, not the banner" % fid) if self.fail_upload: raise RuntimeError("upload blew up") - self.calls.append(("upload", fid, None, None)) + self.calls.append(("upload", fid, None, mime)) - def render(self, fid, out, slide_index=0): - assert (fid, slide_index) == ("deck", rwb.SLIDE_INDEX) - self.renders += 1 + def render(self, fid, out, slide_index=0, check_text=None, strict_cleanup=False): + assert (fid, slide_index, strict_cleanup) == ("deck", rwb.SLIDE_INDEX, True) + self.render_check = check_text + self.renders.append(fid) with open(out, "wb") as fh: fh.write(b"\x89PNG fake") return out + def verbs(self): + return [c[0] for c in self.calls] + def run(fake, write=True, name="Boston", create=False): with mock.patch.object(cc, "list_children", fake.list_children), \ @@ -112,78 +144,205 @@ def run(fake, write=True, name="Boston", create=False): return rwb.sync_chapter("boston", name, write, tmp, create=create) -class TestSlideText(unittest.TestCase): - def test_runs_with_and_without_attributes_are_read(self): - with tempfile.NamedTemporaryFile(suffix=".pptx", delete=False) as fh: - fh.write(_pptx(["AAIF Community Events", "New York"])) - self.addCleanup(os.remove, fh.name) - self.assertEqual(rwb.slide_text(fh.name), "AAIF Community Events New York") +class TestNameCheck(unittest.TestCase): + def test_exactly_the_headline_and_the_name_passes(self): + self.assertIsNone(rwb.name_mismatch("AAIF Community Events new york", "New York")) - def test_names_chapter_is_case_and_space_blind(self): - self.assertTrue(rwb.names_chapter("AAIF Community Events new york", "New York")) + def test_a_trailing_full_stop_is_ignored(self): + self.assertIsNone(rwb.name_mismatch("AAIF Community Events Japan.", "Japan")) def test_another_city_is_refused(self): - self.assertFalse(rwb.names_chapter("AAIF Community Events San Francisco", "Boston")) + self.assertIn("san francisco", + rwb.name_mismatch("AAIF Community Events San Francisco", "Boston")) + + def test_a_qualified_spelling_is_not_a_match(self): + self.assertIsNotNone(rwb.name_mismatch("AAIF Community Events Portland, OR", "Portland")) + self.assertIsNotNone(rwb.name_mismatch("AAIF Community Events New York", "York")) + + def test_a_leftover_template_city_beside_the_new_one_is_refused(self): + self.assertIsNotNone(rwb.name_mismatch( + "AAIF Community Events Boston San Francisco", "Boston")) def test_a_slide_without_the_headline_is_refused(self): - self.assertFalse(rwb.names_chapter("Boston", "Boston")) + self.assertIn("does not start", rwb.name_mismatch("Boston", "Boston")) + + def test_templatecity_expects_the_rebrand_source_city(self): + self.assertEqual(rwb.expected_name("TemplateCity"), cc.SOURCE_NAME) + self.assertEqual(rwb.expected_name("Boston"), "Boston") + + +class TestSlideText(unittest.TestCase): + def test_runs_with_and_without_attributes_are_read(self): + p = _path(_pptx(["AAIF Community Events", "New York"])) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events New York") + + def test_the_fourth_slide_in_presentation_order_not_the_part_name(self): + # Reordered deck: part slide4.xml now sits second; slide2.xml is fourth. + p = _path(_pptx(None, order=(1, 4, 3, 2, 5), part_runs={ + 4: ["AAIF Community Events", "San Francisco"], + 2: ["AAIF Community Events", "Boston"]})) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events Boston") + + def test_xml_entities_are_unescaped(self): + p = _path(_pptx(["AAIF Community Events", "Trinidad & Tobago"])) + self.addCleanup(os.remove, p) + self.assertIsNone(rwb.name_mismatch(rwb.slide_text(p), "Trinidad & Tobago")) + + def test_a_short_deck_says_so(self): + p = _path(_pptx(None, order=(1, 2), part_runs={})) + self.addCleanup(os.remove, p) + with self.assertRaisesRegex(RuntimeError, "2 slide"): + rwb.slide_text(p) class TestSync(unittest.TestCase): + OK = ["AAIF Community Events", "Boston"] + def test_a_deck_naming_another_city_is_never_rendered(self): fake = FakeDrive(["AAIF Community Events", "San Francisco"]) r = run(fake) - self.assertIsNotNone(r.error) - self.assertEqual(fake.renders, 0) - self.assertNotIn("upload", [c[0] for c in fake.calls]) - - def test_a_banner_stamped_with_the_current_deck_is_skipped(self): - fake = FakeDrive(["AAIF Community Events", "Boston"], banner_stamp="m1") - self.assertEqual(run(fake), rwb.Result("Boston", "current")) - self.assertEqual(fake.renders, 0) - - def test_a_moved_deck_is_re_rendered_and_restamped(self): - fake = FakeDrive(["AAIF Community Events", "Boston"], banner_stamp="old", md5="new") - self.assertEqual(run(fake).action, "render") - self.assertEqual(fake.renders, 1) - self.assertEqual(fake.stamp, "new") + self.assertIn("expected 'Boston'", r.error) + self.assertEqual(fake.renders, []) + self.assertNotIn("upload", fake.verbs()) + + def test_a_banner_stamped_with_this_deck_and_name_is_skipped(self): + fake = FakeDrive(self.OK, stamp=rwb.stamp_value("m1", "Boston")) + self.assertEqual(run(fake), rwb.Result("Boston", rwb.CURRENT)) + self.assertEqual(fake.renders, []) + + def test_a_new_name_with_an_unchanged_deck_is_checked_again(self): + """A folder renamed by hand: the deck md5 did not move.""" + fake = FakeDrive(["AAIF Community Events", "Scotland"], + stamp=rwb.stamp_value("m1", "Scotland")) + r = run(fake, name="Edinburgh") + self.assertIn("expected 'Edinburgh'", r.error) + self.assertEqual(fake.renders, []) + + def test_a_moved_deck_is_re_rendered_onto_the_banner_and_restamped(self): + fake = FakeDrive(self.OK, stamp=rwb.stamp_value("old", "Boston"), md5="new") + self.assertEqual(run(fake).action, rwb.RENDER) + self.assertEqual(fake.renders, ["deck"]) + self.assertEqual([c for c in fake.calls if c[0] == "upload"], + [("upload", "wb", None, "image/png")]) + self.assertEqual(fake.stamp, rwb.stamp_value("new", "Boston")) + self.assertLess(fake.verbs().index("upload"), fake.verbs().index("update")) + + def test_the_renderer_rechecks_the_page_it_draws(self): + fake = FakeDrive(self.OK) + run(fake) + self.assertIsNone(fake.render_check("AAIF Community Events Boston")) + self.assertIsNotNone(fake.render_check("AAIF Community Events Oslo")) def test_report_mode_renders_and_writes_nothing(self): - fake = FakeDrive(["AAIF Community Events", "Boston"]) - self.assertEqual(run(fake, write=False).action, "render") - self.assertEqual(fake.renders, 0) - self.assertEqual([c[0] for c in fake.calls], ["get", "get"]) + fake = FakeDrive(self.OK) + self.assertEqual(run(fake, write=False).action, rwb.RENDER) + self.assertEqual(fake.renders, []) + self.assertFalse({"create", "update", "upload"} & set(fake.verbs())) def test_a_failed_upload_is_not_stamped_current(self): - fake = FakeDrive(["AAIF Community Events", "Boston"], banner_stamp="old") + fake = FakeDrive(self.OK, stamp="old") fake.fail_upload = True r = run(fake) self.assertIn("upload blew up", r.error) self.assertEqual(fake.stamp, "old") + def test_a_deck_with_no_checksum_is_an_error_not_current(self): + """Otherwise None == an unstamped banner's missing stamp.""" + fake = FakeDrive(self.OK, md5=None) + r = run(fake) + self.assertIn("no checksum", r.error) + self.assertEqual(fake.renders, []) + + def test_duplicate_banners_are_refused_with_their_ids(self): + fake = FakeDrive(self.OK, banners=2) + r = run(fake) + self.assertIn("wb, wb1", r.error) + self.assertEqual(fake.renders, []) + def test_a_sweep_never_creates_a_missing_banner(self): - fake = FakeDrive(["AAIF Community Events", "Boston"], banner=False) - self.assertEqual(run(fake).action, "absent") - self.assertNotIn("create", [c[0] for c in fake.calls]) - self.assertEqual(fake.renders, 0) + fake = FakeDrive(self.OK, banner=False) + self.assertEqual(run(fake).action, rwb.ABSENT) + self.assertNotIn("create", fake.verbs()) + self.assertEqual(fake.renders, []) def test_templatecity_is_checked_against_the_city_it_clones_from(self): fake = FakeDrive(["AAIF Community Events", "San Francisco"]) - self.assertEqual(run(fake, name="TemplateCity").action, "render") + self.assertEqual(run(fake, name="TemplateCity").action, rwb.RENDER) - def test_a_missing_banner_is_created_once_without_retries(self): - fake = FakeDrive(["AAIF Community Events", "Boston"], banner=False) - self.assertEqual(run(fake, create=True).action, "render") + def test_a_missing_banner_is_created_once_without_retries_and_stamped(self): + fake = FakeDrive(self.OK, banner=False) + self.assertEqual(run(fake, create=True).action, rwb.RENDER) creates = [c for c in fake.calls if c[0] == "create"] self.assertEqual(len(creates), 1) self.assertIs(creates[0][2], gwsmod.NO_RETRY) self.assertEqual(creates[0][3]["parents"], ["boston"]) + self.assertEqual(fake.stamp, rwb.stamp_value("m1", "Boston")) + + def test_a_create_then_failed_upload_names_the_empty_file(self): + fake = FakeDrive(self.OK, banner=False) + fake.fail_upload = True + r = run(fake, create=True) + self.assertIn("EMPTY", r.error) + self.assertIn("wb", r.error) + self.assertIsNone(fake.stamp) def test_errors_are_returned_not_raised(self): - fake = FakeDrive(["AAIF Community Events", "Boston"]) + fake = FakeDrive(self.OK) fake.list_children = lambda fid: (_ for _ in ()).throw(OSError("drive down")) self.assertIn("drive down", run(fake).error) +class TestMain(unittest.TestCase): + CHAPTERS = [("t", "TemplateCity"), ("a", "Aarhus"), ("b", "Boston")] + + def _main(self, results, argv=()): + seen = [] + + def fake_list(parent): + return [{"id": i, "name": n, "mimeType": cc.FOLDER} for i, n in self.CHAPTERS] + + def fake_sync(cid, name, write, tmp, create=False): + seen.append((name, create)) + return results.get(name, rwb.Result(name, rwb.CURRENT)) + with mock.patch.object(cc, "list_children", fake_list), \ + mock.patch.object(rwb, "sync_chapter", fake_sync), \ + mock.patch("sys.stdout", io.StringIO()) as out: + code = rwb.main(list(argv)) + return code, seen, out.getvalue() + + def test_a_sweep_skips_templatecity_and_never_creates(self): + code, seen, _ = self._main({}) + self.assertEqual(seen, [("Aarhus", False), ("Boston", False)]) + self.assertEqual(code, 0) + + def test_templatecity_only_by_name_case_insensitive(self): + _c, seen, _ = self._main({}, ["--chapter", "templatecity"]) + self.assertEqual(seen, [("TemplateCity", False)]) + + def test_report_with_work_exits_2(self): + code, _s, _o = self._main({"Aarhus": rwb.Result("Aarhus", rwb.RENDER)}) + self.assertEqual(code, 2) + + def test_absent_is_listed_not_failed(self): + code, _s, out = self._main({"Boston": rwb.Result("Boston", rwb.ABSENT)}) + self.assertEqual(code, 0) + self.assertIn("No Web Banner.png at all", out) + + def test_any_failure_exits_1(self): + code, _s, _o = self._main({"Boston": rwb.Result("Boston", error="boom")}) + self.assertEqual(code, 1) + + def test_an_unknown_outcome_is_a_failure_not_clean(self): + code, _s, out = self._main({"Boston": rwb.Result("Boston")}) + self.assertEqual(code, 1) + self.assertIn("unknown outcome", out) + + def test_no_match_exits_1(self): + code, _s, _o = self._main({}, ["--chapter", "Atlantis"]) + self.assertEqual(code, 1) + + if __name__ == "__main__": unittest.main() From 37a7515e13896af29dcff32332b19d0232a21bee Mon Sep 17 00:00:00 2001 From: Rahul Parundekar Date: Tue, 3 Nov 2026 13:48:31 -0800 Subject: [PATCH 3/3] fix: address second self-review findings - A folder with no banner is absent before the Banners folder or deck is looked up, so it never fails a sweep or a rename. - New --create flag (with --chapter only). The create hook's fallback command uses it, so a render that failed before the file existed can be finished. - strict_cleanup puts the stray copy's id in the raised error even when the render also failed. - create_chapter prints "Rendered" only for a render. rename_chapter treats an unknown outcome as exit 1, not a KeyError. - A dirty existing Square Logo deck under --resume is not rendered. The advice now comes after --repair-existing. - Runs within a paragraph or shape are joined with nothing, so a split word still matches. Punctuation at the ends of the expected name is stripped too. - check_text rejects a non-string return. The fallback command is shell-quoted. --jobs must be at least 1. The stamp key notes when to rename it. - Tests: - the NO_RETRY create counted at the subprocess; - write passed through main; - sync_one end to end; - rename main wiring; - missing folder, deck and duplicate decks; - the fields mask, the strict cleanup combo, and boundary cases. - Docs now match the code. Co-Authored-By: Claude Opus 5.5 --- lib/aaif_events/slides_export.py | 45 +++++-- lib/aaif_events/tests/test_slides_export.py | 49 +++++++ skills/aaif-create-chapter/SKILL.md | 25 ++-- .../references/rename-chapter.md | 13 +- .../scripts/create_chapter.py | 33 +++-- .../scripts/rename_chapter.py | 11 +- .../scripts/render_web_banner.py | 96 +++++++++---- .../scripts/test_create_chapter.py | 43 +++++- .../scripts/test_rename_chapter.py | 42 +++++- .../scripts/test_render_web_banner.py | 126 ++++++++++++++++-- 10 files changed, 399 insertions(+), 84 deletions(-) diff --git a/lib/aaif_events/slides_export.py b/lib/aaif_events/slides_export.py index 2733efb..6ae15e3 100644 --- a/lib/aaif_events/slides_export.py +++ b/lib/aaif_events/slides_export.py @@ -30,16 +30,19 @@ def _page_text(page): - """Every text run on a Slides API page, top-level shapes and groups, joined - by spaces.""" + """The text of a Slides API page: text in shapes, recursing into groups + (tables are not read). Runs within a shape are joined with nothing, since + one word can be split across runs and each paragraph's last run already + ends in a newline. Shapes are joined with a space.""" out = [] def walk(elements): for el in elements or (): - for text_el in ((el.get("shape") or {}).get("text") or {}).get("textElements", ()): - run = (text_el.get("textRun") or {}).get("content") - if run: - out.append(run) + shape_text = "".join( + (text_el.get("textRun") or {}).get("content") or "" + for text_el in ((el.get("shape") or {}).get("text") or {}).get("textElements", ())) + if shape_text: + out.append(shape_text) walk((el.get("elementGroup") or {}).get("children")) walk(page.get("pageElements")) return " ".join(out) @@ -77,15 +80,20 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 thumbnails only work on native Slides files, not stored .pptx blobs) and trashes it afterward — the source file is never modified. - `check_text(text) -> error or None` is run on the text of the page that is - about to be rendered, the same page object, so a caller can refuse a slide - without trusting that `slideN.xml` numbering follows presentation order. - `strict_cleanup` turns a failure to trash the throwaway copy into an - exception (naming its id) instead of a stderr warning, for callers that - report per-file success: a stray copy beside TemplateCity's decks is + `check_text(text) -> error string or None` is run on the text of the page + that is about to be rendered, the same page object, so a caller can refuse + a slide without trusting that `slideN.xml` numbering follows presentation + order. It is a refusal, not a predicate: anything but None or a str raises + TypeError, so a `lambda t: want in t` cannot pass every wrong slide. + + A failure to trash the throwaway copy is always printed to stderr. + `strict_cleanup` also makes it part of the raised error (naming the copy's + id), whether the render succeeded or failed on its own, for callers that + report per-file results: a stray copy beside TemplateCity's decks is cloned into every later chapter.""" presentation_id = None cleanup_error = None + body_error = None try: # NO_RETRY: a `files.copy` that succeeded server-side but answered like # a timeout would, on a retry, make a SECOND throwaway copy — and the @@ -109,6 +117,9 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 page_object_id = slides[slide_index]["objectId"] if check_text: err = check_text(_page_text(slides[slide_index])) + if err is not None and not isinstance(err, str): + raise TypeError("check_text must return an error string or None, got %r" + % (err,)) if err: raise RuntimeError(err) @@ -119,6 +130,9 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 if not thumb.get("contentUrl"): raise RuntimeError("no thumbnail URL for slide %d of %s" % (slide_index + 1, file_id)) _download(thumb["contentUrl"], out_path) + except Exception as e: + body_error = e + raise finally: # The throwaway copy has no `parents` set, so it lands in the SAME # folder as the source file - not an out-of-sight scratch location. @@ -137,6 +151,13 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 print("WARNING: could not trash temp Slides copy %s: %s" % (presentation_id, e), file=sys.stderr) cleanup_error = e + if strict_cleanup and body_error is not None: + # Replaces the in-flight error on purpose, carrying it, so + # the stray id reaches the caller's failure line. + raise RuntimeError( + "%s; ALSO the temp Slides copy %s beside the source could not " + "be trashed (%s); trash it by hand" + % (body_error, presentation_id, e)) from body_error if cleanup_error is not None and strict_cleanup: raise RuntimeError("rendered, but the temp Slides copy %s beside the source " "could not be trashed (%s); trash it by hand" diff --git a/lib/aaif_events/tests/test_slides_export.py b/lib/aaif_events/tests/test_slides_export.py index ee6424a..08c8269 100644 --- a/lib/aaif_events/tests/test_slides_export.py +++ b/lib/aaif_events/tests/test_slides_export.py @@ -258,6 +258,55 @@ def test_default_cleanup_failure_still_returns(self): out, _c, _d = self._run([self.PAGE], trash_fails=True) self.assertEqual(out, "/tmp/out.png") + def test_strict_cleanup_names_the_orphan_when_the_render_also_failed(self): + err, _c, dl = self._run([self.PAGE], trash_fails=True, strict_cleanup=True, + check_text=lambda t: "wrong city") + self.assertIn("wrong city", str(err)) + self.assertIn("pres1", str(err)) + self.assertIn("could not be trashed", str(err)) + self.assertFalse(dl.called) + + def test_a_boolean_check_text_is_a_type_error_not_a_pass(self): + with mock.patch.object(se, "_gws_json", side_effect=lambda *a, **k: ( + {"id": "pres1"} if a == ("drive", "files", "copy") + else {"slides": [self.PAGE]} if a == ("slides", "presentations", "get") + else {})), mock.patch.object(se, "_download"): + with self.assertRaises(TypeError): + se.render_slide_png("file1", "/tmp/out.png", check_text=lambda t: False) + + def test_the_fields_mask_asks_for_text_only_when_checking(self): + _o, calls, _d = self._run([self.PAGE], check_text=lambda t: None) + get = [k for a, k in calls if a == ("slides", "presentations", "get")][0] + self.assertIn("textRun/content", get["params"]["fields"]) + self.assertIn("elementGroup", get["params"]["fields"]) + _o, calls, _d = self._run([self.PAGE]) + get = [k for a, k in calls if a == ("slides", "presentations", "get")][0] + self.assertEqual(get["params"]["fields"], "slides.objectId") + + def test_slide_index_equal_to_the_count_is_refused(self): + err, _c, _d = self._run([{"objectId": "p1"}], slide_index=1) + self.assertIn("has 1 slide(s); slide 2", str(err)) + + def test_a_missing_thumbnail_url_says_so(self): + def fake(*args, **kwargs): + if args == ("drive", "files", "copy"): + return {"id": "pres1"} + if args == ("slides", "presentations", "get"): + return {"slides": [{"objectId": "p1"}]} + return {} + with mock.patch.object(se, "_gws_json", side_effect=fake), \ + mock.patch.object(se, "_download") as dl: + with self.assertRaisesRegex(RuntimeError, "no thumbnail URL"): + se.render_slide_png("file1", "/tmp/out.png") + self.assertFalse(dl.called) + + def test_a_word_split_across_runs_in_one_shape_is_read_whole(self): + page = {"pageElements": [{"shape": {"text": {"textElements": [ + {"textRun": {"content": "AAIF Community Events\n"}}, + {"textRun": {"content": "Amster"}}, {"textRun": {"content": "dam\n"}}]}}}]} + self.assertEqual(" ".join(se._page_text(page).split()), + "AAIF Community Events Amsterdam") + class TestDownload(unittest.TestCase): """_download must never leave a partial PNG behind, whatever failed.""" diff --git a/skills/aaif-create-chapter/SKILL.md b/skills/aaif-create-chapter/SKILL.md index db24a59..7c3a1e3 100644 --- a/skills/aaif-create-chapter/SKILL.md +++ b/skills/aaif-create-chapter/SKILL.md @@ -172,8 +172,10 @@ template or the rebrand engine is broken. Fix that — don't resume. render_slide_png('', '$(mktemp -d)/slide5.png', slide_index=4) " ``` -- [ ] The run printed **`Rendered Web Banner.png`**. If it warned instead, the - banner still says the template city — run the command it names. +- [ ] The run printed **`Rendered Web Banner.png`** (or `Web Banner.png already + current.` on a `--resume`). If it warned instead, the banner still says + the template city: run the command it names. If it said "not rendered: + fresh files still carry source tokens", fix the template first (above). - [ ] If the Luma page wasn't live, the user has been reminded to create it. - [ ] Unit tests still pass after any engine edit: ```bash @@ -199,19 +201,26 @@ template or the rebrand engine is broken. Fix that — don't resume. from TemplateCity kept its "San Francisco" banner. `render_web_banner.py` renders it from the fourth slide of the chapter's own `Banners (…)/Square Logo.pptx` through Google's renderer. - - `create_chapter` calls it after a clean write (exit `2` if the banner - failed). `rename_chapter` calls it only after the rename verifies (exit `1` - if it failed). + - `create_chapter` calls it once no freshly cloned file carries source + tokens. Exit `2` if the banner failed, the same code as dirty existing files + under `--resume`; a dirty existing `Square Logo.pptx` is not rendered at all + until `--repair-existing` fixes it. `rename_chapter` calls it only after + the rename verifies (exit `1` if it failed). - It refuses a slide that does not say exactly "AAIF Community Events" plus - the chapter's name. + the chapter's name (ignoring case, extra spaces and punctuation at the ends + of the city). - It skips a banner stamped with the current deck and name, so a re-run is cheap. - A sweep leaves TemplateCity alone unless named with `--chapter`, and never - adds a banner to a folder that has none. - - Exit codes: `2` report-only with banners to render, `1` any failure. + adds a banner to a folder that has none. `--chapter X --create` does, which + is what `create_chapter` prints when its render failed before the file + existed. + - Exit codes: `2` report-only with banners to render, `1` any failure, `0` + otherwise (folders with no banner are listed, not failed). ```bash python3 /scripts/render_web_banner.py # report python3 /scripts/render_web_banner.py --write + python3 /scripts/render_web_banner.py --chapter --create --write ``` - **`create_chapter` writes no Chapters List row**, so its 100-chapter cap check is an early warning, not a guarantee: three runs at 99 live chapters each read diff --git a/skills/aaif-create-chapter/references/rename-chapter.md b/skills/aaif-create-chapter/references/rename-chapter.md index 4edd63d..ba23000 100644 --- a/skills/aaif-create-chapter/references/rename-chapter.md +++ b/skills/aaif-create-chapter/references/rename-chapter.md @@ -71,13 +71,16 @@ Also deliberate: re-renders it from `Square Logo.pptx` (`render_web_banner.py`) and exits `1` if that fails. A failed verify skips it and prints the command to run after the fix. A folder with no banner is not given one. - - **The `Badges/` set** is named and lettered with a *lowercase* slug and UPPER - SVG text, which the Title/UPPER name pass does not match. Regenerate it with - `aaif-sync-badges` (`--chapter `), then trash the old-slug files. + - **The `Badges/` set** has file names in a *lowercase* slug, which the + Title/UPPER name pass does not match, and its SVG/PNG contents are never + opened (only `.docx`/`.pptx`/`.xlsx` are rewritten). Regenerate it with + `aaif-sync-badges` (`--chapter `, a case-insensitive substring match, + so check it picked only the one chapter), then trash the old-slug files. - **Luma links** stay until you pass `--slug-from`/`--slug-to`. Both pages had moved, so the links pointed to redirects. - - **The Chapters List `Summary`** prose ("AAIF Tokyo connects…") is website - copy, not a Drive file. + - **The Chapters List `Summary`** cell ("AAIF Tokyo connects…") is on the + feed sheet, outside the chapter folder. Edit it there with the rest of the + row (next bullet). - **The Chapters List row, the intake's city cells and Slack channels are NOT touched.** The first two are `aaif-sync-chapters`' surfaces and must be updated for the engines to keep matching the chapter to its folder; a channel rename diff --git a/skills/aaif-create-chapter/scripts/create_chapter.py b/skills/aaif-create-chapter/scripts/create_chapter.py index 9b7bdcc..56448ae 100755 --- a/skills/aaif-create-chapter/scripts/create_chapter.py +++ b/skills/aaif-create-chapter/scripts/create_chapter.py @@ -808,24 +808,39 @@ def main(): # carries source tokens, in which case the deck may too, and the exit-1 # path below is the real problem. A banner failure is not a failed clone: # it is "done, one follow-up", exit 2, with the command that finishes it. - import render_web_banner # lazy: it imports this module + import render_web_banner as rwb # lazy: it imports this module banner_failed = False + dirty_deck = [fn for fn, _t in ctx["existing_residuals"] if fn == rwb.DECK] if ctx["residuals"]: print("\n%s not rendered: fresh files still carry source tokens (below)." - % render_web_banner.BANNER) + % rwb.BANNER) + elif dirty_deck: + # Under --resume an existing deck still naming the template city is + # left alone on purpose; rendering it would only be refused. + banner_failed = True + print("%s not rendered: the existing %s still carries source tokens. After " + "--write --resume --repair-existing, run: %s" + % (rwb.BANNER, rwb.DECK, rwb.fallback_command(name, create=True)), + file=sys.stderr) else: - r = render_web_banner.sync_one(new_id, name, create=True) + r = rwb.sync_one(new_id, name, create=True) if r.error is not None: banner_failed = True print("WARNING: %s was NOT re-rendered (%s) — it still shows the template " - "city. Run: %s" % (render_web_banner.BANNER, r.error, - render_web_banner.fallback_command(name)), + "city. Run: %s" % (rwb.BANNER, r.error, + rwb.fallback_command(name, create=True)), file=sys.stderr) - elif r.action == render_web_banner.CURRENT: - print("%s already current." % render_web_banner.BANNER) + elif r.action == rwb.CURRENT: + print("%s already current." % rwb.BANNER) + elif r.action == rwb.RENDER: + print("Rendered %s from this chapter's %s." % (rwb.BANNER, rwb.DECK)) else: - print("Rendered %s from this chapter's %s." % (render_web_banner.BANNER, - render_web_banner.DECK)) + # Anything else (ABSENT should be impossible with create=True) is + # not a render, and saying "Rendered" would hide it. + banner_failed = True + print("WARNING: %s: unexpected outcome %r, nothing rendered. Run: %s" + % (rwb.BANNER, r.action, rwb.fallback_command(name, create=True)), + file=sys.stderr) print("\nDone. New chapter folder id: %s" % new_id) print("https://drive.google.com/drive/folders/%s" % new_id) if status == "absent": diff --git a/skills/aaif-create-chapter/scripts/rename_chapter.py b/skills/aaif-create-chapter/scripts/rename_chapter.py index 99fc326..90b1579 100755 --- a/skills/aaif-create-chapter/scripts/rename_chapter.py +++ b/skills/aaif-create-chapter/scripts/rename_chapter.py @@ -469,11 +469,18 @@ def rerender_banner(folder_id, new_name): % (render_web_banner.BANNER, r.error, render_web_banner.fallback_command(new_name)), file=sys.stderr) return 1 - print({render_web_banner.RENDER: "%s rendered from the renamed %s." + msg = {render_web_banner.RENDER: "%s rendered from the renamed %s." % (render_web_banner.BANNER, render_web_banner.DECK), render_web_banner.CURRENT: "%s already current." % render_web_banner.BANNER, render_web_banner.ABSENT: "This chapter has no %s; none was added." - % render_web_banner.BANNER}[r.action]) + % render_web_banner.BANNER}.get(r.action) + if msg is None: + # The rename has landed; an outcome we do not know is not "done". + print("WARNING: %s: unexpected outcome %r, nothing rendered. Run: %s" + % (render_web_banner.BANNER, r.action, + render_web_banner.fallback_command(new_name)), file=sys.stderr) + return 1 + print(msg) return 0 diff --git a/skills/aaif-create-chapter/scripts/render_web_banner.py b/skills/aaif-create-chapter/scripts/render_web_banner.py index d6d3012..13411d9 100755 --- a/skills/aaif-create-chapter/scripts/render_web_banner.py +++ b/skills/aaif-create-chapter/scripts/render_web_banner.py @@ -20,7 +20,8 @@ name.** Rendering a deck that names another city would publish the wrong name with a fresh timestamp on it. The check is exact, not a substring test, so "Portland, OR" does not pass for Portland and a slide still - carrying a leftover city fails. It runs twice: on the deck's own slide 4 + carrying a leftover city fails. ("Exact" ignores only case, runs of + whitespace, and punctuation at either end of the city.) It runs twice: on the deck's own slide 4 (resolved through `presentation.xml`'s slide order, not the `slideN.xml` part name) before anything is written, and again by the renderer on the page it is about to draw. @@ -34,26 +35,30 @@ on a different `gws` client sees no stamps and re-renders everything once: slower, never wrong. * **A stray temp copy is a failure.** The renderer's throwaway Slides copy - lands beside the deck; if it cannot be trashed, that chapter FAILS and the - copy's id is printed. + lands beside the deck; if it cannot be trashed, that chapter FAILS and its + FAILED line names the copy's id, whether or not the render itself worked. A sweep skips TemplateCity (its deck says San Francisco by design, and a stray temp copy there would be cloned into every later chapter) unless it is named with `--chapter`, and never creates a banner in a folder that has none. +`--chapter X --create` does, for the one case that needs it: a new chapter +whose create-hook render failed before its banner file existed. -Read-only by default. Exit codes: 0 all current/rendered, 2 report-only with -banners to render, 1 any failure. +Read-only by default. Exit codes: 0 otherwise (folders with no banner are +listed, not failed), 2 report-only with banners to render, 1 any failure. Usage: python3 render_web_banner.py # report python3 render_web_banner.py --write python3 render_web_banner.py --chapter Aarhus --write + python3 render_web_banner.py --chapter Aarhus --create --write """ import argparse import html import os import posixpath import re +import shlex import sys import tempfile import zipfile @@ -74,6 +79,9 @@ #: Events / " square. SLIDE_INDEX = 3 HEADLINE = "AAIF Community Events" +#: The stamp records the deck and the name, not how it was drawn. Change +#: SLIDE_INDEX or the render size and you must also rename this key (say to +#: "aaifBannerDeck2"), or every banner stays "current" in the old rendering. STAMP = "aaifBannerDeck" PNG = "image/png" TEMPLATE = "TemplateCity" @@ -87,10 +95,12 @@ class Result(NamedTuple): - """One chapter's outcome. Exactly one of `action` / `error` is set.""" + """One chapter's outcome. Exactly one of `action` / `error` is set; + callers treat any other shape as a failure.""" name: str - #: CURRENT (stamp matched, nothing done), RENDER (would render / rendered), - #: or ABSENT (no banner, and not asked to create one). + #: CURRENT (stamp matched, nothing done), RENDER (rendered under + #: write=True, would render under write=False), or ABSENT (no banner, and + #: not asked to create one). action: Optional[str] = None error: Optional[str] = None @@ -99,8 +109,16 @@ def _norm(s): return re.sub(r"\s+", " ", s).strip().casefold() -def fallback_command(name): - return "python3 %s --chapter %r --write" % (SCRIPT, name) +def _edges(s): + """Strip punctuation from both ends (the separator after the headline, a + display full stop after the city).""" + return re.sub(r"^[\W_]+|[\W_]+$", "", s) + + +def fallback_command(name, create=False): + """The command that finishes a failed hook render, shell-quoted.""" + return "python3 %s --chapter %s%s --write" % ( + shlex.quote(SCRIPT), shlex.quote(name), " --create" if create else "") def expected_name(folder): @@ -116,13 +134,14 @@ def name_mismatch(text, want): Exact rather than "contains": a slide saying "Portland, OR" must not pass for Portland, nor one still carrying the template city beside the new one. - Trailing punctuation is ignored (the decks' display style puts a full stop - after the city in some layouts).""" + Case and runs of whitespace are ignored, and so is punctuation at either + end of the city, on both sides of the comparison: a separator after the + headline, a display full stop after the city, or a name like "D.C.".""" t, head = _norm(text), _norm(HEADLINE) if not t.startswith(head): return "slide %d does not start with %r (it says %r)" % (SLIDE_INDEX + 1, HEADLINE, text[:80]) - rest = re.sub(r"^[\W_]+|[\W_]+$", "", t[len(head):]) - if rest != _norm(want): + rest = _edges(t[len(head):]) + if rest != _edges(_norm(want)): return ("slide %d says %r, expected %r — fix the deck, or map the folder in " "DECK_NAME if the deck is right" % (SLIDE_INDEX + 1, rest, want)) return None @@ -148,13 +167,20 @@ def slide_part(z, index=SLIDE_INDEX): raise RuntimeError("slide %d's relationship %s is missing" % (index + 1, rids[index])) +_RUN = re.compile(r"]*)?>([^<]*)") +_PARA = re.compile(r"]*)?>(.*?)", re.S) + + def slide_text(pptx_path, index=SLIDE_INDEX): - """Every text run on the `index`-th slide (presentation order), unescaped - and joined by spaces.""" + """The text of the `index`-th slide (presentation order), unescaped. + + Runs within a paragraph are joined with nothing, since an edit can split + one word across runs ("Amster" + "dam"). Paragraphs are joined with a + space.""" with zipfile.ZipFile(pptx_path) as z: xml = z.read(slide_part(z, index)).decode("utf-8", "replace") - return " ".join(html.unescape(r) - for r in re.findall(r"]*)?>([^<]*)", xml)) + return " ".join("".join(html.unescape(r) for r in _RUN.findall(p)) + for p in _PARA.findall(xml)) def chapters(include_template=False): @@ -179,13 +205,17 @@ def sync_chapter(chapter_id, name, write, tmpdir, create=False): failure must not end the others. A banner is created in a folder that has none only when `create` is set - (the create hook). A sweep reports such a folder instead: the ones without - a banner predate the template that carries it (the Tatooine test chapter), - and a sweep has no business adding files to them.""" + (the create hook, or `--create`). Otherwise such a folder is ABSENT, and + that is decided first, before the Banners folder or deck is looked up: the + folders without a banner predate the template that carries it (the + Tatooine test chapter), may lack the deck too, and are not failures.""" created = None try: want = expected_name(name) kids = cc.list_children(chapter_id) + banner = _find(kids, lambda c: c["name"] == BANNER, BANNER) + if banner is None and not create: + return Result(name, ABSENT) bdir = _find(kids, lambda c: c["mimeType"] == cc.FOLDER and BANNERS_DIR.match(c["name"]), "the Banners folder") if bdir is None: @@ -202,9 +232,6 @@ def sync_chapter(chapter_id, name, write, tmpdir, create=False): return Result(name, error="%s has no checksum (not a stored .pptx?)" % DECK) stamp = stamp_value(md5, want) - banner = _find(kids, lambda c: c["name"] == BANNER, BANNER) - if banner is None and not create: - return Result(name, ABSENT) if banner is not None: props = cc.gws_json("drive", "files", "get", params={ "fileId": banner["id"], "supportsAllDrives": True, @@ -253,12 +280,20 @@ def sync_chapter(chapter_id, name, write, tmpdir, create=False): def sync_one(chapter_id, name, create): - """Render one chapter's banner now (the create/rename hooks). Returns the - `Result`, so a caller can tell a real render from an already-current one.""" + """Render one chapter's banner now (the create/rename hooks). Always + writes. Returns the `Result`, so a caller can tell a real render from an + already-current one.""" with tempfile.TemporaryDirectory() as tmp: return sync_chapter(chapter_id, name, True, tmp, create=create) +def _positive(v): + n = int(v) + if n < 1: + raise argparse.ArgumentTypeError("must be at least 1") + return n + + def main(argv=None): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) @@ -266,8 +301,12 @@ def main(argv=None): help="render and upload (default: report only)") ap.add_argument("--chapter", help="only this chapter (exact name, case-insensitive; " "the only way to include TemplateCity)") - ap.add_argument("--jobs", type=int, default=4) + ap.add_argument("--create", action="store_true", + help="with --chapter: create the banner if the folder has none") + ap.add_argument("--jobs", type=_positive, default=4) a = ap.parse_args(argv) + if a.create and not a.chapter: + ap.error("--create needs --chapter (a sweep never adds a banner)") todo = chapters(include_template=bool(a.chapter)) if a.chapter: @@ -280,7 +319,8 @@ def main(argv=None): pending, current, failed, absent = [], 0, [], [] with tempfile.TemporaryDirectory() as tmp, \ ThreadPoolExecutor(max_workers=a.jobs) as pool: - for r in pool.map(lambda t: sync_chapter(t[0], t[1], a.write, tmp), todo): + for r in pool.map(lambda t: sync_chapter(t[0], t[1], a.write, tmp, + create=a.create), todo): if r.error is not None: failed.append(r) print(" FAILED %-26s %s" % (r.name, r.error)) diff --git a/skills/aaif-create-chapter/scripts/test_create_chapter.py b/skills/aaif-create-chapter/scripts/test_create_chapter.py index 0603c3f..2176211 100644 --- a/skills/aaif-create-chapter/scripts/test_create_chapter.py +++ b/skills/aaif-create-chapter/scripts/test_create_chapter.py @@ -743,7 +743,7 @@ def run_main(self, argv, clone=None, cap_calls=None, banner=None): mock.patch.object(cc, "clone_and_rebrand", clone), \ mock.patch.object(render_web_banner, "sync_one", banner or (lambda fid, name, create: - render_web_banner.Result(name, "render"))): + render_web_banner.Result(name, render_web_banner.RENDER))): cc.main() def test_a_write_re_renders_the_new_chapters_banner(self): @@ -753,7 +753,7 @@ def test_a_write_re_renders_the_new_chapters_banner(self): def banner(fid, name, create): seen.append((fid, name, create)) - return render_web_banner.Result(name, "render") + return render_web_banner.Result(name, render_web_banner.RENDER) with mock.patch("sys.stdout"): self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", banner=banner) @@ -771,7 +771,7 @@ def test_a_failed_banner_render_warns_and_exits_2(self): name, error="slide 4 says the wrong city")) self.assertEqual(cm.exception.code, 2) self.assertIn("NOT re-rendered", err.getvalue()) - self.assertIn("render_web_banner.py --chapter 'Zed' --write", err.getvalue()) + self.assertIn("render_web_banner.py --chapter Zed --create --write", err.getvalue()) def test_an_already_current_banner_does_not_claim_a_render(self): import io @@ -779,7 +779,7 @@ def test_an_already_current_banner_does_not_claim_a_render(self): with mock.patch("sys.stdout", out): self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", banner=lambda fid, name, create: render_web_banner.Result( - name, "current")) + name, render_web_banner.CURRENT)) self.assertIn("already current", out.getvalue()) self.assertNotIn("Rendered", out.getvalue()) @@ -788,13 +788,42 @@ def test_dirty_fresh_files_skip_the_banner(self): def dirty_clone(_tpl, _parent, _name, ctx, existing_id=None): ctx["residuals"].append(("Square Logo.pptx", ["San Francisco"])) return "new-id" - with mock.patch("sys.stdout"), self.assertRaises(SystemExit): + with mock.patch("sys.stdout"), self.assertRaises(SystemExit) as cm: self.run_main(["--city", "Zed", "--write"], clone=dirty_clone, - banner=lambda *a: self.fail("must not render a dirty deck")) + banner=lambda *a, **k: self.fail("must not render a dirty deck")) + # The residuals exit (a message, i.e. 1), not the banner's 2. + self.assertIsInstance(cm.exception.code, str) + + def test_a_dirty_existing_deck_under_resume_is_not_rendered(self): + """It is left dirty on purpose; the advice must come after the repair.""" + import io + + def resumed(_tpl, _parent, _name, ctx, existing_id=None): + ctx["existing_residuals"].append(("Square Logo.pptx", ["San Francisco"])) + return "new-id" + err = io.StringIO() + with mock.patch("sys.stdout"), mock.patch("sys.stderr", err), \ + self.assertRaises(SystemExit) as cm: + self.run_main(["--city", "Zed", "--write"], clone=resumed, + banner=lambda *a, **k: self.fail("must not render a dirty deck")) + self.assertEqual(cm.exception.code, 2) + self.assertIn("--repair-existing, run:", err.getvalue()) + + def test_an_unexpected_outcome_is_not_reported_as_a_render(self): + import io + out, err = io.StringIO(), io.StringIO() + with mock.patch("sys.stdout", out), mock.patch("sys.stderr", err), \ + self.assertRaises(SystemExit) as cm: + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=lambda fid, name, create: render_web_banner.Result( + name, render_web_banner.ABSENT)) + self.assertEqual(cm.exception.code, 2) + self.assertNotIn("Rendered", out.getvalue()) + self.assertIn("unexpected outcome", err.getvalue()) def test_a_plan_run_renders_no_banner(self): self.run_main(["--city", "Zed"], - banner=lambda *a: self.fail("plan must not render")) + banner=lambda *a, **k: self.fail("plan must not render")) def test_plan_runs_never_consult_the_cap(self): """Seeing what a chapter WOULD look like is how someone decides which diff --git a/skills/aaif-create-chapter/scripts/test_rename_chapter.py b/skills/aaif-create-chapter/scripts/test_rename_chapter.py index eef2cf5..2ef30c1 100755 --- a/skills/aaif-create-chapter/scripts/test_rename_chapter.py +++ b/skills/aaif-create-chapter/scripts/test_rename_chapter.py @@ -250,13 +250,53 @@ def fake(fid, name, create): code, _s, err, out = _rerender(_rwb.Result("Edinburgh", error="slide 4 says Scotland")) check("a failed banner render exits 1", code, 1) check("...with the command on stderr", - "--chapter 'Edinburgh' --write" in err and "slide 4 says Scotland" in err, True) + "--chapter Edinburgh --write" in err and "slide 4 says Scotland" in err, True) code, _s, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.CURRENT)) check("an already-current banner exits 0 and does not claim a render", (code, "already current" in out, "rendered" in out), (0, True, False)) code, _s, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.ABSENT)) check("a folder with no banner exits 0 and says none was added", (code, "none was added" in out), (0, True)) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh")) +check("an unknown outcome exits 1 with a warning, not a traceback", + (code, "unexpected outcome" in err), (1, True)) + + +# --- main() wires the hook: only after a verified --write --------------------- +def _main(argv, verify_names): + """Drive rename_chapter.main() over a fake folder holding one non-Office + file. `verify_names` is what the re-read walk sees after the rename.""" + import tempfile as _tf + walks = [[("Scotland Notes.txt", "f1", "Scotland Notes.txt", False)], + [(n, "f1", n, False) for n in verify_names]] + banner, out = [], _io.StringIO() + + def fake_sync(fid, name, create): + banner.append((fid, name, create)) + return _rwb.Result(name, _rwb.RENDER) + with _tf.TemporaryDirectory() as tmp, \ + _mock.patch.object(sys, "argv", ["x"] + argv), \ + _mock.patch.object(rc, "find_folder", lambda name: "folder-1"), \ + _mock.patch.object(rc, "walk", lambda fid: iter(walks.pop(0))), \ + _mock.patch.object(rc, "rename_file", lambda fid, new: None), \ + _mock.patch.object(rc.datadir, "path", lambda *p: os.path.join(tmp, *p)), \ + _mock.patch.object(_rwb, "sync_one", fake_sync), \ + _mock.patch("sys.stdout", out), _mock.patch("sys.stderr", _io.StringIO()): + code = rc.main() + return code, banner, out.getvalue() + + +code, banner, out = _main(["--from", "Scotland", "--to", "Edinburgh", "--write"], + ["Edinburgh Notes.txt"]) +check("a verified write re-renders the banner under the NEW name, never creating", + (code, banner), (0, [("folder-1", "Edinburgh", False)])) +code, banner, out = _main(["--from", "Scotland", "--to", "Edinburgh", "--write"], + ["Scotland Notes.txt"]) +check("a failed verify skips the banner and prints the command to run after", + (code, banner, "render_web_banner.py --chapter Edinburgh --write" in out), + (1, [], True)) +code, banner, out = _main(["--from", "Scotland", "--to", "Edinburgh"], []) +check("a report run never renders", (code, banner), (2, [])) print() print("FAILED %d check(s)" % len(FAILS) if FAILS else diff --git a/skills/aaif-create-chapter/scripts/test_render_web_banner.py b/skills/aaif-create-chapter/scripts/test_render_web_banner.py index 2845ba6..c74cd44 100755 --- a/skills/aaif-create-chapter/scripts/test_render_web_banner.py +++ b/skills/aaif-create-chapter/scripts/test_render_web_banner.py @@ -17,7 +17,10 @@ (e) **a chapter with no banner gets one created exactly once** (NO_RETRY), and only when asked; a sweep never adds one. (f) `main()`'s exit codes: 1 on any failure, 2 for report-only with work, 0 - otherwise; ABSENT is not a failure, an unknown outcome is. + otherwise; ABSENT is not a failure, an unknown outcome is. `main()` + passes `write` through, so the default run stays read-only. + (g) a folder with no banner is ABSENT before anything else is looked up; a + missing Banners folder or deck under an existing banner is a failure. Nothing touches Drive: `create_chapter`'s Drive calls and the renderer are replaced with a fake folder tree. @@ -40,10 +43,12 @@ BANNERS = "Banners (Chapter Specific, Changed Rarely)" -def _pptx(fourth_runs, order=(1, 2, 3, 4, 5), part_runs=None): +def _pptx(fourth_runs, order=(1, 2, 3, 4, 5), part_runs=None, absolute=False): """Bytes of a minimal .pptx. `order` lists slide part numbers in presentation order; the part shown FOURTH gets `fourth_runs`, unless - `part_runs` maps part numbers to runs explicitly.""" + `part_runs` maps part numbers to runs explicitly. Each entry is one + paragraph; a list entry is one paragraph split into several runs. + `absolute` writes package-absolute relationship targets.""" if part_runs is None: part_runs = {order[3]: fourth_runs} buf = io.BytesIO() @@ -51,14 +56,17 @@ def _pptx(fourth_runs, order=(1, 2, 3, 4, 5), part_runs=None): ids = "".join('' % (255 + n, n) for n in order) z.writestr("ppt/presentation.xml", "%s" % ids) - rels = "".join('' - % (n, n) for n in order) + rels = "".join('' + % (n, "/ppt/" if absolute else "", n) for n in order) z.writestr("ppt/_rels/presentation.xml.rels", "%s" % rels) for n in order: - runs = part_runs.get(n, ["AAIF"]) - body = "".join('%s' % r - if i % 2 else "%s" % r - for i, r in enumerate(runs)) + paras = part_runs.get(n, ["AAIF"]) + body = "" + for i, para in enumerate(paras): + runs = para if isinstance(para, list) else [para] + body += "%s" % "".join( + '%s' % r + if i % 2 else "%s" % r for r in runs) z.writestr("ppt/slides/slide%d.xml" % n, "%s" % body) return buf.getvalue() @@ -73,8 +81,11 @@ def _path(data): class FakeDrive: """One chapter folder, `boston`, with a Banners folder and a deck.""" - def __init__(self, runs, stamp=None, banner=True, md5="m1", banners=1): + def __init__(self, runs, stamp=None, banner=True, md5="m1", banners=1, + bdir=True, decks=1): self.deck = _pptx(runs) + self.bdir = bdir + self.decks = decks self.md5 = md5 self.stamp = stamp self.banners = banners if banner else 0 @@ -85,12 +96,14 @@ def __init__(self, runs, stamp=None, banner=True, md5="m1", banners=1): def list_children(self, fid): if fid == "boston": - kids = [{"id": "bdir", "name": BANNERS, "mimeType": cc.FOLDER}] + kids = [{"id": "bdir", "name": BANNERS, "mimeType": cc.FOLDER}] if self.bdir else [] + kids.append({"id": "other", "name": "Event Template", "mimeType": cc.FOLDER}) kids += [{"id": "wb" if i == 0 else "wb%d" % i, "name": rwb.BANNER, "mimeType": "image/png"} for i in range(self.banners)] return kids if fid == "bdir": - return [{"id": "deck", "name": rwb.DECK, "mimeType": "x"}] + return [{"id": "deck" if i == 0 else "deck%d" % i, "name": rwb.DECK, + "mimeType": "x"} for i in range(self.decks)] return [] def gws_json(self, *verb, params=None, body=None, retries=None): @@ -166,6 +179,10 @@ def test_a_leftover_template_city_beside_the_new_one_is_refused(self): def test_a_slide_without_the_headline_is_refused(self): self.assertIn("does not start", rwb.name_mismatch("Boston", "Boston")) + def test_punctuation_at_the_end_of_the_wanted_name_is_ignored_too(self): + self.assertIsNone(rwb.name_mismatch("AAIF Community Events Washington D.C.", + "Washington D.C.")) + def test_templatecity_expects_the_rebrand_source_city(self): self.assertEqual(rwb.expected_name("TemplateCity"), cc.SOURCE_NAME) self.assertEqual(rwb.expected_name("Boston"), "Boston") @@ -190,6 +207,16 @@ def test_xml_entities_are_unescaped(self): self.addCleanup(os.remove, p) self.assertIsNone(rwb.name_mismatch(rwb.slide_text(p), "Trinidad & Tobago")) + def test_a_word_split_across_runs_is_read_whole(self): + p = _path(_pptx(["AAIF Community Events", ["Amster", "dam"]])) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events Amsterdam") + + def test_package_absolute_slide_targets_resolve(self): + p = _path(_pptx(["AAIF Community Events", "Boston"], absolute=True)) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events Boston") + def test_a_short_deck_says_so(self): p = _path(_pptx(None, order=(1, 2), part_runs={})) self.addCleanup(os.remove, p) @@ -261,6 +288,24 @@ def test_duplicate_banners_are_refused_with_their_ids(self): self.assertIn("wb, wb1", r.error) self.assertEqual(fake.renders, []) + def test_no_banner_is_absent_even_without_a_banners_folder_or_deck(self): + for fake in (FakeDrive(self.OK, banner=False, bdir=False), + FakeDrive(self.OK, banner=False, decks=0)): + self.assertEqual(run(fake), rwb.Result("Boston", rwb.ABSENT)) + + def test_a_banner_with_no_banners_folder_is_a_failure(self): + r = run(FakeDrive(self.OK, bdir=False)) + self.assertEqual(r.error, "no Banners folder") + + def test_a_banner_with_no_deck_is_a_failure(self): + r = run(FakeDrive(self.OK, decks=0)) + self.assertIn("no Square Logo.pptx", r.error) + + def test_duplicate_decks_are_refused(self): + fake = FakeDrive(self.OK, decks=2) + self.assertIn("deck, deck1", run(fake).error) + self.assertEqual(fake.renders, []) + def test_a_sweep_never_creates_a_missing_banner(self): fake = FakeDrive(self.OK, banner=False) self.assertEqual(run(fake).action, rwb.ABSENT) @@ -280,6 +325,37 @@ def test_a_missing_banner_is_created_once_without_retries_and_stamped(self): self.assertEqual(creates[0][3]["parents"], ["boston"]) self.assertEqual(fake.stamp, rwb.stamp_value("m1", "Boston")) + def test_the_create_reaches_gws_once_even_when_it_looks_like_a_timeout(self): + """Driven to a counted subprocess, not a recorded kwarg (AGENTS.md).""" + fake = FakeDrive(self.OK, banner=False) + fake_json = fake.gws_json + + def json_through_real_create(*verb, **kw): + if verb[-1] == "create": + return gwsmod.json_out(*verb, **kw) + return fake_json(*verb, **kw) + fake.gws_json = json_through_real_create + with mock.patch("subprocess.run", return_value=mock.Mock( + returncode=1, stdout="", stderr="503 timed out")) as sp, \ + mock.patch("time.sleep"): + r = run(fake, create=True) + self.assertIsNotNone(r.error) + self.assertEqual(sp.call_count, 1) + self.assertNotIn("upload", fake.verbs()) + + def test_sync_one_writes_and_creates(self): + fake = FakeDrive(self.OK, banner=False) + with mock.patch.object(cc, "list_children", fake.list_children), \ + mock.patch.object(cc, "gws_json", fake.gws_json), \ + mock.patch.object(cc, "gws_download", fake.download), \ + mock.patch.object(cc, "gws_upload", fake.upload), \ + mock.patch.object(rwb.slides_export, "render_slide_png", fake.render): + r = rwb.sync_one("boston", "Boston", create=True) + self.assertEqual(r, rwb.Result("Boston", rwb.RENDER)) + self.assertEqual(fake.renders, ["deck"]) + self.assertIn("upload", fake.verbs()) + self.assertEqual(fake.stamp, rwb.stamp_value("m1", "Boston")) + def test_a_create_then_failed_upload_names_the_empty_file(self): fake = FakeDrive(self.OK, banner=False) fake.fail_upload = True @@ -299,12 +375,14 @@ class TestMain(unittest.TestCase): def _main(self, results, argv=()): seen = [] + self.writes = [] def fake_list(parent): return [{"id": i, "name": n, "mimeType": cc.FOLDER} for i, n in self.CHAPTERS] def fake_sync(cid, name, write, tmp, create=False): seen.append((name, create)) + self.writes.append(write) return results.get(name, rwb.Result(name, rwb.CURRENT)) with mock.patch.object(cc, "list_children", fake_list), \ mock.patch.object(rwb, "sync_chapter", fake_sync), \ @@ -339,6 +417,30 @@ def test_an_unknown_outcome_is_a_failure_not_clean(self): self.assertEqual(code, 1) self.assertIn("unknown outcome", out) + def test_report_mode_passes_write_false_and_write_passes_true(self): + self._main({}) + self.assertEqual(self.writes, [False, False]) + self._main({}, ["--write"]) + self.assertEqual(self.writes, [True, True]) + + def test_create_is_passed_through_and_needs_a_chapter(self): + _c, seen, _ = self._main({}, ["--chapter", "Boston", "--create"]) + self.assertEqual(seen, [("Boston", True)]) + with mock.patch("sys.stderr", io.StringIO()), self.assertRaises(SystemExit): + self._main({}, ["--create"]) + + def test_jobs_must_be_positive(self): + with mock.patch("sys.stderr", io.StringIO()), self.assertRaises(SystemExit): + self._main({}, ["--jobs", "0"]) + + def test_the_fallback_command_is_shell_quoted(self): + cmd = rwb.fallback_command("Ada's \"Town\"", create=True) + import shlex + argv = shlex.split(cmd) + self.assertEqual(argv[argv.index("--chapter") + 1], "Ada's \"Town\"") + self.assertIn("--create", argv) + self.assertNotIn("--create", rwb.fallback_command("Boston")) + def test_no_match_exits_1(self): code, _s, _o = self._main({}, ["--chapter", "Atlantis"]) self.assertEqual(code, 1)