2095.4.1
by Martin Pool
Better progress bars during tests |
1 |
# Copyright (C) 2005, 2006 Canonical Ltd
|
1887.1.1
by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines, |
2 |
#
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
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.
|
|
1887.1.1
by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines, |
7 |
#
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
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.
|
|
1887.1.1
by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines, |
12 |
#
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
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
|
|
16 |
||
17 |
||
18 |
||
19 |
"""UI abstraction.
|
|
20 |
||
21 |
This tells the library how to display things to the user. Through this
|
|
22 |
layer different applications can choose the style of UI.
|
|
23 |
||
24 |
At the moment this layer is almost trivial: the application can just
|
|
25 |
choose the style of progress bar.
|
|
26 |
||
27 |
Set the ui_factory member to define the behaviour. The default
|
|
28 |
displays no output.
|
|
29 |
"""
|
|
30 |
||
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
31 |
import sys |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
32 |
|
1996.3.32
by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily |
33 |
from bzrlib.lazy_import import lazy_import |
34 |
lazy_import(globals(), """ |
|
35 |
from bzrlib import (
|
|
36 |
progress,
|
|
37 |
)
|
|
38 |
""") |
|
39 |
||
1773.4.1
by Martin Pool
Add pyflakes makefile target; fix many warnings |
40 |
from bzrlib.symbol_versioning import (deprecated_method, zero_eight) |
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
41 |
|
42 |
||
1185.49.21
by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms. |
43 |
class UIFactory(object): |
44 |
"""UI abstraction.
|
|
45 |
||
46 |
This tells the library how to display things to the user. Through this
|
|
47 |
layer different applications can choose the style of UI.
|
|
48 |
"""
|
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
49 |
|
1594.1.3
by Robert Collins
Fixup pb usage to use nested_progress_bar. |
50 |
def __init__(self): |
51 |
super(UIFactory, self).__init__() |
|
52 |
self._progress_bar_stack = None |
|
53 |
||
1594.1.2
by Robert Collins
Update news and deprecated the old progress bar api. |
54 |
@deprecated_method(zero_eight) |
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
55 |
def progress_bar(self): |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
56 |
"""See UIFactory.nested_progress_bar()."""
|
57 |
raise NotImplementedError(self.progress_bar) |
|
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
58 |
|
59 |
def get_password(self, prompt='', **kwargs): |
|
60 |
"""Prompt the user for a password.
|
|
61 |
||
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
|
|
65 |
they so choose.
|
|
66 |
:return: The password string, return None if the user
|
|
67 |
canceled the request.
|
|
68 |
"""
|
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
69 |
raise NotImplementedError(self.get_password) |
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
70 |
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
71 |
def nested_progress_bar(self): |
72 |
"""Return a nested progress bar.
|
|
73 |
||
2095.4.5
by mbp at sourcefrog
Use regular progress-bar classes, not a special mechanism |
74 |
When the bar has been finished with, it should be released by calling
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
75 |
bar.finished().
|
76 |
"""
|
|
77 |
raise NotImplementedError(self.nested_progress_bar) |
|
78 |
||
1558.8.1
by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning' |
79 |
def clear_term(self): |
80 |
"""Prepare the terminal for output.
|
|
81 |
||
82 |
This will, for example, clear text progress bars, and leave the
|
|
83 |
cursor at the leftmost position."""
|
|
84 |
raise NotImplementedError(self.clear_term) |
|
85 |
||
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
86 |
def get_boolean(self, prompt): |
87 |
"""Get a boolean question answered from the user.
|
|
88 |
||
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.
|
|
92 |
"""
|
|
93 |
raise NotImplementedError(self.get_boolean) |
|
94 |
||
95 |
||
96 |
class CLIUIFactory(UIFactory): |
|
97 |
"""Common behaviour for command line UI factories."""
|
|
98 |
||
99 |
def __init__(self): |
|
100 |
super(CLIUIFactory, self).__init__() |
|
101 |
self.stdin = sys.stdin |
|
102 |
||
103 |
def get_boolean(self, prompt): |
|
104 |
self.clear_term() |
|
105 |
# FIXME: make a regexp and handle case variations as well.
|
|
106 |
while True: |
|
107 |
self.prompt(prompt) |
|
108 |
line = self.stdin.readline() |
|
109 |
if line in ('y\n', 'yes\n'): |
|
110 |
return True |
|
111 |
if line in ('n\n', 'no\n'): |
|
112 |
return False |
|
113 |
||
114 |
def prompt(self, prompt): |
|
115 |
"""Emit prompt on the CLI."""
|
|
116 |
||
117 |
||
118 |
class SilentUIFactory(CLIUIFactory): |
|
1185.49.21
by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms. |
119 |
"""A UI Factory which never prints anything.
|
120 |
||
121 |
This is the default UI, if another one is never registered.
|
|
122 |
"""
|
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
123 |
|
1594.1.2
by Robert Collins
Update news and deprecated the old progress bar api. |
124 |
@deprecated_method(zero_eight) |
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
125 |
def progress_bar(self): |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
126 |
"""See UIFactory.nested_progress_bar()."""
|
1996.3.32
by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily |
127 |
return progress.DummyProgress() |
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
128 |
|
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
129 |
def get_password(self, prompt='', **kwargs): |
130 |
return None |
|
131 |
||
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
132 |
def nested_progress_bar(self): |
1594.1.3
by Robert Collins
Fixup pb usage to use nested_progress_bar. |
133 |
if self._progress_bar_stack is None: |
1996.3.32
by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily |
134 |
self._progress_bar_stack = progress.ProgressBarStack( |
135 |
klass=progress.DummyProgress) |
|
1594.1.3
by Robert Collins
Fixup pb usage to use nested_progress_bar. |
136 |
return self._progress_bar_stack.get_nested() |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
137 |
|
1558.8.1
by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning' |
138 |
def clear_term(self): |
139 |
pass
|
|
140 |
||
141 |
||
142 |
def clear_decorator(func, *args, **kwargs): |
|
143 |
"""Decorator that clears the term"""
|
|
144 |
ui_factory.clear_term() |
|
145 |
func(*args, **kwargs) |
|
146 |
||
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
147 |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
148 |
ui_factory = SilentUIFactory() |
1534.5.9
by Robert Collins
Advise users running upgrade on a checkout to also run it on the branch. |
149 |
"""IMPORTANT: never import this symbol directly. ONLY ever access it as
|
150 |
ui.ui_factory."""
|