#!/usr/bin/env python3 """ patch_index.py -- adds the sky dome to index.html with ONE block of script tags before . It changes nothing else in the page. The dome mounts itself as a full-screen layer on top of the existing app and calls your existing functions (showSkyMap, setSearchCenter, runSearch, applySky). Usage (from the project folder): python3 tools/patch_index.py # patch ./index.html (keeps index.html.bak) python3 tools/patch_index.py --check # report only python3 tools/patch_index.py --remove # take the dome block out again Open the app with ?dome=0 to skip the dome without removing anything. """ import re import shutil import sys from pathlib import Path BEGIN = "" END = "" FILES = ["dome-coords.js", "dome.js", "dome-look.js", "dome-layers.js", "dome-landscape.js", "app-state.js", "dome-select.js", "dome-hud.js", "dome-app.js"] BLOCK = BEGIN + "\n" + "\n".join('' % f for f in FILES) + \ "\n\n" + END + "\n" def main(): args = [a for a in sys.argv[1:] if not a.startswith("--")] target = Path(args[0] if args else "index.html") if not target.is_file(): sys.exit("Cannot find %s. Run this from the folder that contains index.html." % target) text = target.read_text(encoding="utf-8") patched = BEGIN in text if "--check" in sys.argv: print(("Dome is installed in %s" if patched else "Dome is NOT installed in %s") % target) return if "--remove" in sys.argv: if not patched: print("Nothing to remove.") return new = re.sub(re.escape(BEGIN) + r".*?" + re.escape(END) + r"\n?", "", text, flags=re.S) shutil.copy2(target, target.with_name(target.name + ".before-remove")) target.write_text(new, encoding="utf-8") print("Removed the dome block from %s." % target) return if patched: print("Already patched: %s" % target) return if text.count("") != 1: sys.exit("Nothing changed: expected exactly one in %s (found %d)." % (target, text.count(""))) if "three@0.128.0" not in text and "three.min.js" not in text: print("WARNING: index.html does not appear to load Three.js r128. The dome needs it; the page will skip the dome.") backup = target.with_name(target.name + ".bak") shutil.copy2(target, backup) target.write_text(text.replace("", BLOCK + "", 1), encoding="utf-8") print("Patched %s (backup: %s). Restart the server and reload." % (target, backup)) if __name__ == "__main__": main()