mirror of
				https://github.com/avatao-content/test-tutorial-framework
				synced 2025-11-04 02:42:56 +00:00 
			
		
		
		
	Refactor TerminalCommands
This commit is contained in:
		
				
					committed by
					
						
						therealkrispet
					
				
			
			
				
	
			
			
			
						parent
						
							ef2ab5d0bf
						
					
				
				
					commit
					e50a8732fc
				
			@@ -1,33 +1,29 @@
 | 
			
		||||
import logging
 | 
			
		||||
from ast import literal_eval
 | 
			
		||||
 | 
			
		||||
from tfw.components import MessageSender, TerminalCommands
 | 
			
		||||
from tfw.builtins import EventHandler, FSMAwareEventHandler, TFWServerUplinkConnector
 | 
			
		||||
from tfw.components import MessageSender
 | 
			
		||||
from tfw.builtins import TFWServerUplinkConnector
 | 
			
		||||
from tfw.builtins import EventHandler, FSMAwareEventHandler, TerminalCommandsEventHandler
 | 
			
		||||
 | 
			
		||||
LOG = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TerminalCallbackEventHandler(EventHandler):
 | 
			
		||||
class CenatorEventHandler(EventHandler):
 | 
			
		||||
    """
 | 
			
		||||
    Logs commands executed in terminal to messages and invokes an
 | 
			
		||||
    additional callback function to handle special commands.
 | 
			
		||||
    !! Please remove from production code. !!
 | 
			
		||||
    """
 | 
			
		||||
    def __init__(self, key, callback):
 | 
			
		||||
        self.callback = callback
 | 
			
		||||
    def __init__(self, key):
 | 
			
		||||
        super().__init__(key)
 | 
			
		||||
 | 
			
		||||
    def handle_event(self, message):
 | 
			
		||||
        command = message['value']
 | 
			
		||||
        self.cenator(command)
 | 
			
		||||
        self.callback(command)
 | 
			
		||||
 | 
			
		||||
    def cenator(self, command):
 | 
			
		||||
        LOG.debug('User executed command: "%s"', command)
 | 
			
		||||
        MessageSender(self.server_connector).send('JOHN CENA', f'You\'ve executed "{command}"')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestCommands(TerminalCommands):
 | 
			
		||||
class TestCommandsEventHandler(TerminalCommandsEventHandler):
 | 
			
		||||
    """
 | 
			
		||||
    Some example commands useful for debugging.
 | 
			
		||||
    !! Please remove from production code !! and inherit your own
 | 
			
		||||
@@ -54,31 +50,6 @@ class TestCommands(TerminalCommands):
 | 
			
		||||
        else:
 | 
			
		||||
            TFWServerUplinkConnector().send_message(literal_eval(args[0]))
 | 
			
		||||
 | 
			
		||||
    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 & '
 | 
			
		||||
            'clear && echo "Committed seppuku! :)" && sleep infinity'
 | 
			
		||||
        )
 | 
			
		||||
        uplink = TFWServerUplinkConnector()
 | 
			
		||||
        uplink.send_message({
 | 
			
		||||
            'key': 'shell',
 | 
			
		||||
            'data': {
 | 
			
		||||
                'command': 'write',
 | 
			
		||||
                'value': f'{seppuku}\n'
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        uplink.send_message({
 | 
			
		||||
            'key': 'dashboard',
 | 
			
		||||
            'data': {
 | 
			
		||||
                'command': 'reloadFrontend'
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MessageFSMStepsEventHandler(FSMAwareEventHandler):
 | 
			
		||||
    """
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@ from tfw.logging import Log, Logger, LogFormatter, VerboseLogFormatter
 | 
			
		||||
from tao.config import TAOENV
 | 
			
		||||
 | 
			
		||||
from custom_event_handlers import MessageFSMStepsEventHandler
 | 
			
		||||
from custom_event_handlers import TerminalCallbackEventHandler, TestCommands
 | 
			
		||||
from custom_event_handlers import CenatorEventHandler, TestCommandsEventHandler
 | 
			
		||||
from signal_handling import setup_signal_handlers
 | 
			
		||||
 | 
			
		||||
LOG = logging.getLogger(__name__)
 | 
			
		||||
@@ -44,9 +44,10 @@ def main():
 | 
			
		||||
    terminal = TerminalEventHandler(  # Web shell backend
 | 
			
		||||
        key='shell'
 | 
			
		||||
    )
 | 
			
		||||
    commands = TerminalCallbackEventHandler( # Reacts to terminal commands
 | 
			
		||||
        'history.bash',
 | 
			
		||||
        TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc').callback
 | 
			
		||||
    cenator = CenatorEventHandler('history.bash') # Reacts to terminal commands
 | 
			
		||||
    commands = TestCommandsEventHandler( # Catches special commands
 | 
			
		||||
        key='history.bash',
 | 
			
		||||
        bashrc=f'/home/{TAOENV.USER}/.bashrc'
 | 
			
		||||
    )
 | 
			
		||||
    processmanager = ProcessManagingEventHandler(  # Handles 'deploy' button clicks
 | 
			
		||||
        key='processmanager',
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user