~bzr-pqm/bzr/bzr.dev

2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
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
16
17
18
19
"""UI abstraction.
20
21
This tells the library how to display things to the user.  Through this
22
layer different applications can choose the style of UI.
23
24
At the moment this layer is almost trivial: the application can just
25
choose the style of progress bar.
26
27
Set the ui_factory member to define the behaviour.  The default
28
displays no output.
29
"""
30
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
31
import sys
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
32
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
33
from bzrlib.lazy_import import lazy_import
34
lazy_import(globals(), """
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
35
import getpass
36
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
37
from bzrlib import (
2461.1.2 by Vincent Ladeuil
Take jam's remark into account.
38
    osutils,
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
39
    progress,
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
40
    trace,
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
41
    )
42
""")
43
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
44
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
45
class UIFactory(object):
46
    """UI abstraction.
47
48
    This tells the library how to display things to the user.  Through this
49
    layer different applications can choose the style of UI.
50
    """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
51
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
52
    def __init__(self):
53
        super(UIFactory, self).__init__()
54
        self._progress_bar_stack = None
55
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
56
    def get_password(self, prompt='', **kwargs):
57
        """Prompt the user for a password.
58
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
62
                       they so choose.
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
63
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
67
                 transported as is.
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
68
        """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
69
        raise NotImplementedError(self.get_password)
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
70
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
71
    def nested_progress_bar(self):
72
        """Return a nested progress bar.
73
2095.4.5 by mbp at sourcefrog
Use regular progress-bar classes, not a special mechanism
74
        When the bar has been finished with, it should be released by calling
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
75
        bar.finished().
76
        """
77
        raise NotImplementedError(self.nested_progress_bar)
78
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
79
    def clear_term(self):
80
        """Prepare the terminal for output.
81
82
        This will, for example, clear text progress bars, and leave the
83
        cursor at the leftmost position."""
84
        raise NotImplementedError(self.clear_term)
85
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
86
    def get_boolean(self, prompt):
87
        """Get a boolean question answered from the user. 
88
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.
92
        """
93
        raise NotImplementedError(self.get_boolean)
94
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
95
    def recommend_upgrade(self,
96
        current_format_name,
97
        basedir):
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
98
        # this should perhaps be in the TextUIFactory and the default can do
99
        # nothing
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
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"
104
            "  bzr upgrade %s",
105
            current_format_name,
106
            basedir)
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
107
2461.1.1 by Vincent Ladeuil
Fix 110204 by letting TestUIFactory encode password prompt.
108
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
109
class CLIUIFactory(UIFactory):
110
    """Common behaviour for command line UI factories."""
111
112
    def __init__(self):
113
        super(CLIUIFactory, self).__init__()
114
        self.stdin = sys.stdin
115
116
    def get_boolean(self, prompt):
117
        self.clear_term()
118
        # FIXME: make a regexp and handle case variations as well.
119
        while True:
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
120
            self.prompt(prompt + "? [y/n]: ")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
121
            line = self.stdin.readline()
122
            if line in ('y\n', 'yes\n'):
123
                return True
124
            if line in ('n\n', 'no\n'):
125
                return False
126
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
127
    def get_non_echoed_password(self, prompt):
2461.1.2 by Vincent Ladeuil
Take jam's remark into account.
128
        encoding = osutils.get_terminal_encoding()
129
        return getpass.getpass(prompt.encode(encoding, 'replace'))
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
130
131
    def get_password(self, prompt='', **kwargs):
132
        """Prompt the user for a password.
133
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
137
                       they so choose.
138
        :return: The password string, return None if the user 
139
                 canceled the request.
140
        """
141
        prompt += ': '
2461.1.1 by Vincent Ladeuil
Fix 110204 by letting TestUIFactory encode password prompt.
142
        prompt = (prompt % kwargs)
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
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)
146
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
147
    def prompt(self, prompt):
148
        """Emit prompt on the CLI."""
149
150
151
class SilentUIFactory(CLIUIFactory):
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
152
    """A UI Factory which never prints anything.
153
154
    This is the default UI, if another one is never registered.
155
    """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
156
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
157
    def get_password(self, prompt='', **kwargs):
158
        return None
159
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
160
    def nested_progress_bar(self):
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
161
        if self._progress_bar_stack is None:
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
162
            self._progress_bar_stack = progress.ProgressBarStack(
163
                klass=progress.DummyProgress)
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
164
        return self._progress_bar_stack.get_nested()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
165
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
166
    def clear_term(self):
167
        pass
168
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
169
    def recommend_upgrade(self, *args):
170
        pass
171
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
172
173
def clear_decorator(func, *args, **kwargs):
174
    """Decorator that clears the term"""
175
    ui_factory.clear_term()
176
    func(*args, **kwargs)
177
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
178
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
179
ui_factory = SilentUIFactory()
1534.5.9 by Robert Collins
Advise users running upgrade on a checkout to also run it on the branch.
180
"""IMPORTANT: never import this symbol directly. ONLY ever access it as 
181
ui.ui_factory."""