from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from tfw.networking.event_handlers.server_connector import ServerUplinkConnector from tfw.components.decorators 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()