2018-02-12 15:01:24 +00:00
|
|
|
from watchdog.observers import Observer
|
2018-03-20 13:35:21 +00:00
|
|
|
from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler
|
2018-02-12 15:01:24 +00:00
|
|
|
|
2018-02-12 15:43:30 +00:00
|
|
|
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
2018-02-13 16:56:01 +00:00
|
|
|
from tfw.components.decorators import RateLimiter
|
2018-02-12 15:43:30 +00:00
|
|
|
|
|
|
|
from tfw.config.logs import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2018-02-12 15:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DirectoryMonitor:
|
|
|
|
def __init__(self, directory):
|
|
|
|
self.observer = Observer()
|
2018-03-20 13:35:21 +00:00
|
|
|
self.eventhandler = WebideReloadWatchdogEventHandler()
|
2018-02-14 15:09:09 +00:00
|
|
|
self.observer.schedule(self.eventhandler, directory, recursive=True)
|
|
|
|
self.pause, self.resume = self.eventhandler.pause, self.eventhandler.resume
|
2018-02-12 15:01:24 +00:00
|
|
|
|
|
|
|
def watch(self):
|
|
|
|
self.observer.start()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.observer.stop()
|
|
|
|
self.observer.join()
|
2018-02-14 16:44:05 +00:00
|
|
|
|
2018-03-07 08:17:29 +00:00
|
|
|
@property
|
|
|
|
def ignore(self):
|
|
|
|
return self.eventhandler.ignore
|
|
|
|
|
|
|
|
@ignore.setter
|
|
|
|
def ignore(self, value):
|
2018-03-20 10:31:29 +00:00
|
|
|
self.eventhandler.ignore = value if value >= 0 else 0
|
2018-03-07 08:17:29 +00:00
|
|
|
|
2018-02-14 16:44:05 +00:00
|
|
|
@property
|
|
|
|
def pauser(self):
|
|
|
|
return DirectoryMonitor.Pauser(self)
|
|
|
|
|
|
|
|
class Pauser:
|
|
|
|
def __init__(self, directory_monitor):
|
|
|
|
self.directorymonitor = directory_monitor
|
|
|
|
def __enter__(self):
|
|
|
|
self.directorymonitor.pause()
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
self.directorymonitor.resume()
|
2018-03-20 13:32:27 +00:00
|
|
|
|
|
|
|
|
2018-03-20 13:35:21 +00:00
|
|
|
class WebideReloadWatchdogEventHandler(FileSystemWatchdogEventHandler):
|
2018-03-20 13:32:27 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.uplink = ServerUplinkConnector()
|
|
|
|
self._paused = False
|
|
|
|
self.ignore = 0
|
|
|
|
|
|
|
|
def pause(self):
|
|
|
|
self._paused = True
|
|
|
|
|
|
|
|
def resume(self):
|
|
|
|
self._paused = False
|
|
|
|
|
|
|
|
@RateLimiter(rate_per_second=2)
|
|
|
|
def on_modified(self, event):
|
|
|
|
if self._paused: return
|
|
|
|
if self.ignore > 0:
|
|
|
|
self.ignore = self.ignore - 1
|
|
|
|
return
|
|
|
|
log.debug(event)
|
|
|
|
key = 'webide'
|
|
|
|
self.uplink.send(key, {'data': {'command': 'reload'}})
|