mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 07:11:31 +00:00
33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
from tfw.components.terminado_mini_server import TerminadoMiniServer
|
|
from tfw.components.history_monitor import HistoryMonitor
|
|
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):
|
|
super().__init__(key)
|
|
self.working_directory = tfwenv.TERMINADO_DIR
|
|
self.monitor = HistoryMonitor(tfwenv.HISTFILE)
|
|
self.terminado_server = TerminadoMiniServer('/terminal', tfwenv.TERMINADO_PORT, tfwenv.TERMINADO_WD, ['bash'])
|
|
self.commands = {'write': self.write,
|
|
'read': self.read}
|
|
self.monitor.watch()
|
|
self.terminado_server.listen()
|
|
|
|
def handle_event(self, key, data_json):
|
|
log.debug('TerminadoEventHandler received event: {}'.format(data_json))
|
|
data = data_json['data']
|
|
data_json['data'] = self.commands[data['command']](data)
|
|
return data_json
|
|
|
|
def write(self, data):
|
|
self.terminado_server.pty.write(data['shellcmd'])
|
|
|
|
def read(self, data):
|
|
data['count'] = int(data.get('count', 1))
|
|
data['history'] = self.monitor.history[-data['count']:]
|
|
return data
|