94
class ConfirmationUserInterfacePolicy(object):
95
"""Wrapper for a UIFactory that allows or denies all confirmed actions."""
97
def __init__(self, wrapped_ui, default_answer, specific_answers):
98
"""Generate a proxy UI that does no confirmations.
100
:param wrapped_ui: Underlying UIFactory.
101
:param default_answer: Bool for whether requests for
102
confirmation from the user should be noninteractively accepted or
104
:param specific_answers: Map from confirmation_id to bool answer.
106
self.wrapped_ui = wrapped_ui
107
self.default_answer = default_answer
108
self.specific_answers = specific_answers
110
def __getattr__(self, name):
111
return getattr(self.wrapped_ui, name)
114
return '%s(%r, %r, %r)' % (
115
self.__class__.__name__,
118
self.specific_answers)
120
def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
121
if confirmation_id in self.specific_answers:
122
return self.specific_answers[confirmation_id]
123
elif self.default_answer is not None:
124
return self.default_answer
126
return self.wrapped_ui.confirm_action(
127
prompt, confirmation_id, prompt_kwargs)
130
103
class UIFactory(object):
131
104
"""UI abstraction.
133
106
This tells the library how to display things to the user. Through this
134
107
layer different applications can choose the style of UI.
136
UI Factories are also context managers, for some syntactic sugar some users
139
:ivar suppressed_warnings: Identifiers for user warnings that should
143
_user_warning_templates = dict(
144
cross_format_fetch=("Doing on-the-fly conversion from "
145
"%(from_format)s to %(to_format)s.\n"
146
"This may take some time. Upgrade the repositories to the "
147
"same format for better performance."
149
experimental_format_fetch=("Fetching into experimental format "
151
"This format may be unreliable or change in the future "
152
"without an upgrade path.\n"),
154
"The command 'bzr %(deprecated_name)s' "
155
"has been deprecated in bzr %(deprecated_in_version)s. "
156
"Please use 'bzr %(recommended_name)s' instead."),
157
recommend_upgrade=("%(current_format_name)s is deprecated "
158
"and a better format is available.\n"
159
"It is recommended that you upgrade by "
160
"running the command\n"
161
" bzr upgrade %(basedir)s"),
163
u"Stole dead lock %(lock_url)s %(other_holder_info)s."),
166
110
def __init__(self):
167
111
self._task_stack = []
168
self.suppressed_warnings = set()
172
"""Context manager entry support.
174
Override in a concrete factory class if initialisation before use is
177
return self # This is bound to the 'as' clause in a with statement.
179
def __exit__(self, exc_type, exc_val, exc_tb):
180
"""Context manager exit support.
182
Override in a concrete factory class if more cleanup than a simple
183
self.clear_term() is needed when the UIFactory is finished with.
186
return False # propogate exceptions.
188
def be_quiet(self, state):
189
"""Tell the UI to be more quiet, or not.
191
Typically this suppresses progress bars; the application may also look
192
at ui_factory.is_quiet().
196
def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
197
"""Seek user confirmation for an action.
199
If the UI is noninteractive, or the user does not want to be asked
200
about this action, True is returned, indicating bzr should just
203
The confirmation id allows the user to configure certain actions to
204
always be confirmed or always denied, and for UIs to specialize the
205
display of particular confirmations.
207
:param prompt: Suggested text to display to the user.
208
:param prompt_kwargs: A dictionary of arguments that can be
209
string-interpolated into the prompt.
210
:param confirmation_id: Unique string identifier for the confirmation.
212
return self.get_boolean(prompt % prompt_kwargs)
214
def get_password(self, prompt=u'', **kwargs):
113
def get_password(self, prompt='', **kwargs):
215
114
"""Prompt the user for a password.
217
:param prompt: The prompt to present the user (must be unicode)
116
:param prompt: The prompt to present the user
218
117
:param kwargs: Arguments which will be expanded into the prompt.
219
118
This lets front ends display different things if
227
126
raise NotImplementedError(self.get_password)
232
def make_output_stream(self, encoding=None, encoding_type=None):
233
"""Get a stream for sending out bulk text data.
235
This is used for commands that produce bulk text, such as log or diff
236
output, as opposed to user interaction. This should work even for
237
non-interactive user interfaces. Typically this goes to a decorated
238
version of stdout, but in a GUI it might be appropriate to send it to a
239
window displaying the text.
241
:param encoding: Unicode encoding for output; if not specified
242
uses the configured 'output_encoding' if any; otherwise the
244
(See get_terminal_encoding.)
246
:param encoding_type: How to handle encoding errors:
247
replace/strict/escape/exact. Default is replace.
249
# XXX: is the caller supposed to close the resulting object?
251
encoding = config.GlobalStack().get('output_encoding')
253
encoding = osutils.get_terminal_encoding(trace=True)
254
if encoding_type is None:
255
encoding_type = 'replace'
256
out_stream = self._make_output_stream_explicit(encoding, encoding_type)
259
def _make_output_stream_explicit(self, encoding, encoding_type):
260
raise NotImplementedError("%s doesn't support make_output_stream"
261
% (self.__class__.__name__))
263
128
def nested_progress_bar(self):
264
129
"""Return a nested progress bar.
308
def format_user_warning(self, warning_id, message_args):
310
template = self._user_warning_templates[warning_id]
312
fail = "bzr warning: %r, %r" % (warning_id, message_args)
313
warnings.warn("no template for warning: " + fail) # so tests will fail etc
316
return template % message_args
317
except ValueError, e:
318
fail = "bzr unprintable warning: %r, %r, %s" % (
319
warning_id, message_args, e)
320
warnings.warn(fail) # so tests will fail etc
323
173
def get_boolean(self, prompt):
324
174
"""Get a boolean question answered from the user.
326
176
:param prompt: a message to prompt the user with. Should be a single
327
line without terminating \\n.
177
line without terminating \n.
328
178
:return: True or False for y/yes or n/no.
330
180
raise NotImplementedError(self.get_boolean)
332
def get_integer(self, prompt):
333
"""Get an integer from the user.
335
:param prompt: a message to prompt the user with. Could be a multi-line
336
prompt but without a terminating \\n.
338
:return: A signed integer.
340
raise NotImplementedError(self.get_integer)
342
182
def make_progress_view(self):
343
183
"""Construct a new ProgressView object for this UI.
348
188
return NullProgressView()
350
def recommend_upgrade(self, current_format_name, basedir):
351
"""Recommend the user upgrade a control directory.
353
:param current_format_name: Description of the current format
354
:param basedir: Location of the control dir
356
self.show_user_warning('recommend_upgrade',
357
current_format_name=current_format_name, basedir=basedir)
190
def recommend_upgrade(self,
193
# this should perhaps be in the TextUIFactory and the default can do
195
trace.warning("%s is deprecated "
196
"and a better format is available.\n"
197
"It is recommended that you upgrade by "
198
"running the command\n"
359
203
def report_transport_activity(self, transport, byte_count, direction):
360
204
"""Called by transports as they do IO.
367
def log_transport_activity(self, display=False):
368
"""Write out whatever transport activity has been measured.
370
Implementations are allowed to do nothing, but it is useful if they can
371
write a line to the log file.
373
:param display: If False, only log to disk, if True also try to display
374
a message to the user.
377
# Default implementation just does nothing
380
def show_user_warning(self, warning_id, **message_args):
381
"""Show a warning to the user.
383
This is specifically for things that are under the user's control (eg
384
outdated formats), not for internal program warnings like deprecated
387
This can be overridden by UIFactory subclasses to show it in some
388
appropriate way; the default UIFactory is noninteractive and does
389
nothing. format_user_warning maps it to a string, though other
390
presentations can be used for particular UIs.
392
:param warning_id: An identifier like 'cross_format_fetch' used to
393
check if the message is suppressed and to look up the string.
394
:param message_args: Arguments to be interpolated into the message.
398
211
def show_error(self, msg):
399
212
"""Show an error message (not an exception) to the user.
401
214
The message should not have an error prefix or trailing newline. That
402
will be added by the factory if appropriate.
215
will be added by the factory if appropriate.
404
217
raise NotImplementedError(self.show_error)
463
264
def __repr__(self):
464
265
return "%s(%r)" % (self.__class__.__name__, self.responses)
466
def confirm_action(self, prompt, confirmation_id, args):
467
return self.get_boolean(prompt % args)
469
267
def get_boolean(self, prompt):
470
268
return self.responses.pop(0)
472
def get_integer(self, prompt):
473
return self.responses.pop(0)
475
def get_password(self, prompt=u'', **kwargs):
270
def get_password(self, prompt='', **kwargs):
476
271
return self.responses.pop(0)
478
273
def get_username(self, prompt, **kwargs):
479
274
return self.responses.pop(0)
481
276
def assert_all_input_consumed(self):
482
277
if self.responses:
483
278
raise AssertionError("expected all input in %r to be consumed"