baseimage-tutorial-framework/lib/tfw/components/directory_monitor.py

45 lines
1.3 KiB
Python
Raw Normal View History

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
2018-02-13 16:56:01 +00:00
from tfw.components.decorators import RateLimiter
from tfw.config.logs import logging
log = logging.getLogger(__name__)
2018-02-13 13:55:33 +00:00
class WebideReloadEventHandler(FileSystemEventHandler):
def __init__(self):
super().__init__()
self.uplink = ServerUplinkConnector()
self._paused = False
def pause(self):
self._paused = True
def resume(self):
self._paused = False
2018-02-13 13:55:33 +00:00
@RateLimiter(rate_per_second=5)
def on_modified(self, event):
if self._paused: return
2018-02-13 13:55:33 +00:00
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.eventhandler = WebideReloadEventHandler()
self.observer.schedule(self.eventhandler, directory, recursive=True)
self.pause, self.resume = self.eventhandler.pause, self.eventhandler.resume
def watch(self):
self.observer.start()
def stop(self):
self.observer.stop()
self.observer.join()