1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005, 2006 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
28
28
displays no output.
35
import bzrlib.progress
38
class TextUIFactory(object):
39
def progress_bar(self):
41
# this in turn is abstract, and creates either a tty or dots
42
# bar depending on what we think of the terminal
43
return bzrlib.progress.ProgressBar()
46
class SilentUIFactory(object):
47
def progress_bar(self):
48
return bzrlib.progress.DummyProgress()
33
from bzrlib.lazy_import import lazy_import
34
lazy_import(globals(), """
42
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
45
class UIFactory(object):
48
This tells the library how to display things to the user. Through this
49
layer different applications can choose the style of UI.
53
super(UIFactory, self).__init__()
54
self._progress_bar_stack = None
56
@deprecated_method(zero_eight)
57
def progress_bar(self):
58
"""See UIFactory.nested_progress_bar()."""
59
raise NotImplementedError(self.progress_bar)
61
def get_password(self, prompt='', **kwargs):
62
"""Prompt the user for a password.
64
:param prompt: The prompt to present the user
65
:param kwargs: Arguments which will be expanded into the prompt.
66
This lets front ends display different things if
69
:return: The password string, return None if the user canceled the
70
request. Note that we do not touch the encoding, users may
71
have whatever they see fit and the password should be
74
raise NotImplementedError(self.get_password)
76
def nested_progress_bar(self):
77
"""Return a nested progress bar.
79
When the bar has been finished with, it should be released by calling
82
raise NotImplementedError(self.nested_progress_bar)
85
"""Prepare the terminal for output.
87
This will, for example, clear text progress bars, and leave the
88
cursor at the leftmost position."""
89
raise NotImplementedError(self.clear_term)
91
def get_boolean(self, prompt):
92
"""Get a boolean question answered from the user.
94
:param prompt: a message to prompt the user with. Should be a single
95
line without terminating \n.
96
:return: True or False for y/yes or n/no.
98
raise NotImplementedError(self.get_boolean)
101
class CLIUIFactory(UIFactory):
102
"""Common behaviour for command line UI factories."""
105
super(CLIUIFactory, self).__init__()
106
self.stdin = sys.stdin
108
def get_boolean(self, prompt):
110
# FIXME: make a regexp and handle case variations as well.
112
self.prompt(prompt + "? [y/n]: ")
113
line = self.stdin.readline()
114
if line in ('y\n', 'yes\n'):
116
if line in ('n\n', 'no\n'):
119
def get_non_echoed_password(self, prompt):
120
return getpass.getpass(prompt)
122
def get_password(self, prompt='', **kwargs):
123
"""Prompt the user for a password.
125
:param prompt: The prompt to present the user
126
:param kwargs: Arguments which will be expanded into the prompt.
127
This lets front ends display different things if
129
:return: The password string, return None if the user
130
canceled the request.
133
prompt = (prompt % kwargs).encode(sys.stdout.encoding, 'replace')
134
# There's currently no way to say 'i decline to enter a password'
135
# as opposed to 'my password is empty' -- does it matter?
136
return self.get_non_echoed_password(prompt)
138
def prompt(self, prompt):
139
"""Emit prompt on the CLI."""
142
class SilentUIFactory(CLIUIFactory):
143
"""A UI Factory which never prints anything.
145
This is the default UI, if another one is never registered.
148
@deprecated_method(zero_eight)
149
def progress_bar(self):
150
"""See UIFactory.nested_progress_bar()."""
151
return progress.DummyProgress()
153
def get_password(self, prompt='', **kwargs):
156
def nested_progress_bar(self):
157
if self._progress_bar_stack is None:
158
self._progress_bar_stack = progress.ProgressBarStack(
159
klass=progress.DummyProgress)
160
return self._progress_bar_stack.get_nested()
162
def clear_term(self):
166
def clear_decorator(func, *args, **kwargs):
167
"""Decorator that clears the term"""
168
ui_factory.clear_term()
169
func(*args, **kwargs)
51
172
ui_factory = SilentUIFactory()
173
"""IMPORTANT: never import this symbol directly. ONLY ever access it as