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

99 lines
2.7 KiB
Python
Raw Normal View History

2019-06-10 13:32:45 +00:00
import logging
2019-08-07 07:44:03 +00:00
from os.path import isfile
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
def __init__(self, *, patterns, initial_file=''):
self.connector = None
2019-08-07 07:44:03 +00:00
self.filemanager = FileManager(patterns)
self._initial_file = initial_file
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-08-07 07:44:03 +00:00
@property
def initial_file(self):
if not isfile(self._initial_file):
self._initial_file = self.filemanager.files[0]
return self._initial_file
2019-06-28 13:11:02 +00:00
def _reload_frontend(self, event): # pylint: disable=unused-argument
2019-08-07 07:44:03 +00:00
self.send_message({'key': 'ide.reload'})
def send_message(self, message):
self.connector.send_message(message, scope=Scope.WEBSOCKET)
2019-06-27 12:48:27 +00:00
2019-08-07 07:44:03 +00:00
def read(self, message):
if message.get('files'):
self.filemanager.patterns = message['files']
try:
2019-08-07 07:44:03 +00:00
message['content'] = self.filemanager.read_file(message['filename'])
except PermissionError:
2019-08-07 07:44:03 +00:00
message['content'] = 'You have no permission to open that file :('
except FileNotFoundError:
2019-08-07 07:44:03 +00:00
message['content'] = 'This file was removed :('
except Exception: # pylint: disable=broad-except
2019-08-07 07:44:03 +00:00
message['content'] = 'Failed to read 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'])
except Exception: # pylint: disable=broad-except
LOG.exception('Error writing file!')
2019-08-07 07:44:03 +00:00
del message['content']
def handle_event(self, message, _):
try:
2019-08-07 07:44:03 +00:00
if message['filename'] == '':
message['filename'] = self.initial_file
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)
2018-02-13 14:38:46 +00:00
def cleanup(self):
self.monitor.stop()