1

I want to create a module that will watch for folder. I write some of code:

import os, pyinotify

class FileWatcher:
    def start_watch(self, dir):
        wm = pyinotify.WatchManager()
        self.notifier = pyinotify.Notifier(wm, EventProcessor())
        mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_DELETE_SELF | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
        wdd = wm.add_watch(dir, mask, rec=True)
        while True:
            self.notifier.process_events()
            if self.notifier.check_events():
                self.notifier.read_events()

    def stop_watch(self):
        self.notifier.stop()
        print ('\nWatcher stopped')

class EventProcessor(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        print('in CREATE')

    def process_IN_MODIFY(self, event):
        print('in MODIFY')

    def process_IN_DELETE(self, event):
        print('in delete')

    def process_IN_DELETE_SELF(self, event):
        print('in delete self')

    def process_IN_MOVED_FROM(self, event):
        print('in MOVED_FROM')

    def process_IN_MOVED_TO(self, event):
        print('in IN_MOVED_TO')

if __name__ == "__main__":
    watcher = FileWatcher()
    try:
        folder = "/home/user/Desktop/PythonFS"
        watcher.start_watch(folder)
    except KeyboardInterrupt:
        watcher.stop_watch()    

When i modified a file and then removed it the methods process_IN_MODIFY and process_IN_DELETE was never called. How cat i solve it?

But when i create a file the method process_IN_CREATE() was called.

OS is Linux mint 13.

UPD: New Code

Vetalll
  • 3,472
  • 6
  • 24
  • 34
  • 1
    Umm, this code isn't syntactically valid (indentation is missing, just paste it, select it, and press `Ctrl+K`), and it only defines classes and functions, and does not call any of them. – phihag Mar 26 '13 at 16:41
  • It's syntatically vallid code.. Methods should called from notifier, because i put Instance of EventProcessor to constructor of pyinotify.Notifier(...) – Vetalll Mar 26 '13 at 16:47
  • [No it's not](http://ideone.com/NJGsub). And why there are some callable functions in there, the code doesn't actually call them. – phihag Mar 26 '13 at 16:50
  • oh sorry. Parser of code works bad. And ctrl+k doesn't help to me. I changed this line. – Vetalll Mar 26 '13 at 16:54
  • stop_watch and start_watch called from another module.It simple start and stop. I think they are not touching my problem. – Vetalll Mar 26 '13 at 16:59
  • If so, you should be able to reproduce the problem with a [simplified version](http://sscce.org/). If the problem only occurs when you're calling the start/stop functions from another module, you need to post that module. – phihag Mar 26 '13 at 17:03
  • I posted new code with same problem.. – Vetalll Mar 26 '13 at 17:18

1 Answers1

1

Try the following code. It is basically the same as your code; I've only added

f = FileWatcher()
f.start_watch('/tmp/test', None)

at the end to start a FileWatcher. Make sure the directory /tmp/test exists, or else change that line to point to an existent directory.

If a file foo exists in /tmp/test, and if I modify this file, the above program prints

in create   # after modification
in modify   # after saving
in modify
in delete

Now if I delete the file, the program prints:

in delete

import os
import pyinotify


class FileWatcher:
    notifier = None

    def start_watch(self, dir, callback):
        wm = pyinotify.WatchManager()
        self.notifier = pyinotify.Notifier(wm, EventProcessor(callback))
        mask = (pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_DELETE
                | pyinotify.IN_DELETE_SELF | pyinotify.IN_MOVED_FROM
                | pyinotify.IN_MOVED_TO)
        wdd = wm.add_watch(dir, mask, rec=True)
        while True:
            self.notifier.process_events()
            if self.notifier.check_events():
                self.notifier.read_events()


class EventProcessor(pyinotify.ProcessEvent):
    def __init__(self, callback):
        self.event_callback = callback

    def process_IN_CREATE(self, event):
        # if self.event_callback is not None:
        # self.event_callback.on_file_created(os.path.join(event.path,
        # event.name))
        print('in create')

    def process_IN_MODIFY(self, event):
        # if self.event_callback is not None:
        # self.event_callback.on_file_modifed(os.path.join(event.path,
        # event.name))
        print('in modify')

    def process_IN_DELETE(self, event):
        print('in delete')

    def process_IN_DELETE_SELF(self, event):
        print('in delete self')

    def process_IN_MOVED_FROM(self, event):
        print('in moved_from')

    def process_IN_MOVED_TO(self, event):
        print('in moved to')


f = FileWatcher()
f.start_watch('/tmp/test', None)

By the way, once you call f.start_watch, the process falls into a while True loop from which it can not escape. Even calling f.stop_watch from another thread will not somehow break you out of this while loop.

If you do plan on using threading, you may need to pass a threading.Event to start_watch, and check its state inside the while-loop to determine when to break out of the loop.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677