]> git.treefish.org Git - photosort.git/blob - src/dirtrigger.py
continue with daemon implementation
[photosort.git] / src / dirtrigger.py
1 import threading
2 import time
3 from watchdog.events import FileSystemEventHandler
4 from watchdog.observers import Observer
5
6 class DirTrigger(FileSystemEventHandler):
7     COOL_TIME = 10
8     MAX_TIME = 86400
9
10     def __init__(self, dir_path):
11         self._got_event = False
12         self._last_event_time = 0.0
13         self._last_reset_time = 0.0
14         self._lock = threading.Lock()
15         self._observer = Observer()
16         self._observer.schedule(self, path=dir_path, recursive=True)
17
18     def start(self):
19         self._observer.start()
20
21     def stop(self):
22         self._observer.stop()
23
24     def is_triggering(self):
25         self._lock.acquire()
26         try:
27             return ( self._got_event and
28                      (time.time() - self._last_event_time > DirTrigger.COOL_TIME) ) or \
29                      time.time() - self._last_reset_time > DirTrigger.MAX_TIME
30         finally:
31             self._lock.release()
32
33     def reset(self):
34         self._got_event = False
35         self._last_reset_time = time.time()
36
37     def on_any_event(self, event):
38         self._lock.acquire()
39         try:
40             self._got_event = True
41             self._last_event_time = time.time()
42         finally:
43             self._lock.release()