/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-04-17 08:04:15 UTC
  • mfrom: (2423.1.2 benchmarks)
  • Revision ID: pqm@pqm.ubuntu.com-20070417080415-5vn25svmf95ki88z
Also clear SmartTCPServer hooks from TestCase._clear_hooks

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
28
displays no output.
29
29
"""
30
30
 
31
 
 
32
 
import bzrlib.progress
33
 
from bzrlib.symbol_versioning import *
 
31
import sys
 
32
 
 
33
from bzrlib.lazy_import import lazy_import
 
34
lazy_import(globals(), """
 
35
import getpass
 
36
 
 
37
from bzrlib import (
 
38
    progress,
 
39
    trace,
 
40
    )
 
41
""")
 
42
 
 
43
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
34
44
 
35
45
 
36
46
class UIFactory(object):
56
66
        :param kwargs: Arguments which will be expanded into the prompt.
57
67
                       This lets front ends display different things if
58
68
                       they so choose.
59
 
        :return: The password string, return None if the user 
60
 
                 canceled the request.
 
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.
61
74
        """
62
75
        raise NotImplementedError(self.get_password)
63
 
        
 
76
 
64
77
    def nested_progress_bar(self):
65
78
        """Return a nested progress bar.
66
79
 
67
 
        When the bar has been finished with, it should be released bu calling
 
80
        When the bar has been finished with, it should be released by calling
68
81
        bar.finished().
69
82
        """
70
83
        raise NotImplementedError(self.nested_progress_bar)
76
89
        cursor at the leftmost position."""
77
90
        raise NotImplementedError(self.clear_term)
78
91
 
79
 
 
80
 
class SilentUIFactory(UIFactory):
 
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
 
 
101
    def recommend_upgrade(self,
 
102
        current_format_name,
 
103
        basedir):
 
104
        # this should perhaps be in the TextUIFactory and the default can do
 
105
        # nothing
 
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)
 
113
 
 
114
            
 
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:
 
126
            self.prompt(prompt + "? [y/n]: ")
 
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
 
 
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
 
 
152
    def prompt(self, prompt):
 
153
        """Emit prompt on the CLI."""
 
154
 
 
155
 
 
156
class SilentUIFactory(CLIUIFactory):
81
157
    """A UI Factory which never prints anything.
82
158
 
83
159
    This is the default UI, if another one is never registered.
86
162
    @deprecated_method(zero_eight)
87
163
    def progress_bar(self):
88
164
        """See UIFactory.nested_progress_bar()."""
89
 
        return bzrlib.progress.DummyProgress()
 
165
        return progress.DummyProgress()
90
166
 
91
167
    def get_password(self, prompt='', **kwargs):
92
168
        return None
93
169
 
94
170
    def nested_progress_bar(self):
95
171
        if self._progress_bar_stack is None:
96
 
            self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
97
 
                klass=bzrlib.progress.DummyProgress)
 
172
            self._progress_bar_stack = progress.ProgressBarStack(
 
173
                klass=progress.DummyProgress)
98
174
        return self._progress_bar_stack.get_nested()
99
175
 
100
176
    def clear_term(self):
101
177
        pass
102
178
 
 
179
    def recommend_upgrade(self, *args):
 
180
        pass
 
181
 
103
182
 
104
183
def clear_decorator(func, *args, **kwargs):
105
184
    """Decorator that clears the term"""