Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyudev
- from twisted.internet import abstract
- class UDevMonitor(abstract.FileDescriptor):
- """
- Protocol wrapper for pyudev.Monitor.
- @see: U{http://packages.python.org/pyudev/api/monitor.html}.
- """
- def __init__(self, reactor, protocol, subsystem=None, deviceType=None):
- abstract.FileDescriptor.__init__(self, reactor)
- # Set up monitor
- context = pyudev.Context()
- self.monitor = pyudev.Monitor.from_netlink(context)
- if subsystem:
- self.monitor.filter_by(subsystem=subsystem, device_type=deviceType)
- # Connect protocol
- self.protocol = protocol
- self.protocol.makeConnection(self)
- def fileno(self):
- """
- Return monitor's file descriptor.
- """
- return self.monitor.fileno()
- def startReading(self):
- """
- Start waiting for read availability.
- """
- self.monitor.start()
- abstract.FileDescriptor.startReading(self)
- def doRead(self):
- """
- An event is ready, decode it through Monitor and call our protocol.
- """
- event = self.monitor.receive_device()
- if event:
- action, device = event
- self.protocol.eventReceived(action, device)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement