1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 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
56
66
:param kwargs: Arguments which will be expanded into the prompt.
57
67
This lets front ends display different things if
59
:return: The password string, return None if the user
70
:return: The password string, return None if the user canceled the
71
request. Note that we do not touch the encoding, users may
72
have whatever they see fit and the password should be
62
75
raise NotImplementedError(self.get_password)
64
77
def nested_progress_bar(self):
65
78
"""Return a nested progress bar.
67
When the bar has been finished with, it should be released bu calling
80
When the bar has been finished with, it should be released by calling
70
83
raise NotImplementedError(self.nested_progress_bar)
76
89
cursor at the leftmost position."""
77
90
raise NotImplementedError(self.clear_term)
80
class SilentUIFactory(UIFactory):
92
def get_boolean(self, prompt):
93
"""Get a boolean question answered from the user.
95
:param prompt: a message to prompt the user with. Should be a single
96
line without terminating \n.
97
:return: True or False for y/yes or n/no.
99
raise NotImplementedError(self.get_boolean)
101
def recommend_upgrade(self,
104
# this should perhaps be in the TextUIFactory and the default can do
106
trace.warning("%s is deprecated "
107
"and a better format is available.\n"
108
"It is recommended that you upgrade by "
109
"running the command\n"
115
class CLIUIFactory(UIFactory):
116
"""Common behaviour for command line UI factories."""
119
super(CLIUIFactory, self).__init__()
120
self.stdin = sys.stdin
122
def get_boolean(self, prompt):
124
# FIXME: make a regexp and handle case variations as well.
126
self.prompt(prompt + "? [y/n]: ")
127
line = self.stdin.readline()
128
if line in ('y\n', 'yes\n'):
130
if line in ('n\n', 'no\n'):
133
def get_non_echoed_password(self, prompt):
134
return getpass.getpass(prompt)
136
def get_password(self, prompt='', **kwargs):
137
"""Prompt the user for a password.
139
:param prompt: The prompt to present the user
140
:param kwargs: Arguments which will be expanded into the prompt.
141
This lets front ends display different things if
143
:return: The password string, return None if the user
144
canceled the request.
147
prompt = (prompt % kwargs).encode(sys.stdout.encoding, 'replace')
148
# There's currently no way to say 'i decline to enter a password'
149
# as opposed to 'my password is empty' -- does it matter?
150
return self.get_non_echoed_password(prompt)
152
def prompt(self, prompt):
153
"""Emit prompt on the CLI."""
156
class SilentUIFactory(CLIUIFactory):
81
157
"""A UI Factory which never prints anything.
83
159
This is the default UI, if another one is never registered.
86
162
@deprecated_method(zero_eight)
87
163
def progress_bar(self):
88
164
"""See UIFactory.nested_progress_bar()."""
89
return bzrlib.progress.DummyProgress()
165
return progress.DummyProgress()
91
167
def get_password(self, prompt='', **kwargs):
94
170
def nested_progress_bar(self):
95
171
if self._progress_bar_stack is None:
96
self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
97
klass=bzrlib.progress.DummyProgress)
172
self._progress_bar_stack = progress.ProgressBarStack(
173
klass=progress.DummyProgress)
98
174
return self._progress_bar_stack.get_nested()
100
176
def clear_term(self):
179
def recommend_upgrade(self, *args):
104
183
def clear_decorator(func, *args, **kwargs):
105
184
"""Decorator that clears the term"""