mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-06 02:51:22 +00:00
48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
from tfw.components.terminado_mini_server import TerminadoMiniServer
|
|
from tfw.event_handler_base import TriggerlessEventHandler
|
|
from tfw.config import TFWENV
|
|
from tfw.config.logs import logging
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class TerminadoEventHandler(TriggerlessEventHandler):
|
|
def __init__(self, key, monitor):
|
|
super().__init__(key)
|
|
self.working_directory = TFWENV.TERMINADO_DIR
|
|
self._historymonitor = monitor
|
|
self.terminado_server = TerminadoMiniServer('/terminal', TFWENV.TERMINADO_PORT, TFWENV.TERMINADO_WD, ['bash'])
|
|
self.commands = {'write': self.write,
|
|
'read': self.read}
|
|
if self._historymonitor:
|
|
self._historymonitor.watch()
|
|
self.terminado_server.listen()
|
|
|
|
@property
|
|
def historymonitor(self):
|
|
return self._historymonitor
|
|
|
|
def handle_event(self, key, message):
|
|
LOG.debug('TerminadoEventHandler received event: %s', message)
|
|
try:
|
|
message['data'] = self.commands[message['data']['command']](message['data'])
|
|
return message
|
|
except KeyError:
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
def write(self, data):
|
|
self.terminado_server.pty.write(data['shellcmd'])
|
|
|
|
def read(self, data):
|
|
data['count'] = int(data.get('count', 1))
|
|
if self.historymonitor:
|
|
data['history'] = self.historymonitor.history[-data['count']:]
|
|
return data
|
|
|
|
def cleanup(self):
|
|
if self.historymonitor:
|
|
self.historymonitor.stop()
|