Refactor DirectoryMonitor handling in SourceCodeEventHandler

This commit is contained in:
Kristóf Tóth 2018-03-09 09:37:48 +01:00
parent 3b241a3930
commit 0e4be3c22f

View File

@ -63,13 +63,18 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
'select': self.select, 'select': self.select,
'selectdir': self.select_dir} 'selectdir': self.select_dir}
self._monitor = DirectoryMonitor(directory) self._monitor = None
self._monitor.watch() # This runs on a separate thread self.reload_monitor()
@property @property
def monitor(self): def monitor(self):
return self._monitor return self._monitor
def reload_monitor(self):
if self._monitor: self._monitor.stop()
self._monitor = DirectoryMonitor(self.filemanager.workdir)
self._monitor.watch() # This runs on a separate thread
def read(self, data): def read(self, data):
try: data['content'] = self.filemanager.file_contents try: data['content'] = self.filemanager.file_contents
except PermissionError: data['content'] = 'You have no permission to open that file :(' except PermissionError: data['content'] = 'You have no permission to open that file :('
@ -92,9 +97,10 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
def select_dir(self, data): def select_dir(self, data):
try: try:
self.filemanager.workdir = data['directory'] self.filemanager.workdir = data['directory']
self.reload_monitor()
try: try:
self.filemanager.select_file(self.filemanager.files[0]) self.filemanager.select_file(self.filemanager.files[0])
data['content'] = self.filemanager.file_contents self.read(data)
except IndexError: except IndexError:
data['content'] = 'No files in this directory :(' data['content'] = 'No files in this directory :('
except EnvironmentError: except EnvironmentError: