Entry 759

InotifyDirWatch

   

Submitted by Leo Soto on May 7, 2008 at 10:52 p.m.
Language: Python. Code size: 1.1 KB.

class InotifyDirWatch(BaseDirWatch):
    def start_watch(self, path_to_watch):
        from pyinotify import WatchManager, Notifier, EventsCodes, ProcessEvent
        watch_manager = WatchManager()
        class DummyProcessEvent(ProcessEvent):
            "Does nothing, used to flush the Notifier queue"
            def process_default(self, event):
                pass
        self.__notifier = Notifier(watch_manager, DummyProcessEvent())
        mask = (EventsCodes.IN_MODIFY | EventsCodes.IN_CREATE |
                EventsCodes.IN_DELETE | EventsCodes.IN_MOVED_FROM |
                EventsCodes.IN_MOVED_TO)
        watch_manager.add_watch(path_to_watch, mask, rec=True)

    def stop_watch(self):
        self.__notifier.stop()

    def check_for_changes(self):
        if self.__notifier.check_events(timeout=200):
            self.__notifier.read_events()
            # Just flushes the queue, .because we already know what we wanted to
            # know: there was some modification.
            self.__notifier.process_events()
            return True
        return False

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).