Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python watchdog not firing all events

I am trying to watch some text files for when they are modified using watchdog but I only seem to get events for .tmp files. I understand this is how sublime text is saving files, but shouldn't I also get an event fired for the actual file too?

This is what I get when trying to save a file at the location /home/john/resources/css/style.css in sublime text:

/home/john/resources/css/.sublaa.tmp
/home/john/resources/css/.sublaa.tmp
/home/john/resources/css/.sublaa.tmp

It seems I only get events fired for the tmp files, but not for the actual file. This actually works fine on MacOSX, but not Ubuntu.

#!/usr/bin/python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class MyHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        print event.src_path

if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path='.', recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
like image 451
Errol Fitzgerald Avatar asked Apr 05 '14 05:04

Errol Fitzgerald


1 Answers

This is a common problem with editors that create temporary files. In the watchdog package page ( https://pypi.python.org/pypi/watchdog ) you can find this note regarding vim:

About using watchdog with editors like Vim

Vim does not modify files unless directed to do so. It creates backup files and then swaps them in to replace the files you are editing on the disk. This means that if you use Vim to edit your files, the on-modified events for those files will not be triggered by watchdog. You may need to configure Vim to appropriately to disable this feature.

In Sublime to disable the creation of tmp files you must go to Preferences --> Settings-User and disable atomic saves.

"atomic_save": false
like image 94
Juan E. Avatar answered Sep 18 '22 16:09

Juan E.