Simplify terminal handler

This commit is contained in:
R. Richard 2019-08-07 09:47:28 +02:00
parent dded7fd65c
commit e414ea2631

View File

@ -7,40 +7,25 @@ LOG = logging.getLogger(__name__)
class TerminalHandler: class TerminalHandler:
keys = ['shell'] keys = ['terminal.write']
"""
Event handler responsible for managing terminal sessions for frontend xterm
sessions to connect to. You need to instanciate this in order for frontend
terminals to work.
This EventHandler accepts messages that have a data['command'] key specifying def __init__(self, *, port, user, working_directory, histfile):
a command to be executed. self.connector, self._historymonitor = None, None
The API of each command is documented in their respective handler.
"""
def __init__(self, *, port, user, workind_directory, histfile):
"""
:param key: key this EventHandler listens to
:param monitor: tfw.components.HistoryMonitor instance to read command history from
"""
self.connector = None
self._histfile = histfile self._histfile = histfile
self._historymonitor = None
bash_as_user_cmd = ['sudo', '-u', user, 'bash'] bash_as_user_cmd = ['sudo', '-u', user, 'bash']
self.terminado_server = TerminadoMiniServer( self.terminado_server = TerminadoMiniServer(
'/terminal', '/terminal',
port, port,
workind_directory, working_directory,
bash_as_user_cmd bash_as_user_cmd
) )
self.terminado_server.listen()
self.commands = { self.commands = {
'write': self.write, 'terminal.write': self.handle_write
'read': self.read
} }
self.terminado_server.listen()
def start(self): def start(self):
self._historymonitor = BashMonitor(self.connector, self._histfile) self._historymonitor = BashMonitor(self.connector, self._histfile)
self._historymonitor.start() self._historymonitor.start()
@ -51,34 +36,12 @@ class TerminalHandler:
def handle_event(self, message, _): def handle_event(self, message, _):
try: try:
data = message['data'] self.commands[message['key']](message)
message['data'] = self.commands[data['command']](data)
except KeyError: except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
def write(self, data): def handle_write(self, message):
""" self.terminado_server.pty.write(message['command'])
Writes a string to the terminal session (on the pty level).
Useful for pre-typing and executing commands for the user.
:param data: TFW message data containing 'value'
(command to be written to the pty)
"""
self.terminado_server.pty.write(data['value'])
return data
def read(self, data):
"""
Reads the history of commands executed.
:param data: TFW message data containing 'count'
(the number of history elements to return)
:return dict: message with list of commands in data['history']
"""
data['count'] = int(data.get('count', 1))
if self.historymonitor:
data['history'] = self.historymonitor.history[-data['count']:]
return data
def cleanup(self): def cleanup(self):
self.terminado_server.stop() self.terminado_server.stop()