-
Notifications
You must be signed in to change notification settings - Fork 348
/
tuiobject.py
310 lines (238 loc) · 10.9 KB
/
tuiobject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# base TUIObject for Anaconda TUI
#
# Copyright (C) 2012 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
from pyanaconda.ui import common
from pyanaconda.core import util, constants
from pyanaconda import input_checking
from pyanaconda.core.i18n import _
from pyanaconda.core.users import crypt_password
from simpleline.render.adv_widgets import ErrorDialog, GetInputScreen, GetPasswordInputScreen, YesNoDialog
from simpleline.render.screen import UIScreen, Prompt
from simpleline.render.screen_handler import ScreenHandler
def reporting_callback(message):
"""Callback used for general reporting from acceptance conditions.
See: `AskUserInput` and `AskPassword` classes.
"""
print(_(message))
def report_if_failed(message):
"""Decorator function to call reporting function on failed condition.
:param message: Error message which will be printed if condition function fails.
:type message: str
"""
def outer_wrapper(f):
def wrapper(*args):
if not f(*args):
report_func = args[-1]
report_func(message)
return False
return True
return wrapper
return outer_wrapper
def report_check_func():
"""Decorator function to report message from condition function.
Condition function returns the (success, error_message) tuple.
If return_code is False send error_message to report_func and return False otherwise True.
"""
def outer_wrapper(f):
def wrapper(*args):
ret, error_message = f(*args)
if ret:
return True
if error_message:
report_func = args[-1]
report_func(error_message)
return False
return wrapper
return outer_wrapper
class IpmiErrorDialog(ErrorDialog):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# This dialog is run by the error handler. The handler
# might be triggered by an error from a different thread.
# It is possible that we are already asking for a user
# input when we decide to show the dialog. That would
# violate the concurrency check, so disable it.
self.input_manager.skip_concurrency_check = True
def input(self, args, key):
"""Call IPMI ABORTED. Everything else will be done by original implementation."""
util.ipmi_report(constants.IPMI_ABORTED)
super().input(args, key)
class TUIObject(UIScreen, common.UIObject):
"""Base class for Anaconda specific TUI screens. Implements the
common pyanaconda.ui.common.UIObject interface"""
def __init__(self, data):
UIScreen.__init__(self)
common.UIObject.__init__(self, data)
self.title = "Default title"
@property
def showable(self):
return True
def refresh(self, args=None):
"""Put everything to display into self.window list."""
UIScreen.refresh(self, args)
class Dialog(object):
def __init__(self, title, conditions=None, report_func=reporting_callback):
"""Get all required information and ask user for input.
You can use this class by itself by calling the `run()` method or in a container.
When using in a container create this class with all required parameters and call the
`add_to_container()` method.
Also set user prompt to Anaconda's default.
:param title: Name of the item which user is setting.
:type title: str
:param conditions: Optional acceptance conditions. If condition is not valid the
`wrong_input_message` is printed and user must set correct value.
:type conditions: A function func(user_input, report_func) -> bool taking user input and returning bool.
See `report_func` parameter for report_func specification.
:param report_func: Function for printing errors and warnings from conditions.
:type report_func: Function func(message) -- taking one argument message.
"""
super().__init__()
self._title = title
self._conditions = conditions
self._report_func = report_func
self._user_prompt = _("Enter a new value for '%(title)s' and press %(enter)s") % {
# TRANSLATORS: 'title' as a title of the entry
"title": title,
# TRANSLATORS: 'enter' as the key ENTER
"enter": Prompt.ENTER
}
self._no_separator = False
@property
def title(self):
"""Title of the item we want to get user input for.
:returns: Name of the item.
:rtype: str.
"""
return self._title
@title.setter
def title(self, title):
"""Set title of the item we want to get user input for.
:param title: Item title.
:type title: str.
"""
self._title = title
@property
def no_separator(self):
"""Print separator or hide user input as a actual screen part?
:returns: False if separator should be printed. True if not.
:rtype: bool. Default: False
"""
return self._no_separator
@no_separator.setter
def no_separator(self, no_separator):
"""Should the separator be printed?
:param no_separator: True to disable separator, False otherwise.
:type no_separator: bool.
"""
self._no_separator = no_separator
def run(self):
"""Get input from user, run the condition functions and call setter callback at the end.
Repeat asking user for input to the time when all the acceptance conditions will be satisfied.
"""
screen = GetInputScreen(self._user_prompt)
if self._conditions:
for c in self._conditions:
screen.add_acceptance_condition(c, self._report_func)
screen.no_separator = self._no_separator
ScreenHandler.push_screen_modal(screen)
return screen.value
class PasswordDialog(Dialog):
"""Ask for user password and process it."""
def __init__(self, title, policy_name,
report_func=reporting_callback,
process_func=crypt_password,
secret_type=constants.SecretType.PASSWORD,
message=None):
super().__init__(title, report_func=report_func)
self._no_separator = False
self._policy = input_checking.get_policy(policy_name)
self._secret_type = secret_type
self._process_password = process_func
self._dialog_message = message
self._username = ""
@property
def username(self):
return self._username
@username.setter
def username(self, new_username):
self._username = new_username
def run(self):
"""Get password input from user and call setter callback at the end.
Repeat asking user for input to the time when all the password conditions will be satisfied.
"""
password = self._ask_pass_modal(self._get_password_prompt(), self._no_separator)
confirm = self._ask_pass_modal(self._get_confim_prompt(), True)
return self._validate_password(password, confirm)
def _get_password_prompt(self):
if not self._dialog_message:
return self._title
return "{}\n\n{}".format(self._dialog_message, self._title)
def _get_confim_prompt(self):
return "{} (confirm)".format(self._title)
def _ask_pass_modal(self, prompt, no_separator):
pass_screen = GetPasswordInputScreen(prompt)
pass_screen.no_separator = no_separator
ScreenHandler.push_screen_modal(pass_screen)
return pass_screen.value
def _validate_password(self, password, confirm):
"""Validate and process user password."""
if password != confirm:
self._report(_(constants.SECRET_CONFIRM_ERROR_TUI[self._secret_type]))
return None
# If an empty password was provided, unset the value
if not password:
return ""
# prepare a password validation request
password_check_request = input_checking.PasswordCheckRequest()
password_check_request.password = password
password_check_request.password_confirmation = ""
password_check_request.policy = self._policy
# configure username for checking
password_check_request.username = self.username
# validate the password
password_check = input_checking.PasswordValidityCheck()
password_check.run(password_check_request)
# if the score is equal to 0 and we have an error message set
if not password_check.result.password_score and password_check.result.error_message:
self._report(password_check.result.error_message)
return None
if password_check.result.password_quality < self._policy.min_quality:
if self._policy.is_strict:
done_msg = ""
else:
done_msg = _("\nWould you like to use it anyway?")
if password_check.result.error_message:
weak_prefix = _(constants.SECRET_WEAK_WITH_ERROR[self._secret_type])
error = "{} {} {}".format(weak_prefix, password_check.result.error_message, done_msg)
else:
weak_prefix = _(constants.SECRET_WEAK[self._secret_type])
error = "{} {}".format(weak_prefix, done_msg)
if not self._policy.is_strict:
question_window = YesNoDialog(error)
ScreenHandler.push_screen_modal(question_window)
if not question_window.answer:
return None
else:
self._report(error)
return None
if any(char not in constants.PW_ASCII_CHARS for char in password):
self._report(_(constants.SECRET_ASCII[self._secret_type]))
return self._process_password(password)
def _report(self, message):
if self._report_func:
self._report_func(message)