comparison frontends/src/jp/cmd_blog.py @ 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
comparison
equal deleted inserted replaced
1885:edd8dc8df1b9 1886:f3db27508b31
372 372
373 if self.args.inotify != 'false': 373 if self.args.inotify != 'false':
374 try: 374 try:
375 import inotify.adapters 375 import inotify.adapters
376 import inotify.constants 376 import inotify.constants
377 from inotify.calls import InotifyError
377 except ImportError: 378 except ImportError:
378 if self.args.inotify == 'auto': 379 if self.args.inotify == 'auto':
379 inotify = None 380 inotify = None
380 self.disp(u'inotify module not found, deactivating feature. You can install it with {install}'.format(install=INOTIFY_INSTALL)) 381 self.disp(u'inotify module not found, deactivating feature. You can install it with {install}'.format(install=INOTIFY_INSTALL))
381 else: 382 else:
427 self.disp(u'temporary file created at {}\nthis file will NOT BE DELETED AUTOMATICALLY, please delete it yourself when you have finished'.format(self.preview_file_path)) 428 self.disp(u'temporary file created at {}\nthis file will NOT BE DELETED AUTOMATICALLY, please delete it yourself when you have finished'.format(self.preview_file_path))
428 open_cb() 429 open_cb()
429 else: 430 else:
430 open_cb() 431 open_cb()
431 i = inotify.adapters.Inotify(block_duration_s=60) # no need for 1 s duraction, inotify drive actions here 432 i = inotify.adapters.Inotify(block_duration_s=60) # no need for 1 s duraction, inotify drive actions here
432 # XXX: we only check IN_CLOSE_WRITE, but that may need to be changed depending on editor 433
433 # experience will tell the appropriate values 434 def add_watch():
434 i.add_watch(self.current_file_path, mask=inotify.constants.IN_CLOSE_WRITE) 435 i.add_watch(self.content_file_path, mask=inotify.constants.IN_CLOSE_WRITE |
436 inotify.constants.IN_DELETE_SELF)
437 add_watch()
438
435 try: 439 try:
436 for event in i.event_gen(): 440 for event in i.event_gen():
437 if event is not None: 441 if event is not None:
438 self.disp(u"Content updated", 1) 442 self.disp(u"Content updated", 1)
443 if "IN_DELETE_SELF" in event[1]:
444 self.disp(u"IN_DELETE_SELF event catched, changing the watch", 2)
445 add_watch()
439 self.updateContent() 446 self.updateContent()
440 update_cb() 447 update_cb()
448 except InotifyError:
449 self.disp(u"Can't catch inotify events, as the file been deleted?", error=True)
441 finally: 450 finally:
442 os.unlink(self.preview_file_path) 451 os.unlink(self.preview_file_path)
443 i.remove_watch(self.current_file_path) 452 try:
453 i.remove_watch(self.content_file_path)
454 except InotifyError:
455 pass
444 456
445 457
446 class Import(base.CommandAnswering): 458 class Import(base.CommandAnswering):
447 def __init__(self, host): 459 def __init__(self, host):
448 super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog')) 460 super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog'))