3 from watchdog.events import DirDeletedEvent, FileDeletedEvent, FileSystemEventHandler
4 from watchdog.observers import Observer
6 class DirTrigger(FileSystemEventHandler):
8 def __init__(self, dir_path, cool_time, max_time):
9 self._got_event = False
10 self._last_event_time = 0.0
11 self._last_reset_time = 0.0
12 self._cool_time = cool_time
13 self._max_time = max_time
14 self._lock = threading.Lock()
15 self._observer = Observer()
16 self._observer.schedule(self, path=dir_path, recursive=True)
19 self._observer.start()
24 def is_triggering(self):
27 return ( self._got_event and
28 (time.time() - self._last_event_time > self._cool_time) ) or \
29 time.time() - self._last_reset_time > self._max_time
34 self._got_event = False
35 self._last_reset_time = time.time()
37 def on_any_event(self, event):
38 if event.event_type == FileDeletedEvent or event.event_type == DirDeletedEvent:
43 self._got_event = True
44 self._last_event_time = time.time()