From 7854a8136ceac8616538bb616fcc4e96a734789f Mon Sep 17 00:00:00 2001 From: albertogomcas Date: Fri, 20 Jun 2008 09:01:51 +0000 Subject: [PATCH] (libinput.py) new function 'safeinput', more general and friendlier syntax fixed some bugs in numinput (hooke_cli.py, viewer.py,libviewer.py) now use safeinput calls --- hooke_cli.py | 22 ++++++++--------- libinput.py | 69 ++++++++++++++++++++++++++++++++++++++++++---------- libviewer.py | 2 +- viewer.py | 7 +++--- 4 files changed, 72 insertions(+), 28 deletions(-) diff --git a/hooke_cli.py b/hooke_cli.py index 4cc5510..9630ad3 100755 --- a/hooke_cli.py +++ b/hooke_cli.py @@ -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) diff --git a/libinput.py b/libinput.py index a153e65..e863b32 100644 --- a/libinput.py +++ b/libinput.py @@ -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 replyhigh : + while intreplyhigh : 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 diff --git a/libviewer.py b/libviewer.py index 8bb7526..99ff002 100644 --- a/libviewer.py +++ b/libviewer.py @@ -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() diff --git a/viewer.py b/viewer.py index cf8bc01..42f4b6d 100644 --- 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 -- 2.26.2