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
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
201
def get_boolean(self, prompt):
324
202
"""Get a boolean question answered from the user.
326
204
:param prompt: a message to prompt the user with. Should be a single
327
line without terminating \\n.
205
line without terminating \n.
328
206
:return: True or False for y/yes or n/no.
330
208
raise NotImplementedError(self.get_boolean)
348
226
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)
228
def recommend_upgrade(self,
231
# this should perhaps be in the TextUIFactory and the default can do
233
trace.warning("%s is deprecated "
234
"and a better format is available.\n"
235
"It is recommended that you upgrade by "
236
"running the command\n"
359
241
def report_transport_activity(self, transport, byte_count, direction):
360
242
"""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
249
def show_error(self, msg):
399
250
"""Show an error message (not an exception) to the user.
401
252
The message should not have an error prefix or trailing newline. That
402
will be added by the factory if appropriate.
253
will be added by the factory if appropriate.
404
255
raise NotImplementedError(self.show_error)
463
302
def __repr__(self):
464
303
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
305
def get_boolean(self, prompt):
470
306
return self.responses.pop(0)
472
308
def get_integer(self, prompt):
473
309
return self.responses.pop(0)
475
def get_password(self, prompt=u'', **kwargs):
311
def get_password(self, prompt='', **kwargs):
476
312
return self.responses.pop(0)
478
314
def get_username(self, prompt, **kwargs):