Make DirectoryMonitor pausable using the with statement

This commit is contained in:
Kristóf Tóth 2018-02-14 17:44:05 +01:00
parent 0b3db0ae9e
commit c2e161dad3
2 changed files with 16 additions and 5 deletions

View File

@ -42,3 +42,15 @@ class DirectoryMonitor:
def stop(self):
self.observer.stop()
self.observer.join()
@property
def pauser(self):
return DirectoryMonitor.Pauser(self)
class Pauser:
def __init__(self, directory_monitor):
self.directorymonitor = directory_monitor
def __enter__(self):
self.directorymonitor.pause()
def __exit__(self, exc_type, exc_val, exc_tb):
self.directorymonitor.resume()

View File

@ -68,11 +68,10 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
return data
def write(self, data):
self.monitor.pause()
try: self.filemanager.file_contents = data['content']
except Exception: log.exception('Error writing file!')
self.restart_process()
self.monitor.resume()
with self.monitor.pauser:
try: self.filemanager.file_contents = data['content']
except Exception: log.exception('Error writing file!')
self.restart_process()
return data
def select(self, data):