pvl/syslog/syslog.py
changeset 115 9772d43669fb
parent 112 8127c0f4223d
child 139 515b74c6b456
equal deleted inserted replaced
114:2e88e1d8e604 115:9772d43669fb
     1 """
     1 """
     2     Syslog handling.
     2     Syslog handling.
       
     3 
       
     4     XXX: this belongs in pvl.syslog.source (apart from __iter__?)
     3 """
     5 """
     4 
     6 
     5 import select
     7 import select
     6 
     8 
     7 import logging; log = logging.getLogger('pvl.syslog.source')
     9 import logging; log = logging.getLogger('pvl.syslog.source')
    17     def __init__ (self, source, parser, filter, poll=None) :
    19     def __init__ (self, source, parser, filter, poll=None) :
    18         """
    20         """
    19             Using given underlying line source.
    21             Using given underlying line source.
    20                 
    22                 
    21                 source      - source to select() if poll=True
    23                 source      - source to select() if poll=True
    22                 poll        - polling behaviour
    24                 poll        - polling behaviour for source
    23         """
    25         """
    24         
    26         
    25         self.source = source
    27         self.source = source
    26         self.parser = parser
    28         self.parser = parser
    27         self.filter = filter
    29         self.filter = filter
    28 
    30 
    29         self._poll = poll
    31         self.poll = poll
    30 
    32 
    31     def __iter__ (self) :
    33     def __iter__ (self) :
    32         """
    34         """
    33             Yield available input.
    35             Yield available input.
    34         """
    36         """
    36         return self.filter(self.parser(self.source))
    38         return self.filter(self.parser(self.source))
    37         
    39         
    38     def fileno (self) :
    40     def fileno (self) :
    39         return self.source.fileno()
    41         return self.source.fileno()
    40 
    42 
    41     def poll (self, poll=None, reading=(), writing=()) :
    43     def select (self, poll=None, reading=(), writing=()) :
    42         """
    44         """
    43             Poll our source for input, with given polling behaviour:
    45             Poll our source for input, with given polling behaviour:
    44                 True    - select() on source
    46                 True    - select() on source
    45                 False   - peek on source
    47                 False   - peek on source
    46                 float   - timeout in seconds
    48                 float   - timeout in seconds
    47             
    49             
    48             Returns None on unknown, empty sequence on timeout, list of readables on select.
    50             Returns None on unknown, empty sequence on timeout, list of readables on select.
    49         """
    51         """
    50         
    52         
    51         # from __init__
       
    52         if poll is None :
       
    53             poll = self._poll
       
    54 
       
    55         if poll is True :
    53         if poll is True :
    56             timeout = None # block
    54             timeout = None # block
    57             reading += (self, ) # source.fileno()
    55             reading += (self, ) # source.fileno()
    58 
    56 
    59         elif not poll :
    57         elif not poll :
    89 
    87 
    90             XXX: reconnect? or source takes care of that..
    88             XXX: reconnect? or source takes care of that..
    91             TODO: SIGINT -> finish iteration and return?
    89             TODO: SIGINT -> finish iteration and return?
    92         """
    90         """
    93 
    91 
       
    92         # from __init__
       
    93         # note that we must interpret poll here, since False -> never poll
       
    94         if poll is None :
       
    95             poll = self.poll
       
    96 
    94         # mainloop
    97         # mainloop
    95         while True :
    98         while True :
    96             # pull in messages
    99             # pull in messages
    97             for item in self :
   100             for item in self :
    98                 log.debug("%s", item)
   101                 log.debug("%s", item)
    99                 yield item
   102                 yield item
   100             
   103             
   101             # poll
   104             # poll
   102             if poll :
   105             if poll :
   103                 # wait
   106                 # wait
   104                 self.poll(poll)
   107                 self.select(poll)
       
   108 
   105             else :
   109             else :
   106                 # done
   110                 # done
   107                 break
   111                 break
   108 
   112 
   109         log.debug("exit")
   113         log.debug("exit")