pvl.syslog: implement a slightly more flexible poll()
authorTero Marttila <terom@paivola.fi>
Sun, 13 Jan 2013 00:25:01 +0200
changeset 112 8127c0f4223d
parent 111 4b96c153c113
child 113 49e13576d77c
pvl.syslog: implement a slightly more flexible poll()
pvl/syslog/args.py
pvl/syslog/syslog.py
--- a/pvl/syslog/args.py	Sun Jan 13 00:23:34 2013 +0200
+++ b/pvl/syslog/args.py	Sun Jan 13 00:25:01 2013 +0200
@@ -71,7 +71,7 @@
             log.warning("Reading syslog messages from TTY?")
         
         source = tail.Tail(sys.stdin)
-        poll = False # XXX: tty vs pipe vs file?
+        poll = False # XXX: tty vs pipe vs file? False -> just block
     
     # options
     parser = SyslogParser(
--- a/pvl/syslog/syslog.py	Sun Jan 13 00:23:34 2013 +0200
+++ b/pvl/syslog/syslog.py	Sun Jan 13 00:25:01 2013 +0200
@@ -34,22 +34,27 @@
         """
 
         return self.filter(self.parser(self.source))
+        
+    def fileno (self) :
+        return self.source.fileno()
 
-    def poll (self, poll=None) :
+    def poll (self, poll=None, reading=(), writing=()) :
         """
             Poll our source for input, with given polling behaviour:
                 True    - select() on source
                 False   - peek on source
                 float   - timeout in seconds
-
-            Returns True if we have input waiting, False on timeout with no input. None on indeterminate.
+            
+            Returns None on unknown, empty sequence on timeout, list of readables on select.
         """
+        
+        # from __init__
+        if poll is None :
+            poll = self._poll
 
-        reading = writing = ()
-        
         if poll is True :
             timeout = None # block
-            reading += (self.source, ) # file-like object with fileno()
+            reading += (self, ) # source.fileno()
 
         elif not poll :
             timeout = 0.0 # do not block
@@ -57,17 +62,20 @@
         else :
             timeout = float(poll)
 
-        log.debug("%s", timeout)
+        log.debug("%s (%s)", reading, timeout)
     
         # select
         readable, writeable, ex = select.select(reading, writing, [], timeout)
+        
+        log.debug("select: %s", readable)
 
         if readable :
-            return True
+            return readable
 
         elif reading :
             # timeout
-            return False
+            # XXX: this is the same as readable
+            return ()
 
         else :
             # unknown
@@ -83,9 +91,6 @@
             TODO: SIGINT -> finish iteration and return?
         """
 
-        if poll is None :
-            poll = self._poll
-        
         # mainloop
         while True :
             # pull in messages