3260.2.1
by Alexander Belchenko
Don't ask a password if there is no real terminal. (#69851) |
1 |
# Copyright (C) 2005, 2006, 2007, 2008 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 |
"""UI abstraction.
|
|
18 |
||
19 |
This tells the library how to display things to the user. Through this
|
|
20 |
layer different applications can choose the style of UI.
|
|
21 |
||
22 |
At the moment this layer is almost trivial: the application can just
|
|
23 |
choose the style of progress bar.
|
|
24 |
||
25 |
Set the ui_factory member to define the behaviour. The default
|
|
26 |
displays no output.
|
|
27 |
"""
|
|
28 |
||
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
29 |
import sys |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
30 |
|
1996.3.32
by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily |
31 |
from bzrlib.lazy_import import lazy_import |
32 |
lazy_import(globals(), """ |
|
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
33 |
import getpass
|
34 |
||
1996.3.32
by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily |
35 |
from bzrlib import (
|
3260.2.1
by Alexander Belchenko
Don't ask a password if there is no real terminal. (#69851) |
36 |
errors,
|
2461.1.2
by Vincent Ladeuil
Take jam's remark into account. |
37 |
osutils,
|
1996.3.32
by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily |
38 |
progress,
|
2323.6.2
by Martin Pool
Move responsibility for suggesting upgrades to ui object |
39 |
trace,
|
1996.3.32
by John Arbash Meinel
from bzrlib.ui lazy import progress, and make progress import lazily |
40 |
)
|
41 |
""") |
|
42 |
||
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
43 |
|
1185.49.21
by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms. |
44 |
class UIFactory(object): |
45 |
"""UI abstraction.
|
|
46 |
||
47 |
This tells the library how to display things to the user. Through this
|
|
48 |
layer different applications can choose the style of UI.
|
|
49 |
"""
|
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
50 |
|
1594.1.3
by Robert Collins
Fixup pb usage to use nested_progress_bar. |
51 |
def __init__(self): |
52 |
super(UIFactory, self).__init__() |
|
53 |
self._progress_bar_stack = None |
|
54 |
||
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
55 |
def get_password(self, prompt='', **kwargs): |
56 |
"""Prompt the user for a password.
|
|
57 |
||
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
|
|
61 |
they so choose.
|
|
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
62 |
|
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
|
|
66 |
transported as is.
|
|
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
67 |
"""
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
68 |
raise NotImplementedError(self.get_password) |
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
69 |
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
70 |
def nested_progress_bar(self): |
71 |
"""Return a nested progress bar.
|
|
72 |
||
2095.4.5
by mbp at sourcefrog
Use regular progress-bar classes, not a special mechanism |
73 |
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. |
74 |
bar.finished().
|
75 |
"""
|
|
76 |
raise NotImplementedError(self.nested_progress_bar) |
|
77 |
||
1558.8.1
by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning' |
78 |
def clear_term(self): |
79 |
"""Prepare the terminal for output.
|
|
80 |
||
81 |
This will, for example, clear text progress bars, and leave the
|
|
82 |
cursor at the leftmost position."""
|
|
83 |
raise NotImplementedError(self.clear_term) |
|
84 |
||
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
85 |
def get_boolean(self, prompt): |
86 |
"""Get a boolean question answered from the user.
|
|
87 |
||
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.
|
|
91 |
"""
|
|
92 |
raise NotImplementedError(self.get_boolean) |
|
93 |
||
2323.6.2
by Martin Pool
Move responsibility for suggesting upgrades to ui object |
94 |
def recommend_upgrade(self, |
95 |
current_format_name, |
|
96 |
basedir): |
|
2323.6.4
by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which |
97 |
# this should perhaps be in the TextUIFactory and the default can do
|
98 |
# nothing
|
|
2323.6.2
by Martin Pool
Move responsibility for suggesting upgrades to ui object |
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" |
|
103 |
" bzr upgrade %s", |
|
104 |
current_format_name, |
|
105 |
basedir) |
|
2323.6.4
by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which |
106 |
|
2461.1.1
by Vincent Ladeuil
Fix 110204 by letting TestUIFactory encode password prompt. |
107 |
|
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
108 |
class CLIUIFactory(UIFactory): |
109 |
"""Common behaviour for command line UI factories."""
|
|
110 |
||
111 |
def __init__(self): |
|
112 |
super(CLIUIFactory, self).__init__() |
|
113 |
self.stdin = sys.stdin |
|
114 |
||
115 |
def get_boolean(self, prompt): |
|
116 |
self.clear_term() |
|
117 |
# FIXME: make a regexp and handle case variations as well.
|
|
118 |
while True: |
|
2294.4.1
by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests. |
119 |
self.prompt(prompt + "? [y/n]: ") |
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
120 |
line = self.stdin.readline() |
121 |
if line in ('y\n', 'yes\n'): |
|
122 |
return True |
|
123 |
if line in ('n\n', 'no\n'): |
|
124 |
return False |
|
125 |
||
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
126 |
def get_non_echoed_password(self, prompt): |
3260.2.1
by Alexander Belchenko
Don't ask a password if there is no real terminal. (#69851) |
127 |
if not sys.stdin.isatty(): |
128 |
raise errors.NotATerminal() |
|
2461.1.2
by Vincent Ladeuil
Take jam's remark into account. |
129 |
encoding = osutils.get_terminal_encoding() |
130 |
return getpass.getpass(prompt.encode(encoding, 'replace')) |
|
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
131 |
|
132 |
def get_password(self, prompt='', **kwargs): |
|
133 |
"""Prompt the user for a password.
|
|
134 |
||
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
|
|
138 |
they so choose.
|
|
139 |
:return: The password string, return None if the user
|
|
140 |
canceled the request.
|
|
141 |
"""
|
|
142 |
prompt += ': ' |
|
2461.1.1
by Vincent Ladeuil
Fix 110204 by letting TestUIFactory encode password prompt. |
143 |
prompt = (prompt % kwargs) |
2294.4.4
by Vincent Ladeuil
Provide a better implementation for testing passwords. |
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) |
|
147 |
||
1687.1.4
by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean(). |
148 |
def prompt(self, prompt): |
149 |
"""Emit prompt on the CLI."""
|
|
150 |
||
151 |
||
152 |
class SilentUIFactory(CLIUIFactory): |
|
1185.49.21
by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms. |
153 |
"""A UI Factory which never prints anything.
|
154 |
||
155 |
This is the default UI, if another one is never registered.
|
|
156 |
"""
|
|
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
157 |
|
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
158 |
def get_password(self, prompt='', **kwargs): |
159 |
return None |
|
160 |
||
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
161 |
def nested_progress_bar(self): |
1594.1.3
by Robert Collins
Fixup pb usage to use nested_progress_bar. |
162 |
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 |
163 |
self._progress_bar_stack = progress.ProgressBarStack( |
164 |
klass=progress.DummyProgress) |
|
1594.1.3
by Robert Collins
Fixup pb usage to use nested_progress_bar. |
165 |
return self._progress_bar_stack.get_nested() |
1594.1.1
by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar. |
166 |
|
1558.8.1
by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning' |
167 |
def clear_term(self): |
168 |
pass
|
|
169 |
||
2323.6.4
by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which |
170 |
def recommend_upgrade(self, *args): |
171 |
pass
|
|
172 |
||
1558.8.1
by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning' |
173 |
|
174 |
def clear_decorator(func, *args, **kwargs): |
|
175 |
"""Decorator that clears the term"""
|
|
176 |
ui_factory.clear_term() |
|
177 |
func(*args, **kwargs) |
|
178 |
||
1185.49.22
by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py |
179 |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
180 |
ui_factory = SilentUIFactory() |
1534.5.9
by Robert Collins
Advise users running upgrade on a checkout to also run it on the branch. |
181 |
"""IMPORTANT: never import this symbol directly. ONLY ever access it as
|
182 |
ui.ui_factory."""
|