-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch.py
50 lines (40 loc) · 1.31 KB
/
watch.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
"""
I would have liked to use `asyncinotify` or `minotaur`, but those don't give the
full path.
"""
from typing import Union
import logging
import re
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEvent, FileSystemMovedEvent, \
FileSystemEventHandler
from constants import env
from process_image import process_image
WATCH_DIR = Path(env('WATCH_DIR'))
class EventHandler(FileSystemEventHandler):
def on_any_event(self, event: Union[FileSystemEvent, FileSystemMovedEvent]):
if event.is_directory:
return
path = Path(event.dest_path if isinstance(event, FileSystemMovedEvent)
else event.src_path)
if not re.match(r'IMG_\d{8}_\d{6}\.jpg', path.name):
return
try:
process_image(path)
except Exception as e:
print(e)
def setup_watch():
assert WATCH_DIR.exists()
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
observer = Observer()
observer.schedule(EventHandler(), WATCH_DIR, recursive=True)
observer.start()
try:
while observer.is_alive():
observer.join(1)
finally:
observer.stop()
observer.join()