COT.ui_shared module

Abstract user interface superclass.

class UI(force=False)[source]

Bases: object

Abstract user interface functionality.

Can also be used in test code as a stub that autoconfirms everything.

__init__(force=False)[source]

Constructor.

Parameters:force (bool) – See force.
choose_from_list(footer, option_list, default_value, header='', info_list=None)[source]

Prompt the user to choose from a list.

Parameters:
  • footer (str) – Prompt string to display following the list
  • option_list (list) – List of strings to choose amongst
  • default_value (str) – Default value to select if user declines
  • header (str) – String to display prior to the list
  • info_list (list) – Verbose strings to display in place of option_list
Returns:

strdefault_value or an item from option_list.

confirm(prompt)[source]

Prompt user to confirm the requested operation.

Auto-accepts if force is set to True.

Warning

This stub implementation does not actually interact with the user, but instead returns default_confirm_response. Subclasses should override this method.

Parameters:prompt (str) – Message to prompt the user with
Returns:boolTrue (user confirms acceptance) or False (user declines)
confirm_or_die(prompt)[source]

If the user doesn’t agree, abort the program.

A simple wrapper for confirm() that calls sys.exit() if confirm() returns False.

Parameters:prompt (str) – Message to prompt the user with
Raises:SystemExit – if user declines
fill_examples(example_list)[source]

Pretty-print a set of usage examples.

Parameters:example_list (list) – List of (example, description) tuples.
Raises:NotImplementedError – Must be implemented by a subclass.
fill_usage(subcommand, usage_list)[source]

Pretty-print a list of usage strings.

Parameters:
  • subcommand (str) – Subcommand name/keyword
  • usage_list (list) – List of usage strings for this subcommand.
Returns:

str – Concatenation of all usage strings, each appropriately wrapped to the terminal_width value.

get_input(prompt, default_value)[source]

Prompt the user to enter a string.

Auto-inputs the default_value if force is set to True.

Warning

This stub implementation does not actually interact with the user, but instead always returns default_value. Subclasses should override this method.

Parameters:
  • prompt (str) – Message to prompt the user with
  • default_value (str) – Default value to input if the user simply hits Enter without entering a value, or if force.
Returns:

str – Input value

get_password(username, host)[source]

Get password string from the user.

Parameters:
  • username (str) – Username the password is associated with
  • host (str) – Host the password is associated with
Raises:

NotImplementedError – Must be implemented by a subclass.

default_confirm_response = None

Knob for API testing, sets the default response to confirm().

force = None

Whether to automatically select the default value in all cases.

(As opposed to interactively prompting the user.)

terminal_width

Get the width of the terminal in columns.