1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
1
# Copyright (C) 2005 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
21
This tells the library how to display things to the user. Through this
59
56
:param kwargs: Arguments which will be expanded into the prompt.
60
57
This lets front ends display different things if
63
:return: The password string, return None if the user canceled the
64
request. Note that we do not touch the encoding, users may
65
have whatever they see fit and the password should be
59
:return: The password string, return None if the user
68
62
raise NotImplementedError(self.get_password)
70
64
def nested_progress_bar(self):
71
65
"""Return a nested progress bar.
73
When the bar has been finished with, it should be released by calling
67
When the bar has been finished with, it should be released bu calling
76
70
raise NotImplementedError(self.nested_progress_bar)
82
76
cursor at the leftmost position."""
83
77
raise NotImplementedError(self.clear_term)
85
def get_boolean(self, prompt):
86
"""Get a boolean question answered from the user.
88
:param prompt: a message to prompt the user with. Should be a single
89
line without terminating \n.
90
:return: True or False for y/yes or n/no.
92
raise NotImplementedError(self.get_boolean)
94
def recommend_upgrade(self,
97
# this should perhaps be in the TextUIFactory and the default can do
99
trace.warning("%s is deprecated "
100
"and a better format is available.\n"
101
"It is recommended that you upgrade by "
102
"running the command\n"
108
class CLIUIFactory(UIFactory):
109
"""Common behaviour for command line UI factories."""
112
super(CLIUIFactory, self).__init__()
113
self.stdin = sys.stdin
115
def get_boolean(self, prompt):
117
# FIXME: make a regexp and handle case variations as well.
119
self.prompt(prompt + "? [y/n]: ")
120
line = self.stdin.readline()
121
if line in ('y\n', 'yes\n'):
123
if line in ('n\n', 'no\n'):
126
def get_non_echoed_password(self, prompt):
127
if not sys.stdin.isatty():
128
raise errors.NotATerminal()
129
encoding = osutils.get_terminal_encoding()
130
return getpass.getpass(prompt.encode(encoding, 'replace'))
132
def get_password(self, prompt='', **kwargs):
133
"""Prompt the user for a password.
135
:param prompt: The prompt to present the user
136
:param kwargs: Arguments which will be expanded into the prompt.
137
This lets front ends display different things if
139
:return: The password string, return None if the user
140
canceled the request.
143
prompt = (prompt % kwargs)
144
# There's currently no way to say 'i decline to enter a password'
145
# as opposed to 'my password is empty' -- does it matter?
146
return self.get_non_echoed_password(prompt)
148
def prompt(self, prompt):
149
"""Emit prompt on the CLI."""
152
class SilentUIFactory(CLIUIFactory):
80
class SilentUIFactory(UIFactory):
153
81
"""A UI Factory which never prints anything.
155
83
This is the default UI, if another one is never registered.
86
@deprecated_method(zero_eight)
87
def progress_bar(self):
88
"""See UIFactory.nested_progress_bar()."""
89
return bzrlib.progress.DummyProgress()
158
91
def get_password(self, prompt='', **kwargs):
161
94
def nested_progress_bar(self):
162
95
if self._progress_bar_stack is None:
163
self._progress_bar_stack = progress.ProgressBarStack(
164
klass=progress.DummyProgress)
96
self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
97
klass=bzrlib.progress.DummyProgress)
165
98
return self._progress_bar_stack.get_nested()
167
100
def clear_term(self):
170
def recommend_upgrade(self, *args):
174
104
def clear_decorator(func, *args, **kwargs):
175
105
"""Decorator that clears the term"""