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.
See also
Adapted, like the directives of click_extra.sphinx.click, from
Pallets’ Sphinx Themes,
released under a BSD-3-Clause license.
- 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, seeTerminalPreset.Passed as the
promptargument offormat_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:
EchoingStdinLike
click.testing.EchoingStdinbut adds a visible^Din place of the EOT character (\x04).ClickRunner.invoke()adds\x04whenterminate_input=True.
- click_extra.sphinx.click_runner.patch_subprocess()[source]¶
Patch subprocess to work better with
ClickRunner.invoke().subprocess.calloutput is redirected toclick.echoso it shows up in the example output.Caution
The replacement is installed on the
subprocessmodule itself (not thread-local), so for the duration of thewithblock any other code in the same process that callssubprocess.callsees the patched version. Withparallel_read_safe = Truedeclared onClickDomain, a parallel reader running concurrently on a different document gets the patchedsubprocess.calltoo. The redirection is benign (output goes toclick.echo) but the race is real, and the parallel-safe claim is weaker than it looks for documents that themselves shell out viasubprocess.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:
- class click_extra.sphinx.click_runner.ClickRunner(capture=None)[source]¶
Bases:
CliRunnerA sub-class of
click.testing.CliRunnerfor Sphinx directive execution.Produces unfiltered ANSI codes so that the directives of
click_extra.sphinx.clickcan 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=Truecovers Click’s (should_strip_ansi), andforced_color()setsFORCE_COLORfor Rich’s (whichrich-clickuses andcolor=Truenever reaches). The MkDocs plugin shares the latter lever but cannot passcolor=True, since it patches a renderer it never executes.The runner defaults to
capture="fd", overridable through theclick_extra_run_captureconf.pyvalue. Click then sends file descriptors1and2to the capture, so a block also renders what a subprocess, a C extension or a direct write to descriptor1prints, placed after what the command wrote tosys.stdout.sys.stdout.fileno()returns a copy of the original stdout descriptor instead of raisingio.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 tocapture="sys".- without_build_logging()[source]¶
Take the logging setup of the build off the
rootlogger for the block.A build is one process, shared with Sphinx, its extensions and its theme, and any of them may set the
rootlogger up: the Furo theme depends onaccessible-pygments, which callslogging.basicConfig()when imported, and so gives therootlogger a handler and theINFOlevel.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
rootlogger 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: therootlogger holds no handler there, atDEFAULT_LEVEL.A handler the code of the document attached itself, or a level it set, is not of the build, and stays in place.
- isolation(*args, **kwargs)[source]¶
Echo a
^Dmarker at the end of the isolated stdin.Todo
Delete
TerminatedEchoingStdinand this override once the Click floor reaches the release that ships pallets/click#3878, targeted at8.6.0:CliRunner(echo_stdin=True)then echoes the EOT byte as^Ditself.
- invoke(cli, args=None, prog_name=None, input=None, terminate_input=False, env=None, _output_lines=None, _show_prompt=True, **extra)[source]¶
Like
CliRunner.invokebut displays what the user would enter in the terminal for env vars, command arguments, and prompts.- Parameters:
terminate_input – Whether to display
^Dafter 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, seeshow_prompt.
- Return type:
- execute_source(directive)[source]¶
Execute the given code, adding it to the runner’s namespace.
- Return type:
- run_cli(directive)[source]¶
Execute the body of
directiveand 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 asClickRunner.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: