|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from check_entry import COMMUNITY_DIR, discover_entries |
| 7 | +from entry_rules import load_meta |
| 8 | + |
| 9 | +_HEADER = """\ |
| 10 | +# Community |
| 11 | +
|
| 12 | +<!-- This file is generated by community/_ci/build_index.py. Do not edit by hand. --> |
| 13 | +
|
| 14 | +Work built with Opik by the open-source community. These entries are |
| 15 | +**community-contributed and not maintainer-verified** — they are showcased here |
| 16 | +to help people discover what others are building. Standout, real-world projects |
| 17 | +are hosted in-repo (see the Hosted column). |
| 18 | +
|
| 19 | +To add your own, see [CONTRIBUTING.md](CONTRIBUTING.md). |
| 20 | +""" |
| 21 | + |
| 22 | +_TABLE_HEADER = ( |
| 23 | + "| Project | Author | Description | Platform | Links | Hosted | Tags |\n" |
| 24 | + "|---|---|---|---|---|---|---|" |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def load_entries(community_dir: Path) -> list[dict]: |
| 29 | + entries: list[dict] = [] |
| 30 | + for entry in discover_entries(community_dir): |
| 31 | + data, errors = load_meta(entry) |
| 32 | + if errors: |
| 33 | + continue |
| 34 | + entries.append( |
| 35 | + { |
| 36 | + "slug": entry.name, |
| 37 | + "title": str(data.get("title", entry.name)), |
| 38 | + "description": str(data.get("description", "")), |
| 39 | + "author": str(data.get("author", "")), |
| 40 | + "links": data.get("links") or {}, |
| 41 | + "opik_platform": str(data.get("opik_platform", "")), |
| 42 | + "tags": data.get("tags") or [], |
| 43 | + "hosted": bool(data.get("hosted", False)), |
| 44 | + } |
| 45 | + ) |
| 46 | + entries.sort(key=lambda e: e["title"].casefold()) |
| 47 | + return entries |
| 48 | + |
| 49 | + |
| 50 | +def _escape_pipes(value: str) -> str: |
| 51 | + return value.replace("|", "\\|") |
| 52 | + |
| 53 | + |
| 54 | +def _links_cell(links: dict) -> str: |
| 55 | + parts = [ |
| 56 | + f"[{_escape_pipes(str(label))}]({_escape_pipes(str(url))})" |
| 57 | + for label, url in links.items() |
| 58 | + if str(url).startswith("http") |
| 59 | + ] |
| 60 | + return "<br>".join(parts) if parts else "" |
| 61 | + |
| 62 | + |
| 63 | +def _row(entry: dict) -> str: |
| 64 | + project = f"[{_escape_pipes(entry['title'])}]({entry['slug']}/)" |
| 65 | + author = ( |
| 66 | + f"[@{_escape_pipes(entry['author'])}](https://github.com/{_escape_pipes(entry['author'])})" |
| 67 | + if entry["author"] |
| 68 | + else "" |
| 69 | + ) |
| 70 | + tags = _escape_pipes(", ".join(str(t) for t in entry["tags"])) |
| 71 | + hosted = "hosted" if entry["hosted"] else "" |
| 72 | + cells = [ |
| 73 | + project, |
| 74 | + author, |
| 75 | + _escape_pipes(entry["description"]), |
| 76 | + _escape_pipes(entry["opik_platform"]), |
| 77 | + _links_cell(entry["links"]), |
| 78 | + hosted, |
| 79 | + tags, |
| 80 | + ] |
| 81 | + return "| " + " | ".join(cells) + " |" |
| 82 | + |
| 83 | + |
| 84 | +def render_index(entries: list[dict]) -> str: |
| 85 | + lines = [_HEADER] |
| 86 | + if not entries: |
| 87 | + lines.append("_No community contributions yet — be the first!_\n") |
| 88 | + else: |
| 89 | + lines.append(_TABLE_HEADER) |
| 90 | + lines.extend(_row(e) for e in entries) |
| 91 | + lines.append("") |
| 92 | + return "\n".join(lines).rstrip("\n") + "\n" |
| 93 | + |
| 94 | + |
| 95 | +def write_index(community_dir: Path) -> None: |
| 96 | + (community_dir / "README.md").write_text( |
| 97 | + render_index(load_entries(community_dir)), encoding="utf-8" |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def check_index(community_dir: Path) -> bool: |
| 102 | + readme = community_dir / "README.md" |
| 103 | + current = readme.read_text(encoding="utf-8") if readme.is_file() else "" |
| 104 | + return current == render_index(load_entries(community_dir)) |
| 105 | + |
| 106 | + |
| 107 | +def main(argv: list[str] | None = None) -> int: |
| 108 | + args = sys.argv[1:] if argv is None else argv |
| 109 | + if "--check" in args: |
| 110 | + if check_index(COMMUNITY_DIR): |
| 111 | + print("community/README.md is up to date.") |
| 112 | + return 0 |
| 113 | + print("community/README.md is stale. Run: python build_index.py") |
| 114 | + return 1 |
| 115 | + write_index(COMMUNITY_DIR) |
| 116 | + print("Wrote community/README.md") |
| 117 | + return 0 |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + raise SystemExit(main()) |
0 commit comments