Allow listening to inotify events in additional directories using IdeEH

This commit is contained in:
Kristóf Tóth 2018-05-24 16:19:04 +02:00
parent 6489e4452e
commit 0038663bc6
3 changed files with 20 additions and 7 deletions

View File

@ -15,10 +15,12 @@ LOG = logging.getLogger(__name__)
class DirectoryMonitor(ObserverMixin):
def __init__(self, directory):
def __init__(self, directories):
ObserverMixin.__init__(self)
self.eventhandler = IdeReloadWatchdogEventHandler()
self.observer.schedule(self.eventhandler, directory, recursive=True)
for directory in directories:
self.observer.schedule(self.eventhandler, directory, recursive=True)
self.pause, self.resume = self.eventhandler.pause, self.eventhandler.resume
@property

View File

@ -98,17 +98,24 @@ class IdeEventHandler(EventHandlerBase, MonitorManagerMixin):
By default all files in the directory specified in __init__ are displayed
on the fontend. Note that this is a stateful component.
When any file in the selected directory changes they are automatically refreshed
on the frontend (this is done by listening to inotify events).
This EventHandler accepts messages that have a data["command"] key specifying
a command to be executed.
The API of each command is documented in their respective handlers.
"""
def __init__(self, key, directory, allowed_directories, selected_file=None, exclude=None):
def __init__(self, key, directory, allowed_directories, selected_file=None, exclude=None,
additional_watched_directories=None):
"""
:param key: the key this instance should listen to
:param directory: working directory which the EventHandler should serve files from
:param allowed_directories: list of directories that can be switched to using the selectdir command
:param selected_file: file that is selected by default
:param exclude: list of filenames that should not appear between files (for *.o, *.pyc, etc.)
:param additional_watched_directories: refresh the selected file when files change in these directories
(the working directory is watched by default, this is useful for
symlinks and such)
"""
super().__init__(key)
try:
@ -116,7 +123,11 @@ class IdeEventHandler(EventHandlerBase, MonitorManagerMixin):
selected_file=selected_file, exclude=exclude)
except IndexError:
raise EnvironmentError(f'No file(s) in IdeEventHandler working_directory "{directory}"!')
MonitorManagerMixin.__init__(self, DirectoryMonitor, self.filemanager.workdir)
self.watched_directories = [self.filemanager.workdir]
if additional_watched_directories:
self.watched_directories.extend(additional_watched_directories)
MonitorManagerMixin.__init__(self, DirectoryMonitor, self.watched_directories)
self.commands = {'read': self.read,
'write': self.write,

View File

@ -7,10 +7,10 @@ LOG = logging.getLogger(__name__)
class MonitorManagerMixin:
def __init__(self, monitor_type, directory):
def __init__(self, monitor_type, directories):
self._monitor_type = monitor_type
self._monitor = None
self._monitored_directory = directory
self._monitored_directories = directories
self.reload_monitor()
@property
@ -23,5 +23,5 @@ class MonitorManagerMixin:
self._monitor.stop()
except KeyError:
LOG.debug('Working directory was removed ignoring...')
self._monitor = self._monitor_type(self._monitored_directory)
self._monitor = self._monitor_type(self._monitored_directories)
self._monitor.watch() # This runs on a separate thread