Handle input range properly and catch KeyboardInterrupt and exit gracefully

433496
This commit is contained in:
Rob Crittenden 2008-02-20 09:32:25 -05:00
parent 3817577525
commit f82b3b0b28
3 changed files with 25 additions and 11 deletions

View File

@ -74,7 +74,10 @@ def main():
print "Please refine your search and try again." print "Please refine your search and try again."
if counter > 1: if counter > 1:
try:
groupindex = ipaadminutil.select_group(counter, groups) groupindex = ipaadminutil.select_group(counter, groups)
except KeyboardInterrupt:
return 1
if groupindex == "q": if groupindex == "q":
return 0 return 0

View File

@ -97,10 +97,12 @@ def main():
print "Please refine your search and try again." print "Please refine your search and try again."
if counter > 1: if counter > 1:
try:
userindex = ipaadminutil.select_user(counter, users) userindex = ipaadminutil.select_user(counter, users)
except KeyboardInterrupt:
return 1
if userindex == "q": if userindex == "q":
return return 0
if userindex >= 0: if userindex >= 0:
users = [users[userindex]] users = [users[userindex]]

View File

@ -24,6 +24,11 @@ import subprocess
import os import os
def select_user(counter, users): def select_user(counter, users):
"""counter is the number of User objects in users
users is a list of User objects
This purposely doesn't catch KeyboardInterrupt
"""
i = 1 i = 1
print "%s entries were found. Which one would you like to display?" % counter print "%s entries were found. Which one would you like to display?" % counter
for ent in users: for ent in users:
@ -35,12 +40,11 @@ def select_user(counter, users):
return "q" return "q"
if resp == "0": if resp == "0":
userindex = -1 userindex = -1
break; break
try: try:
userindex = int(resp) - 1 userindex = int(resp) - 1
if (userindex >= 0 and userindex <= counter): if (userindex >= 0 and userindex < counter):
break; break
break;
except: except:
# fall through to the error msg # fall through to the error msg
pass pass
@ -50,6 +54,11 @@ def select_user(counter, users):
return userindex return userindex
def select_group(counter, groups): def select_group(counter, groups):
"""counter is the number of Group objects in users
users is a list of Group objects
This purposely doesn't catch KeyboardInterrupt
"""
i = 1 i = 1
print "%s entries were found. Which one would you like to display?" % counter print "%s entries were found. Which one would you like to display?" % counter
for ent in groups: for ent in groups:
@ -61,11 +70,11 @@ def select_group(counter, groups):
return "q" return "q"
if resp == "0": if resp == "0":
groupindex = -1 groupindex = -1
break; break
try: try:
groupindex = int(resp) - 1 groupindex = int(resp) - 1
if (groupindex >= 0 and groupindex <= counter): if (groupindex >= 0 and groupindex < counter):
break; break
except: except:
# fall through to the error msg # fall through to the error msg
pass pass