4 from PyQt4
.QtCore
import QCoreApplication
5 from PyQt4
.QtCore
import QThread
6 from PyQt4
.QtCore
import QEvent
7 from PyQt4
.QtCore
import SIGNAL
8 from pyinotify
import ProcessEvent
9 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
, path
):
27 QThread
.__init
__(self
)
28 self
.receiver
= receiver
34 event_type
= QEvent
.Type(defaults
.INOTIFY_EVENT
)
35 event
= QEvent(event_type
)
36 QCoreApplication
.postEvent(self
.receiver
, event
)
39 # Only capture those events that git cares about
40 mask
= ( EventsCodes
.IN_CREATE
41 | EventsCodes
.IN_DELETE
42 | EventsCodes
.IN_MODIFY
43 | EventsCodes
.IN_MOVED_TO
)
45 notifier
= Notifier(wm
, FileSysEvent(self
))
46 self
.notifier
= notifier
51 wm
.add_watch(self
.path
, mask
)
52 # Register files/directories known to git
53 for file in git
.ls_files().splitlines():
54 wm
.add_watch(file, mask
)
55 directory
= os
.path
.dirname(file)
56 if directory
not in dirs_seen
:
57 wm
.add_watch(directory
, mask
)
58 dirs_seen
[directory
] = True
60 notifier
.process_events()
61 if notifier
.check_events():
62 notifier
.read_events()