cola: add more documentation strings to the cola modules
[git-cola.git] / cola / inotify.py
blobbc0260f30a9b21760bb2aec395136cf778745635
1 #!/usr/bin/env python
2 # Copyright (c) 2008 David Aguilar
3 """This module provides an inotify plugin for Linux and other systems
4 which provide the pyinotify module.
6 """
8 import os
9 import time
10 from PyQt4.QtCore import QCoreApplication
11 from PyQt4.QtCore import QThread
12 from PyQt4.QtCore import QEvent
13 from PyQt4.QtCore import SIGNAL
14 from pyinotify import ProcessEvent
15 from pyinotify import WatchManager, Notifier, EventsCodes
17 import defaults
19 class FileSysEvent(ProcessEvent):
20 def __init__(self, parent):
21 ProcessEvent.__init__(self)
22 self.parent = parent
23 self.last_event_time = time.time()
24 def process_default(self, event):
25 # Prevent notificaiton floods
26 if time.time() - self.last_event_time > 1.0:
27 self.parent.notify()
28 self.last_event_time = time.time()
30 class GitNotifier(QThread):
31 def __init__(self, receiver, git):
32 QThread.__init__(self)
33 self.receiver = receiver
34 self.git = git
35 self.path = git.get_work_tree()
36 self.abort = False
37 self.dirs_seen = {}
38 self.mask = (EventsCodes.IN_CREATE |
39 EventsCodes.IN_DELETE |
40 EventsCodes.IN_MODIFY |
41 EventsCodes.IN_MOVED_TO)
43 def notify(self):
44 if not self.abort:
45 event_type = QEvent.Type(defaults.INOTIFY_EVENT)
46 event = QEvent(event_type)
47 QCoreApplication.postEvent(self.receiver, event)
49 def watch_directory(self, directory):
50 directory = os.path.realpath(directory)
51 if directory not in self.dirs_seen:
52 self.wm.add_watch(directory, self.mask)
53 self.dirs_seen[directory] = True
55 def run(self):
56 # Only capture those events that git cares about
57 self.wm = WatchManager()
58 notifier = Notifier(self.wm, FileSysEvent(self))
59 self.notifier = notifier
60 dirs_seen = {}
61 added_flag = False
62 while not self.abort:
63 if not added_flag:
64 self.watch_directory(self.path)
65 # Register files/directories known to git
66 for file in self.git.ls_files().splitlines():
67 directory = os.path.dirname(file)
68 self.watch_directory(directory)
69 added_flag = True
70 notifier.process_events()
71 if notifier.check_events(timeout=250):
72 notifier.read_events()
73 notifier.stop()