2018-02-08 13:45:07 +00:00
|
|
|
from os.path import splitext, isfile, join, relpath
|
2018-02-06 17:22:37 +00:00
|
|
|
from glob import glob
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2018-01-31 14:50:52 +00:00
|
|
|
from tfw.util import SupervisorMixin
|
|
|
|
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-08 13:45:07 +00:00
|
|
|
self.filename = selected_file or self._relpath(self.files[0])
|
2018-02-06 17:22:37 +00:00
|
|
|
self.language = map_file_extension_to_language(self.filename)
|
|
|
|
|
|
|
|
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
|
|
|
|
self.language = map_file_extension_to_language(self.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-08 16:42:00 +00:00
|
|
|
def __init__(self, anchor, directory, process_name, selected_file=None):
|
2018-01-25 14:24:42 +00:00
|
|
|
super().__init__(anchor)
|
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-08 16:10:58 +00:00
|
|
|
try: self.filemanager.file_contents = data['content']
|
2018-02-09 12:14:13 +00:00
|
|
|
except Exception: log.exception('Error writing file!')
|
2018-02-06 14:34:43 +00:00
|
|
|
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['language'] = self.filemanager.language
|
|
|
|
data['files'] = self.filemanager.files
|
|
|
|
|
2018-02-06 17:22:37 +00:00
|
|
|
def handle_event(self, anchor, data_json):
|
|
|
|
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()
|
|
|
|
|
2018-01-10 15:47:25 +00:00
|
|
|
|
|
|
|
def map_file_extension_to_language(filename):
|
|
|
|
language_map = {
|
|
|
|
'.py': 'python',
|
2018-02-08 14:17:30 +00:00
|
|
|
'.js': 'javascript',
|
|
|
|
'.html': 'html',
|
|
|
|
'.css': 'css',
|
|
|
|
'.java': 'java',
|
|
|
|
'.cpp': 'c_cpp',
|
|
|
|
'.hpp': 'c_cpp',
|
|
|
|
'.c': 'c_cpp',
|
|
|
|
'.h': 'c_cpp',
|
|
|
|
'.cs': 'csharp'
|
2018-01-10 15:47:25 +00:00
|
|
|
}
|
|
|
|
_, extension = splitext(filename)
|
2018-02-08 16:39:59 +00:00
|
|
|
return language_map.get(extension, 'text')
|