-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskevents.py
66 lines (52 loc) · 1.34 KB
/
diskevents.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import division
import os
import sys
if os.name == 'nt':
import wmi # pip install wmi
class DiskEvents(object):
def __init__(self):
ctx = wmi.WMI()
self.watch = ctx.Win32_LogicalDisk.watch_for(DriveType=2)
# watch = ctx.Win32_VolumeChangeEvent.watch_for() # EventType=2
def __iter__(self):
return self
def next(self, timeout=None):
if timeout is None:
timeout_ms = 1000 # for KeyboardInterrupt
else:
timeout_ms = int(timeout*1000)
while True:
try:
event = self.watch(timeout_ms=timeout_ms)
break
except wmi.x_wmi_timed_out:
if timeout is None:
continue
else:
raise TimeoutError
#print event
if event.FileSystem is None:
return {
'path': event.DeviceID,
'present': False,
}
else:
total = int(event.Size)
free = int(event.FreeSpace)
return {
'path': event.DeviceID,
'present': True,
'bytestotal': total,
'bytesused': total - free,
'filesystem': event.FileSystem,
'volumename': event.VolumeName,
'serialno': event.VolumeSerialNumber,
}
else:
class DiskEvents(object):
pass
raise NotImplemented("not implemented for linux (yet)")
if __name__ == '__main__':
import pprint
for event in DiskEvents():
pprint.pprint(event)