From 8839f388130fbe3cda93505722c7bd3f440bf958 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 5 Mar 2016 19:24:10 +0100 Subject: [PATCH] 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) --- frontends/src/jp/cmd_blog.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/frontends/src/jp/cmd_blog.py b/frontends/src/jp/cmd_blog.py index 306d8b27..22cea448 100644 --- a/frontends/src/jp/cmd_blog.py +++ b/frontends/src/jp/cmd_blog.py @@ -374,6 +374,7 @@ class Preview(base.CommandBase, BlogCommon): 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 @@ class Preview(base.CommandBase, BlogCommon): 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):