#!/usr/bin/env python3 """Serve a cascade batch's review.html and write every human verdict straight into the Excel workbook. python3 review_server.py --batch ~/cascade-review/batch # then open http://localhost:8765 python3 review_server.py --batch ... --host 0.0.0.0 # let colleagues on the same network review too REVIEW_PASSWORD=... python3 review_server.py ... # require a password (browser login box; the username is the reviewer name) Each thumbs up / down or comment in the page is POSTed here and saved to /reviews.json (the source of truth, one entry per product x view, last write wins). A couple of seconds after the last change, /cascade_report.xlsx is rebuilt with those verdicts in its Human verdict / comment / reviewer columns (thumbs up = Pass, down = Fail), so the Summary formulas update too. Excel does not reload a file it already has open: reopen the workbook to see new verdicts. """ from __future__ import annotations import argparse import base64 import hmac import json import os import threading import time from datetime import datetime, timezone from functools import partial from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path from build_report_excel import build FIELDS = ("source", "product_id", "view", "human_verdict", "human_comment", "reviewer", "reviewed_at") class Store: def __init__(self, batch: Path, debounce: float, after_build=None): self.batch, self.debounce, self.after_build = batch, debounce, after_build # after_build(reviews_json, xlsx): e.g. push to the Hub self.path, self.xlsx = batch / "reviews.json", batch / "cascade_report.xlsx" self.lock, self.dirty = threading.Lock(), threading.Event() self.reviews = {f"{r['source']}|{r['product_id']}|{r['view']}": r for r in json.loads(self.path.read_text())} if self.path.exists() else {} self.excel = {"updated": None, "error": None, "pending": False} threading.Thread(target=self._excel_worker, daemon=True).start() def put(self, entry: dict) -> dict: r = {k: str(entry.get(k) or "") for k in FIELDS} if r["human_verdict"] not in ("", "pass", "fail") or not (r["source"] and r["product_id"] and r["view"]): raise ValueError("bad review entry") r["reviewed_at"] = datetime.now(timezone.utc).isoformat(timespec="seconds") with self.lock: key = f"{r['source']}|{r['product_id']}|{r['view']}" if r["human_verdict"] or r["human_comment"]: self.reviews[key] = r else: self.reviews.pop(key, None) tmp = self.path.with_suffix(".json.tmp") tmp.write_text(json.dumps(list(self.reviews.values()), indent=1, ensure_ascii=False)) os.replace(tmp, self.path) self.excel["pending"] = True self.dirty.set() return r def _excel_worker(self): while True: self.dirty.wait() time.sleep(self.debounce) # batch quick successive clicks into one rebuild self.dirty.clear() with self.lock: entries = list(self.reviews.values()) try: tmp = self.xlsx.with_name(".cascade_report.tmp.xlsx") msg = build(self.batch, entries, tmp) os.replace(tmp, self.xlsx) self.excel.update(updated=datetime.now().strftime("%H:%M:%S"), error=None) print(msg.replace(str(tmp), str(self.xlsx)), flush=True) if self.after_build: self.after_build(self.path, self.xlsx) except Exception as e: # keep serving; the page shows the error self.excel["error"] = f"{type(e).__name__}: {e}" print("Excel rebuild failed:", self.excel["error"], flush=True) self.excel["pending"] = self.dirty.is_set() class Handler(SimpleHTTPRequestHandler): store: Store password = os.environ.get("REVIEW_PASSWORD", "") # if set, every request needs HTTP Basic auth with it (any username) def _authorized(self) -> bool: """Checks the password; the login username (self.user) becomes the reviewer name.""" self.user = "" if not self.password: return True h = self.headers.get("Authorization", "") try: self.user, _, given = base64.b64decode(h[6:]).decode().partition(":") if h.startswith("Basic ") else ("", "", "") except ValueError: given = "" self.user = self.user.strip() if hmac.compare_digest(given.encode(), self.password.encode()): return True self.send_response(401) self.send_header("WWW-Authenticate", 'Basic realm="Cascade review", charset="UTF-8"') self.send_header("Content-Length", "0") self.end_headers() return False def _json(self, obj, code=200): body = json.dumps(obj).encode() self.send_response(code) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(body))) self.send_header("Cache-Control", "no-store") self.end_headers() self.wfile.write(body) def do_GET(self): if not self._authorized(): return if self.path in ("/", "/index.html"): self.path = "/review.html" if self.path == "/api/reviews": with self.store.lock: return self._json(list(self.store.reviews.values())) if self.path == "/api/excel" and self.store.xlsx.exists(): body = self.store.xlsx.read_bytes() self.send_response(200) self.send_header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") self.send_header("Content-Disposition", 'attachment; filename="cascade_report.xlsx"') self.send_header("Content-Length", str(len(body))) self.end_headers() return self.wfile.write(body) if self.path == "/api/me": return self._json({"user": self.user}) if self.path == "/api/status": return self._json({**self.store.excel, "reviews": len(self.store.reviews)}) if self.path.startswith(("/api/", "/.")) or self.path.endswith((".json", ".xlsx")) and "/" not in self.path.lstrip("/"): return self._json({"error": "not found"}, 404) # don't serve reviews.json / the workbook as files return super().do_GET() def do_POST(self): if not self._authorized(): return if self.path != "/api/review": return self._json({"error": "not found"}, 404) try: n = int(self.headers.get("Content-Length", 0)) entry = json.loads(self.rfile.read(n)) if self.user: entry["reviewer"] = self.user return self._json({"ok": True, "review": self.store.put(entry)}) except (ValueError, KeyError) as e: return self._json({"ok": False, "error": str(e)}, 400) def log_message(self, fmt, *args): if self.path.startswith("/api/review") and self.command == "POST": return if args and str(args[1]).startswith(("4", "5")): super().log_message(fmt, *args) def main() -> None: ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--batch", type=Path, required=True) ap.add_argument("--host", default="127.0.0.1") ap.add_argument("--port", type=int, default=8765) ap.add_argument("--debounce", type=float, default=2.0, help="seconds after the last change before the workbook is rebuilt") a = ap.parse_args() batch = a.batch.expanduser().resolve() if not (batch / "review.html").exists(): raise SystemExit(f"{batch}/review.html not found: run build_review_book.py --batch {batch} first") Handler.store = Store(batch, a.debounce) srv = ThreadingHTTPServer((a.host, a.port), partial(Handler, directory=str(batch))) print(f"Reviewing {batch} ({len(Handler.store.reviews)} saved reviews) at http://{'localhost' if a.host == '127.0.0.1' else a.host}:{a.port}", flush=True) srv.serve_forever() if __name__ == "__main__": main()