2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-04-05 12:43:39 +00:00
|
|
|
from os.path import isfile, join, relpath, exists, isdir, realpath
|
2018-02-06 17:22:37 +00:00
|
|
|
from glob import glob
|
2018-03-15 14:43:42 +00:00
|
|
|
from fnmatch import fnmatchcase
|
|
|
|
from collections import Iterable
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-04-06 13:21:45 +00:00
|
|
|
from tfw import TriggerlessEventHandler
|
2018-04-14 20:57:37 +00:00
|
|
|
from tfw.mixins import MonitorManagerMixin
|
2018-02-06 17:22:37 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-04-06 13:21:45 +00:00
|
|
|
from .directory_monitor import DirectoryMonitor
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-06 17:22:37 +00:00
|
|
|
|
|
|
|
|
2018-04-05 12:43:39 +00:00
|
|
|
class FileManager: # pylint: disable=too-many-instance-attributes
|
2018-04-05 15:16:41 +00:00
|
|
|
def __init__(self, working_directory, allowed_directories, selected_file=None, exclude=None):
|
2018-03-15 14:43:42 +00:00
|
|
|
self._exclude, self.exclude = None, exclude
|
2018-04-05 12:43:39 +00:00
|
|
|
self._allowed_directories, self.allowed_directories = None, allowed_directories
|
2018-03-15 11:04:39 +00:00
|
|
|
self._workdir, self.workdir = None, working_directory
|
2018-03-15 11:18:39 +00:00
|
|
|
self._filename, self.filename = None, selected_file or self.files[0]
|
2018-02-06 17:22:37 +00:00
|
|
|
|
2018-03-15 11:02:56 +00:00
|
|
|
@property
|
|
|
|
def exclude(self):
|
|
|
|
return self._exclude
|
|
|
|
|
|
|
|
@exclude.setter
|
2018-03-15 14:43:42 +00:00
|
|
|
def exclude(self, exclude):
|
2018-03-30 15:50:20 +00:00
|
|
|
if exclude is None:
|
|
|
|
return
|
|
|
|
if not isinstance(exclude, Iterable):
|
|
|
|
raise TypeError('Exclude must be Iterable!')
|
2018-03-15 11:02:56 +00:00
|
|
|
self._exclude = exclude
|
|
|
|
|
2018-03-09 07:37:08 +00:00
|
|
|
@property
|
|
|
|
def workdir(self):
|
|
|
|
return self._workdir
|
|
|
|
|
|
|
|
@workdir.setter
|
|
|
|
def workdir(self, directory):
|
|
|
|
if not exists(directory) or not isdir(directory):
|
|
|
|
raise EnvironmentError('"{}" is not a directory!'.format(directory))
|
2018-04-07 13:00:31 +00:00
|
|
|
if not self._is_in_whitelisted_dir(directory):
|
2018-04-05 15:16:41 +00:00
|
|
|
raise EnvironmentError('Directory "{}" is not in whitelist!'.format(directory))
|
2018-03-09 07:37:08 +00:00
|
|
|
self._workdir = directory
|
|
|
|
|
2018-04-05 12:43:39 +00:00
|
|
|
@property
|
|
|
|
def allowed_directories(self):
|
|
|
|
return self._allowed_directories
|
|
|
|
|
|
|
|
@allowed_directories.setter
|
|
|
|
def allowed_directories(self, directories):
|
|
|
|
self._allowed_directories = directories
|
|
|
|
|
2018-03-15 11:18:39 +00:00
|
|
|
@property
|
|
|
|
def filename(self):
|
|
|
|
return self._filename
|
|
|
|
|
|
|
|
@filename.setter
|
|
|
|
def filename(self, filename):
|
2018-04-14 19:07:33 +00:00
|
|
|
if filename not in self.files:
|
2018-03-25 14:20:49 +00:00
|
|
|
raise EnvironmentError('No such file in workdir!')
|
2018-03-15 11:18:39 +00:00
|
|
|
self._filename = filename
|
2018-02-06 17:22:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def files(self):
|
2018-02-08 15:56:30 +00:00
|
|
|
return [self._relpath(file) for file in glob(join(self._workdir, '**/*'), recursive=True)
|
2018-04-07 13:00:31 +00:00
|
|
|
if isfile(file) and self._is_in_whitelisted_dir(file) and not self._is_blacklisted(file)]
|
2018-02-06 17:22:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def file_contents(self):
|
2018-02-08 13:13:14 +00:00
|
|
|
with open(self._filepath(self.filename), 'r', errors='surrogateescape') as ifile:
|
2018-02-06 17:22:37 +00:00
|
|
|
return ifile.read()
|
|
|
|
|
|
|
|
@file_contents.setter
|
|
|
|
def file_contents(self, value):
|
2018-02-08 13:13:14 +00:00
|
|
|
with open(self._filepath(self.filename), 'w', errors='surrogateescape') as ofile:
|
2018-02-06 17:22:37 +00:00
|
|
|
ofile.write(value)
|
|
|
|
|
2018-04-07 13:00:31 +00:00
|
|
|
def _is_in_whitelisted_dir(self, path):
|
|
|
|
return any(realpath(path).startswith(allowed_dir) for allowed_dir in self.allowed_directories)
|
2018-04-05 15:01:50 +00:00
|
|
|
|
2018-04-07 12:35:42 +00:00
|
|
|
def _is_blacklisted(self, file):
|
|
|
|
return any(fnmatchcase(file, blacklisted) for blacklisted in self.exclude)
|
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
def _filepath(self, filename):
|
|
|
|
return join(self._workdir, filename)
|
|
|
|
|
2018-02-08 13:45:07 +00:00
|
|
|
def _relpath(self, filename):
|
|
|
|
return relpath(self._filepath(filename), start=self._workdir)
|
|
|
|
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-04-14 20:57:37 +00:00
|
|
|
class WebideEventHandler(TriggerlessEventHandler, MonitorManagerMixin):
|
2018-04-05 12:43:39 +00:00
|
|
|
# pylint: disable=too-many-arguments
|
2018-04-05 15:16:41 +00:00
|
|
|
def __init__(self, key, directory, allowed_directories, selected_file=None, exclude=None):
|
2018-02-21 14:28:16 +00:00
|
|
|
super().__init__(key)
|
2018-04-05 12:43:39 +00:00
|
|
|
self.filemanager = FileManager(allowed_directories=allowed_directories, working_directory=directory,
|
|
|
|
selected_file=selected_file, exclude=exclude)
|
2018-04-14 20:57:37 +00:00
|
|
|
MonitorManagerMixin.__init__(self, DirectoryMonitor, self.filemanager.workdir)
|
2018-02-07 11:02:53 +00:00
|
|
|
|
2018-03-09 07:45:30 +00:00
|
|
|
self.commands = {'read': self.read,
|
|
|
|
'write': self.write,
|
|
|
|
'select': self.select,
|
2018-03-15 14:54:07 +00:00
|
|
|
'selectdir': self.select_dir,
|
|
|
|
'exclude': self.exclude}
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-08 14:10:37 +00:00
|
|
|
def read(self, data):
|
2018-03-30 15:50:20 +00:00
|
|
|
try:
|
|
|
|
data['content'] = self.filemanager.file_contents
|
|
|
|
except PermissionError:
|
|
|
|
data['content'] = 'You have no permission to open that file :('
|
|
|
|
except FileNotFoundError:
|
|
|
|
data['content'] = 'This file was removed :('
|
2018-03-30 16:11:38 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2018-03-30 15:50:20 +00:00
|
|
|
data['content'] = 'Failed to read file :('
|
2018-02-08 14:10:37 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def write(self, data):
|
2018-03-20 10:39:37 +00:00
|
|
|
self.monitor.ignore = self.monitor.ignore + 1
|
2018-03-30 15:50:20 +00:00
|
|
|
try:
|
|
|
|
self.filemanager.file_contents = data['content']
|
2018-03-30 16:11:38 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2018-03-30 15:50:20 +00:00
|
|
|
LOG.exception('Error writing file!')
|
2018-03-02 13:02:05 +00:00
|
|
|
del data['content']
|
2018-02-08 14:10:37 +00:00
|
|
|
return data
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-08 14:10:37 +00:00
|
|
|
def select(self, data):
|
2018-03-30 15:50:20 +00:00
|
|
|
try:
|
|
|
|
self.filemanager.filename = data['filename']
|
|
|
|
except EnvironmentError:
|
|
|
|
LOG.exception('Failed to select file "%s"', data['filename'])
|
2018-02-08 14:10:37 +00:00
|
|
|
return data
|
2018-02-07 11:02:53 +00:00
|
|
|
|
2018-03-09 07:45:30 +00:00
|
|
|
def select_dir(self, data):
|
2018-03-09 08:07:21 +00:00
|
|
|
try:
|
|
|
|
self.filemanager.workdir = data['directory']
|
2018-03-09 08:37:48 +00:00
|
|
|
self.reload_monitor()
|
2018-03-09 08:07:21 +00:00
|
|
|
try:
|
2018-03-15 11:18:39 +00:00
|
|
|
self.filemanager.filename = self.filemanager.files[0]
|
2018-03-09 08:37:48 +00:00
|
|
|
self.read(data)
|
2018-03-09 08:07:21 +00:00
|
|
|
except IndexError:
|
|
|
|
data['content'] = 'No files in this directory :('
|
2018-04-06 14:09:05 +00:00
|
|
|
except EnvironmentError as err:
|
|
|
|
LOG.error('Failed to select directory "%s". Reason: %s', data['directory'], str(err))
|
2018-03-09 07:45:30 +00:00
|
|
|
return data
|
|
|
|
|
2018-03-15 14:54:07 +00:00
|
|
|
def exclude(self, data):
|
2018-03-30 15:50:20 +00:00
|
|
|
try:
|
|
|
|
self.filemanager.exclude = list(data['exclude'])
|
|
|
|
except TypeError:
|
|
|
|
LOG.error('Exclude must be Iterable!')
|
2018-03-15 14:54:07 +00:00
|
|
|
return data
|
|
|
|
|
2018-02-09 14:04:00 +00:00
|
|
|
def attach_fileinfo(self, data):
|
|
|
|
data['filename'] = self.filemanager.filename
|
|
|
|
data['files'] = self.filemanager.files
|
2018-03-09 07:52:13 +00:00
|
|
|
data['directory'] = self.filemanager.workdir
|
2018-02-09 14:04:00 +00:00
|
|
|
|
2018-04-13 18:45:34 +00:00
|
|
|
def handle_event(self, message):
|
2018-03-07 13:45:43 +00:00
|
|
|
try:
|
2018-03-08 15:11:43 +00:00
|
|
|
data = message['data']
|
|
|
|
message['data'] = self.commands[data['command']](data)
|
2018-03-07 13:45:43 +00:00
|
|
|
self.attach_fileinfo(data)
|
2018-03-08 15:11:43 +00:00
|
|
|
return message
|
2018-03-07 13:45:43 +00:00
|
|
|
except KeyError:
|
2018-03-25 14:25:01 +00:00
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-13 14:38:46 +00:00
|
|
|
def cleanup(self):
|
|
|
|
self.monitor.stop()
|