Removed sha-bang from non-executable python files + whitespace cleanups.
[hooke.git] / hooke / libinput.py
1 '''
2 Input check routines.
3
4 Copyright (C) 2008 Alberto Gomez-Casado (University of Twente).
5
6 This program is released under the GNU General Public License version 2.
7 '''
8
9 from types import *
10
11
12 def safeinput (message, valid=[]):
13     '''
14     friendlier frontend for alphainput and numinput
15     valid should be a list of 0...n values
16     '''
17
18     #if possible values are not listed we just ask for any non-null input
19     if len(valid)==0:
20         return alphainput(message, '',1,[])
21
22
23     if len(valid)>0:
24         #if valid values are string we use alphainput, if it is only one we take as default
25         if type(valid[0]) is StringType:
26             if len(valid)==1:
27                 return alphainput(message, valid[0], 0,[])
28             else:
29                 return alphainput(message,'', 1,valid)
30
31         #if valid values are numbers we use numinput
32         if type(valid[0]) is IntType:
33             if len(valid)==1:
34                 return numinput(message,valid[0],1,[])
35             else:
36                 return numinput(message,'',1,valid)
37
38
39 def alphainput (message, default, repeat, valid):
40     '''
41     message: prompt for the user
42     default: return value if user input was not correct (and repeat=0)
43     repeat: keeps asking user till it gets a valid input
44     valid: list of allowed answers, empty list for "anything"
45     '''
46     if default and not repeat:
47         print 'Press [enter] for default: ('+str(default)+')'
48     reply=raw_input(message)
49     if len(valid)>0:
50         if reply in valid:
51             return reply
52         else:
53             if repeat==1:
54                 while reply not in valid:
55                     reply=raw_input('You should enter any of these: '+ str(valid) +'\n'+ message)
56                 return reply
57             else:
58                 return default
59     else:
60         if len(reply)>0:
61             return reply
62         else:
63             if not repeat:
64                 return default
65             else:
66                 while len(reply)==0:
67                     print 'Try again'
68                     reply=raw_input(message)
69                 return reply
70
71
72 def checkalphainput (test, default, valid):
73     #useful when input was taken form command args
74     if len(valid)>0:
75         if test in valid:
76             return test
77         else:
78             return default
79     else:
80         #TODO: raise exception?
81         if len(test)>0:
82             return test
83         else:
84             return default
85
86
87 def numinput(message, default, repeat, limits):
88     '''
89     message: prompt for the user
90     default: return value if user input was not correct (and repeat=0)
91     repeat: keeps asking user till it gets a valid input
92     limits: pair of values, input is checked to be between them, empty list for "any number"
93     '''
94     if default and not repeat:
95         print 'Press [enter] for default: '+str(default)
96
97     reply=raw_input(message)
98
99     try:
100         intreply=int(reply)
101     except:
102         intreply=None
103
104     if len(limits)==2:
105         high=int(limits.pop())
106         low=int(limits.pop())
107         if intreply>=low and intreply <= high:
108             return intreply
109         else:
110             if repeat==1:
111                 while intreply<low or intreply>high :
112                     reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message)
113                     try:
114                         intreply=int(reply)
115                     except:
116                         intreply=None
117                 return intreply
118             else:
119                 return default
120     else:
121         if intreply!=None:
122             return intreply
123         else:
124             if not repeat:
125                 return default
126             else:
127                 while intreply==None:
128                     print 'Try again'
129                     reply=raw_input(message)
130                     try:
131                         intreply=int(reply)
132                     except:
133                         intreply=None
134                 return intreply
135
136
137 def checknuminput(test,default,limits):
138     #useful when input was taken from command args
139     if len(limits)==2:
140         high=int(limits.pop())
141         low=int(limits.pop())
142         if test>=low and test <= high:
143             return int(test)
144         else:
145             return default
146     else:
147         if len(test)>0:
148             return int(test)
149         else:
150             return default