10
raise TypeError('Filemonitor cannot cannot be used on %s platform'%sys.platform)
14
Monitors a file for changes and triggers a visualization action
16
ValidEvents ={'create':pyinotify.IN_CREATE,
17
'delete':pyinotify.IN_DELETE,
18
'close_write':pyinotify.IN_CLOSE_WRITE,
19
"close_nowrite":pyinotify.IN_CLOSE_NOWRITE,
20
'access':pyinotify.IN_ACCESS,
21
'attrib':pyinotify.IN_ATTRIB,
22
'modify':pyinotify.IN_MODIFY}
23
def __init__(self,filepath,events,visaction,**kwargs):
25
Sets up monitoring of a file
28
- `filepath`: full path of the file to monitor
29
- `event`: events to monitor: list of strings
30
- `visaction`: Callable which will perform the action which takes filepath as argument
32
self.filepath = filepath
34
self.visaction = visaction
35
# Setting up the watch manager
36
self.wm = pyinotify.WatchManager()
37
self.mask = self._get_mask(events)
38
self.handler = _HandleEvents()
40
self.handler.debug = kwargs['debug']
41
self.handler.set_action(self.visaction)
42
self.notifier = pyinotify.ThreadedNotifier(self.wm, self.handler)
44
wdd = self.wm.add_watch(self.filepath, self.mask, rec=True)
45
#self.wm.rm_watch(wdd.values())
47
def _get_mask(self, events):
49
Returns the mask for the notifier
52
codes = [self.ValidEvents[e] for e in events]
54
raise KeyError('%s is not a valid event'%e)
68
class _HandleEvents(pyinotify.ProcessEvent):
70
def set_action(self,action):
73
def process_IN_CREATE(self, event):
75
print "Creating:", event.pathname
76
self.action(event.pathname)
78
def process_IN_DELETE(self, event):
80
print "Removing:", event.pathname
81
self.action(event.pathname)
83
def process_IN_ACCESS(self,event):
85
print "Accessing:", event.pathname
86
self.action(event.pathname)
88
def process_IN_ATTRIB(self,event):
90
print "Changing metadata:", event.pathname
91
self.action(event.pathname)
93
def process_IN_MODIFY(self,event):
95
print "Modifying:", event.pathname
96
self.action(event.pathname)
98
def process_IN_CLOSE_WRITE(self,event):
100
print "Closing writable file:", event.pathname
101
self.action(event.pathname)
103
def process_IN_CLOSE_NOWRITE(self,event):
105
print "Closing read-only file:", event.pathname
106
self.action(event.pathname)