~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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
30
30
 
31
31
import sys
32
32
 
33
 
import bzrlib.progress
 
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
 
34
44
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
35
45
 
36
46
 
57
67
        :param kwargs: Arguments which will be expanded into the prompt.
58
68
                       This lets front ends display different things if
59
69
                       they so choose.
60
 
        :return: The password string, return None if the user 
61
 
                 canceled the request.
 
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.
62
75
        """
63
76
        raise NotImplementedError(self.get_password)
64
 
        
 
77
 
65
78
    def nested_progress_bar(self):
66
79
        """Return a nested progress bar.
67
80
 
68
 
        When the bar has been finished with, it should be released bu calling
 
81
        When the bar has been finished with, it should be released by calling
69
82
        bar.finished().
70
83
        """
71
84
        raise NotImplementedError(self.nested_progress_bar)
86
99
        """
87
100
        raise NotImplementedError(self.get_boolean)
88
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
 
89
115
 
90
116
class CLIUIFactory(UIFactory):
91
117
    """Common behaviour for command line UI factories."""
98
124
        self.clear_term()
99
125
        # FIXME: make a regexp and handle case variations as well.
100
126
        while True:
101
 
            self.prompt(prompt)
 
127
            self.prompt(prompt + "? [y/n]: ")
102
128
            line = self.stdin.readline()
103
129
            if line in ('y\n', 'yes\n'):
104
130
                return True
105
131
            if line in ('n\n', 'no\n'):
106
132
                return False
107
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
 
108
154
    def prompt(self, prompt):
109
155
        """Emit prompt on the CLI."""
110
156
 
118
164
    @deprecated_method(zero_eight)
119
165
    def progress_bar(self):
120
166
        """See UIFactory.nested_progress_bar()."""
121
 
        return bzrlib.progress.DummyProgress()
 
167
        return progress.DummyProgress()
122
168
 
123
169
    def get_password(self, prompt='', **kwargs):
124
170
        return None
125
171
 
126
172
    def nested_progress_bar(self):
127
173
        if self._progress_bar_stack is None:
128
 
            self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
129
 
                klass=bzrlib.progress.DummyProgress)
 
174
            self._progress_bar_stack = progress.ProgressBarStack(
 
175
                klass=progress.DummyProgress)
130
176
        return self._progress_bar_stack.get_nested()
131
177
 
132
178
    def clear_term(self):
133
179
        pass
134
180
 
 
181
    def recommend_upgrade(self, *args):
 
182
        pass
 
183
 
135
184
 
136
185
def clear_decorator(func, *args, **kwargs):
137
186
    """Decorator that clears the term"""