baseimage-tutorial-framework/tfw/components/ide/ide_handler.py

100 lines
2.8 KiB
Python
Raw Normal View History

2019-06-10 13:32:45 +00:00
import logging
from tfw.internals.networking import Scope
from tfw.internals.inotify import InotifyObserver
from .file_manager import FileManager
2019-05-20 12:52:02 +00:00
LOG = logging.getLogger(__name__)
2019-06-27 12:48:27 +00:00
BUILD_ARTIFACTS = (
"*.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
)
class IdeHandler:
2019-08-08 13:05:37 +00:00
keys = ['ide']
type_id = 'ControlEventHandler'
2019-08-07 07:44:03 +00:00
2019-09-02 13:49:38 +00:00
def __init__(self, *, patterns, initial_file=None):
self.connector = None
2019-08-07 07:44:03 +00:00
self.filemanager = FileManager(patterns)
2019-09-02 13:49:38 +00:00
self._initial_file = initial_file or ''
2019-06-28 13:11:02 +00:00
self.monitor = InotifyObserver(
2019-08-07 07:44:03 +00:00
path=self.filemanager.parents,
2019-06-28 13:11:02 +00:00
exclude=BUILD_ARTIFACTS
)
2019-06-27 12:48:27 +00:00
self.monitor.on_modified = self._reload_frontend
self.monitor.start()
self.commands = {
2019-08-07 07:44:03 +00:00
'ide.read' : self.read,
'ide.write' : self.write
}
2019-09-02 13:49:38 +00:00
def _reload_frontend(self, event): # pylint: disable=unused-argument
self.send_message({'key': 'ide.reload'})
2019-08-07 07:44:03 +00:00
@property
def initial_file(self):
2019-09-02 13:49:38 +00:00
if not self.filemanager.is_allowed(self._initial_file):
2019-08-07 07:44:03 +00:00
self._initial_file = self.filemanager.files[0]
return self._initial_file
def send_message(self, message):
self.connector.send_message(message, scope=Scope.WEBSOCKET)
2019-06-27 12:48:27 +00:00
2019-09-02 13:49:38 +00:00
def handle_event(self, message, _):
try:
self.commands[message['key']](message)
message['files'] = self.filemanager.files
self.send_message(message)
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
2019-08-07 07:44:03 +00:00
def read(self, message):
2019-09-02 14:21:19 +00:00
if 'patterns' in message:
self.filemanager.patterns = message['patterns']
try:
2019-09-02 13:49:38 +00:00
message['filename'] = message.get('filename') or self.initial_file
2019-08-07 07:44:03 +00:00
message['content'] = self.filemanager.read_file(message['filename'])
2019-09-02 13:49:38 +00:00
except (PermissionError, ValueError):
2019-08-07 07:44:03 +00:00
message['content'] = 'You have no permission to open that file :('
except FileNotFoundError:
2019-09-02 13:49:38 +00:00
message['content'] = 'This file does not exist :('
except Exception: # pylint: disable=broad-except
2019-08-07 07:44:03 +00:00
message['content'] = 'Failed to read file :('
2019-09-02 13:49:38 +00:00
LOG.exception('Error reading file!')
2018-06-01 14:20:20 +00:00
2019-08-07 07:44:03 +00:00
def write(self, message):
try:
2019-08-07 07:44:03 +00:00
self.filemanager.write_file(message['filename'], message['content'])
2019-09-02 13:49:38 +00:00
except KeyError:
LOG.error('You must provide a filename to write!')
except Exception: # pylint: disable=broad-except
LOG.exception('Error writing file!')
2019-08-07 07:44:03 +00:00
del message['content']
2018-02-13 14:38:46 +00:00
def cleanup(self):
self.monitor.stop()