baseimage-tutorial-framework/lib/tfw/components/terminado_event_handler.py

33 lines
1.3 KiB
Python
Raw Normal View History

from tfw.components.terminado_mini_server import TerminadoMiniServer
2018-03-03 21:46:14 +00:00
from tfw.components.history_monitor import HistoryMonitor
from tfw.event_handler_base import TriggerlessEventHandler
from tfw.config import tfwenv
2018-01-31 14:50:52 +00:00
from tfw.config.logs import logging
2018-01-29 17:36:29 +00:00
log = logging.getLogger(__name__)
class TerminadoEventHandler(TriggerlessEventHandler):
def __init__(self, key):
super().__init__(key)
self.working_directory = tfwenv.TERMINADO_DIR
2018-03-03 21:46:14 +00:00
self.monitor = HistoryMonitor(tfwenv.HISTFILE)
self.terminado_server = TerminadoMiniServer('/terminal', tfwenv.TERMINADO_PORT, tfwenv.TERMINADO_WD, ['bash'])
2018-03-03 21:46:14 +00:00
self.commands = {'write': self.write,
'read': self.read}
self.monitor.watch()
self.terminado_server.listen()
def handle_event(self, key, data_json):
2018-03-03 13:14:44 +00:00
log.debug('TerminadoEventHandler received event: {}'.format(data_json))
data = data_json['data']
2018-03-03 21:46:14 +00:00
data_json['data'] = self.commands[data['command']](data)
return data_json
2018-03-03 13:14:44 +00:00
def write(self, data):
self.terminado_server.pty.write(data['shellcmd'])
2018-03-03 21:46:14 +00:00
def read(self, data):
data['count'] = int(data.get('count', 1))
data['history'] = self.monitor.history[-data['count']:]
2018-03-03 21:46:14 +00:00
return data