-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpynotifyEventHandler.py
32 lines (22 loc) · 1011 Bytes
/
pynotifyEventHandler.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
import logging
import pyinotify
def startDirectoryMonitor(newSubjectsFolder, newSubjectHandler):
logging.debug('Creating custom event handler for pyinotify...')
handler = FileCreatedEventHandler(newSubjectHandler)
wm = pyinotify.WatchManager()
eventsFlag = pyinotify.IN_MOVED_TO | pyinotify.IN_CREATE
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch(newSubjectsFolder, eventsFlag)
logging.debug('Starting notifier infinite loop...')
notifier.loop()
class FileCreatedEventHandler(pyinotify.ProcessEvent):
def __init__(self, newSubjectHandler):
self.newSubjectHandler = newSubjectHandler
def process_IN_CREATE(self, event):
logging.debug("File {0}".format(event.pathname))
self.stopThread()
self.newSubjectHandler.newSubject(event.pathname)
def process_IN_MOVED_TO(self, event):
logging.debug("File {0}".format(event.pathname))
self.stopThread()
self.newSubjectHandler.newSubject(event.pathname)