Пример #1
0
 def __init__(self, message):
     """
     :param message: the message to show to the user
     :type message: str
     """
     super().__init__()
     self.title = N_("Error")
     self._message = message
Пример #2
0
 def __init__(self, help_path):
     """
     :param help_path: help file name
     :type help_path: str
     """
     super().__init__()
     self.title = N_("Help")
     self.help_path = help_path
Пример #3
0
 def __init__(self, message=None):
     """
     :param message: password prompt question
     :type message: string
     """
     super().__init__()
     self.title = N_("Password")
     self._message = message or _("Enter your passphrase")
     self._password = None
Пример #4
0
 def __init__(self, message):
     """
     :param message: the message to show to the user
     :type message: unicode
     """
     super().__init__()
     self.title = N_("Question")
     self._message = message
     self._response = None
Пример #5
0
class Prompt(object):
    """Class to create a prompt message with options."""
    # Default message of the prompt
    DEFAULT_MESSAGE = N_("Please make a selection from the above")

    # String to use in a prompt when we want users to press the key ENTER.
    ENTER = N_("ENTER")

    # TRANSLATORS: 'q' to quit
    QUIT = C_('TUI|Spoke Navigation', 'q')
    QUIT_DESCRIPTION = N_("to quit")

    # TRANSLATORS:'c' to continue
    CONTINUE = C_('TUI|Spoke Navigation', 'c')
    CONTINUE_DESCRIPTION = N_("to continue")

    # TRANSLATORS:'r' to refresh
    REFRESH = C_('TUI|Spoke Navigation', 'r')
    REFRESH_DESCRIPTION = N_("to refresh")

    # TRANSLATORS:'h' to help
    HELP = C_('TUI|Spoke Navigation', 'h')
    HELP_DESCRIPTION = N_("to help")

    def __init__(self, message=DEFAULT_MESSAGE):
        """
        :param message: the message of the prompt
        :type message: str|None
        """
        self.message = message
        self.options = dict()

    def set_message(self, message):
        """Set the prompt message.

        :param message: the message of the prompt
        :type message: str|None
        """
        self.message = message

    def add_option(self, key, description):
        """Add an option to the prompt.
        Causes a warning if the option already exists.

        :param key: the key for choosing the option
        :type key: str

        :param description: the description of the option
        :type description: str
        """
        if key in self.options:
            log.warning("The option '%s' does already exist in '%s'.", key,
                        self)

        self.options[key] = description

    def update_option(self, key, description):
        """Update an option in the prompt.
        Causes a warning if the option does not exist.

        :param key: the key for choosing the option
        :type key: str

        :param description: the description of the option
        :type description: str
        """
        if key not in self.options:
            log.warning("The option '%s' does not exist in '%s'.", key, self)

        self.options[key] = description

    def add_refresh_option(self, description=REFRESH_DESCRIPTION):
        """Add the option to refresh."""
        if Prompt.REFRESH in self.options:
            self.update_option(Prompt.REFRESH, description)
        else:
            self.add_option(Prompt.REFRESH, description)

    def add_continue_option(self, description=CONTINUE_DESCRIPTION):
        """Add the option to continue."""
        if Prompt.CONTINUE in self.options:
            self.update_option(Prompt.CONTINUE, description)
        else:
            self.add_option(Prompt.CONTINUE, description)

    def add_quit_option(self, description=QUIT_DESCRIPTION):
        """Add the option to quit."""
        if Prompt.QUIT in self.options:
            self.update_option(Prompt.QUIT, description)
        else:
            self.add_option(Prompt.QUIT, description)

    def add_help_option(self, description=HELP_DESCRIPTION):
        """Add the option to help."""
        if Prompt.HELP in self.options:
            self.update_option(Prompt.HELP, description)
        else:
            self.add_option(Prompt.HELP, description)

    def remove_option(self, key):
        """Remove an option with the given key.

        :param key: the key of the option
        :type key: str

        :return: the removed option
        :rtype: str|None
        """
        return self.options.pop(key, None)

    def __str__(self):
        """Return the string representation of the prompt."""
        if not self.message and not self.options:
            return ""

        parts = []

        if self.message:
            parts.append(_(self.message))

        if self.options:
            opt_list = [
                "'%s' %s" % (key, _(self.options[key]))
                for key in sorted(self.options.keys())
            ]
            opt_str = "[%s]" % ", ".join(opt_list)
            parts.append(opt_str)

        return " ".join(parts) + ": "