onyo.lib.ui module

class onyo.lib.ui.UI(debug=False, quiet=False, yes=False)[source]

Bases: object

An object handling user interaction, including printing, errors, requests, and others.

logger

The logger to display information with.

Type:

Logger

quiet

Activate the quiet mode (requires that yes=True). This will suppresses all output generation.

Type:

bool

yes

Activate the yes mode, which suppresses all interactive requests to the user, and instead answers them with yes.

Type:

bool

__init__(debug=False, quiet=False, yes=False)[source]

Initialize the User Interface object for user communication of Onyo.

Parameters:
  • debug (bool (default: False)) – Activate the debug mode to display additional information via Onyo, and to print the full traceback stack if errors occur.

  • quiet (bool (default: False)) – Activate the quiet mode (requires that yes=True) to suppress all output generation.

  • yes (bool (default: False)) – Activate the yes mode to suppress all interactive requests to the user, and instead answers them with yes.

error(error, end='\\n')[source]

Print an error message, if the UI is not set to quiet mode.

Parameters:
  • error (str | Exception) – Prints the string, or the message of an error. If debug mode is activated, displays the full traceback of an exception.

  • end (str (default: '\\n')) – Specify the string at the end of prints. Per default, prints end with a line break.

Return type:

None

log(message)[source]

Log a message at logging.INFO level.

Parameters:

message (str) – The message to log.

Return type:

None

log_debug(*args, **kwargs)[source]

Log at logging.DEBUG level.

Parameters:
  • args – passed to Logger.debug

  • kwargs – passed to Logger.debug

Return type:

None

print(*args, **kwargs)[source]

Print a message, if the UI is not set to quiet mode.

Parameters:
  • args – passed on to builtin print.

  • kwargs – passed on to builtin print.

Return type:

None

request_user_response(question, default='yes', answers=None)[source]

Print question and read a response from stdin.

Returns True when user answers yes, False when no, and asks again if the input is neither.

If the UI is set to yes=True the default answer is assumed without asking the user.

Parameters:
  • question (str) – The question to which the user should respond. This is appended by an indication of what is the default response (just hit enter).

  • default (str) – Define a default answer. This is answer is assumed when self.yes is set (non-interactive mode) or when the response was empty, i.e. user just hit enter.

  • answers (list of tuple) – Defined ways to answer the question and what to return accordingly. First element of a tuple is the return value, second element a list of strings. If the user’s response matches any of these strings, the return value is returned. If the user’s response doesn’t match any, the question is repeated. By default, this function poses a yes-no question, where ‘y,’Y’,’yes’ are returned as True, and ‘n’, ‘N’, ‘no’ as False.

Return type:

Any

rich_print(*args, **kwargs)[source]

Refactoring helper to print via the rich package.

Proxy for rich.Console.print. Takes stderr: bool option to use a stderr Console instead of a stdout Console.

Return type:

None

Notes

This is to be fused with the regular UI.print, UI.error, etc. so that UI decides whether and how to use rich. The stderr option should consequently be replaced by print’s standard file option.

set_debug(debug=False)[source]

Toggle debug mode.

Parameters:

debug (bool (default: False)) – Activates debug mode, and configures the log level of the logger.

Return type:

None

set_quiet(quiet=False)[source]

Toggle quiet mode.

Parameters:

quiet (bool (default: False)) – True suppresses of all user output. Requires yes mode to be active.

Raises:

ValueError – If tried to activate quiet mode without yes=True.

Return type:

None

set_yes(yes=False)[source]

Toggle auto-response ‘yes’ to all questions.

Parameters:

yes (bool (default: False)) – Activate yes mode, which suppresses all user requests and answers them positively. Allows the activation of the quiet mode.

Return type:

None