diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py new file mode 100644 index 0000000..18ddc4c --- /dev/null +++ b/lib/tfw/components/directory_monitor.py @@ -0,0 +1,26 @@ +from watchdog.observers import Observer +from watchdog.events import FileSystemEventHandler + +from tfw.message_sender import MessageSender + + +class MessagingEventHandler(FileSystemEventHandler): + def __init__(self): + super().__init__() + self.message_sender = MessageSender() + + def on_modified(self, event): + self.message_sender.send('Watchdog', 'CECA') + + +class DirectoryMonitor: + def __init__(self, directory): + self.observer = Observer() + self.observer.schedule(MessagingEventHandler(), directory, recursive=True) + + def watch(self): + self.observer.start() + + def stop(self): + self.observer.stop() + self.observer.join() diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index abb5738..eb22dce 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -3,6 +3,7 @@ from glob import glob from tfw.util import SupervisorMixin from tfw.event_handler_base import EventHandlerBase +from tfw.components.directory_monitor import DirectoryMonitor from tfw.config.logs import logging log = logging.getLogger(__name__) @@ -56,6 +57,9 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): 'select': self.select } + self.monitor = DirectoryMonitor(directory) + self.monitor.watch() # This runs on a separate thread TODO: when to call stop()? + def read(self, data): try: data['content'] = self.filemanager.file_contents except PermissionError: data['content'] = 'You have no permission to open that file :(' diff --git a/requirements.txt b/requirements.txt index 5cfef8d..e057890 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ tornado==4.5.3 pyzmq==16.0.4 transitions==0.6.4 terminado==0.8.1 +watchdog==0.8.3