2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
2018-03-25 13:43:59 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
2018-03-20 13:35:21 +00:00
|
|
|
from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler
|
2018-02-12 15:01:24 +00:00
|
|
|
|
2019-06-04 11:58:03 +00:00
|
|
|
from tfw.networking import Scope
|
|
|
|
from tfw.event_handlers import TFWServerUplinkConnector
|
2018-07-26 11:59:06 +00:00
|
|
|
from tfw.decorators.rate_limiter import RateLimiter
|
|
|
|
from tfw.mixins.observer_mixin import ObserverMixin
|
2018-02-12 15:43:30 +00:00
|
|
|
|
2018-03-25 14:06:59 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-12 15:01:24 +00:00
|
|
|
|
|
|
|
|
2018-04-14 18:36:43 +00:00
|
|
|
class DirectoryMonitor(ObserverMixin):
|
2018-06-18 13:13:39 +00:00
|
|
|
def __init__(self, ide_key, directories):
|
|
|
|
self.eventhandler = IdeReloadWatchdogEventHandler(ide_key)
|
2018-05-24 14:19:04 +00:00
|
|
|
for directory in directories:
|
|
|
|
self.observer.schedule(self.eventhandler, directory, recursive=True)
|
|
|
|
|
2018-02-14 15:09:09 +00:00
|
|
|
self.pause, self.resume = self.eventhandler.pause, self.eventhandler.resume
|
2018-02-12 15:01:24 +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-04-20 15:33:06 +00:00
|
|
|
class IdeReloadWatchdogEventHandler(FileSystemWatchdogEventHandler):
|
2018-06-18 13:13:39 +00:00
|
|
|
def __init__(self, ide_key):
|
2018-03-20 13:32:27 +00:00
|
|
|
super().__init__()
|
2018-06-18 13:13:39 +00:00
|
|
|
self.ide_key = ide_key
|
2019-06-04 11:58:03 +00:00
|
|
|
self.uplink = TFWServerUplinkConnector()
|
2018-03-20 13:32:27 +00:00
|
|
|
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):
|
2018-03-30 15:50:20 +00:00
|
|
|
if self._paused:
|
|
|
|
return
|
2018-03-20 13:32:27 +00:00
|
|
|
if self.ignore > 0:
|
|
|
|
self.ignore = self.ignore - 1
|
|
|
|
return
|
2018-03-25 14:06:59 +00:00
|
|
|
LOG.debug(event)
|
2019-05-26 16:26:33 +00:00
|
|
|
self.uplink.send_message({
|
2018-06-18 13:13:39 +00:00
|
|
|
'key': self.ide_key,
|
2018-06-04 20:16:44 +00:00
|
|
|
'data': {'command': 'reload'}
|
2019-05-26 16:26:33 +00:00
|
|
|
}, Scope.WEBSOCKET)
|
2018-03-21 16:18:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|