2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2019-06-24 12:29:31 +00:00
|
|
|
from tfw.networking import Scope
|
2019-06-27 12:36:18 +00:00
|
|
|
from tfw.event_handlers import FrontendEventHandlerBase
|
2019-06-20 14:04:21 +00:00
|
|
|
from tfw.components import FileManager
|
2019-06-24 12:29:31 +00:00
|
|
|
from tfw.components.inotify import InotifyObserver
|
2018-03-25 14:06:59 +00:00
|
|
|
|
2019-05-20 12:52:02 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-08 13:45:07 +00:00
|
|
|
|
2019-06-27 12:48:27 +00:00
|
|
|
BUILD_ARTIFACTS = (
|
2019-06-27 12:12:01 +00:00
|
|
|
"*.a",
|
|
|
|
"*.class",
|
|
|
|
"*.dll",
|
|
|
|
"*.dylib",
|
|
|
|
"*.elf",
|
|
|
|
"*.exe",
|
|
|
|
"*.jar",
|
|
|
|
"*.ko",
|
|
|
|
"*.la",
|
|
|
|
"*.lib",
|
|
|
|
"*.lo",
|
|
|
|
"*.o",
|
|
|
|
"*.obj",
|
|
|
|
"*.out",
|
|
|
|
"*.py[cod]",
|
|
|
|
"*.so",
|
|
|
|
"*.so.*",
|
|
|
|
"*.tar.gz",
|
|
|
|
"*.zip",
|
|
|
|
"*__pycache__*"
|
2019-06-27 12:48:27 +00:00
|
|
|
)
|
2019-06-24 12:29:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IdeEventHandler(FrontendEventHandlerBase):
|
2018-07-16 09:17:06 +00:00
|
|
|
# pylint: disable=too-many-arguments,anomalous-backslash-in-string
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Event handler implementing the backend of our browser based IDE.
|
|
|
|
By default all files in the directory specified in __init__ are displayed
|
|
|
|
on the fontend. Note that this is a stateful component.
|
|
|
|
|
2018-05-24 14:19:04 +00:00
|
|
|
When any file in the selected directory changes they are automatically refreshed
|
|
|
|
on the frontend (this is done by listening to inotify events).
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
This EventHandler accepts messages that have a data['command'] key specifying
|
2018-04-18 17:44:26 +00:00
|
|
|
a command to be executed.
|
2018-06-01 14:20:20 +00:00
|
|
|
|
|
|
|
The API of each command is documented in their respective handler.
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-06-04 19:20:36 +00:00
|
|
|
def __init__(self, key, directory, allowed_directories, selected_file=None, exclude=None):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
:param key: the key this instance should listen to
|
|
|
|
:param directory: working directory which the EventHandler should serve files from
|
2018-06-04 19:47:10 +00:00
|
|
|
:param allowed_directories: list of directories that can be switched to using selectdir
|
2018-04-18 17:44:26 +00:00
|
|
|
:param selected_file: file that is selected by default
|
2018-06-01 14:20:20 +00:00
|
|
|
:param exclude: list of filenames that should not appear between files (for .o, .pyc, etc.)
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-02-21 14:28:16 +00:00
|
|
|
super().__init__(key)
|
2018-04-26 09:09:31 +00:00
|
|
|
try:
|
2018-06-04 19:47:10 +00:00
|
|
|
self.filemanager = FileManager(
|
|
|
|
allowed_directories=allowed_directories,
|
|
|
|
working_directory=directory,
|
|
|
|
selected_file=selected_file,
|
|
|
|
exclude=exclude
|
|
|
|
)
|
2018-04-26 09:09:31 +00:00
|
|
|
except IndexError:
|
2018-06-04 19:47:10 +00:00
|
|
|
raise EnvironmentError(
|
|
|
|
f'No file(s) in IdeEventHandler working_directory "{directory}"!'
|
|
|
|
)
|
2018-05-24 14:19:04 +00:00
|
|
|
|
2019-06-27 12:12:01 +00:00
|
|
|
self.monitor = InotifyObserver(self.filemanager.allowed_directories, exclude=BUILD_ARTIFACTS)
|
2019-06-27 12:48:27 +00:00
|
|
|
self.monitor.on_modified = self._reload_frontend
|
2019-06-24 12:29:31 +00:00
|
|
|
self.monitor.start()
|
2018-06-04 19:20:36 +00:00
|
|
|
|
|
|
|
self.commands = {
|
|
|
|
'read': self.read,
|
|
|
|
'write': self.write,
|
|
|
|
'select': self.select,
|
|
|
|
'selectdir': self.select_dir,
|
|
|
|
'exclude': self.exclude
|
|
|
|
}
|
2018-01-10 15:47:25 +00:00
|
|
|
|
2019-06-27 12:48:27 +00:00
|
|
|
def _reload_frontend(self, event):
|
|
|
|
self.server_connector.send_message({
|
|
|
|
'key': 'ide',
|
|
|
|
'data': {'command': 'reload'}
|
|
|
|
}, Scope.WEBSOCKET)
|
|
|
|
|
2018-02-08 14:10:37 +00:00
|
|
|
def read(self, data):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Read the currently selected file.
|
|
|
|
|
2018-08-04 21:27:18 +00:00
|
|
|
:return dict: TFW message data containing key 'content'
|
|
|
|
(contents of the selected file)
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
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-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Overwrites a file with the desired string.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing key 'content'
|
|
|
|
(new file content)
|
|
|
|
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
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-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Selects a file from the current directory.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing 'filename'
|
|
|
|
(name of file to select relative to the current directory)
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
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-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Select a new working directory to display files from.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing 'directory'
|
|
|
|
(absolute path of diretory to select.
|
|
|
|
must be a path whitelisted in
|
|
|
|
self.allowed_directories)
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-03-09 08:07:21 +00:00
|
|
|
try:
|
|
|
|
self.filemanager.workdir = data['directory']
|
|
|
|
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-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Overwrite list of excluded files
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing 'exclude'
|
|
|
|
(list of unix-style filename patterns to be excluded,
|
|
|
|
e.g.: ["\*.pyc", "\*.o")
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
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):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Basic information included in every response to the frontend.
|
|
|
|
"""
|
2018-02-09 14:04:00 +00:00
|
|
|
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()
|