Refactor DirectoryMonitor managing code to a mixin

This commit is contained in:
Kristóf Tóth 2018-04-14 22:57:37 +02:00
parent 1c29b700c2
commit a65d3127ba
4 changed files with 35 additions and 33 deletions

View File

@ -5,17 +5,18 @@ from os.path import isdir, exists
from tfw import TriggerlessEventHandler from tfw import TriggerlessEventHandler
from tfw.config.logs import logging from tfw.config.logs import logging
from tfw.mixins import MonitorManagerMixin
from .directory_monitor import DirectoryMonitor from .directory_monitor import DirectoryMonitor
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class DirectoryMonitoringEventHandler(TriggerlessEventHandler): class DirectoryMonitoringEventHandler(TriggerlessEventHandler, MonitorManagerMixin):
def __init__(self, key, directory): def __init__(self, key, directory):
super().__init__(key) super().__init__(key)
self._directory = directory self._directory = directory
self._monitor = None MonitorManagerMixin.__init__(self, DirectoryMonitor, self._directory)
self.reload_monitor()
self.commands = {'pause': self.pause, self.commands = {'pause': self.pause,
'resume': self.resume, 'resume': self.resume,
'ignore': self.ignore, 'ignore': self.ignore,
@ -31,19 +32,6 @@ class DirectoryMonitoringEventHandler(TriggerlessEventHandler):
raise EnvironmentError('No such directory!') raise EnvironmentError('No such directory!')
self._directory = directory self._directory = directory
@property
def monitor(self):
return self._monitor
def reload_monitor(self):
if self._monitor:
try:
self._monitor.stop()
except KeyError:
LOG.debug('Working directory was removed ignoring...')
self._monitor = DirectoryMonitor(self._directory)
self._monitor.watch() # This runs on a separate thread
def handle_event(self, message): def handle_event(self, message):
try: try:
message['data'] = self.commands[message['data']['command']](message['data']) message['data'] = self.commands[message['data']['command']](message['data'])

View File

@ -7,6 +7,7 @@ from fnmatch import fnmatchcase
from collections import Iterable from collections import Iterable
from tfw import TriggerlessEventHandler from tfw import TriggerlessEventHandler
from tfw.mixins import MonitorManagerMixin
from tfw.config.logs import logging from tfw.config.logs import logging
from .directory_monitor import DirectoryMonitor from .directory_monitor import DirectoryMonitor
@ -90,12 +91,13 @@ class FileManager: # pylint: disable=too-many-instance-attributes
return relpath(self._filepath(filename), start=self._workdir) return relpath(self._filepath(filename), start=self._workdir)
class WebideEventHandler(TriggerlessEventHandler): class WebideEventHandler(TriggerlessEventHandler, MonitorManagerMixin):
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def __init__(self, key, directory, allowed_directories, selected_file=None, exclude=None): def __init__(self, key, directory, allowed_directories, selected_file=None, exclude=None):
super().__init__(key) super().__init__(key)
self.filemanager = FileManager(allowed_directories=allowed_directories, working_directory=directory, self.filemanager = FileManager(allowed_directories=allowed_directories, working_directory=directory,
selected_file=selected_file, exclude=exclude) selected_file=selected_file, exclude=exclude)
MonitorManagerMixin.__init__(self, DirectoryMonitor, self.filemanager.workdir)
self.commands = {'read': self.read, self.commands = {'read': self.read,
'write': self.write, 'write': self.write,
@ -103,22 +105,6 @@ class WebideEventHandler(TriggerlessEventHandler):
'selectdir': self.select_dir, 'selectdir': self.select_dir,
'exclude': self.exclude} 'exclude': self.exclude}
self._monitor = None
self.reload_monitor()
@property
def monitor(self):
return self._monitor
def reload_monitor(self):
if self._monitor:
try:
self._monitor.stop()
except KeyError:
LOG.debug('Working directory was removed ignoring...')
self._monitor = DirectoryMonitor(self.filemanager.workdir)
self._monitor.watch() # This runs on a separate thread
def read(self, data): def read(self, data):
try: try:
data['content'] = self.filemanager.file_contents data['content'] = self.filemanager.file_contents

View File

@ -4,3 +4,4 @@
from .supervisor_mixin import SupervisorMixin from .supervisor_mixin import SupervisorMixin
from .callback_mixin import CallbackMixin from .callback_mixin import CallbackMixin
from .observer_mixin import ObserverMixin from .observer_mixin import ObserverMixin
from .monitor_manager_mixin import MonitorManagerMixin

View File

@ -0,0 +1,27 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
class MonitorManagerMixin:
def __init__(self, monitor_type, directory):
self._monitor_type = monitor_type
self._monitor = None
self._monitored_directory = directory
self.reload_monitor()
@property
def monitor(self):
return self._monitor
def reload_monitor(self):
if self._monitor:
try:
self._monitor.stop()
except KeyError:
LOG.debug('Working directory was removed ignoring...')
self._monitor = self._monitor_type(self._monitored_directory)
self._monitor.watch() # This runs on a separate thread