baseimage-tutorial-framework/lib/tfw/components/directory_monitor.py
2018-02-13 15:06:19 +01:00

36 lines
993 B
Python

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
from tfw.util import RateLimiter
from tfw.config.logs import logging
log = logging.getLogger(__name__)
class WebideReloadEventHandler(FileSystemEventHandler):
def __init__(self):
super().__init__()
self.uplink = ServerUplinkConnector()
@RateLimiter(rate_per_second=5)
def on_modified(self, event):
log.debug(event)
anchor = 'anchor_webide'
self.uplink.send(anchor, {'anchor': anchor,
'data': {'command': 'reload'}})
class DirectoryMonitor:
def __init__(self, directory):
self.observer = Observer()
self.observer.schedule(WebideReloadEventHandler(), directory, recursive=True)
def watch(self):
self.observer.start()
def stop(self):
self.observer.stop()
self.observer.join()