failprint ¤
failprint package.
Run a command, print its output only if it fails.
Modules:
-
capture
–Deprecated. Import from
failprint
directly. -
cli
–Deprecated. Import from
failprint
directly. -
debug
–Deprecated. Import from
failprint
directly. -
formats
–Deprecated. Import from
failprint
directly. -
lazy
–Deprecated. Import from
failprint
directly. -
process
–Deprecated. Import from
failprint
directly. -
runners
–Deprecated. Import from
failprint
directly.
Classes:
-
ArgParser
–A custom argument parser with a helper method to add boolean flags.
-
Capture
–An enum to store the different possible output types.
-
CaptureManager
–Context manager to capture standard output and error at the file descriptor level.
-
Format
–Class to define a display format.
-
LazyCallable
–This class allows users to create and pass lazy callables to the runner.
-
RunResult
–Placeholder for a run result.
Functions:
-
accept_custom_format
–Store the value in
formats
if it starts with custom. -
add_flags
–Add some boolean flags to the parser.
-
as_python_statement
–Transform a callable and its arguments into a Python statement string.
-
as_shell_command
–Rebuild a command line from system arguments.
-
get_parser
–Return the CLI argument parser.
-
main
–Run the main program.
-
printable_command
–Transform a command or function into a string.
-
run
–Run a command in a subprocess or a Python function, and print its output if it fails.
-
run_command
–Run a command.
-
run_function
–Run a function.
-
run_function_get_code
–Run a function and return a exit code.
-
run_pty_subprocess
–Run a command in a PTY subprocess.
-
run_subprocess
–Run a command in a subprocess.
Attributes:
-
CmdFuncType
–Type for a command or function.
-
CmdType
–Type for a command.
-
WINDOWS
–A boolean variable indicating whether the current system is Windows.
CmdFuncType module-attribute
¤
CmdFuncType = Union[CmdType, Callable, LazyCallable]
Type for a command or function.
CmdType module-attribute
¤
Type for a command.
WINDOWS module-attribute
¤
WINDOWS = startswith('win') or name == 'nt'
A boolean variable indicating whether the current system is Windows.
ArgParser ¤
Bases: ArgumentParser
A custom argument parser with a helper method to add boolean flags.
Methods:
-
add_bool_argument
–Add a boolean flag/argument to the parser.
add_bool_argument ¤
add_bool_argument(
truthy: Sequence[str],
falsy: Sequence[str],
truthy_help: str = "",
falsy_help: str = "",
**kwargs: Any,
) -> None
Add a boolean flag/argument to the parser.
Parameters:
-
truthy
(Sequence[str]
) –Values that will store true in the destination.
-
falsy
(Sequence[str]
) –Values that will store false in the destination.
-
truthy_help
(str
, default:''
) –Help for the truthy arguments.
-
falsy_help
(str
, default:''
) –Help for the falsy arguments.
-
**kwargs
(Any
, default:{}
) –Remaining keyword arguments passed to
argparse.ArgumentParser.add_argument
.
Source code in src/failprint/_internal/cli.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
Capture ¤
Bases: Enum
An enum to store the different possible output types.
Methods:
-
cast
–Cast a value to an actual Capture enumeration value.
-
here
–Context manager to capture standard output/error.
Attributes:
-
BOTH
–Capture both standard output and error.
-
NONE
–Do not capture anything.
-
STDERR
–Capture standard error.
-
STDOUT
–Capture standard output.
cast classmethod
¤
Cast a value to an actual Capture enumeration value.
Parameters:
Returns:
-
Capture
–A Capture enumeration value.
Source code in src/failprint/_internal/capture.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
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/_internal/capture.py
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 |
|
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.
Methods:
-
__enter__
–Set up the necessary file descriptors and temporary files to capture output.
-
__exit__
–Restore the original file descriptors and reads the captured output.
Attributes:
Source code in src/failprint/_internal/capture.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
output property
¤
output: str
Captured output.
Raises:
-
RuntimeError
–When accessing captured output before exiting the context manager.
__enter__ ¤
__enter__() -> CaptureManager
Set up the necessary file descriptors and temporary files to capture output.
Source code in src/failprint/_internal/capture.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
__exit__ ¤
__exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
exc_traceback: TracebackType | None,
) -> None
Restore the original file descriptors and reads the captured output.
Source code in src/failprint/_internal/capture.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
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.
Attributes:
-
accept_ansi
–Whether to accept ANSI sequences.
-
progress_template
–The template to show progress.
-
template
–The main template.
Source code in src/failprint/_internal/formats.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
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.
Methods:
-
__call__
–Call the lazy callable.
Attributes:
-
args
–The
*args
to pass when calling. -
call
–The original callable.
-
kwargs
–The
**kwargs
to pass when calling. -
name
–The name of the callable, if any.
Source code in src/failprint/_internal/lazy.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
__call__ ¤
__call__() -> _R
Call the lazy callable.
Source code in src/failprint/_internal/lazy.py
40 41 42 |
|
RunResult ¤
Placeholder for a run result.
Parameters:
Attributes:
Source code in src/failprint/_internal/runners.py
40 41 42 43 44 45 46 47 48 49 50 |
|
accept_custom_format ¤
Store the value in formats
if it starts with custom.
Parameters:
-
string
(str
) –A format name.
Returns:
-
str
–The format name, or
custom
if it started withcustom=
.
Source code in src/failprint/_internal/formats.py
88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
add_flags ¤
Add some boolean flags to the parser.
We made this method separate and public for its use in duty.
Parameters:
-
parser
(ArgParser
) –The parser to add flags to.
-
set_defaults
(bool
, default:True
) –Whether to set default values on arguments.
Returns:
-
ArgParser
–The augmented parser.
Source code in src/failprint/_internal/cli.py
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
as_python_statement ¤
as_python_statement(
func: Callable | LazyCallable,
args: Sequence | None = None,
kwargs: dict | None = None,
) -> str
Transform a callable and its arguments into a Python statement string.
Parameters:
-
func
(Callable | LazyCallable
) –The callable to transform.
-
args
(Sequence | None
, default:None
) –Positional arguments passed to the function.
-
kwargs
(dict | None
, default:None
) –Keyword arguments passed to the function.
Returns:
-
str
–A Python statement.
Source code in src/failprint/_internal/formats.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
as_shell_command ¤
Rebuild a command line from system arguments.
Parameters:
Returns:
-
str
–A printable and shell-runnable command.
Source code in src/failprint/_internal/formats.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
get_parser ¤
get_parser() -> ArgParser
Return the CLI argument parser.
Returns:
-
ArgParser
–An argparse parser.
Source code in src/failprint/_internal/cli.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
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/_internal/cli.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
printable_command ¤
printable_command(
cmd: CmdFuncType,
args: Sequence | None = None,
kwargs: dict | None = None,
) -> str
Transform a command or function into a string.
Parameters:
-
cmd
(CmdFuncType
) –The command or function to transform.
-
args
(Sequence | None
, default:None
) –Positional arguments passed to the function.
-
kwargs
(dict | None
, default:None
) –Keyword arguments passed to the function.
Returns:
-
str
–A shell command or python statement string.
Source code in src/failprint/_internal/formats.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
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/_internal/runners.py
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 121 122 123 124 125 126 127 128 129 130 131 |
|
run_command ¤
run_command(
cmd: CmdType,
*,
capture: Capture = BOTH,
ansi: bool = False,
pty: bool = False,
stdin: str | None = None,
) -> tuple[int, str]
Run a command.
Parameters:
-
cmd
(CmdType
) –The command to run.
-
capture
(Capture
, default:BOTH
) –The output to capture.
-
ansi
(bool
, default:False
) –Whether to accept ANSI sequences.
-
pty
(bool
, default:False
) –Whether to run in a PTY.
-
stdin
(str | None
, default:None
) –String to use as standard input.
Returns:
Source code in src/failprint/_internal/runners.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
run_function ¤
run_function(
func: Callable,
*,
args: Sequence | None = None,
kwargs: dict | None = None,
capture: Capture = BOTH,
stdin: str | None = None,
) -> tuple[int, str]
Run a function.
Parameters:
-
func
(Callable
) –The function to run.
-
args
(Sequence | None
, default:None
) –Positional arguments passed to the function.
-
kwargs
(dict | None
, default:None
) –Keyword arguments passed to the function.
-
capture
(Capture
, default:BOTH
) –The output to capture.
-
stdin
(str | None
, default:None
) –String to use as standard input.
Returns:
Source code in src/failprint/_internal/runners.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
run_function_get_code ¤
Run a function and return a exit code.
Parameters:
-
func
(Callable
) –The function to run.
-
args
(Sequence
) –Positional arguments passed to the function.
-
kwargs
(dict
) –Keyword arguments passed to the function.
Returns:
-
int
–An exit code.
Source code in src/failprint/_internal/runners.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
run_pty_subprocess ¤
run_pty_subprocess(
cmd: list[str],
*,
capture: Capture = BOTH,
stdin: str | None = None,
) -> tuple[int, str]
Run a command in a PTY subprocess.
Parameters:
-
cmd
(list[str]
) –The command to run.
-
capture
(Capture
, default:BOTH
) –The output to capture.
-
stdin
(str | None
, default:None
) –String to use as standard input.
Returns:
Source code in src/failprint/_internal/process.py
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 |
|
run_subprocess ¤
run_subprocess(
cmd: CmdType,
*,
capture: Capture = BOTH,
shell: bool = False,
stdin: str | None = None,
) -> tuple[int, str]
Run a command in a subprocess.
Parameters:
-
cmd
(CmdType
) –The command to run.
-
capture
(Capture
, default:BOTH
) –The output to capture.
-
shell
(bool
, default:False
) –Whether to run the command in a shell.
-
stdin
(str | None
, default:None
) –String to use as standard input.
Returns:
Source code in src/failprint/_internal/process.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 |
|