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.
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
Flows
_parser
argparse.ArgumentParser clew.query_command._parser()
The argument parser for clew query .
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
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.
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
Flows
query_main
int clew.query_command.query_main(list[str] argv)
Parse arguments, answer, and print JSON.
Called by _run, main
Tested by
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 01 more lines, not shown