#!/usr/bin/env python3 from __future__ import annotations import argparse import json import re from datetime import datetime, timezone from pathlib import Path from typing import Dict, List def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Publish accepted social requirements to markdown/html pages.") parser.add_argument( "--input-dir", default="", help="Directory under data/social_requirements containing llm_requirement_accepted_curated.json", ) return parser.parse_args() def latest_run_dir(base: Path) -> Path: dirs = [p for p in base.iterdir() if p.is_dir() and re.match(r"^\d{8}_\d{6}_utc$", p.name)] if not dirs: raise FileNotFoundError(f"No run directories found in {base}") return sorted(dirs, key=lambda p: p.name)[-1] def load_accepted(input_dir: Path) -> List[Dict]: curated = input_dir / "llm_requirement_accepted_curated.json" raw = input_dir / "llm_requirement_accepted.json" if curated.exists(): payload = json.loads(curated.read_text(encoding="utf-8")) return payload.get("accepted", []) if raw.exists(): payload = json.loads(raw.read_text(encoding="utf-8")) return payload.get("accepted", []) raise FileNotFoundError(f"No accepted requirements file found in {input_dir}") def render_markdown(run_dir: Path, accepted: List[Dict]) -> str: lines: List[str] = [] lines.append("# Social User Requirements (LLM Curated)") lines.append("") lines.append(f"- Source run: `{run_dir.name}`") lines.append(f"- Generated at: `{datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}`") lines.append(f"- Accepted requirements: **{len(accepted)}**") lines.append("") lines.append("## Requirements") lines.append("") for idx, item in enumerate(accepted, start=1): cid = item.get("cluster_id", "") req = str(item.get("requirement", item.get("normalized_requirement", ""))).strip() reason = str(item.get("reason", "")).strip() count = item.get("demand_count", 0) lines.append(f"### {idx}. {req}") lines.append(f"- Cluster: `{cid}`") lines.append(f"- Mentions: {count}") if reason: lines.append(f"- Why accepted: {reason}") examples = item.get("examples", []) if examples: ex = examples[0] title = ex.get("title", "source post") link = ex.get("permalink", "#") lines.append(f"- Evidence: [{title}]({link})") lines.append("") return "\n".join(lines).strip() + "\n" def render_html(run_dir: Path, accepted: List[Dict]) -> str: cards: List[str] = [] for idx, item in enumerate(accepted, start=1): cid = item.get("cluster_id", "") req = str(item.get("requirement", item.get("normalized_requirement", ""))).strip() reason = str(item.get("reason", "")).strip() count = item.get("demand_count", 0) examples = item.get("examples", []) evidence_html = "" if examples: ex = examples[0] title = ex.get("title", "source post") link = ex.get("permalink", "#") evidence_html = f'

Evidence: {title}

' cards.append( f"""

{idx}. {req}

Cluster: {cid} | Mentions: {count}

{reason}

{evidence_html}
""" ) stamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC") return f""" Social User Requirements

Social User Requirements (LLM Curated)

Source run: {run_dir.name} | Generated: {stamp} | Accepted: {len(accepted)}

{''.join(cards)}
""" def main() -> None: args = parse_args() root = Path(__file__).resolve().parents[1] base = root / "data" / "social_requirements" run_dir = Path(args.input_dir) if args.input_dir else latest_run_dir(base) accepted = load_accepted(run_dir) out_dir = root / "pages" / "requirements" out_dir.mkdir(parents=True, exist_ok=True) md_path = out_dir / "social_user_requirements.md" html_path = out_dir / "social_user_requirements.html" md_path.write_text(render_markdown(run_dir, accepted), encoding="utf-8") html_path.write_text(render_html(run_dir, accepted), encoding="utf-8") print(f"Source: {run_dir}") print(f"Accepted requirements: {len(accepted)}") print(f"Markdown page: {md_path}") print(f"HTML page: {html_path}") if __name__ == "__main__": main()