(libviewer.py, viewer.py, liboutlet.py, libinput.py) added some comments
authoralbertogomcas <devnull@localhost>
Thu, 19 Jun 2008 15:27:59 +0000 (15:27 +0000)
committeralbertogomcas <devnull@localhost>
Thu, 19 Jun 2008 15:27:59 +0000 (15:27 +0000)
(libviewer.py, viewer.py, liboutlet.py) now using safe inputs

libinput.py
liboutlet.py
libviewer.py
viewer.py

index af82cb8a54318a58d55611f9998a51cd124dbeb8..a153e652c58c9b5b21eab55204aee59bcc65f2e9 100644 (file)
@@ -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 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
+
index 81f875cc6da53541e0d3cf4c7184a77fcfc801ce..06e09401a1062239a3fae7b5d6e4fd5397dd4058 100644 (file)
@@ -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
index 37fe2eba7047b5a3ab51a800e2acfade9bb4772e..8bb7526a8ed6b1b2f193394b4523a33c5c3019bf 100644 (file)
@@ -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()
index bae4355ae3b88b3da24ee81f94d7665922769e6c..cf8bc0114a2894fbc877d112674b43b3915d1157 100644 (file)
--- 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()