| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
pyudev._qt_base |
| 19 |
=============== |
| 20 |
|
| 21 |
Base mixin class for Qt4,Qt5 support. |
| 22 |
|
| 23 |
.. moduleauthor:: Sebastian Wiesner <[email protected]> |
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
from pyudev.device import Device |
| 28 |
|
| 29 |
|
| 30 |
class MonitorObserverMixin: |
| 31 |
""" |
| 32 |
Base mixin for pyqt monitor observers. |
| 33 |
""" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
def _setup_notifier(self, monitor, notifier_class): |
| 38 |
self.monitor = monitor |
| 39 |
self.notifier = notifier_class(monitor.fileno(), notifier_class.Read, self) |
| 40 |
self.notifier.activated[int].connect(self._process_udev_event) |
| 41 |
|
| 42 |
@property |
| 43 |
def enabled(self): |
| 44 |
""" |
| 45 |
Whether this observer is enabled or not. |
| 46 |
|
| 47 |
If ``True`` (the default), this observer is enabled, and emits events. |
| 48 |
Otherwise it is disabled and does not emit any events. This merely |
| 49 |
reflects the state of the ``enabled`` property of the underlying |
| 50 |
:attr:`notifier`. |
| 51 |
|
| 52 |
.. versionadded:: 0.14 |
| 53 |
""" |
| 54 |
return self.notifier.isEnabled() |
| 55 |
|
| 56 |
@enabled.setter |
| 57 |
def enabled(self, value): |
| 58 |
self.notifier.setEnabled(value) |
| 59 |
|
| 60 |
def _process_udev_event(self): |
| 61 |
""" |
| 62 |
Attempt to receive a single device event from the monitor, process |
| 63 |
the event and emit corresponding signals. |
| 64 |
|
| 65 |
Called by ``QSocketNotifier``, if data is available on the udev |
| 66 |
monitoring socket. |
| 67 |
""" |
| 68 |
device = self.monitor.poll(timeout=0) |
| 69 |
if device is not None: |
| 70 |
self._emit_event(device) |
| 71 |
|
| 72 |
def _emit_event(self, device): |
| 73 |
self.deviceEvent.emit(device) |
| 74 |
|
| 75 |
|
| 76 |
class QUDevMonitorObserverMixin(MonitorObserverMixin): |
| 77 |
""" |
| 78 |
Obsolete monitor observer mixin. |
| 79 |
""" |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
def _setup_notifier(self, monitor, notifier_class): |
| 84 |
MonitorObserverMixin._setup_notifier(self, monitor, notifier_class) |
| 85 |
self._action_signal_map = { |
| 86 |
"add": self.deviceAdded, |
| 87 |
"remove": self.deviceRemoved, |
| 88 |
"change": self.deviceChanged, |
| 89 |
"move": self.deviceMoved, |
| 90 |
} |
| 91 |
|
| 92 |
import warnings |
| 93 |
|
| 94 |
warnings.warn( |
| 95 |
"Will be removed in 1.0. Use pyudev.pyqt4.MonitorObserver instead.", |
| 96 |
DeprecationWarning, |
| 97 |
) |
| 98 |
|
| 99 |
def _emit_event(self, device): |
| 100 |
self.deviceEvent.emit(device.action, device) |
| 101 |
signal = self._action_signal_map.get(device.action) |
| 102 |
if signal is not None: |
| 103 |
signal.emit(device) |
| 104 |
|
| 105 |
|
| 106 |
def make_init(qobject, socket_notifier): |
| 107 |
""" |
| 108 |
Generates an initializer to observer the given ``monitor`` |
| 109 |
(a :class:`~pyudev.Monitor`): |
| 110 |
|
| 111 |
``parent`` is the parent :class:`~PyQt{4,5}.QtCore.QObject` of this |
| 112 |
object. It is passed unchanged to the inherited constructor of |
| 113 |
:class:`~PyQt{4,5}.QtCore.QObject`. |
| 114 |
""" |
| 115 |
|
| 116 |
def __init__(self, monitor, parent=None): |
| 117 |
qobject.__init__(self, parent) |
| 118 |
|
| 119 |
self._setup_notifier(monitor, socket_notifier) |
| 120 |
|
| 121 |
return __init__ |
| 122 |
|
| 123 |
|
| 124 |
class MonitorObserverGenerator: |
| 125 |
""" |
| 126 |
Class to generate a MonitorObserver class. |
| 127 |
""" |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@staticmethod |
| 132 |
def make_monitor_observer(qobject, signal, socket_notifier): |
| 133 |
"""Generates an observer for device events integrating into the |
| 134 |
PyQt{4,5} mainloop. |
| 135 |
|
| 136 |
This class inherits :class:`~PyQt{4,5}.QtCore.QObject` to turn device |
| 137 |
events into Qt signals: |
| 138 |
|
| 139 |
>>> from pyudev import Context, Monitor |
| 140 |
>>> from pyudev.pyqt4 import MonitorObserver |
| 141 |
>>> context = Context() |
| 142 |
>>> monitor = Monitor.from_netlink(context) |
| 143 |
>>> monitor.filter_by(subsystem='input') |
| 144 |
>>> observer = MonitorObserver(monitor) |
| 145 |
>>> def device_event(device): |
| 146 |
... print('event {0} on device {1}'.format(device.action, device)) |
| 147 |
>>> observer.deviceEvent.connect(device_event) |
| 148 |
>>> monitor.start() |
| 149 |
|
| 150 |
This class is a child of :class:`~{PySide, PyQt{4,5}}.QtCore.QObject`. |
| 151 |
|
| 152 |
""" |
| 153 |
return type( |
| 154 |
str("MonitorObserver"), |
| 155 |
(qobject, MonitorObserverMixin), |
| 156 |
{ |
| 157 |
str("__init__"): make_init(qobject, socket_notifier), |
| 158 |
str("deviceEvent"): signal(Device), |
| 159 |
}, |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
class QUDevMonitorObserverGenerator: |
| 164 |
""" |
| 165 |
Class to generate a MonitorObserver class. |
| 166 |
""" |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
@staticmethod |
| 171 |
def make_monitor_observer(qobject, signal, socket_notifier): |
| 172 |
"""Generates an observer for device events integrating into the |
| 173 |
PyQt{4,5} mainloop. |
| 174 |
|
| 175 |
This class inherits :class:`~PyQt{4,5}.QtCore.QObject` to turn device |
| 176 |
events into Qt signals: |
| 177 |
|
| 178 |
>>> from pyudev import Context, Monitor |
| 179 |
>>> from pyudev.pyqt4 import MonitorObserver |
| 180 |
>>> context = Context() |
| 181 |
>>> monitor = Monitor.from_netlink(context) |
| 182 |
>>> monitor.filter_by(subsystem='input') |
| 183 |
>>> observer = MonitorObserver(monitor) |
| 184 |
>>> def device_event(device): |
| 185 |
... print('event {0} on device {1}'.format(device.action, device)) |
| 186 |
>>> observer.deviceEvent.connect(device_event) |
| 187 |
>>> monitor.start() |
| 188 |
|
| 189 |
This class is a child of :class:`~{PyQt{4,5}, PySide}.QtCore.QObject`. |
| 190 |
|
| 191 |
""" |
| 192 |
return type( |
| 193 |
str("QUDevMonitorObserver"), |
| 194 |
(qobject, QUDevMonitorObserverMixin), |
| 195 |
{ |
| 196 |
str("__init__"): make_init(qobject, socket_notifier), |
| 197 |
|
| 198 |
str("deviceEvent"): signal(str, Device), |
| 199 |
|
| 200 |
str("deviceAdded"): signal(Device), |
| 201 |
|
| 202 |
str("deviceRemoved"): signal(Device), |
| 203 |
|
| 204 |
str("deviceChanged"): signal(Device), |
| 205 |
|
| 206 |
str("deviceMoved"): signal(Device), |
| 207 |
}, |
| 208 |
) |