From 53dc8044bc8abe6956c73359c9e6d45c170a6e7b Mon Sep 17 00:00:00 2001 From: albertogomcas Date: Thu, 19 Jun 2008 15:27:59 +0000 Subject: [PATCH] (libviewer.py, viewer.py, liboutlet.py, libinput.py) added some comments (libviewer.py, viewer.py, liboutlet.py) now using safe inputs --- libinput.py | 173 +++++++++++++++++++++++++++------------------------ liboutlet.py | 7 ++- libviewer.py | 37 ++++++----- viewer.py | 26 ++++---- 4 files changed, 137 insertions(+), 106 deletions(-) diff --git a/libinput.py b/libinput.py index af82cb8..a153e65 100644 --- a/libinput.py +++ b/libinput.py @@ -9,91 +9,104 @@ This program is released under the GNU General Public License version 2. ''' def alphainput (message, default, repeat, valid): - if default and not repeat: - print 'Enter for default: '+str(default) - reply=raw_input(message) - if len(valid)>0: - if reply in valid: - return reply - else: - if repeat==1: - while reply not in valid: - reply=raw_input('You should enter any of these: '+ str(valid) +'\n'+ message) - return reply - else: - return default - else: - if len(reply)>0: - return reply - else: - if not repeat: - return default - else: - while len(reply)==0: - print 'Try again' - reply=raw_input(message) - return reply + ''' + message: prompt for the user + default: return value if user input was not correct (and repeat=0) + repeat: keeps asking user till it gets a valid input + valid: list of allowed answers, empty list for "anything" + ''' + if default and not repeat: + print 'Press [enter] for default: ('+str(default)+')' + reply=raw_input(message) + if len(valid)>0: + if reply in valid: + return reply + else: + if repeat==1: + while reply not in valid: + reply=raw_input('You should enter any of these: '+ str(valid) +'\n'+ message) + return reply + else: + return default + else: + if len(reply)>0: + return reply + else: + if not repeat: + return default + else: + while len(reply)==0: + print 'Try again' + reply=raw_input(message) + return reply - + def checkalphainput (test, default, valid): -#useful when input was taken form command args - if len(valid)>0: - if test in valid: - return test - else: - return default - else: - #TODO: raise exception? - if len(test)>0: - return test - else: - return default + #useful when input was taken form command args + if len(valid)>0: + if test in valid: + return test + else: + return default + else: + #TODO: raise exception? + if len(test)>0: + return test + else: + return default def numinput(message, default, repeat, limits): - if default and not repeat: - print 'Enter for default: '+str(default) - reply=raw_input(message) - if reply: - reply=int(reply) - if len(limits)==2: - high=int(limits.pop()) - low=int(limits.pop()) - if reply>=low and reply <= high: - return reply - else: - if repeat==1: - while replyhigh : - reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message) - if reply: - reply=int(reply) - return reply - else: - return default - else: - if len(reply)>0: - return int(reply) - else: - if not repeat: - return default - else: - while len(reply)==0: - print 'Try again' - reply=raw_input(message) - return reply + ''' + message: prompt for the user + default: return value if user input was not correct (and repeat=0) + repeat: keeps asking user till it gets a valid input + 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) + reply=raw_input(message) + if reply: + reply=int(reply) + if len(limits)==2: + high=int(limits.pop()) + low=int(limits.pop()) + if reply>=low and reply <= high: + return reply + else: + if repeat==1: + while replyhigh : + reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message) + if reply: + reply=int(reply) + return reply + else: + return default + else: + if len(reply)>0: + return int(reply) + else: + if not repeat: + return default + else: + while len(reply)==0: + print 'Try again' + reply=raw_input(message) + return reply def checknuminput(test,default,limits): - if len(limits)==2: - high=int(limits.pop()) - low=int(limits.pop()) - if test>=low and test <= high: - return int(test) - else: - return default - else: - if len(test)>0: - return int(test) - else: - return default - + #useful when input was taken from command args + if len(limits)==2: + high=int(limits.pop()) + low=int(limits.pop()) + if test>=low and test <= high: + return int(test) + else: + return default + else: + if len(test)>0: + return int(test) + else: + return default + diff --git a/liboutlet.py b/liboutlet.py index 81f875c..06e0940 100644 --- a/liboutlet.py +++ b/liboutlet.py @@ -16,12 +16,15 @@ class Outlet(object): def __init__(self): self.buffer=[] + #relations is still unused self.relations=[] def push(self, args): + #queue new entry self.buffer.append(args) def pop(self): + #delete last entry return self.buffer.pop(); def printbuf(self): @@ -31,6 +34,7 @@ class Outlet(object): j=j+1 def delete(self, number): + #delete entry matching given index if len(self.buffer)>int(number)-1 and int(number)>0: self.buffer.pop(int(number)-1) @@ -44,7 +48,8 @@ class Outlet(object): return self.buffer[0] def read_type(self,dtype): - aux=[] + #returns entries matching a given type (force, distance, point...) + aux=[] index=0 if dtype=='all': return self.buffer diff --git a/libviewer.py b/libviewer.py index 37fe2eb..8bb7526 100644 --- a/libviewer.py +++ b/libviewer.py @@ -1,7 +1,7 @@ #!/usr/bin/env python ''' -Basic Viewer and ascii saver examples +Basic Viewer and ascii saver example Copyright (C) 2008 Alberto Gomez-Casado (University of Twente). @@ -10,34 +10,43 @@ This program is released under the GNU General Public License version 2. import liboutlet as lout +import libinput as linput class Viewer(object): - source=[] - data=[] - dtype='all' - action=[] - + source=[] + data=[] + dtype='all' + action=[] #alias to call the actual viewer function, makes it general + - def setdtype(self, dt): - self.dtype=dt + def setdtype(self, dt): + #determines wich type of data will be retrieved from outlet + self.dtype=dt - def show(self): - self.source.printbuf() + def show(self): + #TODO should print only data matching 'type' + self.source.printbuf() - def getdata(self): - self.data=self.source.read_type(self.dtype) + def getdata(self): + #retrieves data from outlet + self.data=self.source.read_type(self.dtype) class Ascii(Viewer): +#example viewer, it just retrieves data and writes it to a text file +#probably this should be in viewer.py? def __init__(self,outref): self.source=outref - self.action=self.dump + #tells the viewer which outlet has the data (so far only one in hooke) + self.action=self.dump + #this allows to call dump (or any other function, depending on the viewer) from the CLI using 'vwaction' def dump(self): + #retrieves and saves data self.getdata() - destination=raw_input('Enter filename:') + destination=linput.alphainput('Enter filename:','results.txt',0,[]) destfile=open(destination,'w+') destfile.write('\n'.join(self.data)) destfile.close() diff --git a/viewer.py b/viewer.py index bae4355..cf8bc01 100644 --- a/viewer.py +++ b/viewer.py @@ -10,25 +10,29 @@ This program is released under the GNU General Public License version 2. import libviewer as lview +import libinput as limput class viewerCommands: def _plug_init(self): - self.viewerlist=[] + self.viewerlist=[] + #we keep a list of different viewers so it's possible to retrieve different data + #or process the same data differently def do_vwnew(self,args): - self.viewerlist.append(lview.Ascii(self.outlet)) - dt=raw_input('What type of data will this viewer handle? (force/distance/all)') - print dt - self.viewerlist[-1].setdtype(dt) + #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? + print dt + self.viewerlist[-1].setdtype(dt) def do_vwaction(self,args): - ''' - triggers default action of viewer number n (default 0) - ''' + ''' + triggers default action of viewer number n (default 0) + ''' - if len(args)==0: - args=0 - self.viewerlist[int(args)].action() + if len(args)==0: + args=0 + self.viewerlist[int(args)].action() -- 2.26.2