changeset 1886:f3db27508b31

jp (blog/preview): inotify improvments: - InotifyError are catched (can happen if file is deleted during preview for instance) - handle IN_DELETE_SELF event which happen when editor write content in a new file and switch to it (tested with vim)
author Goffi <goffi@goffi.org>
date Sat, 05 Mar 2016 19:24:10 +0100
parents edd8dc8df1b9
children 16527dd5a81b
files frontends/src/jp/cmd_blog.py
diffstat 1 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_blog.py	Sat Mar 05 19:21:35 2016 +0100
+++ b/frontends/src/jp/cmd_blog.py	Sat Mar 05 19:24:10 2016 +0100
@@ -374,6 +374,7 @@
             try:
                 import inotify.adapters
                 import inotify.constants
+                from inotify.calls import InotifyError
             except ImportError:
                 if self.args.inotify == 'auto':
                     inotify = None
@@ -429,18 +430,29 @@
         else:
             open_cb()
             i = inotify.adapters.Inotify(block_duration_s=60) # no need for 1 s duraction, inotify drive actions here
-            # XXX: we only check IN_CLOSE_WRITE, but that may need to be changed depending on editor
-            #      experience will tell the appropriate values
-            i.add_watch(self.current_file_path, mask=inotify.constants.IN_CLOSE_WRITE)
+
+            def add_watch():
+                i.add_watch(self.content_file_path, mask=inotify.constants.IN_CLOSE_WRITE |
+                                                         inotify.constants.IN_DELETE_SELF)
+            add_watch()
+
             try:
                 for event in i.event_gen():
                     if event is not None:
                         self.disp(u"Content updated", 1)
+                        if "IN_DELETE_SELF" in event[1]:
+                            self.disp(u"IN_DELETE_SELF event catched, changing the watch", 2)
+                            add_watch()
                         self.updateContent()
                         update_cb()
+            except InotifyError:
+                self.disp(u"Can't catch inotify events, as the file been deleted?", error=True)
             finally:
                 os.unlink(self.preview_file_path)
-                i.remove_watch(self.current_file_path)
+                try:
+                    i.remove_watch(self.content_file_path)
+                except InotifyError:
+                    pass
 
 
 class Import(base.CommandAnswering):