askrobots / dbbasic-face
Public
Puppet Stage: a browser puppet that lip-syncs to local text-to-speech and takes stage directions, live or from a screenplay.
build.py · 3.1 KB · 90 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """Generate the DBBASIC stage page object from the main app's sources. |
| 3 | |
| 4 | Assembles puppet-stage/objects/puppet/stage.py from: |
| 5 | ../../public/index.html (stage DOM + CSS, script tags stripped) |
| 6 | ../../public/stage.js (verbatim frontend runtime) |
| 7 | ./shim.js (DBBASIC transport adapter, loaded first) |
| 8 | ../../characters/*/ (embedded as window.PUPPET_CHARACTERS) |
| 9 | ../../assets/music/*.json, ../../assets/sfx/*.json |
| 10 | (embedded as window.PUPPET_AUDIO — small JSON |
| 11 | note patterns, same embed-not-serve treatment |
| 12 | as characters) |
| 13 | |
| 14 | Re-run after changing any of those so the package cannot drift from the app. |
| 15 | """ |
| 16 | |
| 17 | import json |
| 18 | import re |
| 19 | from pathlib import Path |
| 20 | |
| 21 | HERE = Path(__file__).resolve().parent |
| 22 | ROOT = HERE.parent.parent |
| 23 | OUT = HERE / "puppet-stage" / "objects" / "puppet" / "stage.py" |
| 24 | |
| 25 | index_html = (ROOT / "public" / "index.html").read_text() |
| 26 | stage_js = (ROOT / "public" / "stage.js").read_text() |
| 27 | shim_js = (HERE / "shim.js").read_text() |
| 28 | |
| 29 | css = re.search(r"<style>(.*?)</style>", index_html, re.S).group(1) |
| 30 | body = re.search(r"<body>(.*?)</body>", index_html, re.S).group(1) |
| 31 | body = re.sub(r"<script[^>]*>.*?</script>", "", body, flags=re.S).strip() |
| 32 | |
| 33 | characters = {} |
| 34 | for manifest_path in sorted((ROOT / "characters").glob("*/manifest.json")): |
| 35 | manifest = json.loads(manifest_path.read_text()) |
| 36 | svg = (manifest_path.parent / manifest["svg"]).read_text() |
| 37 | characters[manifest_path.parent.name] = {"manifest": manifest, "svg": svg} |
| 38 | |
| 39 | examples = {} |
| 40 | for script_path in sorted((ROOT / "examples").glob("*.txt")): |
| 41 | examples[script_path.stem] = script_path.read_text() |
| 42 | |
| 43 | audio = {"music": {}, "sfx": {}} |
| 44 | for kind in audio: |
| 45 | for asset_path in sorted((ROOT / "assets" / kind).glob("*.json")): |
| 46 | audio[kind][asset_path.stem] = json.loads(asset_path.read_text()) |
| 47 | |
| 48 | html = f"""<!doctype html> |
| 49 | <html lang="en"> |
| 50 | <head> |
| 51 | <meta charset="utf-8"> |
| 52 | <title>Puppet Stage</title> |
| 53 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 54 | <style>{css}</style> |
| 55 | </head> |
| 56 | <body> |
| 57 | {body} |
| 58 | <script>window.PUPPET_CHARACTERS = {json.dumps(characters)};</script> |
| 59 | <script>window.PUPPET_EXAMPLES = {json.dumps(examples)};</script> |
| 60 | <script>window.PUPPET_AUDIO = {json.dumps(audio)};</script> |
| 61 | <script> |
| 62 | {shim_js} |
| 63 | </script> |
| 64 | <script> |
| 65 | {stage_js} |
| 66 | </script> |
| 67 | </body> |
| 68 | </html> |
| 69 | """ |
| 70 | |
| 71 | assert "'''" not in html, "generated page would break the stage.py literal" |
| 72 | |
| 73 | stage_py = f'''"""Puppet Stage page object. GENERATED by integrations/dbbasic/build.py — do not edit. |
| 74 | |
| 75 | Serves the complete stage (HTML, CSS, runtime, characters) as one page. |
| 76 | Speech and direction go through the puppet_speak and puppet_cues objects. |
| 77 | """ |
| 78 | |
| 79 | _HTML = r\'\'\'{html}\'\'\' |
| 80 | |
| 81 | |
| 82 | def GET(request): |
| 83 | return {{"content_type": "text/html; charset=utf-8", "body": _HTML}} |
| 84 | ''' |
| 85 | |
| 86 | OUT.parent.mkdir(parents=True, exist_ok=True) |
| 87 | OUT.write_text(stage_py) |
| 88 | print(f"wrote {OUT} ({len(stage_py)//1024} KB, characters: {', '.join(characters)}, " |
| 89 | f"examples: {', '.join(examples)}, music: {', '.join(audio['music'])}, " |
| 90 | f"sfx: {', '.join(audio['sfx'])})") |