2018-02-16 10:09:58 +00:00
|
|
|
from os.path import isfile, join, relpath
|
2018-02-06 17:22:37 +00:00
|
|
|
from glob import glob
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-13 16:51:19 +00:00
|
|
|
from tfw.components.mixins import SupervisorMixin
|
2018-01-31 14:50:52 +00:00
|
|
|
from tfw.event_handler_base import EventHandlerBase
|
2018-02-12 15:01:24 +00:00
|
|
|
from tfw.components.directory_monitor import DirectoryMonitor
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
from tfw.config.logs import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class FileManager:
|
2018-02-08 15:49:01 +00:00
|
|
|
def __init__(self, working_directory, selected_file=None):
|
2018-02-08 15:56:30 +00:00
|
|
|
self.exclude = ['__pycache__']
|
2018-02-06 17:22:37 +00:00
|
|
|
self._workdir = working_directory
|
2018-02-15 15:32:41 +00:00
|
|
|
self.filename = selected_file or self.files[0]
|
2018-02-06 17:22:37 +00:00
|
|
|
|
|
|
|
def select_file(self, filename):
|
2018-02-07 13:50:29 +00:00
|
|
|
if not filename in self.files:
|
2018-02-07 11:02:53 +00:00
|
|
|
raise EnvironmentError('No such file in workdir!')
|
2018-02-06 17:22:37 +00:00
|
|
|
self.filename = filename
|
|
|
|
|
|
|
|
@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
|
|
|
|
not any(word in file for word 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-01-24 11:17:15 +00:00
|
|
|
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
2018-02-21 14:28:16 +00:00
|
|
|
def __init__(self, key, directory, process_name, selected_file=None):
|
|
|
|
super().__init__(key)
|
2018-02-08 15:49:01 +00:00
|
|
|
self.filemanager = FileManager(directory, selected_file=selected_file)
|
2018-02-07 11:02:53 +00:00
|
|
|
self.process_name = process_name
|
|
|
|
|
2018-01-18 13:35:53 +00:00
|
|
|
self.commands = {
|
2018-02-06 17:22:37 +00:00
|
|
|
'read': self.read,
|
2018-02-07 11:02:53 +00:00
|
|
|
'write': self.write,
|
|
|
|
'select': self.select
|
2018-01-18 13:35:53 +00:00
|
|
|
}
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-12 15:01:24 +00:00
|
|
|
self.monitor = DirectoryMonitor(directory)
|
2018-02-13 14:38:46 +00:00
|
|
|
self.monitor.watch() # This runs on a separate thread
|
2018-02-12 15:01:24 +00:00
|
|
|
|
2018-02-08 14:10:37 +00:00
|
|
|
def read(self, data):
|
2018-02-08 16:10:58 +00:00
|
|
|
try: data['content'] = self.filemanager.file_contents
|
|
|
|
except PermissionError: data['content'] = 'You have no permission to open that file :('
|
2018-02-09 14:59:26 +00:00
|
|
|
except FileNotFoundError: data['content'] = 'This file was removed :('
|
|
|
|
except Exception: data['content'] = 'Failed to read file :('
|
2018-02-08 14:10:37 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def write(self, data):
|
2018-02-14 16:44:05 +00:00
|
|
|
with self.monitor.pauser:
|
|
|
|
try: self.filemanager.file_contents = data['content']
|
|
|
|
except Exception: log.exception('Error writing file!')
|
|
|
|
self.restart_process()
|
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):
|
|
|
|
try: self.filemanager.select_file(data['filename'])
|
2018-02-08 16:10:58 +00:00
|
|
|
except EnvironmentError: log.exception('Failed to select file "{}"'.format(data['filename']))
|
2018-02-08 14:10:37 +00:00
|
|
|
return data
|
2018-02-07 11:02:53 +00:00
|
|
|
|
2018-02-09 14:04:00 +00:00
|
|
|
def attach_fileinfo(self, data):
|
|
|
|
data['filename'] = self.filemanager.filename
|
|
|
|
data['files'] = self.filemanager.files
|
|
|
|
|
2018-02-21 14:28:16 +00:00
|
|
|
def handle_event(self, key, data_json):
|
2018-02-06 17:22:37 +00:00
|
|
|
data = data_json['data']
|
2018-02-08 14:10:37 +00:00
|
|
|
data_json['data'] = self.commands[data['command']](data)
|
2018-02-09 14:04:00 +00:00
|
|
|
self.attach_fileinfo(data)
|
2018-02-06 17:22:37 +00:00
|
|
|
return data_json
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-02-13 14:38:46 +00:00
|
|
|
def cleanup(self):
|
|
|
|
self.monitor.stop()
|