Skip to content

Add knowledge-mapper run CLI subcommand (#10)#47

Merged
DaviddeBest-TNO merged 2 commits into
mainfrom
10-cli
Jun 25, 2026
Merged

Add knowledge-mapper run CLI subcommand (#10)#47
DaviddeBest-TNO merged 2 commits into
mainfrom
10-cli

Conversation

@DaviddeBest-TNO

Copy link
Copy Markdown
Contributor

Closes #10.

What

Adds a typer-based knowledge-mapper CLI with one subcommand, run, that starts a Python-defined KnowledgeBase and replaces the usual asyncio.run(...) boilerplate:

knowledge-mapper run path/to/my_app.py:kb

The PATH:ATTR spec points at a Python file and the name of the KnowledgeBase variable inside it.

Behaviour

  • Loads the file via importlib.util and retrieves the named attribute. The file's parent directory is added to sys.path so the module can import siblings just like python file.py.
  • Runs the full async lifecycle: connect → register → start_handling_loop.
  • On SIGINT / SIGTERM: cancels the handling task, then unregister + close run in finally, and the process exits cleanly.
  • If start_handling_loop raises, unregister + close still run.

The issue's spec was written before the SDK switched to async; this implementation adapts the lifecycle accordingly (and adds close() to match the README lifecycle, as discussed).

Layout

  • src/knowledge_mapper/cli.pyload_kb, run_kb, and the typer app with the run subcommand. A @app.callback() is added so the app keeps a subcommand structure (room for a future sparql subcommand, per the issue).
  • pyproject.toml — adds typer>=0.15 dep and registers the knowledge-mapper = knowledge_mapper.cli:app console script.
  • tests/test_cli.py — 9 behavior-focused tests written TDD-style covering load happy/sad paths, sibling imports, full lifecycle order, cleanup on error, graceful SIGINT shutdown, and an end-to-end typer CliRunner test.
  • examples/10-cli.py — minimal KB definition with no asyncio.run / __main__ block, runnable via knowledge-mapper run 10-cli.py:kb.
  • README.md, examples/README.md, CONTEXT.md updated.

Verification

  • uv run pytest — 122 tests pass (113 existing + 9 new)
  • uv run ruff check . — clean
  • uv run knowledge-mapper --help and knowledge-mapper run --help render correctly
  • Smoke-tested loading examples/10-cli.py via the loader — KI registered as expected

DaviddeBest-TNO and others added 2 commits June 25, 2026 18:19
Adds a typer-based CLI entry point so users can start a Python-defined
KnowledgeBase without writing asyncio.run boilerplate:

    knowledge-mapper run path/to/file.py:kb

The command:
- loads the named KnowledgeBase attribute from the given Python file
  (the file's parent directory is added to sys.path so sibling imports
  work just like `python file.py`)
- runs the full async lifecycle: connect, register, start_handling_loop
- on SIGINT/SIGTERM cancels the handling loop and unregisters + closes
  the KB before exiting cleanly
- still unregisters + closes if the handling loop raises

Includes an example (examples/10-cli.py) that defines a KB without any
asyncio.run or __main__ block, README/CONTEXT updates, and unit + typer
CliRunner integration tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@DaviddeBest-TNO DaviddeBest-TNO merged commit a13f364 into main Jun 25, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI: knowledge-mapper run — start a Python-defined KnowledgeBase

1 participant