Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 78 additions & 5 deletions lib/aaif_events/slides_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
_gws_json = _gws_mod.json_out


_PNG_MAGIC = b"\x89PNG\r\n\x1a\n"


def _page_text(page):
"""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 ():
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)


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
Expand All @@ -39,18 +61,39 @@ 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 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
Expand All @@ -63,16 +106,33 @@ 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 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)

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
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.
Expand All @@ -90,3 +150,16 @@ 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 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"
% (presentation_id, cleanup_error))
return out_path
126 changes: 124 additions & 2 deletions lib/aaif_events/tests/test_slides_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,117 @@ 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")

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."""

Expand All @@ -210,9 +321,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):
Expand All @@ -224,6 +335,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"<html>" + 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")
Expand Down
30 changes: 30 additions & 0 deletions skills/aaif-create-chapter/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ template or the rebrand engine is broken. Fix that — don't resume.
render_slide_png('<Slides.pptx file id>', '$(mktemp -d)/slide5.png', slide_index=4)
"
```
- [ ] 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
Expand All @@ -192,6 +196,32 @@ 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 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 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 (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. `--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 <skill-root>/scripts/render_web_banner.py # report
python3 <skill-root>/scripts/render_web_banner.py --write
python3 <skill-root>/scripts/render_web_banner.py --chapter <City> --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
99, each pass, and the next sync refuses all three rows together.
Expand Down
16 changes: 16 additions & 0 deletions skills/aaif-create-chapter/references/rename-chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ 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 `--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** 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 <new>`, 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`** 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
Expand Down
Loading
Loading