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