failprint ¤
failprint package.
Run a command, print its output only if it fails.
Capture ¤
Bases: Enum
An enum to store the different possible output types.
cast classmethod
¤
Cast a value to an actual Capture enumeration value.
Parameters:
Returns:
-
Capture
–A Capture enumeration value.
Source code in src/failprint/capture.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
here ¤
here(stdin: str | None = None) -> Iterator[CaptureManager]
Context manager to capture standard output/error.
Parameters:
-
stdin
(str | None
, default:None
) –Optional input.
Yields:
-
CaptureManager
–A lazy string with the captured contents.
Examples:
>>> def print_things() -> None:
... print("1")
... sys.stderr.write("2\n")
... os.system("echo 3")
... subprocess.run(["sh", "-c", "echo 4 >&2"])
>>> with Capture.BOTH.here() as captured:
... print_things()
... print(captured)
1
2
3
4
Source code in src/failprint/capture.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
CaptureManager ¤
Context manager to capture standard output and error at the file descriptor level.
Usable directly through Capture.here
.
Examples:
>>> def print_things() -> None:
... print("1")
... sys.stderr.write("2\n")
... os.system("echo 3")
... subprocess.run(["sh", "-c", "echo 4 >&2"])
>>> with CaptureManager(Capture.BOTH) as captured:
... print_things()
... print(captured)
1
2
3
4
Parameters:
-
capture
(Capture
, default:BOTH
) –What to capture.
-
stdin
(str | None
, default:None
) –Optional input.
Source code in src/failprint/capture.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
output property
¤
output: str
Captured output.
Raises:
-
RuntimeError
–When accessing captured output before exiting the context manager.
Format ¤
Class to define a display format.
Parameters:
-
template
(str
) –The main template.
-
progress_template
(str | None
, default:None
) –The template to show progress.
-
accept_ansi
(bool
, default:True
) –Whether to accept ANSI sequences.
Source code in src/failprint/formats.py
49 50 51 52 53 54 55 56 57 58 59 |
|
LazyCallable ¤
Bases: Generic[_R]
This class allows users to create and pass lazy callables to the runner.
Parameters:
-
call
(Callable[_P, _R]
) –The origin callable.
-
args
(tuple
) –The
*args
to pass when calling. -
kwargs
(dict
) –The
**kwargs
to pass when calling. -
name
(str | None
, default:None
) –The name of the callable.
Source code in src/failprint/lazy.py
22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
main ¤
Run the main program.
This function is executed when you type failprint
or python -m failprint
.
Parameters:
Returns:
-
int
–An exit code.
Source code in src/failprint/cli.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
run ¤
run(
cmd: CmdFuncType,
*,
args: Sequence | None = None,
kwargs: dict | None = None,
number: int = 1,
capture: str | bool | Capture | None = None,
title: str | None = None,
fmt: str | None = None,
pty: bool = False,
progress: bool = True,
nofail: bool = False,
quiet: bool = False,
silent: bool = False,
stdin: str | None = None,
command: str | None = None
) -> RunResult
Run a command in a subprocess or a Python function, and print its output if it fails.
Parameters:
-
cmd
(CmdFuncType
) –The command to run.
-
args
(Sequence | None
, default:None
) –Arguments to pass to the callable.
-
kwargs
(dict | None
, default:None
) –Keyword arguments to pass to the callable.
-
number
(int
, default:1
) –The command number.
-
capture
(str | bool | Capture | None
, default:None
) –The output to capture.
-
title
(str | None
, default:None
) –The command title.
-
fmt
(str | None
, default:None
) –The output format.
-
pty
(bool
, default:False
) –Whether to run in a PTY.
-
progress
(bool
, default:True
) –Whether to show progress.
-
nofail
(bool
, default:False
) –Whether to always succeed.
-
quiet
(bool
, default:False
) –Whether to not print the command output.
-
silent
(bool
, default:False
) –Don't print anything.
-
stdin
(str | None
, default:None
) –String to use as standard input.
-
command
(str | None
, default:None
) –The command to display.
Returns:
-
RunResult
–The command exit code, or 0 if
nofail
is True.
Source code in src/failprint/runners.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|