(libviewer.py, viewer.py, liboutlet.py, libinput.py) added some comments
[hooke.git] / libinput.py
1 #!/usr/bin/env python
2
3 '''
4 Input check routines.
5
6 Copyright (C) 2008 Alberto Gomez-Casado (University of Twente).
7
8 This program is released under the GNU General Public License version 2.
9 '''
10
11 def alphainput (message, default, repeat, valid):
12     '''
13     message: prompt for the user
14     default: return value if user input was not correct (and repeat=0)
15     repeat: keeps asking user till it gets a valid input
16     valid: list of allowed answers, empty list for "anything"
17     ''' 
18     if default and not repeat:
19         print 'Press [enter] for default: ('+str(default)+')'
20     reply=raw_input(message)
21     if len(valid)>0:
22         if reply in valid: 
23             return reply
24         else:
25             if repeat==1:
26                 while reply not in valid:
27                     reply=raw_input('You should enter any of these: '+ str(valid) +'\n'+ message)
28                 return reply
29             else:
30                 return default
31     else:
32         if len(reply)>0:
33             return reply
34         else:
35             if not repeat:
36                 return default
37             else:
38                 while len(reply)==0:
39                     print 'Try again'
40                     reply=raw_input(message)
41                 return reply
42
43                     
44
45 def checkalphainput (test, default, valid):
46     #useful when input was taken form command args
47     if len(valid)>0:
48         if test in valid: 
49             return test
50         else:
51             return default
52     else:
53         #TODO: raise exception?
54         if len(test)>0:
55             return test
56         else:
57             return default
58
59
60 def numinput(message, default, repeat, limits):
61     '''
62     message: prompt for the user
63     default: return value if user input was not correct (and repeat=0)
64     repeat: keeps asking user till it gets a valid input
65     limits: pair of values, input is checked to be between them, empty list for "any number"
66     ''' 
67     if default and not repeat:
68         print 'Enter for default: '+str(default)
69     reply=raw_input(message)
70     if reply:
71         reply=int(reply)
72     if len(limits)==2:
73         high=int(limits.pop())
74         low=int(limits.pop())
75         if reply>=low and reply <= high:
76             return reply
77         else:
78             if repeat==1:
79                 while reply<low or reply>high :
80                     reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message)
81                     if reply:
82                         reply=int(reply)
83                 return reply
84             else:
85                 return default
86     else:
87         if len(reply)>0:
88             return int(reply)
89         else:
90             if not repeat:
91                 return default
92             else:
93                 while len(reply)==0:
94                     print 'Try again'
95                     reply=raw_input(message)
96                 return reply
97
98 def checknuminput(test,default,limits):
99     #useful when input was taken from command args
100     if len(limits)==2:
101         high=int(limits.pop())
102         low=int(limits.pop())
103         if test>=low and test <= high:
104             return int(test)
105         else:
106             return default
107     else:
108         if len(test)>0:
109             return int(test)
110         else:
111             return default
112