229
class CLIUIFactory(UIFactory):
230
"""Deprecated in favor of TextUIFactory."""
232
@deprecated_method(deprecated_in((1, 18, 0)))
233
def __init__(self, stdin=None, stdout=None, stderr=None):
234
UIFactory.__init__(self)
235
self.stdin = stdin or sys.stdin
236
self.stdout = stdout or sys.stdout
237
self.stderr = stderr or sys.stderr
239
_accepted_boolean_strings = dict(y=True, n=False, yes=True, no=False)
241
def get_boolean(self, prompt):
243
self.prompt(prompt + "? [y/n]: ")
244
line = self.stdin.readline()
245
line = line.rstrip('\n')
246
val = bool_from_string(line, self._accepted_boolean_strings)
250
def get_non_echoed_password(self):
251
isatty = getattr(self.stdin, 'isatty', None)
252
if isatty is not None and isatty():
253
# getpass() ensure the password is not echoed and other
254
# cross-platform niceties
255
password = getpass.getpass('')
257
# echo doesn't make sense without a terminal
258
password = self.stdin.readline()
261
elif password[-1] == '\n':
262
password = password[:-1]
265
def get_password(self, prompt='', **kwargs):
266
"""Prompt the user for a password.
268
:param prompt: The prompt to present the user
269
:param kwargs: Arguments which will be expanded into the prompt.
270
This lets front ends display different things if
272
:return: The password string, return None if the user
273
canceled the request.
276
self.prompt(prompt, **kwargs)
277
# There's currently no way to say 'i decline to enter a password'
278
# as opposed to 'my password is empty' -- does it matter?
279
return self.get_non_echoed_password()
281
def get_username(self, prompt, **kwargs):
282
"""Prompt the user for a username.
284
:param prompt: The prompt to present the user
285
:param kwargs: Arguments which will be expanded into the prompt.
286
This lets front ends display different things if
288
:return: The username string, return None if the user
289
canceled the request.
292
self.prompt(prompt, **kwargs)
293
username = self.stdin.readline()
296
elif username[-1] == '\n':
297
username = username[:-1]
300
def prompt(self, prompt, **kwargs):
301
"""Emit prompt on the CLI.
303
:param kwargs: Dictionary of arguments to insert into the prompt,
304
to allow UIs to reformat the prompt.
307
# See <https://launchpad.net/bugs/365891>
308
prompt = prompt % kwargs
309
prompt = prompt.encode(osutils.get_terminal_encoding(), 'replace')
311
self.stderr.write(prompt)
314
"""Write an already-formatted message."""
315
self.stdout.write(msg + '\n')
318
229
class SilentUIFactory(UIFactory):
319
230
"""A UI Factory which never prints anything.