1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-14 21:57:17 +00:00

Make TestCommands general instead of a command for every feature

This commit is contained in:
Kristóf Tóth 2018-05-26 22:53:02 +02:00
parent 1210aea1b5
commit a6724b947b

View File

@ -1,3 +1,5 @@
from ast import literal_eval
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tfw.components import IdeEventHandler, TerminalEventHandler, ProcessManagingEventHandler, BashMonitor from tfw.components import IdeEventHandler, TerminalEventHandler, ProcessManagingEventHandler, BashMonitor
@ -16,53 +18,77 @@ def cenator(history):
class TestCommands(TerminalCommands): class TestCommands(TerminalCommands):
"""
Some example commands useful for debugging.
Please remove from production code and inherit your own
class from TerminalCommands if you need to define custom
commands in your challenge.
"""
# pylint: disable=unused-argument, attribute-defined-outside-init, no-self-use # pylint: disable=unused-argument, attribute-defined-outside-init, no-self-use
def command_selectdir(self, *args): def command_sendmessage(self, *args):
TFWServerConnector().send_to_eventhandler({'key': 'ide', """
'data': {'command': 'selectdir', Insert TFW message template as first argument if executed without args.
'directory': args[0]}})
def command_trigger(self, *args): Evaluate first argumen as a dict and send it to the frontend.
TFWServerConnector().send({'key': '', This is useful for playing around with frontend APIs.
'trigger': args[0]}) """
if not args:
def command_togglenext(self, *args): message_template = """'{"key": "", "data": {"command": ""}}'"""
if not hasattr(self, 'togglenext_visible'): TFWServerConnector().send_to_eventhandler({
self.togglenext_visible = True 'key': 'shell',
TFWServerConnector().send({'key': 'messagecontrol', 'data': {
'data': {'command': 'showbutton', 'command': 'write',
'next_visibility': self.togglenext_visible}}) 'shellcmd': f'sendmessage {message_template}'
self.togglenext_visible = not self.togglenext_visible }
})
else:
TFWServerConnector().send(literal_eval(args[0]))
def command_seppuku_tfw(self, *args): def command_seppuku_tfw(self, *args):
"""
Restart tfw_server.py and event_handler_main.py.
This can speed up development when combined with mounting
volumes from host to container.
"""
seppuku = ('nohup sh -c "supervisorctl restart tfwserver event_handler_main" &> /dev/null & ' seppuku = ('nohup sh -c "supervisorctl restart tfwserver event_handler_main" &> /dev/null & '
'clear && echo "Committed seppuku! :)" && sleep infinity') 'clear && echo "Committed seppuku! :)" && sleep infinity')
uplink = TFWServerConnector() uplink = TFWServerConnector()
uplink.send_to_eventhandler({'key': 'shell', uplink.send_to_eventhandler({
'data': {'command': 'write', 'key': 'shell',
'shellcmd': f'{seppuku}\n'}}) 'data': {
uplink.send({'key': 'dashboard', 'command': 'write',
'data' :{'command': 'reload_frontend'}}) 'shellcmd': f'{seppuku}\n'
}
def command_changelayout(self, *args): })
message = {'key': 'dashboard', uplink.send({
'data': {'command': 'layout', 'key': 'dashboard',
'layout': args[0]}} 'data': {
if len(args) >= 2: 'command': 'reload_frontend'
message['data']['hide_messages'] = (args[1] in ['yes', 'y', '1', 'true']) }
TFWServerConnector().send(message) })
if __name__ == '__main__': if __name__ == '__main__':
ide = IdeEventHandler(key='ide', allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR], ide = IdeEventHandler( # Web IDE backend
directory=TFWENV.IDE_WD, exclude=['*.pyc'], additional_watched_directories=[TFWENV.WEBSERVICE_DIR]) key='ide', allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR],
terminado = TerminalEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE)) directory=TFWENV.IDE_WD, exclude=['*.pyc'],
terminado.historymonitor.subscribe_callback(cenator) additional_watched_directories=[TFWENV.WEBSERVICE_DIR]
commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc') )
terminado.historymonitor.subscribe_callback(commands.callback) terminal = TerminalEventHandler( # Web shell backend
processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor) key='shell',
monitor=BashMonitor(TFWENV.HISTFILE)
)
processmanager = ProcessManagingEventHandler( # Handles 'deploy' button clicks
key='processmanager',
dirmonitor=ide.monitor
)
eventhandlers = {ide, terminado, processmanager} terminal.historymonitor.subscribe_callback(cenator)
commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc')
terminal.historymonitor.subscribe_callback(commands.callback)
eventhandlers = {ide, terminal, processmanager}
try: try:
IOLoop.instance().start() IOLoop.instance().start()
finally: finally: