Debian Codegraph @

src/pyudev/_qt_base.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010, 2011, 2012, 2013 Sebastian Wiesner <[email protected]>
3
4 # This library is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License as published by the
6 # Free Software Foundation; either version 2.1 of the License, or (at your
7 # option) any later version.
8
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 # isort: LOCAL
27 from pyudev.device import Device
28
29
30 class MonitorObserverMixin:
31 """
32 Base mixin for pyqt monitor observers.
33 """
34
35 # pylint: disable=too-few-public-methods
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 # pylint: disable=too-few-public-methods
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 # isort: STDLIB
92 import warnings # pylint: disable=import-outside-toplevel
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 # pylint: disable=protected-access
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 # pylint: disable=too-few-public-methods
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 # pylint: disable=too-few-public-methods
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 #: emitted upon arbitrary device events
198 str("deviceEvent"): signal(str, Device),
199 #: emitted if a device was added
200 str("deviceAdded"): signal(Device),
201 #: emitted if a device was removed
202 str("deviceRemoved"): signal(Device),
203 #: emitted if a device was changed
204 str("deviceChanged"): signal(Device),
205 #: emitted if a device was moved
206 str("deviceMoved"): signal(Device),
207 },
208 )