I think you basically have the right idea, but that it could be implemented more easily.
The ProcessEvent
class in the pyinotify module already has a hook you can use to filter the processing of events. It's specified via an optional pevent
keyword argument given on the call to the constructor and is saved in the instance's self.pevent
attribute. The default value is None
. It's value is used in the class' __call__()
method as shown in the following snippet from the pyinotify.py
source file:
def __call__(self, event):
stop_chaining = False
if self.pevent is not None:
# By default methods return None so we set as guideline
# that methods asking for stop chaining must explicitly
# return non None or non False values, otherwise the default
# behavior will be to accept chain call to the corresponding
# local method.
stop_chaining = self.pevent(event)
if not stop_chaining:
return _ProcessEvent.__call__(self, event)
So you could use it only allow events for files with certain suffixes (aka extensions) with something like this:
SUFFIXES = {".md", ".markdown"}
def suffix_filter(event):
# return True to stop processing of event (to "stop chaining")
return os.path.splitext(event.name)[1] not in SUFFIXES
processevent = ProcessEvent(pevent=suffix_filter)