Skip to content

formats ¤

Output-printing formats.

Format ¤

Format(
    template: str,
    *,
    progress_template: str | None = None,
    accept_ansi: bool = True
)

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
def __init__(self, template: str, *, progress_template: str | None = None, accept_ansi: bool = True) -> None:
    """Initialize the object.

    Arguments:
        template: The main template.
        progress_template: The template to show progress.
        accept_ansi: Whether to accept ANSI sequences.
    """
    self.template = template
    self.progress_template = progress_template
    self.accept_ansi = accept_ansi

accept_custom_format ¤

accept_custom_format(string: str) -> str

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 with custom=.

Source code in src/failprint/formats.py
84
85
86
87
88
89
90
91
92
93
94
95
96
def accept_custom_format(string: str) -> str:
    """Store the value in `formats` if it starts with custom.

    Arguments:
        string: A format name.

    Returns:
        The format name, or `custom` if it started with `custom=`.
    """
    if string.startswith("custom="):
        formats["custom"] = Format(string[7:])
        return "custom"
    return string

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/formats.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def 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.

    Arguments:
        func: The callable to transform.
        args: Positional arguments passed to the function.
        kwargs: Keyword arguments passed to the function.

    Returns:
        A Python statement.
    """
    if isinstance(func, LazyCallable):
        callable_name = func.name or _get_callable_name(func.call)
        args = args or func.args
        kwargs = kwargs or func.kwargs
    else:
        callable_name = _get_callable_name(func)
    args_str = [repr(arg) for arg in args] if args else []
    kwargs_str = [f"{k}={v!r}" for k, v in kwargs.items()] if kwargs else []
    arguments = ", ".join(args_str + kwargs_str)
    return f"{callable_name}({arguments})"

as_shell_command ¤

as_shell_command(cmd: list[str]) -> str

Rebuild a command line from system arguments.

Parameters:

  • cmd (list[str]) –

    The command as a list of strings.

Returns:

  • str

    A printable and shell-runnable command.

Source code in src/failprint/formats.py
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
def as_shell_command(cmd: list[str]) -> str:
    """Rebuild a command line from system arguments.

    Arguments:
        cmd: The command as a list of strings.

    Returns:
        A printable and shell-runnable command.
    """
    parts = []
    for part in cmd:
        if not part:
            parts.append('""')
            continue
        has_spaces = " " in part
        has_double_quotes = '"' in part
        has_single_quotes = "'" in part
        if has_double_quotes and not has_single_quotes:
            # double quotes, no single quotes
            # -> wrap in single quotes
            part = f"'{part}'"  # noqa: PLW2901
        elif has_single_quotes and has_double_quotes:
            # double and single quotes
            # -> escape double quotes, wrap in double quotes
            part = part.replace('"', r"\"")  # noqa: PLW2901
            part = f'"{part}"'  # noqa: PLW2901
        elif has_single_quotes or has_spaces:
            # spaces or single quotes
            # -> wrap in double quotes
            part = f'"{part}"'  # noqa: PLW2901
        parts.append(part)
    return " ".join(parts)

escape ¤

escape(text: str) -> str

Escape text for ansiprint by replacing < and > with special strings.

Parameters:

  • text (str) –

    The text to escape.

Returns:

  • str

    The escaped text.

Source code in src/failprint/formats.py
22
23
24
25
26
27
28
29
30
31
def escape(text: str) -> str:
    """Escape text for ansiprint by replacing `<` and `>` with special strings.

    Parameters:
        text: The text to escape.

    Returns:
        The escaped text.
    """
    return text.replace("<", LT).replace(">", GT)

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/formats.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def printable_command(cmd: CmdFuncType, args: Sequence | None = None, kwargs: dict | None = None) -> str:
    """Transform a command or function into a string.

    Arguments:
        cmd: The command or function to transform.
        args: Positional arguments passed to the function.
        kwargs: Keyword arguments passed to the function.

    Returns:
        A shell command or python statement string.
    """
    if isinstance(cmd, str):
        return cmd
    if callable(cmd):
        return as_python_statement(cmd, args, kwargs)
    return as_shell_command(cmd)

unescape ¤

unescape(text: str) -> str

Unescape text by replacing special strings with < and >.

Parameters:

  • text (str) –

    The text to unescape.

Returns:

  • str

    The unescaped text.

Source code in src/failprint/formats.py
34
35
36
37
38
39
40
41
42
43
def unescape(text: str) -> str:
    """Unescape text by replacing special strings with `<` and `>`.

    Parameters:
        text: The text to unescape.

    Returns:
        The unescaped text.
    """
    return text.replace(LT, "<").replace(GT, ">")