Skip to content

Instantly share code, notes, and snippets.

@dpnova
Created February 19, 2015 03:07
Show Gist options
  • Save dpnova/a7830b34e7c465baace7 to your computer and use it in GitHub Desktop.
Save dpnova/a7830b34e7c465baace7 to your computer and use it in GitHub Desktop.
udev and twisted together. repasting from: http://pastebin.com/TnrsS15s by @ralphm
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment