-
Notifications
You must be signed in to change notification settings - Fork 689
/
polling_observer.py
246 lines (202 loc) · 9.02 KB
/
polling_observer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# -*- coding: utf-8 -*-
# polling_observer.py: Generic polling observer implementation.
#
# Copyright (C) 2010 Gora Khargosh <gora.khargosh@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
:module: watchdog.observers.polling_observer
:author: Gora Khargosh <gora.khargosh@gmail.com>
"""
from __future__ import with_statement
import os.path
import threading
try:
import queue
except ImportError:
import Queue as queue
#from watchdog.utils import echo
from watchdog.utils.dirsnapshot import DirectorySnapshot
from watchdog.utils import \
real_absolute_path, \
absolute_path, \
DaemonThread
from watchdog.utils import EventEmitter
from watchdog.events import \
DirMovedEvent, \
DirDeletedEvent, \
DirCreatedEvent, \
DirModifiedEvent, \
FileMovedEvent, \
FileDeletedEvent, \
FileCreatedEvent, \
FileModifiedEvent, \
EventQueue
class _PollingEventEmitter(EventEmitter):
"""Daemon thread that monitors a given path recursively and emits
file system events.
"""
def __init__(self, path, event_queue,
recursive=False, interval=1):
"""Monitors a given path and appends file system modification
events to the output queue."""
super(_PollingEventEmitter, self).__init__(path,
event_queue,
recursive,
interval)
self._snapshot = DirectorySnapshot(path, recursive=recursive)
#@echo.echo
def _get_directory_snapshot_diff(self):
"""Obtains a diff of two directory snapshots."""
with self.lock:
new_snapshot = DirectorySnapshot(self.path,
recursive=self.is_recursive)
diff = new_snapshot - self._snapshot
self._snapshot = new_snapshot
return diff
def run(self):
"""
Appends events to the output event queue
based on the diff between two states of the same directory.
"""
while not self.is_stopped:
self.stopped_event.wait(self.interval)
diff = self._get_directory_snapshot_diff()
for path in diff.files_deleted:
self.event_queue.put(FileDeletedEvent(path))
for path in diff.files_modified:
self.event_queue.put(FileModifiedEvent(path))
for path in diff.files_created:
self.event_queue.put(FileCreatedEvent(path))
for path, dest_path in diff.files_moved.items():
self.event_queue.put(FileMovedEvent(path, dest_path))
for path in diff.dirs_modified:
self.event_queue.put(DirModifiedEvent(path))
for path in diff.dirs_deleted:
self.event_queue.put(DirDeletedEvent(path))
for path in diff.dirs_created:
self.event_queue.put(DirCreatedEvent(path))
for path, dest_path in diff.dirs_moved.items():
self.event_queue.put(DirMovedEvent(path, dest_path))
class PollingObserver(DaemonThread):
"""Observer daemon thread that spawns threads for each path to be monitored.
"""
def __init__(self, interval=0.5):
super(PollingObserver, self).__init__(interval=interval)
self._lock = threading.Lock()
# All the emitters created as a result of scheduling a group of
# paths under a name.
self._emitters_for_name = {}
# Collection of all the emitters.
self._emitters = set()
# Used to detect emitters with duplicate signatures.
self._emitter_for_signature = {}
# Event queue that will be filled by worker-emitter threads with events.
self._event_queue = EventQueue()
# Maintains a mapping of names to their event handlers.
# There's a one-to-one mapping between names and event handlers.
self._handler_for_name = {}
@property
def event_queue(self):
return self._event_queue
#@echo.echo
def create_event_emitter(self, path, handler, event_queue, recursive, interval):
return _PollingEventEmitter(path=path,
handler=handler,
event_queue=event_queue,
recursive=recursive,
interval=interval)
#@echo.echo
def schedule(self, name, event_handler, paths=None, recursive=False):
"""Schedules monitoring specified paths and calls methods in the
given callback handler based on events occurring in the file system.
"""
if not paths:
raise ValueError('Please specify a few paths.')
if isinstance(paths, basestring):
paths = [paths]
with self._lock:
if name in self._emitters_for_name:
raise ValueError("Duplicate watch entry named '%s'" % name)
self._emitters_for_name[name] = set()
for path in paths:
if not isinstance(path, basestring):
raise TypeError("Path must be string, not '%s'." %
type(path).__name__)
path = real_absolute_path(path)
if not os.path.isdir(path):
raise ValueError("Path '%s' is not a directory." % path)
try:
# If we have an emitter for this path already with
# this signature, we don't create a new
# emitter. Instead we add the handler to the event
# object, which when dispatched calls the handler code.
emitter = self._emitter_for_signature[(path, recursive)]
emitter.add_handler(handler)
except KeyError:
# Create a new emitter and start it.
emitter = self.create_event_emitter(path=path,
handler=event_handler,
event_queue=self.event_queue,
recursive=recursive,
interval=self.interval)
self._handler_for_name[name] = event_handler
self._emitters_for_name[name].add(emitter)
self._emitters.add(emitter)
self._emitter_for_signature[(path, recursive)] = emitter
for emitter in self._emitters:
if not emitter.is_alive():
emitter.start()
def unschedule(self, *names):
"""Stops monitoring specified paths for file system events."""
with self._lock:
for name in names:
# Each handler is given a name.
handler = self._handler_for_name[name]
for emitter in self._emitters_for_name[name]:
if handler in emitter.handlers:
emitter.remove_handler(handler)
if not emitter.handlers:
emitter.stop()
del self._emitter_for_signature[(emitter.path, emitter.is_recursive)]
del self._emitters_for_name[name]
@property
def __handlers(self):
return self._handlers
def __add_handler(self, handler):
with self._lock:
self._handlers.add(handler)
def __remove_handler(self, handler):
with self._lock:
self._handlers.remove(handler)
def run(self):
while not self.is_stopped:
try:
event = self.event_queue.get(block=True, timeout=self.interval)
event.dispatch()
self.event_queue.task_done()
except queue.Empty:
continue
self._clean_up()
def _clean_up(self):
for emitter in self._emitters:
emitter.join()
self._emitters_for_name.clear()
self._emitter_for_signature.clear()
self._emitters.clear()