/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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 (
38
    progress,
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
39
    trace,
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
40
    )
41
""")
42
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
43
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
44
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):
54
        super(UIFactory, self).__init__()
55
        self._progress_bar_stack = None
56
1594.1.2 by Robert Collins
Update news and deprecated the old progress bar api.
57
    @deprecated_method(zero_eight)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
58
    def progress_bar(self):
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
59
        """See UIFactory.nested_progress_bar()."""
60
        raise NotImplementedError(self.progress_bar)
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
61
62
    def get_password(self, prompt='', **kwargs):
63
        """Prompt the user for a password.
64
65
        :param prompt: The prompt to present the user
66
        :param kwargs: Arguments which will be expanded into the prompt.
67
                       This lets front ends display different things if
68
                       they so choose.
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
69
70
        :return: The password string, return None if the user canceled the
71
                 request. Note that we do not touch the encoding, users may
72
                 have whatever they see fit and the password should be
73
                 transported as is.
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
74
        """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
75
        raise NotImplementedError(self.get_password)
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
76
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
77
    def nested_progress_bar(self):
78
        """Return a nested progress bar.
79
2095.4.5 by mbp at sourcefrog
Use regular progress-bar classes, not a special mechanism
80
        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.
81
        bar.finished().
82
        """
83
        raise NotImplementedError(self.nested_progress_bar)
84
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
85
    def clear_term(self):
86
        """Prepare the terminal for output.
87
88
        This will, for example, clear text progress bars, and leave the
89
        cursor at the leftmost position."""
90
        raise NotImplementedError(self.clear_term)
91
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
92
    def get_boolean(self, prompt):
93
        """Get a boolean question answered from the user. 
94
95
        :param prompt: a message to prompt the user with. Should be a single
96
        line without terminating \n.
97
        :return: True or False for y/yes or n/no.
98
        """
99
        raise NotImplementedError(self.get_boolean)
100
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
101
    def recommend_upgrade(self,
102
        current_format_name,
103
        basedir):
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
104
        # this should perhaps be in the TextUIFactory and the default can do
105
        # nothing
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
106
        trace.warning("%s is deprecated "
107
            "and a better format is available.\n"
108
            "It is recommended that you upgrade by "
109
            "running the command\n"
110
            "  bzr upgrade %s",
111
            current_format_name,
112
            basedir)
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
113
2323.6.2 by Martin Pool
Move responsibility for suggesting upgrades to ui object
114
            
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
115
class CLIUIFactory(UIFactory):
116
    """Common behaviour for command line UI factories."""
117
118
    def __init__(self):
119
        super(CLIUIFactory, self).__init__()
120
        self.stdin = sys.stdin
121
122
    def get_boolean(self, prompt):
123
        self.clear_term()
124
        # FIXME: make a regexp and handle case variations as well.
125
        while True:
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
126
            self.prompt(prompt + "? [y/n]: ")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
127
            line = self.stdin.readline()
128
            if line in ('y\n', 'yes\n'):
129
                return True
130
            if line in ('n\n', 'no\n'):
131
                return False
132
2294.4.4 by Vincent Ladeuil
Provide a better implementation for testing passwords.
133
    def get_non_echoed_password(self, prompt):
134
        return getpass.getpass(prompt)
135
136
    def get_password(self, prompt='', **kwargs):
137
        """Prompt the user for a password.
138
139
        :param prompt: The prompt to present the user
140
        :param kwargs: Arguments which will be expanded into the prompt.
141
                       This lets front ends display different things if
142
                       they so choose.
143
        :return: The password string, return None if the user 
144
                 canceled the request.
145
        """
146
        prompt += ': '
147
        prompt = (prompt % kwargs).encode(sys.stdout.encoding, 'replace')
148
        # There's currently no way to say 'i decline to enter a password'
149
        # as opposed to 'my password is empty' -- does it matter?
150
        return self.get_non_echoed_password(prompt)
151
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
152
    def prompt(self, prompt):
153
        """Emit prompt on the CLI."""
154
155
156
class SilentUIFactory(CLIUIFactory):
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
157
    """A UI Factory which never prints anything.
158
159
    This is the default UI, if another one is never registered.
160
    """
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
161
1594.1.2 by Robert Collins
Update news and deprecated the old progress bar api.
162
    @deprecated_method(zero_eight)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
163
    def progress_bar(self):
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
164
        """See UIFactory.nested_progress_bar()."""
1996.3.32 by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily
165
        return progress.DummyProgress()
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
166
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
167
    def get_password(self, prompt='', **kwargs):
168
        return None
169
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
170
    def nested_progress_bar(self):
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
171
        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
172
            self._progress_bar_stack = progress.ProgressBarStack(
173
                klass=progress.DummyProgress)
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
174
        return self._progress_bar_stack.get_nested()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
175
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
176
    def clear_term(self):
177
        pass
178
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
179
    def recommend_upgrade(self, *args):
180
        pass
181
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
182
183
def clear_decorator(func, *args, **kwargs):
184
    """Decorator that clears the term"""
185
    ui_factory.clear_term()
186
    func(*args, **kwargs)
187
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
188
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
189
ui_factory = SilentUIFactory()
1534.5.9 by Robert Collins
Advise users running upgrade on a checkout to also run it on the branch.
190
"""IMPORTANT: never import this symbol directly. ONLY ever access it as 
191
ui.ui_factory."""