Updated TODO
[ugit.git] / ugitlibs / inotify.py
blob3460befc3ec430b588ef66288a62274c1d7ee594
1 #!/usr/bin/env python
2 import os
3 import cmds
4 from PyQt4.QtCore import QThread, SIGNAL
5 from pyinotify import ProcessEvent
6 from pyinotify import WatchManager, Notifier, EventsCodes
8 class FileSysEvent(ProcessEvent):
9 def __init__(self, parent):
10 ProcessEvent.__init__(self)
11 self.parent = parent
12 def process_default(self, event):
13 self.parent.notify()
15 class GitNotifier(QThread):
16 def __init__(self, path):
17 QThread.__init__(self)
18 self.path = path
19 self.abort = False
21 def notify(self):
22 if not self.abort:
23 self.emit( SIGNAL('timeForRescan()') )
25 def run(self):
26 # Only capture those events that git cares about
27 mask =( EventsCodes.IN_CREATE
28 | EventsCodes.IN_DELETE
29 | EventsCodes.IN_MOVED_TO
30 | EventsCodes.IN_MODIFY )
32 wm = WatchManager()
33 notifier = Notifier(wm, FileSysEvent(self))
34 self.notifier = notifier
36 dirs_seen = {}
37 added_flag = False
38 while not self.abort:
40 if not added_flag:
41 wm.add_watch(self.path, mask)
42 # Register files/directories known to git
43 for file in cmds.git_ls_files():
44 wm.add_watch(file, mask)
45 directory = os.path.dirname(file)
46 if directory not in dirs_seen:
47 wm.add_watch(directory, mask)
48 dirs_seen[directory] = True
49 added_flag = True
51 notifier.process_events()
53 if notifier.check_events():
54 notifier.read_events()
56 self.msleep(200)
58 notifier.stop()