Advertisement
Guest User

pyudev Twisted integration

a guest
Oct 26th, 2011
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import pyudev
  2. from twisted.internet import abstract
  3.  
  4. class UDevMonitor(abstract.FileDescriptor):
  5.     """
  6.    Protocol wrapper for pyudev.Monitor.
  7.  
  8.    @see: U{http://packages.python.org/pyudev/api/monitor.html}.
  9.    """
  10.  
  11.     def __init__(self, reactor, protocol, subsystem=None, deviceType=None):
  12.         abstract.FileDescriptor.__init__(self, reactor)
  13.  
  14.         # Set up monitor
  15.         context = pyudev.Context()
  16.         self.monitor = pyudev.Monitor.from_netlink(context)
  17.         if subsystem:
  18.             self.monitor.filter_by(subsystem=subsystem, device_type=deviceType)
  19.  
  20.         # Connect protocol
  21.         self.protocol = protocol
  22.         self.protocol.makeConnection(self)
  23.  
  24.  
  25.     def fileno(self):
  26.         """
  27.        Return monitor's file descriptor.
  28.        """
  29.         return self.monitor.fileno()
  30.  
  31.  
  32.     def startReading(self):
  33.         """
  34.        Start waiting for read availability.
  35.        """
  36.         self.monitor.start()
  37.         abstract.FileDescriptor.startReading(self)
  38.  
  39.  
  40.     def doRead(self):
  41.         """
  42.        An event is ready, decode it through Monitor and call our protocol.
  43.        """
  44.         event = self.monitor.receive_device()
  45.         if event:
  46.             action, device = event
  47.             self.protocol.eventReceived(action, device)
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement