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.
109
UI Factories are also context managers, for some syntactic sugar some users
112
:ivar suppressed_warnings: Identifiers for user warnings that should
116
_user_warning_templates = dict(
117
cross_format_fetch=("Doing on-the-fly conversion from "
118
"%(from_format)s to %(to_format)s.\n"
119
"This may take some time. Upgrade the repositories to the "
120
"same format for better performance."
110
124
def __init__(self):
111
125
self._task_stack = []
126
self.suppressed_warnings = set()
130
"""Context manager entry support.
132
Override in a concrete factory class if initialisation before use is
135
return self # This is bound to the 'as' clause in a with statement.
137
def __exit__(self, exc_type, exc_val, exc_tb):
138
"""Context manager exit support.
140
Override in a concrete factory class if more cleanup than a simple
141
self.clear_term() is needed when the UIFactory is finished with.
144
return False # propogate exceptions.
146
def be_quiet(self, state):
147
"""Tell the UI to be more quiet, or not.
149
Typically this suppresses progress bars; the application may also look
150
at ui_factory.is_quiet().
113
154
def get_password(self, prompt='', **kwargs):
114
155
"""Prompt the user for a password.
134
178
version of stdout, but in a GUI it might be appropriate to send it to a
135
179
window displaying the text.
137
:param encoding: Unicode encoding for output; default is the
138
terminal encoding, which may be different from the user encoding.
181
:param encoding: Unicode encoding for output; if not specified
182
uses the configured 'output_encoding' if any; otherwise the
139
184
(See get_terminal_encoding.)
141
186
:param encoding_type: How to handle encoding errors:
144
189
# XXX: is the caller supposed to close the resulting object?
145
190
if encoding is None:
146
encoding = osutils.get_terminal_encoding()
191
from bzrlib import config
192
encoding = config.GlobalConfig().get_user_option(
195
encoding = osutils.get_terminal_encoding(trace=True)
147
196
if encoding_type is None:
148
197
encoding_type = 'replace'
149
198
out_stream = self._make_output_stream_explicit(encoding, encoding_type)
171
220
if not self._task_stack:
172
221
warnings.warn("%r finished but nothing is active"
174
elif task != self._task_stack[-1]:
175
warnings.warn("%r is not the active task %r"
176
% (task, self._task_stack[-1]))
223
if task in self._task_stack:
224
self._task_stack.remove(task)
178
del self._task_stack[-1]
226
warnings.warn("%r is not in active stack %r"
227
% (task, self._task_stack))
179
228
if not self._task_stack:
180
229
self._progress_all_finished()
250
def format_user_warning(self, warning_id, message_args):
252
template = self._user_warning_templates[warning_id]
254
fail = "failed to format warning %r, %r" % (warning_id, message_args)
255
warnings.warn(fail) # so tests will fail etc
258
return template % message_args
259
except ValueError, e:
260
fail = "failed to format warning %r, %r: %s" % (
261
warning_id, message_args, e)
262
warnings.warn(fail) # so tests will fail etc
201
265
def get_boolean(self, prompt):
202
266
"""Get a boolean question answered from the user.
228
292
def recommend_upgrade(self,
229
293
current_format_name,
231
# this should perhaps be in the TextUIFactory and the default can do
295
# XXX: this should perhaps be in the TextUIFactory and the default can do
298
# XXX: Change to show_user_warning - that will accomplish the previous
299
# xxx. -- mbp 2010-02-25
233
300
trace.warning("%s is deprecated "
234
301
"and a better format is available.\n"
235
302
"It is recommended that you upgrade by "
259
326
# Default implementation just does nothing
329
def show_user_warning(self, warning_id, **message_args):
330
"""Show a warning to the user.
332
This is specifically for things that are under the user's control (eg
333
outdated formats), not for internal program warnings like deprecated
336
This can be overridden by UIFactory subclasses to show it in some
337
appropriate way; the default UIFactory is noninteractive and does
338
nothing. format_user_warning maps it to a string, though other
339
presentations can be used for particular UIs.
341
:param warning_id: An identifier like 'cross_format_fetch' used to
342
check if the message is suppressed and to look up the string.
343
:param message_args: Arguments to be interpolated into the message.
262
347
def show_error(self, msg):
263
348
"""Show an error message (not an exception) to the user.
265
350
The message should not have an error prefix or trailing newline. That
266
will be added by the factory if appropriate.
351
will be added by the factory if appropriate.
268
353
raise NotImplementedError(self.show_error)
275
360
"""Show a warning to the user."""
276
361
raise NotImplementedError(self.show_warning)
363
def warn_cross_format_fetch(self, from_format, to_format):
364
"""Warn about a potentially slow cross-format transfer.
366
This is deprecated in favor of show_user_warning, but retained for api
367
compatibility in 2.0 and 2.1.
369
self.show_user_warning('cross_format_fetch', from_format=from_format,
372
def warn_experimental_format_fetch(self, inter):
373
"""Warn about fetching into experimental repository formats."""
374
if inter.target._format.experimental:
375
trace.warning("Fetching into experimental format %s.\n"
376
"This format may be unreliable or change in the future "
377
"without an upgrade path.\n" % (inter.target._format,))
280
380
class SilentUIFactory(UIFactory):