~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Wouter van Heyst
  • Date: 2006-06-06 22:37:30 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: larstiq@larstiq.dyndns.org-20060606223730-a308c5429fc6c617
change branch.{get,set}_parent to store a relative path but return full urls

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2005 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
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
28
30
 
29
31
import sys
30
32
 
31
 
from bzrlib.lazy_import import lazy_import
32
 
lazy_import(globals(), """
33
 
import getpass
34
 
 
35
 
from bzrlib import (
36
 
    errors,
37
 
    osutils,
38
 
    progress,
39
 
    trace,
40
 
    )
41
 
""")
 
33
import bzrlib.progress
 
34
from bzrlib.symbol_versioning import *
42
35
 
43
36
 
44
37
class UIFactory(object):
52
45
        super(UIFactory, self).__init__()
53
46
        self._progress_bar_stack = None
54
47
 
 
48
    @deprecated_method(zero_eight)
 
49
    def progress_bar(self):
 
50
        """See UIFactory.nested_progress_bar()."""
 
51
        raise NotImplementedError(self.progress_bar)
 
52
 
55
53
    def get_password(self, prompt='', **kwargs):
56
54
        """Prompt the user for a password.
57
55
 
59
57
        :param kwargs: Arguments which will be expanded into the prompt.
60
58
                       This lets front ends display different things if
61
59
                       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.
 
60
        :return: The password string, return None if the user 
 
61
                 canceled the request.
67
62
        """
68
63
        raise NotImplementedError(self.get_password)
69
 
 
 
64
        
70
65
    def nested_progress_bar(self):
71
66
        """Return a nested progress bar.
72
67
 
73
 
        When the bar has been finished with, it should be released by calling
 
68
        When the bar has been finished with, it should be released bu calling
74
69
        bar.finished().
75
70
        """
76
71
        raise NotImplementedError(self.nested_progress_bar)
91
86
        """
92
87
        raise NotImplementedError(self.get_boolean)
93
88
 
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
89
 
108
90
class CLIUIFactory(UIFactory):
109
91
    """Common behaviour for command line UI factories."""
116
98
        self.clear_term()
117
99
        # FIXME: make a regexp and handle case variations as well.
118
100
        while True:
119
 
            self.prompt(prompt + "? [y/n]: ")
 
101
            self.prompt(prompt)
120
102
            line = self.stdin.readline()
121
103
            if line in ('y\n', 'yes\n'):
122
104
                return True
123
105
            if line in ('n\n', 'no\n'):
124
106
                return False
125
107
 
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
108
    def prompt(self, prompt):
149
109
        """Emit prompt on the CLI."""
150
110
 
155
115
    This is the default UI, if another one is never registered.
156
116
    """
157
117
 
 
118
    @deprecated_method(zero_eight)
 
119
    def progress_bar(self):
 
120
        """See UIFactory.nested_progress_bar()."""
 
121
        return bzrlib.progress.DummyProgress()
 
122
 
158
123
    def get_password(self, prompt='', **kwargs):
159
124
        return None
160
125
 
161
126
    def nested_progress_bar(self):
162
127
        if self._progress_bar_stack is None:
163
 
            self._progress_bar_stack = progress.ProgressBarStack(
164
 
                klass=progress.DummyProgress)
 
128
            self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
 
129
                klass=bzrlib.progress.DummyProgress)
165
130
        return self._progress_bar_stack.get_nested()
166
131
 
167
132
    def clear_term(self):
168
133
        pass
169
134
 
170
 
    def recommend_upgrade(self, *args):
171
 
        pass
172
 
 
173
135
 
174
136
def clear_decorator(func, *args, **kwargs):
175
137
    """Decorator that clears the term"""