94 lines
4.5 KiB
Python
94 lines
4.5 KiB
Python
"""Command-line access; read-only unless build or --report is requested."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
from jsonschema.exceptions import SchemaError, ValidationError
|
|
|
|
from .library import KnowledgeBase
|
|
from .render import build, generated_outputs
|
|
from .evaluation import evaluate
|
|
|
|
|
|
def _reject_constant(value):
|
|
raise ValueError(f"JSON non-finite constant is not allowed: {value}")
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(description="Offline CAD part knowledge")
|
|
parser.add_argument("--root", type=Path, help="Source checkout containing references/ and schemas/")
|
|
commands = parser.add_subparsers(dest="command", required=True)
|
|
search = commands.add_parser("search")
|
|
search.add_argument("query")
|
|
context = commands.add_parser("context")
|
|
context.add_argument("family_ids", nargs="+")
|
|
context.add_argument("--features", nargs="*", default=[])
|
|
context.add_argument("--examples", action="store_true")
|
|
context.add_argument("--format", choices=["json", "markdown"], default="json")
|
|
generation = commands.add_parser("generation-context", help="Admitted generation text only",
|
|
description="Offline text loader for registered generation type IDs and legacy family IDs; checks admission and hashes, not deployment versions or variant applicability conditions.")
|
|
generation.add_argument("family_ids", nargs="+", metavar="TYPE_OR_FAMILY_ID")
|
|
generation.add_argument("--engine-build", help="Optional diagnostic deployment label; never a loading condition")
|
|
generation.add_argument("--experimental", action="store_true", help="Explicit opt-in for isolated A/B trials")
|
|
generation.add_argument("--format", choices=["json", "markdown"], default="json")
|
|
validate = commands.add_parser("validate")
|
|
validate.add_argument("family_id")
|
|
inputs = validate.add_mutually_exclusive_group(required=True)
|
|
inputs.add_argument("--params", type=Path)
|
|
inputs.add_argument("--example", action="store_true")
|
|
commands.add_parser("check")
|
|
commands.add_parser("build")
|
|
evaluation = commands.add_parser("evaluate")
|
|
evaluation.add_argument("--report", type=Path)
|
|
args = parser.parse_args(argv)
|
|
try:
|
|
kb = KnowledgeBase(args.root)
|
|
if args.command == "search":
|
|
result = kb.search(args.query)
|
|
elif args.command == "context":
|
|
result = kb.build_context(args.family_ids, args.features, args.examples)
|
|
if args.format == "markdown":
|
|
print(result["text"])
|
|
return 0
|
|
elif args.command == "generation-context":
|
|
result = kb.build_generation_context(args.family_ids, args.engine_build,
|
|
experimental=args.experimental)
|
|
if args.format == "markdown":
|
|
print(result["text"], end="\n" if result["text"] else "")
|
|
return 0
|
|
elif args.command == "validate":
|
|
if args.example:
|
|
family = kb._require(args.family_id, "family")
|
|
params = kb.documents[family["meta"]["example_id"]]["meta"]["params"]
|
|
else:
|
|
params = json.loads(args.params.read_text(encoding="utf-8"), parse_constant=_reject_constant)
|
|
result = kb.validate_params(args.family_id, params)
|
|
elif args.command == "build":
|
|
result = build(kb)
|
|
elif args.command == "check":
|
|
result = kb.check()
|
|
for relative, expected in generated_outputs(kb).items():
|
|
file = kb.root / relative
|
|
if not file.exists() or file.read_text(encoding="utf-8") != expected:
|
|
result["errors"].append(f"Generated output missing/stale: {relative}; run build")
|
|
result["passed"] = not result["errors"]
|
|
else:
|
|
result = evaluate(kb)
|
|
if args.report:
|
|
args.report.parent.mkdir(parents=True, exist_ok=True)
|
|
args.report.write_text(json.dumps(result, ensure_ascii=False, indent=2, allow_nan=False) + "\n", encoding="utf-8")
|
|
print(json.dumps(result, ensure_ascii=False, indent=2, allow_nan=False))
|
|
return 0 if result.get("passed", result.get("valid", True)) else 1
|
|
except (OSError, ValueError, KeyError, yaml.YAMLError, SchemaError, ValidationError) as exc:
|
|
print(json.dumps({"error": str(exc)}, ensure_ascii=False), file=sys.stderr)
|
|
return 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|