1
# Copyright (C) 2005, 2006, 2007, 2008 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
19
This tells the library how to display things to the user. Through this
20
layer different applications can choose the style of UI.
22
At the moment this layer is almost trivial: the application can just
23
choose the style of progress bar.
25
Set the ui_factory member to define the behaviour. The default
31
from bzrlib.lazy_import import lazy_import
32
lazy_import(globals(), """
44
class UIFactory(object):
47
This tells the library how to display things to the user. Through this
48
layer different applications can choose the style of UI.
52
super(UIFactory, self).__init__()
53
self._progress_bar_stack = None
55
def get_password(self, prompt='', **kwargs):
56
"""Prompt the user for a password.
58
:param prompt: The prompt to present the user
59
:param kwargs: Arguments which will be expanded into the prompt.
60
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
68
raise NotImplementedError(self.get_password)
70
def nested_progress_bar(self):
71
"""Return a nested progress bar.
73
When the bar has been finished with, it should be released by calling
76
raise NotImplementedError(self.nested_progress_bar)
79
"""Prepare the terminal for output.
81
This will, for example, clear text progress bars, and leave the
82
cursor at the leftmost position."""
83
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):
153
"""A UI Factory which never prints anything.
155
This is the default UI, if another one is never registered.
158
def get_password(self, prompt='', **kwargs):
161
def nested_progress_bar(self):
162
if self._progress_bar_stack is None:
163
self._progress_bar_stack = progress.ProgressBarStack(
164
klass=progress.DummyProgress)
165
return self._progress_bar_stack.get_nested()
167
def clear_term(self):
170
def recommend_upgrade(self, *args):
174
def clear_decorator(func, *args, **kwargs):
175
"""Decorator that clears the term"""
176
ui_factory.clear_term()
177
func(*args, **kwargs)
180
ui_factory = SilentUIFactory()
181
"""IMPORTANT: never import this symbol directly. ONLY ever access it as