From 0038663bc6f274b37ad89d1cdc580d1201f628e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 24 May 2018 16:19:04 +0200 Subject: [PATCH] Allow listening to inotify events in additional directories using IdeEH --- lib/tfw/components/directory_monitor.py | 6 ++++-- lib/tfw/components/ide_event_handler.py | 15 +++++++++++++-- lib/tfw/mixins/monitor_manager_mixin.py | 6 +++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 2fa724c..ba423be 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -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 diff --git a/lib/tfw/components/ide_event_handler.py b/lib/tfw/components/ide_event_handler.py index b7bd790..7c9fda3 100644 --- a/lib/tfw/components/ide_event_handler.py +++ b/lib/tfw/components/ide_event_handler.py @@ -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, diff --git a/lib/tfw/mixins/monitor_manager_mixin.py b/lib/tfw/mixins/monitor_manager_mixin.py index 0c39725..16060ce 100644 --- a/lib/tfw/mixins/monitor_manager_mixin.py +++ b/lib/tfw/mixins/monitor_manager_mixin.py @@ -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