273
282
'CMD_IGNORE_ENTRY': _('(I)gnore')
276
def UI_PromptUser(q, params=''):
279
if UI_mode == 'text':
280
cmd, arg = Text_PromptUser(q)
284
ypath, yarg = GetDataFromYast()
287
arg = yarg['selected']
288
if cmd == 'CMD_ABORT':
290
cmd = 'XXXINVALIDXXX'
285
class PromptQuestion(object):
296
self.headers = list()
297
self.functions = list()
300
def promptUser(self, params=''):
303
if UI_mode == 'text':
304
cmd, arg = self.Text_PromptUser()
308
ypath, yarg = GetDataFromYast()
311
arg = yarg['selected']
312
if cmd == 'CMD_ABORT':
314
cmd = 'XXXINVALIDXXX'
317
def Text_PromptUser(self):
319
explanation = self.explanation
320
headers = self.headers
321
functions = self.functions
323
default = self.default
324
options = self.options
325
selected = self.selected
326
helptext = self.helptext
329
functions.append('CMD_HELP')
334
for cmd in functions:
335
if not CMDS.get(cmd, False):
336
raise AppArmorException(_('PromptUser: Unknown command %s') % cmd)
340
key = get_translated_hotkey(menutext).lower()
342
if keys.get(key, False):
343
raise AppArmorException(_('PromptUser: Duplicate hotkey for %(command)s: %(menutext)s ') % { 'command': cmd, 'menutext': menutext })
347
if default and default == cmd:
348
menutext = '[%s]' % menutext
350
menu_items.append(menutext)
353
if default and CMDS[default]:
354
defaulttext = CMDS[default]
355
defmsg = _('PromptUser: Invalid hotkey in default item')
357
default_key = get_translated_hotkey(defaulttext, defmsg).lower()
359
if not keys.get(default_key, False):
360
raise AppArmorException(_('PromptUser: Invalid default %s') % default)
363
header_copy = headers[:]
365
header = header_copy.pop(0)
367
if len(header) > widest:
371
formatstr = '%-' + str(widest) + 's %s\n'
373
function_regexp = '^('
374
function_regexp += '|'.join(keys.keys())
376
function_regexp += '|\d'
377
function_regexp += ')$'
379
ans = 'XXXINVALIDXXX'
380
while not re.search(function_regexp, ans, flags=re.IGNORECASE):
384
prompt += '= %s =\n\n' % title
387
header_copy = headers[:]
389
header = header_copy.pop(0)
390
value = header_copy.pop(0)
391
prompt += formatstr % (header + ':', value)
395
prompt += explanation + '\n\n'
398
for index, option in enumerate(options):
399
if selected == index:
400
format_option = ' [%s - %s]'
402
format_option = ' %s - %s '
403
prompt += format_option % (index + 1, option)
406
prompt += ' / '.join(menu_items)
408
sys.stdout.write(prompt + '\n')
410
ans = getkey().lower()
414
if options and selected > 0:
416
ans = 'XXXINVALIDXXX'
419
if options and selected < len(options) - 1:
421
ans = 'XXXINVALIDXXX'
423
# elif keys.get(ans, False) == 'CMD_HELP':
424
# sys.stdout.write('\n%s\n' %helptext)
425
# ans = 'XXXINVALIDXXX'
427
elif is_number(ans) == 10:
428
# If they hit return choose default option
431
elif options and re.search('^\d$', ans):
433
if ans > 0 and ans <= len(options):
435
ans = 'XXXINVALIDXXX'
437
if keys.get(ans, False) == 'CMD_HELP':
438
sys.stdout.write('\n%s\n' % helptext)
441
if keys.get(ans, False):
293
446
def confirm_and_abort():
294
447
ans = UI_YesNo(_('Are you sure you want to abandon this set of profile changes and exit?'), 'n')
314
467
ypath, yarg = GetDataFromYast()
317
def Text_PromptUser(question):
318
title = question['title']
319
explanation = question['explanation']
320
headers = question['headers']
321
functions = question['functions']
323
default = question['default']
324
options = question['options']
325
selected = question.get('selected', 0)
326
helptext = question['helptext']
328
functions.append('CMD_HELP')
333
for cmd in functions:
334
if not CMDS.get(cmd, False):
335
raise AppArmorException(_('PromptUser: Unknown command %s') % cmd)
339
key = get_translated_hotkey(menutext).lower()
341
if keys.get(key, False):
342
raise AppArmorException(_('PromptUser: Duplicate hotkey for %s: %s ') % (cmd, menutext))
346
if default and default == cmd:
347
menutext = '[%s]' % menutext
349
menu_items.append(menutext)
352
if default and CMDS[default]:
353
defaulttext = CMDS[default]
354
defmsg = _('PromptUser: Invalid hotkey in default item')
356
default_key = get_translated_hotkey(defaulttext, defmsg).lower()
358
if not keys.get(default_key, False):
359
raise AppArmorException(_('PromptUser: Invalid default %s') % default)
362
header_copy = headers[:]
364
header = header_copy.pop(0)
366
if len(header) > widest:
370
formatstr = '%-' + str(widest) + 's %s\n'
372
function_regexp = '^('
373
function_regexp += '|'.join(keys.keys())
375
function_regexp += '|\d'
376
function_regexp += ')$'
378
ans = 'XXXINVALIDXXX'
379
while not re.search(function_regexp, ans, flags=re.IGNORECASE):
383
prompt += '= %s =\n\n' % title
386
header_copy = headers[:]
388
header = header_copy.pop(0)
389
value = header_copy.pop(0)
390
prompt += formatstr % (header + ':', value)
394
prompt += explanation + '\n\n'
397
for index, option in enumerate(options):
398
if selected == index:
399
format_option = ' [%s - %s]'
401
format_option = ' %s - %s '
402
prompt += format_option % (index + 1, option)
405
prompt += ' / '.join(menu_items)
407
sys.stdout.write(prompt + '\n')
409
ans = getkey().lower()
413
if options and selected > 0:
415
ans = 'XXXINVALIDXXX'
418
if options and selected < len(options) - 1:
420
ans = 'XXXINVALIDXXX'
422
# elif keys.get(ans, False) == 'CMD_HELP':
423
# sys.stdout.write('\n%s\n' %helptext)
424
# ans = 'XXXINVALIDXXX'
426
elif is_number(ans) == 10:
427
# If they hit return choose default option
430
elif options and re.search('^\d$', ans):
432
if ans > 0 and ans <= len(options):
434
ans = 'XXXINVALIDXXX'
436
if keys.get(ans, False) == 'CMD_HELP':
437
sys.stdout.write('\n%s\n' % helptext)
440
if keys.get(ans, False):
445
469
def is_number(number):
447
471
return int(number)