~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Martin Pool
  • Date: 2010-03-26 10:25:47 UTC
  • mfrom: (5110.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5118.
  • Revision ID: mbp@canonical.com-20100326102547-74ta65m7w7ecjebq
merge 2.1.1, including fetch format warning, back to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
108
111
    """
109
112
 
 
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
 
110
121
    def __init__(self):
111
122
        self._task_stack = []
 
123
        self.suppressed_warnings = set()
112
124
        self._quiet = False
113
125
 
114
126
    def be_quiet(self, state):
210
222
        """
211
223
        pass
212
224
 
 
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
 
213
240
    def get_boolean(self, prompt):
214
241
        """Get a boolean question answered from the user.
215
242
 
240
267
    def recommend_upgrade(self,
241
268
        current_format_name,
242
269
        basedir):
243
 
        # this should perhaps be in the TextUIFactory and the default can do
 
270
        # XXX: this should perhaps be in the TextUIFactory and the default can do
244
271
        # nothing
 
272
        #
 
273
        # XXX: Change to show_user_warning - that will accomplish the previous
 
274
        # xxx. -- mbp 2010-02-25
245
275
        trace.warning("%s is deprecated "
246
276
            "and a better format is available.\n"
247
277
            "It is recommended that you upgrade by "
271
301
        # Default implementation just does nothing
272
302
        pass
273
303
 
 
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
 
274
322
    def show_error(self, msg):
275
323
        """Show an error message (not an exception) to the user.
276
324
        
288
336
        raise NotImplementedError(self.show_warning)
289
337
 
290
338
    def warn_cross_format_fetch(self, from_format, to_format):
291
 
        """Warn about a potentially slow cross-format transfer"""
292
 
        # See <https://launchpad.net/bugs/456077> asking for a warning here
293
 
        trace.warning("Doing on-the-fly conversion from %s to %s.\n"
294
 
            "This may take some time. Upgrade the repositories to the "
295
 
            "same format for better performance.\n" %
296
 
            (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)
297
346
 
298
347
    def warn_experimental_format_fetch(self, inter):
299
348
        """Warn about fetching into experimental repository formats."""