1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
This tells the library how to display things to the user. Through this
22
layer different applications can choose the style of UI.
24
At the moment this layer is almost trivial: the application can just
25
choose the style of progress bar.
27
Set the ui_factory member to define the behaviour. The default
33
from bzrlib.lazy_import import lazy_import
34
lazy_import(globals(), """
43
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
46
class UIFactory(object):
49
This tells the library how to display things to the user. Through this
50
layer different applications can choose the style of UI.
54
super(UIFactory, self).__init__()
55
self._progress_bar_stack = None
57
@deprecated_method(zero_eight)
58
def progress_bar(self):
59
"""See UIFactory.nested_progress_bar()."""
60
raise NotImplementedError(self.progress_bar)
62
def get_password(self, prompt='', **kwargs):
63
"""Prompt the user for a password.
65
:param prompt: The prompt to present the user
66
:param kwargs: Arguments which will be expanded into the prompt.
67
This lets front ends display different things if
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
75
raise NotImplementedError(self.get_password)
77
def nested_progress_bar(self):
78
"""Return a nested progress bar.
80
When the bar has been finished with, it should be released by calling
83
raise NotImplementedError(self.nested_progress_bar)
86
"""Prepare the terminal for output.
88
This will, for example, clear text progress bars, and leave the
89
cursor at the leftmost position."""
90
raise NotImplementedError(self.clear_term)
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):
157
"""A UI Factory which never prints anything.
159
This is the default UI, if another one is never registered.
162
@deprecated_method(zero_eight)
163
def progress_bar(self):
164
"""See UIFactory.nested_progress_bar()."""
165
return progress.DummyProgress()
167
def get_password(self, prompt='', **kwargs):
170
def nested_progress_bar(self):
171
if self._progress_bar_stack is None:
172
self._progress_bar_stack = progress.ProgressBarStack(
173
klass=progress.DummyProgress)
174
return self._progress_bar_stack.get_nested()
176
def clear_term(self):
179
def recommend_upgrade(self, *args):
183
def clear_decorator(func, *args, **kwargs):
184
"""Decorator that clears the term"""
185
ui_factory.clear_term()
186
func(*args, **kwargs)
189
ui_factory = SilentUIFactory()
190
"""IMPORTANT: never import this symbol directly. ONLY ever access it as