~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui.py

  • Committer: Aaron Bentley
  • Date: 2005-09-13 02:42:07 UTC
  • mto: (1185.1.16)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: aaron.bentley@utoronto.ca-20050913024207-489d573af4b76c4d
Fixed issues with pull not having a default location after branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 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
28
28
displays no output.
29
29
"""
30
30
 
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
 
    osutils,
39
 
    progress,
40
 
    trace,
41
 
    )
42
 
""")
43
 
 
44
 
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
45
 
 
46
 
 
47
 
class UIFactory(object):
48
 
    """UI abstraction.
49
 
 
50
 
    This tells the library how to display things to the user.  Through this
51
 
    layer different applications can choose the style of UI.
52
 
    """
53
 
 
54
 
    def __init__(self):
55
 
        super(UIFactory, self).__init__()
56
 
        self._progress_bar_stack = None
57
 
 
58
 
    @deprecated_method(zero_eight)
59
 
    def progress_bar(self):
60
 
        """See UIFactory.nested_progress_bar()."""
61
 
        raise NotImplementedError(self.progress_bar)
62
 
 
63
 
    def get_password(self, prompt='', **kwargs):
64
 
        """Prompt the user for a password.
65
 
 
66
 
        :param prompt: The prompt to present the user
67
 
        :param kwargs: Arguments which will be expanded into the prompt.
68
 
                       This lets front ends display different things if
69
 
                       they so choose.
70
 
 
71
 
        :return: The password string, return None if the user canceled the
72
 
                 request. Note that we do not touch the encoding, users may
73
 
                 have whatever they see fit and the password should be
74
 
                 transported as is.
75
 
        """
76
 
        raise NotImplementedError(self.get_password)
77
 
 
78
 
    def nested_progress_bar(self):
79
 
        """Return a nested progress bar.
80
 
 
81
 
        When the bar has been finished with, it should be released by calling
82
 
        bar.finished().
83
 
        """
84
 
        raise NotImplementedError(self.nested_progress_bar)
85
 
 
86
 
    def clear_term(self):
87
 
        """Prepare the terminal for output.
88
 
 
89
 
        This will, for example, clear text progress bars, and leave the
90
 
        cursor at the leftmost position."""
91
 
        raise NotImplementedError(self.clear_term)
92
 
 
93
 
    def get_boolean(self, prompt):
94
 
        """Get a boolean question answered from the user. 
95
 
 
96
 
        :param prompt: a message to prompt the user with. Should be a single
97
 
        line without terminating \n.
98
 
        :return: True or False for y/yes or n/no.
99
 
        """
100
 
        raise NotImplementedError(self.get_boolean)
101
 
 
102
 
    def recommend_upgrade(self,
103
 
        current_format_name,
104
 
        basedir):
105
 
        # this should perhaps be in the TextUIFactory and the default can do
106
 
        # nothing
107
 
        trace.warning("%s is deprecated "
108
 
            "and a better format is available.\n"
109
 
            "It is recommended that you upgrade by "
110
 
            "running the command\n"
111
 
            "  bzr upgrade %s",
112
 
            current_format_name,
113
 
            basedir)
114
 
 
115
 
 
116
 
class CLIUIFactory(UIFactory):
117
 
    """Common behaviour for command line UI factories."""
118
 
 
119
 
    def __init__(self):
120
 
        super(CLIUIFactory, self).__init__()
121
 
        self.stdin = sys.stdin
122
 
 
123
 
    def get_boolean(self, prompt):
124
 
        self.clear_term()
125
 
        # FIXME: make a regexp and handle case variations as well.
126
 
        while True:
127
 
            self.prompt(prompt + "? [y/n]: ")
128
 
            line = self.stdin.readline()
129
 
            if line in ('y\n', 'yes\n'):
130
 
                return True
131
 
            if line in ('n\n', 'no\n'):
132
 
                return False
133
 
 
134
 
    def get_non_echoed_password(self, prompt):
135
 
        encoding = osutils.get_terminal_encoding()
136
 
        return getpass.getpass(prompt.encode(encoding, 'replace'))
137
 
 
138
 
    def get_password(self, prompt='', **kwargs):
139
 
        """Prompt the user for a password.
140
 
 
141
 
        :param prompt: The prompt to present the user
142
 
        :param kwargs: Arguments which will be expanded into the prompt.
143
 
                       This lets front ends display different things if
144
 
                       they so choose.
145
 
        :return: The password string, return None if the user 
146
 
                 canceled the request.
147
 
        """
148
 
        prompt += ': '
149
 
        prompt = (prompt % kwargs)
150
 
        # There's currently no way to say 'i decline to enter a password'
151
 
        # as opposed to 'my password is empty' -- does it matter?
152
 
        return self.get_non_echoed_password(prompt)
153
 
 
154
 
    def prompt(self, prompt):
155
 
        """Emit prompt on the CLI."""
156
 
 
157
 
 
158
 
class SilentUIFactory(CLIUIFactory):
159
 
    """A UI Factory which never prints anything.
160
 
 
161
 
    This is the default UI, if another one is never registered.
162
 
    """
163
 
 
164
 
    @deprecated_method(zero_eight)
165
 
    def progress_bar(self):
166
 
        """See UIFactory.nested_progress_bar()."""
167
 
        return progress.DummyProgress()
168
 
 
169
 
    def get_password(self, prompt='', **kwargs):
170
 
        return None
171
 
 
172
 
    def nested_progress_bar(self):
173
 
        if self._progress_bar_stack is None:
174
 
            self._progress_bar_stack = progress.ProgressBarStack(
175
 
                klass=progress.DummyProgress)
176
 
        return self._progress_bar_stack.get_nested()
177
 
 
178
 
    def clear_term(self):
179
 
        pass
180
 
 
181
 
    def recommend_upgrade(self, *args):
182
 
        pass
183
 
 
184
 
 
185
 
def clear_decorator(func, *args, **kwargs):
186
 
    """Decorator that clears the term"""
187
 
    ui_factory.clear_term()
188
 
    func(*args, **kwargs)
 
31
 
 
32
 
 
33
 
 
34
 
 
35
import bzrlib.progress
 
36
 
 
37
 
 
38
class TextUIFactory(object):
 
39
    def progress_bar(self):
 
40
 
 
41
        # this in turn is abstract, and creates either a tty or dots
 
42
        # bar depending on what we think of the terminal
 
43
        return bzrlib.progress.ProgressBar()
 
44
 
 
45
 
 
46
class SilentUIFactory(object):
 
47
    def progress_bar(self):
 
48
        return bzrlib.progress.DummyProgress()
189
49
 
190
50
 
191
51
ui_factory = SilentUIFactory()
192
 
"""IMPORTANT: never import this symbol directly. ONLY ever access it as 
193
 
ui.ui_factory."""