6

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()
Errol Fitzgerald
  • 2,978
  • 2
  • 26
  • 34
  • 1
    I don't know, but I think you should try https://github.com/seb-m/pyinotify and see if it has the same problem. – John Zwinck Apr 05 '14 at 09:58
  • Begin by figuring out what sublime text *actually* does when saving files. E.g. use `inotifywait -m .` to confirm the create/delete events you expect does happen in fact occur. Otherwise it's all speculations. I'm assuming there's no such issues when using touch and rm. – gwohpq9 Apr 12 '14 at 10:00
  • if you change your MyHandler class to watchdogs LoggingEventHandler(), u can track the changes back. However, i am not sure why it is losing the changes when we write our own handler – user1 Mar 11 '18 at 20:58

1 Answers1

2

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
Community
  • 1
  • 1
Juan E.
  • 1,808
  • 16
  • 28
  • I am aware of this, I was inquiring about the fact that I do not get an event for the actual file as well. – Errol Fitzgerald Apr 08 '14 at 04:31
  • If you are aware of this you should know that you won't get an event for the actual file unless you change the behaviour of your editor. The file that watchdog is watching gets deleted when you save your changes, in that moment watchdog losses the pointer to the file. A new file with the same name as the file you watched gets created when your last temp file is renamed. – Juan E. Apr 08 '14 at 11:45
  • You can also patch watchdog so that it doesn't check if it is watching the same inode https://github.com/gorakhargosh/watchdog/issues/56#issuecomment-1796482 – Juan E. Apr 08 '14 at 12:10
  • At some point the actual file `home/john/resources/css/style.css` is either being created, deleted, moved or modified. There should be an event for this. If I close the file, and sublime, how does watchdog not fire an event. The actual file needs to be written to at some point. – Errol Fitzgerald Apr 09 '14 at 04:06
  • 1
    You should check the issues in github, you will find that watchdog uses inotify in Linux and that causes plenty of bugs with deleted and moved files (what sublime does when it uses atomic save). https://github.com/gorakhargosh/watchdog/search?q=move+linux&type=Issues https://github.com/gorakhargosh/watchdog/search?q=delete+linux&type=Issues – Juan E. Apr 09 '14 at 05:27
  • 1
    @JuanE. of which all except one (moving files from an *unwatched* folder to a watched folder) is closed. – gwohpq9 Apr 12 '14 at 10:03