mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-26 19:01:31 +00:00
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
from functools import wraps
|
|
|
|
from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler
|
|
|
|
from tfw.networking import Scope
|
|
from tfw.event_handlers import TFWServerUplinkConnector
|
|
from tfw.decorators.rate_limiter import RateLimiter
|
|
from tfw.mixins.observer_mixin import ObserverMixin
|
|
|
|
from tfw.config.logs import logging
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class DirectoryMonitor(ObserverMixin):
|
|
def __init__(self, ide_key, directories):
|
|
self.eventhandler = IdeReloadWatchdogEventHandler(ide_key)
|
|
for directory in directories:
|
|
self.observer.schedule(self.eventhandler, directory, recursive=True)
|
|
|
|
self.pause, self.resume = self.eventhandler.pause, self.eventhandler.resume
|
|
|
|
@property
|
|
def ignore(self):
|
|
return self.eventhandler.ignore
|
|
|
|
@ignore.setter
|
|
def ignore(self, value):
|
|
self.eventhandler.ignore = value if value >= 0 else 0
|
|
|
|
@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()
|
|
|
|
|
|
class IdeReloadWatchdogEventHandler(FileSystemWatchdogEventHandler):
|
|
def __init__(self, ide_key):
|
|
super().__init__()
|
|
self.ide_key = ide_key
|
|
self.uplink = TFWServerUplinkConnector()
|
|
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)
|
|
self.uplink.send_message({
|
|
'key': self.ide_key,
|
|
'data': {'command': 'reload'}
|
|
}, Scope.WEBSOCKET)
|
|
|
|
|
|
def with_monitor_paused(fun):
|
|
@wraps(fun)
|
|
def wrapper(self, *args, **kwargs):
|
|
if self.monitor:
|
|
with self.monitor.pauser:
|
|
return fun(self, *args, **kwargs)
|
|
return fun(self, *args, **kwargs)
|
|
return wrapper
|