1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
21
This tells the library how to display things to the user.  Through this
 
 
22
layer different applications can choose the style of UI.
 
 
24
At the moment this layer is almost trivial: the application can just
 
 
25
choose the style of progress bar.
 
 
27
Set the ui_factory member to define the behaviour.  The default
 
 
33
from bzrlib.lazy_import import lazy_import
 
 
34
lazy_import(globals(), """
 
 
45
class UIFactory(object):
 
 
48
    This tells the library how to display things to the user.  Through this
 
 
49
    layer different applications can choose the style of UI.
 
 
53
        super(UIFactory, self).__init__()
 
 
54
        self._progress_bar_stack = None
 
 
56
    def get_password(self, prompt='', **kwargs):
 
 
57
        """Prompt the user for a password.
 
 
59
        :param prompt: The prompt to present the user
 
 
60
        :param kwargs: Arguments which will be expanded into the prompt.
 
 
61
                       This lets front ends display different things if
 
 
64
        :return: The password string, return None if the user canceled the
 
 
65
                 request. Note that we do not touch the encoding, users may
 
 
66
                 have whatever they see fit and the password should be
 
 
69
        raise NotImplementedError(self.get_password)
 
 
71
    def nested_progress_bar(self):
 
 
72
        """Return a nested progress bar.
 
 
74
        When the bar has been finished with, it should be released by calling
 
 
77
        raise NotImplementedError(self.nested_progress_bar)
 
 
80
        """Prepare the terminal for output.
 
 
82
        This will, for example, clear text progress bars, and leave the
 
 
83
        cursor at the leftmost position."""
 
 
84
        raise NotImplementedError(self.clear_term)
 
 
86
    def get_boolean(self, prompt):
 
 
87
        """Get a boolean question answered from the user. 
 
 
89
        :param prompt: a message to prompt the user with. Should be a single
 
 
90
        line without terminating \n.
 
 
91
        :return: True or False for y/yes or n/no.
 
 
93
        raise NotImplementedError(self.get_boolean)
 
 
95
    def recommend_upgrade(self,
 
 
98
        # this should perhaps be in the TextUIFactory and the default can do
 
 
100
        trace.warning("%s is deprecated "
 
 
101
            "and a better format is available.\n"
 
 
102
            "It is recommended that you upgrade by "
 
 
103
            "running the command\n"
 
 
109
class CLIUIFactory(UIFactory):
 
 
110
    """Common behaviour for command line UI factories."""
 
 
113
        super(CLIUIFactory, self).__init__()
 
 
114
        self.stdin = sys.stdin
 
 
116
    def get_boolean(self, prompt):
 
 
118
        # FIXME: make a regexp and handle case variations as well.
 
 
120
            self.prompt(prompt + "? [y/n]: ")
 
 
121
            line = self.stdin.readline()
 
 
122
            if line in ('y\n', 'yes\n'):
 
 
124
            if line in ('n\n', 'no\n'):
 
 
127
    def get_non_echoed_password(self, prompt):
 
 
128
        encoding = osutils.get_terminal_encoding()
 
 
129
        return getpass.getpass(prompt.encode(encoding, 'replace'))
 
 
131
    def get_password(self, prompt='', **kwargs):
 
 
132
        """Prompt the user for a password.
 
 
134
        :param prompt: The prompt to present the user
 
 
135
        :param kwargs: Arguments which will be expanded into the prompt.
 
 
136
                       This lets front ends display different things if
 
 
138
        :return: The password string, return None if the user 
 
 
139
                 canceled the request.
 
 
142
        prompt = (prompt % kwargs)
 
 
143
        # There's currently no way to say 'i decline to enter a password'
 
 
144
        # as opposed to 'my password is empty' -- does it matter?
 
 
145
        return self.get_non_echoed_password(prompt)
 
 
147
    def prompt(self, prompt):
 
 
148
        """Emit prompt on the CLI."""
 
 
151
class SilentUIFactory(CLIUIFactory):
 
 
152
    """A UI Factory which never prints anything.
 
 
154
    This is the default UI, if another one is never registered.
 
 
157
    def get_password(self, prompt='', **kwargs):
 
 
160
    def nested_progress_bar(self):
 
 
161
        if self._progress_bar_stack is None:
 
 
162
            self._progress_bar_stack = progress.ProgressBarStack(
 
 
163
                klass=progress.DummyProgress)
 
 
164
        return self._progress_bar_stack.get_nested()
 
 
166
    def clear_term(self):
 
 
169
    def recommend_upgrade(self, *args):
 
 
173
def clear_decorator(func, *args, **kwargs):
 
 
174
    """Decorator that clears the term"""
 
 
175
    ui_factory.clear_term()
 
 
176
    func(*args, **kwargs)
 
 
179
ui_factory = SilentUIFactory()
 
 
180
"""IMPORTANT: never import this symbol directly. ONLY ever access it as