asv_runner.console#

A set of utilities for writing output to the console.

Module Contents#

Classes#

Log

Functions#

isatty

Determines if a file is a tty.

_color_text

Returns a string wrapped in ANSI color codes for coloring the text in a terminal.

_write_with_fallback

Writes the supplied string to the given file-like object, handling potential UnicodeEncodeErrors by falling back to the locale’s preferred encoding.

color_print

Prints colored and styled text to the terminal using ANSI escape sequences.

#### Parameters
*args (`tuple` of `str`):
The positional arguments should come in pairs (`msg`, `color`), where `msg`
is the string to display and `color` is the color to display it in. `color`
is an ANSI terminal color name. Must be one of: black, red, green, brown,
blue, magenta, cyan, lightgrey, default, darkgrey, lightred, lightgreen,
yellow, lightblue, lightmagenta, lightcyan, white, or '' (the empty string).

`file` (writable file-like object, optional):
Where to write to. Defaults to `sys.stdout`. If `file` is not a tty (as determined
by calling its `isatty` member, if one exists), no coloring will be included. It's
passed as a keyword argument.

`end` (`str`, optional):
The ending of the message. Defaults to "

get_answer_default

Prompts the user for input and returns the entered value or a default.

truncate_left

Data#

API#

asv_runner.console.WIN#

None

asv_runner.console.isatty(file)#

Determines if a file is a tty.

Parameters

file (file-like object)

The file-like object to check.

Returns

isatty (bool)

Returns True if the file is a tty, False otherwise.

Notes

Most built-in Python file-like objects have an isatty member, but some user-defined types may not. In such cases, this function assumes those are not ttys.

asv_runner.console._color_text(text, color)#

Returns a string wrapped in ANSI color codes for coloring the text in a terminal.

Parameters

text (str)

The string to colorize.

color (str)

An ANSI terminal color name. Must be one of the following: ‘black’, ‘red’, ‘green’, ‘brown’, ‘blue’, ‘magenta’, ‘cyan’, ‘lightgrey’, ‘default’, ‘darkgrey’, ‘lightred’, ‘lightgreen’, ‘yellow’, ‘lightblue’, ‘lightmagenta’, ‘lightcyan’, ‘white’, or ‘’ (the empty string).

Returns

colored_text (str)

The input string, bounded by the appropriate ANSI color codes.

Notes

This function wraps the input text with ANSI color codes based on the given color. It won’t actually affect the text until it is printed to the terminal.

asv_runner.console._unicode_translations#

None

asv_runner.console._write_with_fallback(s, fileobj)#

Writes the supplied string to the given file-like object, handling potential UnicodeEncodeErrors by falling back to the locale’s preferred encoding.

Parameters

s (str): The Unicode string to be written to the file-like object. Raises a ValueError if s is not a Unicode string.

fileobj (file-like object): The file-like object to which the string s is to be written. On Python 3, this must be a text stream. On Python 2, this must be a file byte stream.

Notes

This function first tries to write the input string s to the file object fileobj. If a UnicodeError occurs during this process (indicating that the string contains characters not representable in the file’s encoding), the function falls back to encoding the string in the locale’s preferred encoding before writing.

If the string s still cannot be encoded in the locale’s preferred encoding, the function translates the string to replace problematic Unicode characters with ASCII ones using the _unicode_translations dictionary, and then encodes and writes the resulting string to fileobj using the “replace” error handling scheme (which replaces any non-encodable characters with a suitable replacement marker).

After the write operation, the function flushes the file object’s output buffer to ensure that the written data is actually saved to the file.

asv_runner.console.color_print(*args, **kwargs)#
Prints colored and styled text to the terminal using ANSI escape sequences.

#### Parameters
*args (`tuple` of `str`):
The positional arguments should come in pairs (`msg`, `color`), where `msg`
is the string to display and `color` is the color to display it in. `color`
is an ANSI terminal color name. Must be one of: black, red, green, brown,
blue, magenta, cyan, lightgrey, default, darkgrey, lightred, lightgreen,
yellow, lightblue, lightmagenta, lightcyan, white, or '' (the empty string).

`file` (writable file-like object, optional):
Where to write to. Defaults to `sys.stdout`. If `file` is not a tty (as determined
by calling its `isatty` member, if one exists), no coloring will be included. It's
passed as a keyword argument.

`end` (`str`, optional):
The ending of the message. Defaults to "

“. The end will be printed after resetting any color or font state. It’s passed as a keyword argument.

#### Notes
This function allows you to print text in various colors to the console, which can
be helpful for distinguishing different kinds of output or for drawing attention to
particular messages.

It works by applying ANSI escape sequences to the input strings according to the
specified colors. These escape sequences are interpreted by the terminal emulator
to apply the specified colors and styles.

#### Example
```{code-block} python
color_print('This is the color ', 'default', 'GREEN', 'green')
```
asv_runner.console.get_answer_default(prompt, default, use_defaults=False)#

Prompts the user for input and returns the entered value or a default.

Parameters

prompt (str): The string that is presented to the user.

default (any): The value returned if the user doesn’t enter anything and just hits Enter. This value is also shown in the prompt to indicate to the user what the default is.

use_defaults (bool, optional): If True, the function will immediately return the default value without prompting the user for input. Defaults to False.

Returns

The user’s input, or the provided default value if the user didn’t enter anything.

Notes

This function enhances the built-in input function by allowing a default value to be specified, which is returned if the user doesn’t enter anything.

asv_runner.console.truncate_left(s, l)#
class asv_runner.console.Log#

Initialization

_stream_formatter(record)#

The formatter for standard output

indent()#

A context manager to increase the indentation level.

dot()#
set_nitems(n)#

Set the number of remaining items to process. Each of these steps should be incremented through using step.

Can be called multiple times. The progress percentage is ensured to be non-decreasing, except if 100% was already reached in which case it is restarted from 0%.

step()#

Write that a step has been completed. A percentage is displayed along with it.

If we are stepping beyond the number of items, stop counting.

enable(verbose=False)#
set_level(level)#
is_debug_enabled()#
_message(routine, message, reserve_space=False, color=None, continued=False)#
info(*args, **kwargs)#
warning(*args, **kwargs)#
debug(*args, **kwargs)#
error(*args, **kwargs)#
add(msg)#
add_padded(msg)#

Final part of two-part info message. Should be preceded by a call to info/warn/…(msg, reserve_space=True)

flush()#

Flush any trailing newlines. Needs to be called before printing to stdout via other means, after using Log.