query_command.py

clew/query_command.py

`clew query` — the stable export API as a command that prints JSON.

_locate

tuple[Path, Path | None] clew.query_command._locate(argparse.Namespace args)

Resolve which database answers and which working tree bodies come from.

clew/query_command.py:87

Source · 18 lines
def _locate(args: argparse.Namespace) -> tuple[Path, Path | None]:
    """DERIVED THROUGH `target_for`, NEVER REIMPLEMENTED, as `cli._resolve_output` does for a
    build — so `clew query` reads exactly where a build wrote. Imported here because `mcp_server`
    is a view over the pipeline and must not be on this module's import path otherwise.

    @brief Resolve the index and the tree.
    @return The pair.
    @version 1
    """
    repo = Path(args.repo_root).expanduser().resolve() if args.repo_root else None
    if args.index:
        return Path(args.index).expanduser().resolve(), repo
    from .mcp_server.state import target_for

    tree = repo or Path.cwd().resolve()
    return Path(target_for(tree).db_path), tree

Activity · 6 steps
lines 96–96repo = Path(args.repo_root).expanduser().resolve() if args.repo_root else Nonelines 97–97◇ if args.indexlines 98–98return Path(args.index).expanduser().resolve(), repolines 99–99from .mcp_server.state import target_forlines 101–101tree = repo or Path.cwd().resolve()lines 102–102return Path(target_for(tree).db_path), tree

Flows

_parser

argparse.ArgumentParser clew.query_command._parser()

The argument parser for clew query .

clew/query_command.py:53

Show on the map →

Called by query_main

Calls none

Source · 29 lines
def _parser() -> argparse.ArgumentParser:
    """@brief Build the parser. @return Parser with `dossier` and `search` subcommands."""
    from .query import DEFAULT_BODY_LINES, EXPORT_CORPORA

    parser = argparse.ArgumentParser(
        prog=f"clew {QUERY_COMMAND}",
        description="Print a dossier or search reply from a clew index as JSON.",
    )
    sub = parser.add_subparsers(dest="verb", required=True)
    dossier = sub.add_parser("dossier", help="everything the index holds about named subjects")
    dossier.add_argument("subjects", nargs="+", help="one name or path; several = one batch")
    dossier.add_argument("--kind")
    dossier.add_argument("--qualified")
    dossier.add_argument("--depth", type=int, default=1)
    dossier.add_argument("--direction", default="forward")
    dossier.add_argument("--max-body-lines", type=int, default=DEFAULT_BODY_LINES)
    dossier.add_argument("--max-neighbors", type=int, default=8)
    search = sub.add_parser("search", help="search or enumerate one corpus")
    search.add_argument("text", nargs="?", default="")
    search.add_argument("--corpus", default="symbols", help=", ".join(EXPORT_CORPORA))
    search.add_argument("--limit", type=int, default=25)
    stats = sub.add_parser("stats", help="how much of the index can be trusted (graph quality)")
    for each in (dossier, search, stats):
        each.add_argument("--index", help="path to a built clew.db")
        each.add_argument("--repo-root", help="working tree (default: the current directory)")
        each.add_argument("--indent", type=int, default=None, help="pretty-print JSON")
    return parser

Activity · 21 steps
lines 55–55from .query import DEFAULT_BODY_LINES, EXPORT_CORPORAlines 57–60parser = argparse.ArgumentParser( prog=f"clew {QUERY_COMMAND}", description="Print a dossier or search reply from a clew index as JSON.", )lines 61–61sub = parser.add_subparsers(dest="verb", required=True)lines 62–62dossier = sub.add_parser("dossier", help="everything the index holds about named subjects")lines 63–63dossier.add_argument("subjects", nargs="+", help="one name or path; several = one batch")lines 64–64dossier.add_argument("--kind")lines 65–65dossier.add_argument("--qualified")lines 66–66dossier.add_argument("--depth", type=int, default=1)lines 67–67dossier.add_argument("--direction", default="forward")lines 68–68dossier.add_argument("--max-body-lines", type=int, default=DEFAULT_BODY_LINES)lines 69–69dossier.add_argument("--max-neighbors", type=int, default=8)lines 70–70search = sub.add_parser("search", help="search or enumerate one corpus")lines 71–71search.add_argument("text", nargs="?", default="")lines 72–72search.add_argument("--corpus", default="symbols", help=", ".join(EXPORT_CORPORA))lines 73–73search.add_argument("--limit", type=int, default=25)lines 74–74stats = sub.add_parser("stats", help="how much of the index can be trusted (graph quality)")lines 75–75◇ for each in (dossier, search, stats)lines 76–76each.add_argument("--index", help="path to a built clew.db")lines 77–77each.add_argument("--repo-root", help="working tree (default: the current directory)")lines 78–78each.add_argument("--indent", type=int, default=None, help="pretty-print JSON")lines 79–79return parser

