-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect_storage_insert.py
40 lines (30 loc) · 1.15 KB
/
detect_storage_insert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pyudev import Context, Monitor, Device
from pyudev.glib import MonitorObserver
class StorageMonitor:
callbacks = []
def __init__(self):
"""
Start listening for storage events.
"""
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='block')
monitor_observer = MonitorObserver(monitor)
monitor_observer.connect('device-event', self._device_event)
monitor.start()
def add_usb_insert_notifier(self, callback):
# type: (function) -> None
"""
The supplied callback will be called when a USB device with a partition
is connected. The first argument given will be the device's block and
the device object.
"""
self.callbacks.append(callback)
def _device_event(self, observer, device):
# type: (MonitorObserver, Device) -> None
"""
Internal function which is called when a block event occurred.
"""
if device.action == 'add' and device.device_type == 'partition':
for c in self.callbacks:
c(device.device_node, device)