Created
February 19, 2015 03:07
-
-
Save dpnova/a7830b34e7c465baace7 to your computer and use it in GitHub Desktop.
udev and twisted together. repasting from: http://pastebin.com/TnrsS15s by @ralphm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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