2018-02-12 15:01:24 +00:00
|
|
|
|
from watchdog.observers import Observer
|
|
|
|
|
from watchdog.events import FileSystemEventHandler
|
2018-03-15 20:02:49 +00:00
|
|
|
|
from os.path import exists, isdir
|
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-03-15 19:35:57 +00:00
|
|
|
|
from tfw.event_handler_base import TriggerlessEventHandler
|
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
|
|
|
|
|
|
|
|
|
|
2018-02-13 13:55:33 +00:00
|
|
|
|
class WebideReloadEventHandler(FileSystemEventHandler):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.uplink = ServerUplinkConnector()
|
2018-02-14 15:09:09 +00:00
|
|
|
|
self._paused = False
|
2018-03-07 08:17:29 +00:00
|
|
|
|
self.ignore = 0
|
2018-02-14 15:09:09 +00:00
|
|
|
|
|
|
|
|
|
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):
|
2018-02-14 15:09:09 +00:00
|
|
|
|
if self._paused: return
|
2018-03-07 08:17:29 +00:00
|
|
|
|
if self.ignore > 0:
|
|
|
|
|
self.ignore = self.ignore - 1
|
|
|
|
|
return
|
2018-02-13 13:55:33 +00:00
|
|
|
|
log.debug(event)
|
2018-02-21 14:28:16 +00:00
|
|
|
|
key = 'webide'
|
2018-02-28 14:57:44 +00:00
|
|
|
|
self.uplink.send(key, {'data': {'command': 'reload'}})
|
2018-02-13 13:55:33 +00:00
|
|
|
|
|
|
|
|
|
|
2018-02-12 15:01:24 +00:00
|
|
|
|
class DirectoryMonitor:
|
|
|
|
|
def __init__(self, directory):
|
|
|
|
|
self.observer = Observer()
|
2018-02-14 15:09:09 +00:00
|
|
|
|
self.eventhandler = WebideReloadEventHandler()
|
|
|
|
|
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-07 10:36:21 +00:00
|
|
|
|
self.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-15 19:35:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DirectoryMonitoringEventHandler(TriggerlessEventHandler):
|
|
|
|
|
def __init__(self, key, directory):
|
|
|
|
|
super().__init__(key)
|
|
|
|
|
self._directory = directory
|
|
|
|
|
self._monitor = None
|
|
|
|
|
self.reload_monitor()
|
2018-03-15 20:05:16 +00:00
|
|
|
|
self.commands = {'pause': self.pause,
|
|
|
|
|
'resume': self.resume,
|
|
|
|
|
'ignore': self.ignore,
|
|
|
|
|
'selectdir': self.selectdir}
|
2018-03-15 19:35:57 +00:00
|
|
|
|
|
2018-03-15 20:02:49 +00:00
|
|
|
|
@property
|
|
|
|
|
def directory(self):
|
|
|
|
|
return self._directory
|
|
|
|
|
|
|
|
|
|
@directory.setter
|
|
|
|
|
def directory(self, directory):
|
|
|
|
|
if not exists(directory) or not isdir(directory): raise EnvironmentError('No such directory!')
|
|
|
|
|
self._directory = directory
|
|
|
|
|
|
2018-03-15 19:35:57 +00:00
|
|
|
|
@property
|
|
|
|
|
def monitor(self):
|
|
|
|
|
return self._monitor
|
|
|
|
|
|
|
|
|
|
def reload_monitor(self):
|
|
|
|
|
if self._monitor:
|
|
|
|
|
try: self._monitor.stop()
|
|
|
|
|
except KeyError: logging.debug('Working directory was removed – ignoring...')
|
|
|
|
|
self._monitor = DirectoryMonitor(self._directory)
|
|
|
|
|
self._monitor.watch() # This runs on a separate thread
|
|
|
|
|
|
|
|
|
|
def handle_event(self, key, message):
|
|
|
|
|
try:
|
|
|
|
|
message['data'] = self.commands[message['data']['command']](message['data'])
|
|
|
|
|
return message
|
|
|
|
|
except KeyError:
|
|
|
|
|
log.error('IGNORING MESSAGE: Invalid message received: {}'.format(message))
|
|
|
|
|
|
|
|
|
|
def pause(self, data):
|
|
|
|
|
self.monitor.pause()
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def resume(self, data):
|
|
|
|
|
self.monitor.resume()
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def ignore(self, data):
|
|
|
|
|
self.monitor.ignore += data['ignore']
|
|
|
|
|
return data
|
|
|
|
|
|
2018-03-15 20:05:16 +00:00
|
|
|
|
def selectdir(self, data):
|
|
|
|
|
try:
|
|
|
|
|
self.directory = data['directory']
|
|
|
|
|
self.reload_monitor()
|
|
|
|
|
return data
|
|
|
|
|
except EnvironmentError:
|
|
|
|
|
log.error('DirManagingEH failed to switch directory!')
|
|
|
|
|
|
2018-03-15 19:35:57 +00:00
|
|
|
|
def cleanup(self):
|
|
|
|
|
self.monitor.stop()
|