#!/usr/bin/env python3 """ wiki_ls.py — list what is actually in a wiki. One call, no bodies. Cheap enough to run before a pull: it tells you what titles exist, which is the only thing a prefix filter can be built from, and it shows the `sub_url` Gitea settled on for each — worth a look the first time a title contains a dash or a slash, because the escaping is not what anyone guesses. wiki_ls.py wiki_ls.py --prefix "Simple Chains" wiki_ls.py --repo other/repo --titles Usage: wiki_ls.py [--repo owner/repo] [--prefix TITLE] [--titles] [--urls] """ import argparse import os import sys _HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, _HERE) sys.path.insert(0, os.path.join(_HERE, "..", "..", "sync", "scripts")) import _gitea # noqa: E402 import wikimap # noqa: E402 def cell(v): return (str(v or "").strip().replace("|", "\\|")) or "—" def main(): ap = argparse.ArgumentParser() ap.add_argument("--repo", help="owner/repo (default: the repo in CWD)") ap.add_argument("--prefix", default="", help="only titles at or under this") ap.add_argument("--titles", action="store_true", help="print one title per line and nothing else") ap.add_argument("--urls", action="store_true", help="add the browser URL") a = ap.parse_args() login = _gitea.require_login() base = _gitea.repo_base(a.repo) slug = _gitea.repo_slug(login, a.repo) listing = _gitea.paginate(login, "%s/wiki/pages" % base) if not isinstance(listing, list): _gitea.die("unexpected listing from %s/wiki/pages" % base) rows = sorted((p for p in listing if wikimap.matches_prefix(p.get("title") or "", a.prefix)), key=lambda p: p.get("title") or "") if not rows: print("no page at or under %r in %s" % (a.prefix, slug) if a.prefix else "%s has no wiki pages" % slug) return 0 if a.titles: for p in rows: print(p.get("title") or "") return 0 head = ["title", "sub_url", "updated", "by"] + (["url"] if a.urls else []) print("| %s |" % " | ".join(head)) print("|%s|" % "|".join("---" for _ in head)) for p in rows: c = (p.get("last_commit") or {}).get("author") or {} row = [cell(p.get("title")), "`%s`" % cell(p.get("sub_url")), cell((c.get("date") or "")[:10]), cell(c.get("name"))] if a.urls: row.append(cell(p.get("html_url"))) print("| %s |" % " | ".join(row)) print("\n%d page(s) in %s" % (len(rows), slug)) return 0 if __name__ == "__main__": sys.exit(main())