2 # Copyright (c) 2008 David Aguilar
3 """This module provides an inotify plugin for Linux and other systems
4 which provide the pyinotify module.
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
19 class FileSysEvent(ProcessEvent
):
20 def __init__(self
, parent
):
21 ProcessEvent
.__init
__(self
)
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:
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
35 self
.path
= git
.get_work_tree()
38 self
.mask
= (EventsCodes
.IN_CREATE |
39 EventsCodes
.IN_DELETE |
40 EventsCodes
.IN_MODIFY |
41 EventsCodes
.IN_MOVED_TO
)
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
56 # Only capture those events that git cares about
57 self
.wm
= WatchManager()
58 notifier
= Notifier(self
.wm
, FileSysEvent(self
))
59 self
.notifier
= notifier
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
)
70 notifier
.process_events()
71 if notifier
.check_events(timeout
=250):
72 notifier
.read_events()