2018-03-09 07:37:08 +00:00
|
|
|
|
from os.path import isfile, join, relpath, exists, isdir
|
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-02-23 13:07:41 +00:00
|
|
|
|
from tfw.event_handler_base import TriggerlessEventHandler
|
2018-02-12 15:01:24 +00:00
|
|
|
|
from tfw.components.directory_monitor import DirectoryMonitor
|
2018-02-06 17:22:37 +00:00
|
|
|
|
from tfw.config.logs import logging
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-06 17:22:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileManager:
|
2018-03-15 14:43:42 +00:00
|
|
|
|
def __init__(self, working_directory, selected_file=None, exclude=None):
|
|
|
|
|
self._exclude, self.exclude = None, exclude
|
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))
|
|
|
|
|
self._workdir = directory
|
|
|
|
|
|
2018-03-15 11:18:39 +00:00
|
|
|
|
@property
|
|
|
|
|
def filename(self):
|
|
|
|
|
return self._filename
|
|
|
|
|
|
|
|
|
|
@filename.setter
|
|
|
|
|
def filename(self, filename):
|
2018-02-07 13:50:29 +00:00
|
|
|
|
if not filename 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)
|
|
|
|
|
if isfile(file) and
|
2018-03-15 14:43:42 +00:00
|
|
|
|
not any(fnmatchcase(file, blacklisted) for blacklisted in self.exclude)]
|
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)
|
|
|
|
|
|
|
|
|
|
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-02-26 16:06:22 +00:00
|
|
|
|
class SourceCodeEventHandler(TriggerlessEventHandler):
|
2018-03-09 07:41:12 +00:00
|
|
|
|
def __init__(self, key, directory, selected_file=None, exclude=None):
|
2018-02-21 14:28:16 +00:00
|
|
|
|
super().__init__(key)
|
2018-03-09 07:41:12 +00:00
|
|
|
|
self.filemanager = FileManager(directory, selected_file=selected_file, exclude=exclude)
|
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-03-09 08:37:48 +00:00
|
|
|
|
self._monitor = None
|
|
|
|
|
self.reload_monitor()
|
2018-03-02 09:23:10 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def monitor(self):
|
|
|
|
|
return self._monitor
|
2018-02-12 15:01:24 +00:00
|
|
|
|
|
2018-03-09 08:37:48 +00:00
|
|
|
|
def reload_monitor(self):
|
2018-03-09 08:50:13 +00:00
|
|
|
|
if self._monitor:
|
2018-03-30 15:50:20 +00:00
|
|
|
|
try:
|
|
|
|
|
self._monitor.stop()
|
|
|
|
|
except KeyError:
|
|
|
|
|
logging.debug('Working directory was removed – ignoring...')
|
2018-03-09 08:37:48 +00:00
|
|
|
|
self._monitor = DirectoryMonitor(self.filemanager.workdir)
|
|
|
|
|
self._monitor.watch() # This runs on a separate thread
|
|
|
|
|
|
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 :('
|
|
|
|
|
except EnvironmentError:
|
2018-03-25 14:25:01 +00:00
|
|
|
|
LOG.error('Failed to select directory "%s"', data['directory'])
|
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-03-08 15:11:43 +00:00
|
|
|
|
def handle_event(self, key, 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()
|