(libinput.py) new function 'safeinput', more general and friendlier syntax
authoralbertogomcas <devnull@localhost>
Fri, 20 Jun 2008 09:01:51 +0000 (09:01 +0000)
committeralbertogomcas <devnull@localhost>
Fri, 20 Jun 2008 09:01:51 +0000 (09:01 +0000)
      fixed some bugs in numinput
(hooke_cli.py, viewer.py,libviewer.py) now use safeinput calls

hooke_cli.py
libinput.py
libviewer.py
viewer.py

index 4cc5510f327f9bcd6eeaf0d8cb1dd965ccde707d..9630ad3889782838365c36f4ebf0341a5a1a4bb2 100755 (executable)
@@ -214,7 +214,7 @@ Syntax: loadlist [playlist file]
     def do_loadlist(self, args):
         #checking for args: if nothing is given as input, we warn and exit.
         while len(args)==0:
-            args=linp.alphainput('File to load?','',0,[])
+            args=linp.safeinput('File to load?')
         
         arglist=args.split()
         play_to_load=arglist[0]
@@ -270,7 +270,7 @@ Syntax: genlist [input files]
     def do_genlist(self,args):
         #args list is: input path, output name
         if len(args)==0:
-            args=linp.alphainput('Input files?','',1,[])
+            args=linp.safeinput('Input files?',[])
                     
         arglist=args.split()      
         list_path=arglist[0]
@@ -321,7 +321,7 @@ Syntax: genlist [input files]
         Syntax: savelist [filename]
         '''
         while len(args)==0:
-            args=linp.alphainput('Output files?','',1,[])
+            args=linp.safeinput('Output file?',['savedlist.txt'])
     
         output_filename=args
         
@@ -387,7 +387,7 @@ If the curve is not in the current playlist, it politely asks if we want to add
         '''
         
         if filename=='':
-            filename=linp.alphainput('Jump to?','',0,[])
+            filename=linp.safeinput('Jump to?')
             
         filepath=os.path.abspath(filename)
         print filepath
@@ -406,7 +406,7 @@ If the curve is not in the current playlist, it politely asks if we want to add
                     c+=1  
             except IndexError:
                 #We've found the end of the list.
-                answer=linp.alphainput('Curve not found in playlist. Add it to list?','y',0,[])
+                answer=linp.safeinput('Curve not found in playlist. Add it to list?',['y'])
                 if answer.lower()[0]=='y':
                     try:
                         self.do_addtolist(filepath)
@@ -611,7 +611,7 @@ If you have a multiple plot, the optional plot to export argument tells Hooke wh
         
         dest=0
         if args=='':
-            name=linp.alphainput('Filename?',self.current.path+'.png',0,[])
+            name=linp.safeinput('Filename?',[self.current.path+'.png'])
         else:
             args=args.split()
             name=args[0]
@@ -646,7 +646,7 @@ Syntax: txt [filename] {plot to export}
         whichplot=0
         args=args.split()
         if len(args)==0:
-            filename=linp.alphainput('Filename?',self.current.path+'.txt',0,[])
+            filename=linp.safeinput('Filename?',[self.current.path+'.txt'])
         else:
             filename=linp.checkalphainput(args[0],self.current.path+'.txt',[])
             try:
@@ -746,7 +746,7 @@ Syntax notelog [filename]
     def do_notelog(self,args):
         
         if len(args)==0:
-            args=linp.alphainput('Notelog filename?','notelog.txt',0,[])
+            args=linp.safeinput('Notelog filename?',['notelog.txt'])
             
         note_lines='Notes taken at '+time.asctime()+'\n'
         for item in self.current_list:
@@ -776,7 +776,7 @@ Syntax copylog [directory]
     def do_copylog(self,args):
         
         if len(args)==0:
-            args=linp.alphainput('Destination directory?','',0,[])  #TODO default
+            args=linp.safeinput('Destination directory?')  #TODO default
         
         mydir=os.path.abspath(args)
         if not os.path.isdir(mydir):
@@ -928,9 +928,9 @@ Syntax: quit
         we_exit='N'
         
         if (not self.playlist_saved) or (not self.notes_saved):
-            we_exit=linp.alphainput('You did not save your playlist and/or notes. Exit?','n',0,[])
+            we_exit=linp.safeinput('You did not save your playlist and/or notes. Exit?',['n'])
         else:
-            we_exit=linp.alphainput('Exit?','y',0,[])
+            we_exit=linp.alphainput('Exit?',['y'])
         
         if we_exit[0].upper()=='Y':
             wx.CallAfter(self.frame.Close)
