1
# Copyright (C) 2005, 2006 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
28
28
displays no output.
33
from bzrlib.lazy_import import lazy_import
34
lazy_import(globals(), """
40
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
43
class UIFactory(object):
46
This tells the library how to display things to the user. Through this
47
layer different applications can choose the style of UI.
51
super(UIFactory, self).__init__()
52
self._progress_bar_stack = None
54
@deprecated_method(zero_eight)
55
def progress_bar(self):
56
"""See UIFactory.nested_progress_bar()."""
57
raise NotImplementedError(self.progress_bar)
59
def get_password(self, prompt='', **kwargs):
60
"""Prompt the user for a password.
62
:param prompt: The prompt to present the user
63
:param kwargs: Arguments which will be expanded into the prompt.
64
This lets front ends display different things if
66
:return: The password string, return None if the user
69
raise NotImplementedError(self.get_password)
71
def nested_progress_bar(self):
72
"""Return a nested progress bar.
74
When the bar has been finished with, it should be released bu calling
77
raise NotImplementedError(self.nested_progress_bar)
80
"""Prepare the terminal for output.
82
This will, for example, clear text progress bars, and leave the
83
cursor at the leftmost position."""
84
raise NotImplementedError(self.clear_term)
86
def get_boolean(self, prompt):
87
"""Get a boolean question answered from the user.
89
:param prompt: a message to prompt the user with. Should be a single
90
line without terminating \n.
91
:return: True or False for y/yes or n/no.
93
raise NotImplementedError(self.get_boolean)
95
def show_progress_line(self, msg):
96
"""Show one line of text that will be cleared."""
97
raise NotImplementedError(self.show_progress_line)
100
class CLIUIFactory(UIFactory):
101
"""Common behaviour for command line UI factories."""
104
super(CLIUIFactory, self).__init__()
105
self.stdin = sys.stdin
107
def get_boolean(self, prompt):
109
# FIXME: make a regexp and handle case variations as well.
112
line = self.stdin.readline()
113
if line in ('y\n', 'yes\n'):
115
if line in ('n\n', 'no\n'):
118
def prompt(self, prompt):
119
"""Emit prompt on the CLI."""
122
class SilentUIFactory(CLIUIFactory):
123
"""A UI Factory which never prints anything.
125
This is the default UI, if another one is never registered.
128
@deprecated_method(zero_eight)
129
def progress_bar(self):
130
"""See UIFactory.nested_progress_bar()."""
131
return progress.DummyProgress()
133
def get_password(self, prompt='', **kwargs):
136
def nested_progress_bar(self):
137
if self._progress_bar_stack is None:
138
self._progress_bar_stack = progress.ProgressBarStack(
139
klass=progress.DummyProgress)
140
return self._progress_bar_stack.get_nested()
142
def clear_term(self):
145
def show_progress_line(self, msg):
148
def message(self, msg):
152
def clear_decorator(func, *args, **kwargs):
153
"""Decorator that clears the term"""
154
ui_factory.clear_term()
155
func(*args, **kwargs)
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()
158
51
ui_factory = SilentUIFactory()
159
"""IMPORTANT: never import this symbol directly. ONLY ever access it as