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):
28 return ( self._got_event and
29 (now - self._last_event_time > self._cool_time) ) or \
30 now - self._last_reset_time > self._max_time
37 self._got_event = False
38 self._last_reset_time = time.time()
42 def on_any_event(self, event):
43 if event.event_type == FileDeletedEvent or event.event_type == DirDeletedEvent:
48 self._got_event = True
49 self._last_event_time = time.time()