Flows

_payload

dict[str, Any] clew.query_command._payload(argparse.Namespace args, Path db, Path|None repo)

Build the payload the parsed request asks for.

clew/query_command.py:113

Source · 24 lines
def _payload(args: argparse.Namespace, db: Path, repo: Path | None) -> dict[str, Any]:
    """@brief Dispatch to the export function. @return The payload."""
    from . import query as q

    if args.verb == "search":
        return q.export_search(db, args.text, corpus=args.corpus, limit=args.limit, repo_root=repo)
    if args.verb == "stats":
        from . import wire

        return wire.one(q.graph_stats(db))
    subject: str | list[str] = args.subjects[0] if len(args.subjects) == 1 else args.subjects
    return q.export_dossier(
        db,
        subject,
        kind=args.kind,
        qualified=args.qualified,
        repo_root=repo,
        max_body_lines=args.max_body_lines,
        depth=args.depth,
        direction=args.direction,
        max_neighbors=args.max_neighbors,
    )

Activity · 8 steps
lines 115–115from . import query as qlines 117–117◇ if args.verb == "search"lines 118–118return q.export_search(db, args.text, corpus=args.corpus, limit=args.limit, repo_root=repo)lines 119–119◇ if args.verb == "stats"lines 120–120from . import wirelines 122–122return wire.one(q.graph_stats(db))lines 123–123subject: str | list[str] = args.subjects[0] if len(args.subjects) == 1 else args.subjectslines 124–134return q.export_dossier( db, subject, kind=args.kind, qualified=args.qualified, repo_root=repo, max_body_lines=args.max_body_lines, depth=args.depth, direction=args.direction, max_neighbors=args.max_neighbors, )

Flows

query_main

int clew.query_command.query_main(list[str] argv)

Parse arguments, answer, and print JSON.

clew/query_command.py:142

Source · 19 lines
def query_main(argv: list[str]) -> int:
    """NOTHING ON STDOUT UNLESS IT IS THE ANSWER, so a host can parse stdout without filtering.

    @brief Entry point for `clew query`.
    @return Exit status.
    @version 1
    """
    args = _parser().parse_args(argv)
    db, repo = _locate(args)
    if not db.is_file():
        sys.stderr.write(f"clew query: no index at {db} — build one with `clew --repo-root`\n")
        return 1
    try:
        payload = _payload(args, db, repo)
    except ValueError as exc:
        sys.stderr.write(f"clew query: refused — {exc}\n")
        return 2
    sys.stdout.write(json.dumps(payload, indent=args.indent) + "\n")
    return 0

1 more lines, not shown

Activity · 12 steps
lines 149–149args = _parser().parse_args(argv)lines 150–150db, repo = _locate(args)lines 151–151◇ if not db.is_file()lines 152–152sys.stderr.write(f"clew query: no index at {db} — build one with `clew --repo-root`\n")lines 153–153return 1lines 154–154trylines 155–155payload = _payload(args, db, repo)lines 156–156except ValueError as exclines 157–157sys.stderr.write(f"clew query: refused — {exc}\n")lines 158–158return 2lines 159–159sys.stdout.write(json.dumps(payload, indent=args.indent) + "\n")lines 160–160return 0

Requirements

REQ-DDB-QUERY-014

Flows