~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Alexander Belchenko
  • Date: 2007-01-30 23:05:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2259.
  • Revision ID: bialix@ukr.net-20070130230535-kx1rd478rtigyc3v
standalone installer: win98 support

Show diffs side-by-side

added added

removed removed

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