models/utils: more unicode fixes
[git-cola.git] / cola / inotify.py
blob20c44826e1cfa9c317f5408674450ff4d8871a1e
1 #!/usr/bin/env python
2 # Copyright (c) 2008 David Aguilar
3 import os
4 import time
5 from PyQt4.QtCore import QCoreApplication
6 from PyQt4.QtCore import QThread
7 from PyQt4.QtCore import QEvent
8 from PyQt4.QtCore import SIGNAL
9 from pyinotify import ProcessEvent
10 from pyinotify import WatchManager, Notifier, EventsCodes
12 import defaults
14 class FileSysEvent(ProcessEvent):
15 def __init__(self, parent):
16 ProcessEvent.__init__(self)
17 self.parent = parent
18 self.last_event_time = time.time()
19 def process_default(self, event):
20 # Prevent notificaiton floods
21 if time.time() - self.last_event_time > 1.0:
22 self.parent.notify()
23 self.last_event_time = time.time()
25 class GitNotifier(QThread):
26 def __init__(self, receiver, git):
27 QThread.__init__(self)
28 self.receiver = receiver
29 self.git = git
30 self.path = git.get_work_tree()
31 self.abort = False
32 self.dirs_seen = {}
33 self.mask = (EventsCodes.IN_CREATE |
34 EventsCodes.IN_DELETE |
35 EventsCodes.IN_MODIFY |
36 EventsCodes.IN_MOVED_TO)
38 def notify(self):
39 if not self.abort:
40 event_type = QEvent.Type(defaults.INOTIFY_EVENT)
41 event = QEvent(event_type)
42 QCoreApplication.postEvent(self.receiver, event)
44 def watch_directory(self, directory):
45 directory = os.path.realpath(directory)
46 if directory not in self.dirs_seen:
47 self.wm.add_watch(directory, self.mask)
48 self.dirs_seen[directory] = True
50 def run(self):
51 # Only capture those events that git cares about
52 self.wm = WatchManager()
53 notifier = Notifier(self.wm, FileSysEvent(self))
54 self.notifier = notifier
55 dirs_seen = {}
56 added_flag = False
57 while not self.abort:
58 if not added_flag:
59 self.watch_directory(self.path)
60 # Register files/directories known to git
61 for file in self.git.ls_files().splitlines():
62 directory = os.path.dirname(file)
63 self.watch_directory(directory)
64 added_flag = True
65 notifier.process_events()
66 if notifier.check_events(timeout=250):
67 notifier.read_events()
68 notifier.stop()