Pyinotify
Jump to navigation
Jump to search
classes
WatchManager
wm = pyinotify.WatchManager()
- The watch manager stores the watches and provides operations on watches.
dict_of_watch_descriptors = wm.add_watch( '/dir', event_flags)
- sym links not followed
- can also call with rec=True for recursive
- dict_of_watch_descriptors = { keys: path; values: corresponding watch descriptors }
- to look for new directories created, use arg
auto_add=True
and make sure to search with flag pyinotify.IN_CREATE
ProcessEvent
class myEventHandler( pyinotify.ProcessEvent ): ...
- Fires off methods whose name follows convention
def process_EVENT_TYPE( self, event ):
- use "process_default" method as a catchall with the lowest precedence
Notifier
notifier = pyinotify.Notifier( wm, instanceOfMyventHandler )
- can also call with arg
timeout=10
seconds if you don't want to block
notifier.loop()
to start polling, will block until SIGINT is sent
- can add as optional argument to
loop( callback=somefunction )
- Don't forget to call
notifier.stop()
when finished (e.g., in a finally block of a try-catch-finally block)
- Coalesce multiple open-write-close events into one using
notifier.coalesce_events()
while using a notifier with a timeout
Example
#!/usr/bin/env python
import pyinotify
import os
import stat
def ProcessFile( path ):
print "Processing " + path
class onImageSavedToDisk( pyinotify.ProcessEvent ):
def process_IN_CLOSE_WRITE( self, event ):
print "close-write: {0} raised a {1} event.".format( event.pathname, event.maskname )
if os.stat( event.pathname )[ stat.ST_SIZE ] > 1:
ProcessFile( event.pathname )
wm = pyinotify.WatchManager()
# create a notifier that checks the target directory every 5 seconds
handler = onImageSavedToDisk()
notifier = pyinotify.Notifier( wm, read_freq=5, default_proc_fun=handler )
# The reason why we only check every 5 seconds is that we coalesce
# multiple open-write-close notifications
notifier.coalesce_events()
wm.add_watch( '/home/colettace/tmp', pyinotify.IN_CLOSE_WRITE )# , rec=True, auto_add=True )
try:
notifier.loop()
finally:
notifier.stop()