Skip to content

repl

repl

Generic prompt_toolkit REPL driver for command-group trees.

NamespaceCompleter

NamespaceCompleter(root: CommandGroup)

Bases: Completer

Hierarchical tab-completer for a command-group tree.

Initialise the completer for a command tree.

Parameters:

Name Type Description Default
root CommandGroup

The root command-group to complete against.

required
Source code in src/applied_motion/cli/compose/repl.py
def __init__(self, root: CommandGroup) -> None:
    """Initialise the completer for a command tree.

    Args:
        root: The root command-group to complete against.
    """
    self.root = root

get_completions

get_completions(document, complete_event)

Yield completion candidates for the current input.

Parameters:

Name Type Description Default
document

The current input document.

required
complete_event

Completion event object.

required
Source code in src/applied_motion/cli/compose/repl.py
def get_completions(self, document, complete_event):  # noqa: ANN001, ANN201
    """Yield completion candidates for the current input.

    Args:
        document: The current input document.
        complete_event: Completion event object.
    """
    text = document.text_before_cursor
    tokens = text.split()
    at_word_boundary = text == "" or text.endswith(" ")
    consumed = tokens if at_word_boundary else tokens[:-1]
    partial = "" if at_word_boundary else tokens[-1].lower()

    group = self.root
    for token in consumed:
        key = token.lower()
        if key in group.children:
            group = group.children[key]
        else:
            return

    options = list(group.children.keys()) + list(group.commands.keys())
    if group is self.root:
        options = options + _RESERVED_CMDS
    for option in options:
        if option.startswith(partial):
            yield Completion(option, start_position=-len(partial))

render_help

render_help(root: CommandGroup) -> str

Render an aggregated help listing for a command tree.

Source code in src/applied_motion/cli/compose/repl.py
def render_help(root: CommandGroup) -> str:
    """Render an aggregated help listing for a command tree."""
    lines = ["[festo.brand]Commands[/]"]

    def _append(group: CommandGroup, indent: int) -> None:
        for cmd in group.commands.values():
            lines.append(format_help_line(cmd.usage or cmd.name, cmd.help, indent=indent))
        for child_name, child in group.children.items():
            lines.append(format_group_header(child_name, child.help, indent=indent))
            _append(child, indent + 1)

    _append(root, 0)
    lines.append(format_help_line("help", "Show this reference"))
    lines.append(format_help_line("quit", "Exit"))
    return "\n".join(lines)

run_repl

run_repl(
    root: CommandGroup,
    prompt: str = "> ",
    console: Console | None = None,
    intro: bool = True,
) -> None

Run an interactive REPL over a command tree.

Source code in src/applied_motion/cli/compose/repl.py
def run_repl(  # noqa: C901
    root: CommandGroup,
    prompt: str = "> ",
    console: Console | None = None,
    intro: bool = True,
) -> None:
    """Run an interactive REPL over a command tree."""
    console = console or festo_console()
    session: PromptSession[str] = PromptSession(history=InMemoryHistory(), completer=NamespaceCompleter(root))

    if intro:
        console.print(render_help(root))

    with patch_stdout(raw=True):
        while True:
            try:
                raw = session.prompt(prompt).strip()
            except (EOFError, KeyboardInterrupt):
                console.print("\n[festo.muted]Exiting.[/]")
                break

            if not raw:
                continue

            tokens = raw.split()
            head = tokens[0].lower()

            if head in ("quit", "exit"):
                break
            if head == "help":
                console.print(render_help(root))
                continue

            try:
                root.dispatch(tokens)
            except UsageError as exc:
                console.print(f"[festo.err]✗[/] {exc}")
            except UnknownCommandError as exc:
                console.print(f"[festo.err]✗[/] {exc}  (type [festo.ok]help[/])")
            except NotImplementedError as exc:
                console.print(f"[festo.warn]![/] Not supported: {exc}")
            except AttributeError as exc:
                console.print(f"[festo.warn]![/] Not available: {exc}")
            except (KeyError, ValueError, IndexError, RuntimeError, CommandError) as exc:
                console.print(f"[festo.err]✗[/] {exc}")
            except Exception as exc:  # noqa: BLE001
                logger.exception("Unexpected error processing command %r", raw)
                console.print(f"[festo.err]✗[/] Unexpected error: {exc}")