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)
This commit is contained in:
Goffi 2016-03-05 19:24:10 +01:00
commit 8839f38813

View file

@ -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):