~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: 2010-01-07 17:02:44 UTC
  • mfrom: (4934.1.14 2.1.0rc1-set-mtime)
  • Revision ID: pqm@pqm.ubuntu.com-20100107170244-3cgdapvuokgf8l42
(jam,
        gz) (bug #488724) Set the mtime of files touched in a TreeTransform.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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
105
105
 
106
106
    This tells the library how to display things to the user.  Through this
107
107
    layer different applications can choose the style of UI.
108
 
 
109
 
    :ivar suppressed_warnings: Identifiers for user warnings that should 
110
 
        no be emitted.
111
108
    """
112
109
 
113
 
    _user_warning_templates = dict(
114
 
        cross_format_fetch=("Doing on-the-fly conversion from "
115
 
            "%(from_format)s to %(to_format)s.\n"
116
 
            "This may take some time. Upgrade the repositories to the "
117
 
            "same format for better performance."
118
 
            )
119
 
        )
120
 
 
121
110
    def __init__(self):
122
111
        self._task_stack = []
123
 
        self.suppressed_warnings = set()
124
 
        self._quiet = False
125
 
 
126
 
    def be_quiet(self, state):
127
 
        """Tell the UI to be more quiet, or not.
128
 
 
129
 
        Typically this suppresses progress bars; the application may also look
130
 
        at ui_factory.is_quiet().
131
 
        """
132
 
        self._quiet = state
133
112
 
134
113
    def get_password(self, prompt='', **kwargs):
135
114
        """Prompt the user for a password.
146
125
        """
147
126
        raise NotImplementedError(self.get_password)
148
127
 
149
 
    def is_quiet(self):
150
 
        return self._quiet
151
 
 
152
128
    def make_output_stream(self, encoding=None, encoding_type=None):
153
129
        """Get a stream for sending out bulk text data.
154
130
 
195
171
        if not self._task_stack:
196
172
            warnings.warn("%r finished but nothing is active"
197
173
                % (task,))
198
 
        if task in self._task_stack:
199
 
            self._task_stack.remove(task)
 
174
        elif task != self._task_stack[-1]:
 
175
            warnings.warn("%r is not the active task %r"
 
176
                % (task, self._task_stack[-1]))
200
177
        else:
201
 
            warnings.warn("%r is not in active stack %r"
202
 
                % (task, self._task_stack))
 
178
            del self._task_stack[-1]
203
179
        if not self._task_stack:
204
180
            self._progress_all_finished()
205
181
 
222
198
        """
223
199
        pass
224
200
 
225
 
    def format_user_warning(self, warning_id, message_args):
226
 
        try:
227
 
            template = self._user_warning_templates[warning_id]
228
 
        except KeyError:
229
 
            fail = "failed to format warning %r, %r" % (warning_id, message_args)
230
 
            warnings.warn(fail)   # so tests will fail etc
231
 
            return fail
232
 
        try:
233
 
            return template % message_args
234
 
        except ValueError, e:
235
 
            fail = "failed to format warning %r, %r: %s" % (
236
 
                warning_id, message_args, e)
237
 
            warnings.warn(fail)   # so tests will fail etc
238
 
            return fail
239
 
 
240
201
    def get_boolean(self, prompt):
241
202
        """Get a boolean question answered from the user.
242
203
 
267
228
    def recommend_upgrade(self,
268
229
        current_format_name,
269
230
        basedir):
270
 
        # XXX: this should perhaps be in the TextUIFactory and the default can do
 
231
        # this should perhaps be in the TextUIFactory and the default can do
271
232
        # nothing
272
 
        #
273
 
        # XXX: Change to show_user_warning - that will accomplish the previous
274
 
        # xxx. -- mbp 2010-02-25
275
233
        trace.warning("%s is deprecated "
276
234
            "and a better format is available.\n"
277
235
            "It is recommended that you upgrade by "
301
259
        # Default implementation just does nothing
302
260
        pass
303
261
 
304
 
    def show_user_warning(self, warning_id, **message_args):
305
 
        """Show a warning to the user.
306
 
 
307
 
        This is specifically for things that are under the user's control (eg
308
 
        outdated formats), not for internal program warnings like deprecated
309
 
        APIs.
310
 
 
311
 
        This can be overridden by UIFactory subclasses to show it in some 
312
 
        appropriate way; the default UIFactory is noninteractive and does
313
 
        nothing.  format_user_warning maps it to a string, though other
314
 
        presentations can be used for particular UIs.
315
 
 
316
 
        :param warning_id: An identifier like 'cross_format_fetch' used to 
317
 
            check if the message is suppressed and to look up the string.
318
 
        :param message_args: Arguments to be interpolated into the message.
319
 
        """
320
 
        pass
321
 
 
322
262
    def show_error(self, msg):
323
263
        """Show an error message (not an exception) to the user.
324
264
        
325
265
        The message should not have an error prefix or trailing newline.  That
326
 
        will be added by the factory if appropriate.
 
266
        will be added by the factory if appropriate. 
327
267
        """
328
268
        raise NotImplementedError(self.show_error)
329
269
 
335
275
        """Show a warning to the user."""
336
276
        raise NotImplementedError(self.show_warning)
337
277
 
338
 
    def warn_cross_format_fetch(self, from_format, to_format):
339
 
        """Warn about a potentially slow cross-format transfer.
340
 
        
341
 
        This is deprecated in favor of show_user_warning, but retained for api
342
 
        compatibility in 2.0 and 2.1.
343
 
        """
344
 
        self.show_user_warning('cross_format_fetch', from_format=from_format,
345
 
            to_format=to_format)
346
 
 
347
 
    def warn_experimental_format_fetch(self, inter):
348
 
        """Warn about fetching into experimental repository formats."""
349
 
        if inter.target._format.experimental:
350
 
            trace.warning("Fetching into experimental format %s.\n"
351
 
                "This format may be unreliable or change in the future "
352
 
                "without an upgrade path.\n" % (inter.target._format,))
353
 
 
354
278
 
355
279
 
356
280
class SilentUIFactory(UIFactory):
372
296
    def get_username(self, prompt, **kwargs):
373
297
        return None
374
298
 
375
 
    def _make_output_stream_explicit(self, encoding, encoding_type):
376
 
        return NullOutputStream(encoding)
377
 
 
378
299
    def show_error(self, msg):
379
300
        pass
380
301
 
440
361
 
441
362
    def log_transport_activity(self, display=False):
442
363
        pass
443
 
 
444
 
 
445
 
class NullOutputStream(object):
446
 
    """Acts like a file, but discard all output."""
447
 
 
448
 
    def __init__(self, encoding):
449
 
        self.encoding = encoding
450
 
 
451
 
    def write(self, data):
452
 
        pass
453
 
 
454
 
    def writelines(self, data):
455
 
        pass
456
 
 
457
 
    def close(self):
458
 
        pass