~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Aaron Bentley
  • Date: 2007-02-06 14:52:16 UTC
  • mfrom: (2266 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2268.
  • Revision ID: abentley@panoramicfeedback.com-20070206145216-fcpi8o3ufvuzwbp9
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
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
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
16
16
 
 
17
 
 
18
 
17
19
"""UI abstraction.
18
20
 
19
21
This tells the library how to display things to the user.  Through this
30
32
 
31
33
from bzrlib.lazy_import import lazy_import
32
34
lazy_import(globals(), """
33
 
import getpass
34
 
 
35
35
from bzrlib import (
36
 
    errors,
37
 
    osutils,
38
36
    progress,
39
 
    trace,
40
37
    )
41
38
""")
42
39
 
 
40
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
 
41
 
43
42
 
44
43
class UIFactory(object):
45
44
    """UI abstraction.
52
51
        super(UIFactory, self).__init__()
53
52
        self._progress_bar_stack = None
54
53
 
 
54
    @deprecated_method(zero_eight)
 
55
    def progress_bar(self):
 
56
        """See UIFactory.nested_progress_bar()."""
 
57
        raise NotImplementedError(self.progress_bar)
 
58
 
55
59
    def get_password(self, prompt='', **kwargs):
56
60
        """Prompt the user for a password.
57
61
 
59
63
        :param kwargs: Arguments which will be expanded into the prompt.
60
64
                       This lets front ends display different things if
61
65
                       they so choose.
62
 
 
63
 
        :return: The password string, return None if the user canceled the
64
 
                 request. Note that we do not touch the encoding, users may
65
 
                 have whatever they see fit and the password should be
66
 
                 transported as is.
 
66
        :return: The password string, return None if the user 
 
67
                 canceled the request.
67
68
        """
68
69
        raise NotImplementedError(self.get_password)
69
 
 
 
70
        
70
71
    def nested_progress_bar(self):
71
72
        """Return a nested progress bar.
72
73
 
91
92
        """
92
93
        raise NotImplementedError(self.get_boolean)
93
94
 
94
 
    def recommend_upgrade(self,
95
 
        current_format_name,
96
 
        basedir):
97
 
        # this should perhaps be in the TextUIFactory and the default can do
98
 
        # nothing
99
 
        trace.warning("%s is deprecated "
100
 
            "and a better format is available.\n"
101
 
            "It is recommended that you upgrade by "
102
 
            "running the command\n"
103
 
            "  bzr upgrade %s",
104
 
            current_format_name,
105
 
            basedir)
106
 
 
107
95
 
108
96
class CLIUIFactory(UIFactory):
109
97
    """Common behaviour for command line UI factories."""
116
104
        self.clear_term()
117
105
        # FIXME: make a regexp and handle case variations as well.
118
106
        while True:
119
 
            self.prompt(prompt + "? [y/n]: ")
 
107
            self.prompt(prompt)
120
108
            line = self.stdin.readline()
121
109
            if line in ('y\n', 'yes\n'):
122
110
                return True
123
111
            if line in ('n\n', 'no\n'):
124
112
                return False
125
113
 
126
 
    def get_non_echoed_password(self, prompt):
127
 
        if not sys.stdin.isatty():
128
 
            raise errors.NotATerminal()
129
 
        encoding = osutils.get_terminal_encoding()
130
 
        return getpass.getpass(prompt.encode(encoding, 'replace'))
131
 
 
132
 
    def get_password(self, prompt='', **kwargs):
133
 
        """Prompt the user for a password.
134
 
 
135
 
        :param prompt: The prompt to present the user
136
 
        :param kwargs: Arguments which will be expanded into the prompt.
137
 
                       This lets front ends display different things if
138
 
                       they so choose.
139
 
        :return: The password string, return None if the user 
140
 
                 canceled the request.
141
 
        """
142
 
        prompt += ': '
143
 
        prompt = (prompt % kwargs)
144
 
        # There's currently no way to say 'i decline to enter a password'
145
 
        # as opposed to 'my password is empty' -- does it matter?
146
 
        return self.get_non_echoed_password(prompt)
147
 
 
148
114
    def prompt(self, prompt):
149
115
        """Emit prompt on the CLI."""
150
116
 
155
121
    This is the default UI, if another one is never registered.
156
122
    """
157
123
 
 
124
    @deprecated_method(zero_eight)
 
125
    def progress_bar(self):
 
126
        """See UIFactory.nested_progress_bar()."""
 
127
        return progress.DummyProgress()
 
128
 
158
129
    def get_password(self, prompt='', **kwargs):
159
130
        return None
160
131
 
167
138
    def clear_term(self):
168
139
        pass
169
140
 
170
 
    def recommend_upgrade(self, *args):
171
 
        pass
172
 
 
173
141
 
174
142
def clear_decorator(func, *args, **kwargs):
175
143
    """Decorator that clears the term"""