mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-09-09 01:04:11 +00:00
71 lines
1.9 KiB
Python
71 lines
1.9 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
from os.path import isdir, exists
|
|
|
|
from tfw import EventHandlerBase
|
|
from tfw.config.logs import logging
|
|
from tfw.mixins import MonitorManagerMixin
|
|
from .directory_monitor import DirectoryMonitor
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class DirectoryMonitoringEventHandler(EventHandlerBase, MonitorManagerMixin):
|
|
def __init__(self, key, directory):
|
|
super().__init__(key)
|
|
self._directory = directory
|
|
MonitorManagerMixin.__init__(
|
|
self,
|
|
DirectoryMonitor,
|
|
key,
|
|
self._directory
|
|
)
|
|
|
|
self.commands = {
|
|
'pause': self.pause,
|
|
'resume': self.resume,
|
|
'ignore': self.ignore,
|
|
'selectdir': self.selectdir
|
|
}
|
|
|
|
@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
|
|
|
|
def handle_event(self, message):
|
|
try:
|
|
message['data'] = self.commands[message['data']['command']](message['data'])
|
|
return message
|
|
except KeyError:
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', 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
|
|
|
|
def selectdir(self, data):
|
|
try:
|
|
self.directory = data['directory']
|
|
self.reload_monitor()
|
|
return data
|
|
except EnvironmentError:
|
|
LOG.error('Failed to switch directory!')
|
|
|
|
def cleanup(self):
|
|
self.monitor.stop()
|