/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3948.2.2 by Martin Pool
Corrections to finishing progress bars
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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
"""UI abstraction.
18
19
This tells the library how to display things to the user.  Through this
20
layer different applications can choose the style of UI.
21
22
At the moment this layer is almost trivial: the application can just
23
choose the style of progress bar.
24
25
Set the ui_factory member to define the behaviour.  The default
26
displays no output.
27
"""
28
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
29
import os
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
30
import sys
3882.8.12 by Martin Pool
Give a warning, not an error, if a progress bar is not finished in order
31
import warnings
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 (
3260.2.1 by Alexander Belchenko
Don't ask a password if there is no real terminal. (#69851)
38
    errors,
2461.1.2 by Vincent Ladeuil
Take jam's remark into account.
39
    osutils,
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
40
    progress,
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
41
    trace,
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
42
    )
43
""")
44
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
45
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
46
class UIFactory(object):
47
    """UI abstraction.
48
49
    This tells the library how to display things to the user.  Through this
50
    layer different applications can choose the style of UI.
51
    """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
52
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
53
    def __init__(self):
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
54
        self._task_stack = []
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
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
        """
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
77
        if self._task_stack:
3882.8.3 by Martin Pool
Move display of transport throughput into TextProgressView
78
            t = progress.ProgressTask(self._task_stack[-1], self)
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
79
        else:
3882.8.3 by Martin Pool
Move display of transport throughput into TextProgressView
80
            t = progress.ProgressTask(None, self)
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
81
        self._task_stack.append(t)
82
        return t
83
84
    def progress_finished(self, task):
85
        if task != self._task_stack[-1]:
3948.2.2 by Martin Pool
Corrections to finishing progress bars
86
            warnings.warn("%r is not the active task %r" 
87
                % (task, self._task_stack[-1]))
3882.8.12 by Martin Pool
Give a warning, not an error, if a progress bar is not finished in order
88
        else:
89
            del self._task_stack[-1]
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
90
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
91
    def clear_term(self):
92
        """Prepare the terminal for output.
93
94
        This will, for example, clear text progress bars, and leave the
95
        cursor at the leftmost position."""
3948.2.2 by Martin Pool
Corrections to finishing progress bars
96
        pass
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
97
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
98
    def get_boolean(self, prompt):
99
        """Get a boolean question answered from the user. 
100
101
        :param prompt: a message to prompt the user with. Should be a single
102
        line without terminating \n.
103
        :return: True or False for y/yes or n/no.
104
        """
105
        raise NotImplementedError(self.get_boolean)
106
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
107
    def recommend_upgrade(self,
108
        current_format_name,
109
        basedir):
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
110
        # this should perhaps be in the TextUIFactory and the default can do
111
        # nothing
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
112
        trace.warning("%s is deprecated "
113
            "and a better format is available.\n"
114
            "It is recommended that you upgrade by "
115
            "running the command\n"
116
            "  bzr upgrade %s",
117
            current_format_name,
118
            basedir)
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
119
3882.7.5 by Martin Pool
Further mockup of transport-based activity indicator.
120
    def report_transport_activity(self, transport, byte_count, direction):
121
        """Called by transports as they do IO.
122
        
123
        This may update a progress bar, spinner, or similar display.
124
        By default it does nothing.
125
        """
126
        pass
127
128
2461.1.1 by Vincent Ladeuil
Fix 110204 by letting TestUIFactory encode password prompt.
129
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
130
class CLIUIFactory(UIFactory):
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
131
    """Common behaviour for command line UI factories.
132
    
133
    This is suitable for dumb terminals that can't repaint existing text."""
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
134
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
135
    def __init__(self, stdin=None, stdout=None, stderr=None):
136
        UIFactory.__init__(self)
137
        self.stdin = stdin or sys.stdin
138
        self.stdout = stdout or sys.stdout
139
        self.stderr = stderr or sys.stderr
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
140
141
    def get_boolean(self, prompt):
142
        self.clear_term()
143
        # FIXME: make a regexp and handle case variations as well.
144
        while True:
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
145
            self.prompt(prompt + "? [y/n]: ")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
146
            line = self.stdin.readline()
147
            if line in ('y\n', 'yes\n'):
148
                return True
149
            if line in ('n\n', 'no\n'):
150
                return False
151
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
152
    def get_non_echoed_password(self, prompt):
3260.2.1 by Alexander Belchenko
Don't ask a password if there is no real terminal. (#69851)
153
        if not sys.stdin.isatty():
154
            raise errors.NotATerminal()
2461.1.2 by Vincent Ladeuil
Take jam's remark into account.
155
        encoding = osutils.get_terminal_encoding()
156
        return getpass.getpass(prompt.encode(encoding, 'replace'))
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
157
158
    def get_password(self, prompt='', **kwargs):
159
        """Prompt the user for a password.
160
161
        :param prompt: The prompt to present the user
162
        :param kwargs: Arguments which will be expanded into the prompt.
163
                       This lets front ends display different things if
164
                       they so choose.
165
        :return: The password string, return None if the user 
166
                 canceled the request.
167
        """
168
        prompt += ': '
2461.1.1 by Vincent Ladeuil
Fix 110204 by letting TestUIFactory encode password prompt.
169
        prompt = (prompt % kwargs)
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
170
        # There's currently no way to say 'i decline to enter a password'
171
        # as opposed to 'my password is empty' -- does it matter?
172
        return self.get_non_echoed_password(prompt)
173
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
174
    def prompt(self, prompt):
175
        """Emit prompt on the CLI."""
176
177
178
class SilentUIFactory(CLIUIFactory):
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
179
    """A UI Factory which never prints anything.
180
181
    This is the default UI, if another one is never registered.
182
    """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
183
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
184
    def __init__(self):
185
        CLIUIFactory.__init__(self)
186
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
187
    def get_password(self, prompt='', **kwargs):
188
        return None
189
3948.2.2 by Martin Pool
Corrections to finishing progress bars
190
    def show_progress(self, task):
191
        pass
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
192
3882.8.4 by Martin Pool
All UI factories should support note()
193
    def note(self, msg):
194
        pass
195
3882.8.2 by Martin Pool
ProgressTask holds a reference to the ui that displays it
196
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
197
def clear_decorator(func, *args, **kwargs):
198
    """Decorator that clears the term"""
199
    ui_factory.clear_term()
200
    func(*args, **kwargs)
201
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
202
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
203
ui_factory = SilentUIFactory()
1534.5.9 by Robert Collins
Advise users running upgrade on a checkout to also run it on the branch.
204
"""IMPORTANT: never import this symbol directly. ONLY ever access it as 
205
ui.ui_factory."""
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
206
207
208
def make_ui_for_terminal(stdin, stdout, stderr):
209
    """Construct and return a suitable UIFactory for a text mode program.
210
211
    If stdout is a smart terminal, this gets a smart UIFactory with 
212
    progress indicators, etc.  If it's a dumb terminal, just plain text output.
213
    """
214
    isatty = getattr(stdin, 'isatty', None)
215
    if isatty is None:
216
        cls = CLIUIFactory
217
    elif not isatty():
218
        cls = CLIUIFactory
219
    elif os.environ.get('TERM') in (None, 'dumb', ''):
220
        # e.g. emacs compile window
221
        cls = CLIUIFactory
222
    else:
223
        from bzrlib.ui.text import TextUIFactory
224
        cls = TextUIFactory
225
    return cls(stdin=stdin, stdout=stdout, stderr=stderr)