onyo.lib.ui module

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

Bases: object

An object to handle user interaction.

Includes printing, errors, requests, etc.

__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)) – Suppress all output. Requires yes=True.

  • yes (bool (default: False)) – Answer “yes” to all user-interactive prompts.

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

Print an error message.

When provided, Exceptions will print tracebacks in debug mode.

Nothing is printed when quiet is True.

Parameters:
  • error (str | Exception) – Error message to print. Exceptions will have their message printed and traceback added to the debug log.

  • end (str (default: '\\n')) – String to end the message with. Default is "\n".

Return type:

None

format_traceback(e)[source]

Format an Exception’s traceback suitable for logging.

Parameters:

e (Exception) – Exception to extract the traceback from.

Return type:

str

log(message, level=20)[source]

Log a message.

Parameters:
  • message (str) – Message to log.

  • level (int (default: 20)) – Level to log at.

Return type:

None

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

Log at DEBUG level.

Parameters:
Return type:

None

logger: Logger

The logger to display information with.

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

Print a message.

Nothing is printed when quiet is True.

Parameters:
  • args – Passed to builtin print()

  • kwargs – Passed to builtin print()

Return type:

None

quiet: bool

Suppress all output. Requires yes=True.

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

Print a question and read a response from stdin.

When yes is True, the default answer is used without prompting the user.

When a user’s response matches any of the answers, the corresponding return value is returned. If the user’s response doesn’t match any answers, the question is repeated.

Parameters:
  • question (str) – Question that the user should respond to.

  • default (str (default: 'yes')) – Default answer used when the answer is empty (user hit only enter) or yes is True.

  • answers (list[tuple] | None (default: None)) –

    List of answers and corresponding value to return for those answers. The first element is the return value, and the second is a list of strings.

    Default is ‘y’, ‘Y’, or ‘yes’ return True and ‘n’, ‘N’, or ‘no’ return False.

Return type:

Any

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

Print via the rich module.

A proxy for rich.Console.print.

Parameters:
  • stderr – Bool to use a stderr Rich Console instead of a stdout Rich Console.

  • args – Passed to rich.Console.print()

  • kwargs – Passed to rich.Console.print()

Return type:

None

set_debug(debug=False)[source]

Toggle debug mode.

Parameters:

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

Return type:

None

set_quiet(quiet=False)[source]

Toggle quiet mode.

Parameters:

quiet (bool (default: False)) – Suppress all output. Requires yes=True.

Raises:

ValueError – Tried to activate quiet mode without yes=True.

Return type:

None

set_yes(yes=False)[source]

Toggle auto-response “yes” to all user-interactive prompts.

Parameters:

yes (bool (default: False)) – Answer “yes” to all user-interactive prompts.

Return type:

None

yes: bool

Answer “yes” to all user-interactive prompts.