click_extra.sphinx.click_runner module¶

Runner executing the bodies of the click:* directives.

ClickRunner runs every block of a document in one shared namespace, and returns what a terminal would show for it: the invocations, the input, the output, the prompts and the tracebacks. The exec plumbing around it lives here too: the redirected subprocess.call, the program name read off a command line, and the ^D that TerminatedEchoingStdin echoes.

click_extra.sphinx.click_runner.PROMPT_SIGIL = '$'¶

Sigil a block draws before the command it ran.

Fixed rather than taken from PROMPT, which answers to the platform the build runs on: a page would otherwise prompt with a > for every reader whose documentation was built on Windows. A capture drawn as another terminal swaps it for that terminal’s own, see TerminalPreset.

Passed as the prompt argument of format_cli_prompt(), which is what builds the line for both the results code block and the SVG capture drawn from it.

class click_extra.sphinx.click_runner.TerminatedEchoingStdin(input, output)[source]¶

Bases: EchoingStdin

Like click.testing.EchoingStdin but adds a visible ^D in place of the EOT character (\x04).

ClickRunner.invoke() adds \x04 when terminate_input=True.

click_extra.sphinx.click_runner.patch_subprocess()[source]¶

Patch subprocess to work better with ClickRunner.invoke().

subprocess.call output is redirected to click.echo so it shows up in the example output.

Caution

The replacement is installed on the subprocess module itself (not thread-local), so for the duration of the with block any other code in the same process that calls subprocess.call sees the patched version. With parallel_read_safe = True declared on ClickDomain, a parallel reader running concurrently on a different document gets the patched subprocess.call too. The redirection is benign (output goes to click.echo) but the race is real, and the parallel-safe claim is weaker than it looks for documents that themselves shell out via subprocess.call.

click_extra.sphinx.click_runner.program_from_command_line(command_line)[source]¶

The program name Click is given for a command line a block displays.

A block documenting a multi-word program has to hand Click all of it: the command reads its own name back out of the context, for its usage line and for any output quoting the invocation that produced it, like the provenance header of a Carapace spec. Handing over the last word alone makes an image contradict the prompt drawn right above it.

Only an interpreter prefix is dropped, since it names no program: see _INTERPRETER_RE.

Return type:

str

class click_extra.sphinx.click_runner.ClickRunner(capture=None)[source]¶

Bases: CliRunner

A sub-class of click.testing.CliRunner for Sphinx directive execution.

Produces unfiltered ANSI codes so that the directives of click_extra.sphinx.click can render colors in the HTML output. Because Click Extra executes the documented command here, invoke() forces color across both color systems a CLI might use: color=True covers Click’s (should_strip_ansi), and forced_color() sets FORCE_COLOR for Rich’s (which rich-click uses and color=True never reaches). The MkDocs plugin shares the latter lever but cannot pass color=True, since it patches a renderer it never executes.

The runner defaults to capture="fd", overridable through the click_extra_run_capture conf.py value. Click then sends file descriptors 1 and 2 to the capture, so a block also renders what a subprocess, a C extension or a direct write to descriptor 1 prints, placed after what the command wrote to sys.stdout. sys.stdout.fileno() returns a copy of the original stdout descriptor instead of raising io.UnsupportedOperation, so a command calling it does not abort the build. But what the command writes through that copy goes to the stdout of the build, not to the page. On Windows, where fd-backed streams are not supported, the default falls back to capture="sys".

without_build_logging()[source]¶

Take the logging setup of the build off the root logger for the block.

A build is one process, shared with Sphinx, its extensions and its theme, and any of them may set the root logger up: the Furo theme depends on accessible-pygments, which calls logging.basicConfig() when imported, and so gives the root logger a handler and the INFO level.

A documented command adds the handler of Click Extra beside the ones it finds, so each of its records would print a second time, in the format of the build. It also leaves the root logger the level it found, so the one of the build would let through the records a command logs before its verbosity options apply. The shell of a reader has none of this: the root logger holds no handler there, at DEFAULT_LEVEL.

A handler the code of the document attached itself, or a level it set, is not of the build, and stays in place.

Return type:

Generator[None, None, None]

isolation(*args, **kwargs)[source]¶

Echo a ^D marker at the end of the isolated stdin.

Todo

Delete TerminatedEchoingStdin and this override once the Click floor reaches the release that ships pallets/click#3878, targeted at 8.6.0: CliRunner(echo_stdin=True) then echoes the EOT byte as ^D itself.

invoke(cli, args=None, prog_name=None, input=None, terminate_input=False, env=None, _output_lines=None, _show_prompt=True, **extra)[source]¶

Like CliRunner.invoke but displays what the user would enter in the terminal for env vars, command arguments, and prompts.

Parameters:
  • terminate_input – Whether to display ^D after a list of input.

  • _output_lines – A list used internally to collect lines to be displayed.

  • _show_prompt – Whether to draw the invocation above the output. Set from the directive’s :show-prompt: / :hide-prompt: options, see show_prompt.

Return type:

Result

execute_source(directive)[source]¶

Execute the given code, adding it to the runner’s namespace.

Return type:

None

run_cli(directive)[source]¶

Execute the body of directive and return what a terminal would show.

The lines mix the invocations, the input, the output, the prompts and the tracebacks of every command the body ran.

The body can call these functions without importing them:

  • invoke(): which is the same as ClickRunner.invoke()

  • isolated_filesystem(): A context manager that changes to a temporary directory while executing the block.

Raises:

RuntimeError – when the body assigns a variable named like one of these functions, naming the document line that does.

Return type:

list[str]