Implement proof of concept directory event monitoring

This commit is contained in:
Kristóf Tóth 2018-02-12 16:01:24 +01:00
parent 4582538060
commit 9c006451bf
3 changed files with 31 additions and 0 deletions

View File

@ -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()

View File

@ -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 :('

View File

@ -2,3 +2,4 @@ tornado==4.5.3
pyzmq==16.0.4
transitions==0.6.4
terminado==0.8.1
watchdog==0.8.3