1
2 import fcntl, signal, os
3
6 self.watchers = {}
7 self.installed = 0
9 if self.installed:
10 return
11 signal.signal(signal.SIGIO, self.fire)
12 self.installed = 1
14 if not self.installed:
15 return
16 signal.signal(signal.SIGIO, signal.SIG_DFL)
17 self.installed = 0
18 - def add(self, watcher):
26 - def fire(self, signum, frame):
27
28
29 for watcher in self.watchers.values():
30 watcher.callback()
31
33 DN_ACCESS = fcntl.DN_ACCESS
34 DN_MODIFY = fcntl.DN_MODIFY
35 DN_CREATE = fcntl.DN_CREATE
36 DN_DELETE = fcntl.DN_DELETE
37 DN_RENAME = fcntl.DN_RENAME
38 DN_ATTRIB = fcntl.DN_ATTRIB
39
40 handler = [None]
41
44
45 """This object watches a directory for changes. The .callback
46 attribute should be set to a function to be run every time something
47 happens to it. Be aware that it will be called more times than you
48 expect."""
49
50 if callback:
51 self.callback = callback
52 else:
53 self.callback = self.fire
54 self.dirname = dirname
55 self.flags = reduce(lambda x, y: x | y, flags) | fcntl.DN_MULTISHOT
56 self.fd = os.open(dirname, os.O_RDONLY)
57
58
59
60
61 if not self.handler[0]:
62 self.handler[0] = DNotify_Handler()
63 self.handler[0].add(self)
64 fcntl.fcntl(self.fd, fcntl.F_NOTIFY, self.flags)
69 print self.dirname, "changed!"
70
72 d = DNotify(".")
73 while 1:
74 signal.pause()
75
77
78
79
80
81 count = [0]
82 d1 = DNotify(".")
83 def fire1(count=count, d1=d1):
84 print "./ changed!", count[0]
85 count[0] += 1
86 if count[0] > 5:
87 d1.remove()
88 del(d1)
89
90
91 d1.callback = fire1
92 def fire2(): print "foo/ changed!"
93 d2 = DNotify("foo", fire2)
94 while 1:
95 signal.pause()
96
97
98 if __name__ == '__main__':
99 test_dnotify2()
100