index a153e652c58c9b5b21eab55204aee59bcc65f2e9..e863b32dabc9831cc7bbf050479ae627cf991fca 100644 (file)
@@ -8,6 +8,38 @@ Copyright (C) 2008 Alberto Gomez-Casado (University of Twente).
 This program is released under the GNU General Public License version 2.
 '''
 
+from types import *
+
+
+
+def safeinput (message, valid=[]):
+    '''
+    friendlier frontend for alphainput and numinput
+    valid should be a list of 0...n values
+    '''
+
+    #if possible values are not listed we just ask for any non-null input 
+    if len(valid)==0:
+        return alphainput(message, '',1,[])
+    
+    
+    if len(valid)>0:
+        #if valid values are string we use alphainput, if it is only one we take as default
+        if type(valid[0]) is StringType:
+            if len(valid)==1:
+                return alphainput(message, valid[0], 0,[])
+            else:
+                return alphainput(message,'', 1,valid)
+            
+        #if valid values are numbers we use numinput
+        if type(valid[0]) is IntType:
+            if len(valid)==1:
+                return numinput(message,valid[0],1,[])
+            else:
+                return numinput(message,'',1,valid)
+    
+    
+
 def alphainput (message, default, repeat, valid):
     '''
     message: prompt for the user
@@ -65,35 +97,46 @@ def numinput(message, default, repeat, limits):
     limits: pair of values, input is checked to be between them, empty list for "any number"
     ''' 
     if default and not repeat:
-        print 'Enter for default: '+str(default)
+        print 'Press [enter] for default: '+str(default)
+        
     reply=raw_input(message)
-    if reply:
-        reply=int(reply)
+    
+    try:
+        intreply=int(reply)
+    except:
+        intreply=None
+              
     if len(limits)==2:
         high=int(limits.pop())
         low=int(limits.pop())
-        if reply>=low and reply <= high:
-            return reply
+        if intreply>=low and intreply <= high:
+            return intreply
         else:
             if repeat==1:
-                while reply<low or reply>high :
+                while intreply<low or intreply>high :
                     reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message)
-                    if reply:
-                        reply=int(reply)
-                return reply
+                    try:
+                        intreply=int(reply)
+                    except:
+                        intreply=None
+                return intreply
             else:
                 return default
     else:
-        if len(reply)>0:
-            return int(reply)
+        if intreply!=None:
+            return intreply
         else:
             if not repeat:
                 return default
             else:
-                while len(reply)==0:
+                while intreply==None:
                     print 'Try again'
                     reply=raw_input(message)
-                return reply
+                    try:
+                        intreply=int(reply)
+                    except:
+                        intreply=None
+                return intreply
 
 def checknuminput(test,default,limits):
     #useful when input was taken from command args
index 8bb7526a8ed6b1b2f193394b4523a33c5c3019bf..99ff002202d20a82a3f6c96af9469e7c65e8f862 100644 (file)
@@ -46,7 +46,7 @@ class Ascii(Viewer):
        def dump(self):
                #retrieves and saves data
                self.getdata()
-               destination=linput.alphainput('Enter filename:','results.txt',0,[])
+               destination=linput.safeinput('Enter filename:',['results.txt'])
                destfile=open(destination,'w+')
                destfile.write('\n'.join(self.data))
                destfile.close()
index cf8bc0114a2894fbc877d112674b43b3915d1157..42f4b6d38c3ae62da449931394fa297302e704e7 100644 (file)
--- a/viewer.py
+++ b/viewer.py
@@ -10,7 +10,7 @@ This program is released under the GNU General Public License version 2.
 
 
 import libviewer as lview
-import libinput as limput
+import libinput as linput
 
 class viewerCommands:
        
@@ -23,7 +23,8 @@ class viewerCommands:
     def do_vwnew(self,args):
         #creates a new viewer
         self.viewerlist.append(lview.Ascii(self.outlet))
-        dt=limput.alphainput('What type of data will this viewer handle? (force/distance/all)','',1,['force', 'distance', 'all']) #TODO update types, make a list somewhere?
+        dt=linput.safeinput('What type of data will this viewer handle? (force/distance/all)',['force', 'distance', 'all']) 
+                    #TODO update types, make a list somewhere?
         print dt
         self.viewerlist[-1].setdtype(dt)
 
@@ -35,4 +36,4 @@ class viewerCommands:
 
         if len(args)==0:
             args=0
-        self.viewerlist[int(args)].action()
+        self.viewerlist[int(args)].action()
\ No newline at end of file