1
  5   ˆl–Ü4ý( ©¤ýÔâ�fàŸ¸Å£�þB gmâÈèÿóÂƒh¹ÁÀ¿  B   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þZ#Ž,Ü¢#á�a	HçÃ„í¶÷Â_ŠI|¥�+Wé3ž¯ÂÁ      1
 ?÷     # Copyright (C) 2005-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

__docformat__ = "google"

"""Abstraction for interacting with the user.

Applications can choose different types of UI, and they deal with displaying
messages or progress to the user, and with gathering different types of input.

Several levels are supported, and you can also register new factories such as
for a GUI.

breezy.ui.UIFactory

    Semi-abstract base class

breezy.ui.SilentUIFactory

    Produces no output and cannot take any input; useful for programs using
    breezy in batch mode or for programs such as loggerhead.

breezy.ui.CannedInputUIFactory

    For use in testing; the input values to be returned are provided
    at construction.

breezy.ui.text.TextUIFactory

    Standard text command-line interface, with stdin, stdout, stderr.
    May make more or less advanced use of them, eg in drawing progress bars,
    depending on the detected capabilities of the terminal.
    GUIs may choose to subclass this so that unimplemented methods fall
    back to working through the terminal.
"""

import warnings


_valid_boolean_strings = dict(yes=True, no=False,
                              y=True, n=False,
                              on=True, off=False,
                              true=True, false=False)
_valid_boolean_strings['1'] = True
_valid_boolean_strings['0'] = False


def bool_from_string(s, accepted_values=None):
    """Returns a boolean if the string can be interpreted as such.

    Interpret case insensitive strings as booleans. The default values
    includes: 'yes', 'no, 'y', 'n', 'true', 'false', '0', '1', 'on',
    'off'. Alternative values can be provided with the 'accepted_values'
    parameter.

    Args:
      s: A string that should be interpreted as a boolean. It should be of
         type string or unicode.

      accepted_values: An optional dict with accepted strings as keys and
         True/False as values. The strings will be tested against a lowered
         version of 's'.

    Returns:
      True or False for accepted strings, None otherwise.
    """
    if accepted_values is None:
        accepted_values = _valid_boolean_strings
    val = None
    if isinstance(s, str):
        try:
            val = accepted_values[s.lower()]
        except KeyError:
            pass
    return val


class ConfirmationUserInterfacePolicy:
    """Wrapper for a UIFactory that allows or denies all confirmed actions."""

    def __init__(self, wrapped_ui, default_answer, specific_answers):
        """Generate a proxy UI that does no confirmations.

        Args:
          wrapped_ui: Underlying UIFactory.
          default_answer: Bool for whether requests for
            confirmation from the user should be noninteractively accepted or
            denied.
          specific_answers: Map from confirmation_id to bool answer.
        """
        self.wrapped_ui = wrapped_ui
        self.default_answer = default_answer
        self.specific_answers = specific_answers

    def __getattr__(self, name):
        return getattr(self.wrapped_ui, name)

    def __repr__(self):
        return '{}({!r}, {!r}, {!r})'.format(
            self.__class__.__name__,
            self.wrapped_ui,
            self.default_answer,
   