2 # Copyright (c) 2008 David Aguilar
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
14 class FileSysEvent(ProcessEvent
):
15 def __init__(self
, parent
):
16 ProcessEvent
.__init
__(self
)
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:
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
30 self
.path
= git
.get_work_tree()
33 self
.mask
= (EventsCodes
.IN_CREATE |
34 EventsCodes
.IN_DELETE |
35 EventsCodes
.IN_MODIFY |
36 EventsCodes
.IN_MOVED_TO
)
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
51 # Only capture those events that git cares about
52 self
.wm
= WatchManager()
53 notifier
= Notifier(self
.wm
, FileSysEvent(self
))
54 self
.notifier
= notifier
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
)
65 notifier
.process_events()
66 if notifier
.check_events(timeout
=250):
67 notifier
.read_events()