mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 00:11:33 +00:00
Implement proof of concept directory event monitoring
This commit is contained in:
parent
4582538060
commit
9c006451bf
26
lib/tfw/components/directory_monitor.py
Normal file
26
lib/tfw/components/directory_monitor.py
Normal 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()
|
@ -3,6 +3,7 @@ from glob import glob
|
|||||||
|
|
||||||
from tfw.util import SupervisorMixin
|
from tfw.util import SupervisorMixin
|
||||||
from tfw.event_handler_base import EventHandlerBase
|
from tfw.event_handler_base import EventHandlerBase
|
||||||
|
from tfw.components.directory_monitor import DirectoryMonitor
|
||||||
|
|
||||||
from tfw.config.logs import logging
|
from tfw.config.logs import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -56,6 +57,9 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
|||||||
'select': self.select
|
'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):
|
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 :('
|
||||||
|
@ -2,3 +2,4 @@ tornado==4.5.3
|
|||||||
pyzmq==16.0.4
|
pyzmq==16.0.4
|
||||||
transitions==0.6.4
|
transitions==0.6.4
|
||||||
terminado==0.8.1
|
terminado==0.8.1
|
||||||
|
watchdog==0.8.3
|
||||||
|
Loading…
Reference in New Issue
Block a user