'''
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 reply<low or reply>high :
- 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 reply<low or reply>high :
+ 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
+
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):
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)
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
#!/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).
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()
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()