~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-05-29 15:57:16 UTC
  • mfrom: (3427.5.9 dep_warnings)
  • Revision ID: pqm@pqm.ubuntu.com-20080529155716-0w3kic8lioa63231
(jam) Enable Deprecation Warnings when running -Werror and when
        running selftest

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, 2008 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
 
 
19
17
"""UI abstraction.
20
18
 
21
19
This tells the library how to display things to the user.  Through this
28
26
displays no output.
29
27
"""
30
28
 
31
 
 
32
 
import bzrlib.progress
33
 
from bzrlib.symbol_versioning import *
 
29
import sys
 
30
 
 
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
""")
34
42
 
35
43
 
36
44
class UIFactory(object):
44
52
        super(UIFactory, self).__init__()
45
53
        self._progress_bar_stack = None
46
54
 
47
 
    @deprecated_method(zero_eight)
48
 
    def progress_bar(self):
49
 
        """See UIFactory.nested_progress_bar()."""
50
 
        raise NotImplementedError(self.progress_bar)
51
 
 
52
55
    def get_password(self, prompt='', **kwargs):
53
56
        """Prompt the user for a password.
54
57
 
56
59
        :param kwargs: Arguments which will be expanded into the prompt.
57
60
                       This lets front ends display different things if
58
61
                       they so choose.
59
 
        :return: The password string, return None if the user 
60
 
                 canceled the request.
 
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.
61
67
        """
62
68
        raise NotImplementedError(self.get_password)
63
 
        
 
69
 
64
70
    def nested_progress_bar(self):
65
71
        """Return a nested progress bar.
66
72
 
67
 
        When the bar has been finished with, it should be released bu calling
 
73
        When the bar has been finished with, it should be released by calling
68
74
        bar.finished().
69
75
        """
70
76
        raise NotImplementedError(self.nested_progress_bar)
76
82
        cursor at the leftmost position."""
77
83
        raise NotImplementedError(self.clear_term)
78
84
 
79
 
 
80
 
class SilentUIFactory(UIFactory):
 
85
    def get_boolean(self, prompt):
 
86
        """Get a boolean question answered from the user. 
 
87
 
 
88
        :param prompt: a message to prompt the user with. Should be a single
 
89
        line without terminating \n.
 
90
        :return: True or False for y/yes or n/no.
 
91
        """
 
92
        raise NotImplementedError(self.get_boolean)
 
93
 
 
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
 
 
108
class CLIUIFactory(UIFactory):
 
109
    """Common behaviour for command line UI factories."""
 
110
 
 
111
    def __init__(self):
 
112
        super(CLIUIFactory, self).__init__()
 
113
        self.stdin = sys.stdin
 
114
 
 
115
    def get_boolean(self, prompt):
 
116
        self.clear_term()
 
117
        # FIXME: make a regexp and handle case variations as well.
 
118
        while True:
 
119
            self.prompt(prompt + "? [y/n]: ")
 
120
            line = self.stdin.readline()
 
121
            if line in ('y\n', 'yes\n'):
 
122
                return True
 
123
            if line in ('n\n', 'no\n'):
 
124
                return False
 
125
 
 
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
    def prompt(self, prompt):
 
149
        """Emit prompt on the CLI."""
 
150
 
 
151
 
 
152
class SilentUIFactory(CLIUIFactory):
81
153
    """A UI Factory which never prints anything.
82
154
 
83
155
    This is the default UI, if another one is never registered.
84
156
    """
85
157
 
86
 
    @deprecated_method(zero_eight)
87
 
    def progress_bar(self):
88
 
        """See UIFactory.nested_progress_bar()."""
89
 
        return bzrlib.progress.DummyProgress()
90
 
 
91
158
    def get_password(self, prompt='', **kwargs):
92
159
        return None
93
160
 
94
161
    def nested_progress_bar(self):
95
162
        if self._progress_bar_stack is None:
96
 
            self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
97
 
                klass=bzrlib.progress.DummyProgress)
 
163
            self._progress_bar_stack = progress.ProgressBarStack(
 
164
                klass=progress.DummyProgress)
98
165
        return self._progress_bar_stack.get_nested()
99
166
 
100
167
    def clear_term(self):
101
168
        pass
102
169
 
 
170
    def recommend_upgrade(self, *args):
 
171
        pass
 
172
 
103
173
 
104
174
def clear_decorator(func, *args, **kwargs):
105
175
    """Decorator that clears the term"""