mirror of
				https://github.com/avatao-content/test-tutorial-framework
				synced 2025-11-04 02:42:56 +00:00 
			
		
		
		
	Simplify *main setup
This commit is contained in:
		@@ -11,33 +11,16 @@ LOG = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
class CenatorEventHandler:
 | 
			
		||||
    keys = ['history.bash']
 | 
			
		||||
    # pylint: disable=no-self-use
 | 
			
		||||
    """
 | 
			
		||||
    Logs commands executed in terminal to messages and invokes an
 | 
			
		||||
    additional callback function to handle special commands.
 | 
			
		||||
    !! Please remove from production code. !!
 | 
			
		||||
    """
 | 
			
		||||
    def handle_event(self, message, server_connector):
 | 
			
		||||
 | 
			
		||||
    def handle_event(self, message, server_connector):  # pylint: disable=no-self-use
 | 
			
		||||
        command = message['value']
 | 
			
		||||
        LOG.debug('User executed command: "%s"', command)
 | 
			
		||||
        MessageSender(server_connector).send('JOHN CENA', f'You\'ve executed "{command}"')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestCommandsEventHandler(TerminalCommandsEventHandler):
 | 
			
		||||
    """
 | 
			
		||||
    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_sendmessage(self, *args):
 | 
			
		||||
        """
 | 
			
		||||
        Insert TFW message template as first argument if executed without args.
 | 
			
		||||
 | 
			
		||||
        Evaluate first argumen as a dict and send it to the frontend.
 | 
			
		||||
        This is useful for playing around with frontend APIs.
 | 
			
		||||
        """
 | 
			
		||||
        if not args:
 | 
			
		||||
            message_template = """'{"key": "", "data": {"command": ""}}'"""
 | 
			
		||||
            TFWUplinkConnector().send_message({
 | 
			
		||||
@@ -52,12 +35,7 @@ class TestCommandsEventHandler(TerminalCommandsEventHandler):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MessageFSMStepsEventHandler:
 | 
			
		||||
    # pylint: disable=no-self-use
 | 
			
		||||
    """
 | 
			
		||||
    This example EventHandler is capable of detecting FSM state.
 | 
			
		||||
    !! Please remove from production code !!
 | 
			
		||||
    """
 | 
			
		||||
    def handle_event(self, message, server_connector):
 | 
			
		||||
    def handle_event(self, message, server_connector):  # pylint: disable=no-self-use
 | 
			
		||||
        """
 | 
			
		||||
        When the FSM steps this method is invoked.
 | 
			
		||||
        Receives a 'data' field from an fsm_update message as kwargs.
 | 
			
		||||
 
 | 
			
		||||
@@ -22,69 +22,61 @@ LOG = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    # pylint: disable=unused-variable,too-many-locals
 | 
			
		||||
    # pylint: disable=unused-variable
 | 
			
		||||
    Logger([
 | 
			
		||||
        Log(stderr, LogFormatter(20)),
 | 
			
		||||
        Log(TFWENV.LOGFILE, VerboseLogFormatter())
 | 
			
		||||
    ]).start()
 | 
			
		||||
    eh_factory = EventHandlerFactory()
 | 
			
		||||
 | 
			
		||||
    # TFW component EventHandlers (builtins, required for their respective functionalities)
 | 
			
		||||
    fsm = FSMManagingEventHandler(  # TFW FSM
 | 
			
		||||
    eh_factory = EventHandlerFactory()
 | 
			
		||||
    # TFW builtin EventHandlers (required for their respective functionalities)
 | 
			
		||||
    # TFW FSM
 | 
			
		||||
    fsm_eh = eh_factory.build(FSMManagingEventHandler(
 | 
			
		||||
        fsm_type=partial(
 | 
			
		||||
            YamlFSM,
 | 
			
		||||
            'test_fsm.yml',
 | 
			
		||||
            {}  # jinja2 variables, use empty dict to enable jinja2 parsing without any variables
 | 
			
		||||
            {}  # jinja2 variables, empty dict enables jinja2 without any variables
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
    fsm_eh = eh_factory.build(fsm)
 | 
			
		||||
 | 
			
		||||
    ide = IdeEventHandler(  # Web IDE backend
 | 
			
		||||
    ))
 | 
			
		||||
    # Web IDE backend
 | 
			
		||||
    ide_eh = eh_factory.build(IdeEventHandler(
 | 
			
		||||
        allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR],
 | 
			
		||||
        directory=TFWENV.IDE_WD,
 | 
			
		||||
        exclude=['*.pyc']
 | 
			
		||||
    )
 | 
			
		||||
    ide_eh = eh_factory.build(ide)
 | 
			
		||||
 | 
			
		||||
    terminal = TerminalEventHandler()  # Web shell backend
 | 
			
		||||
    terminal_eh = eh_factory.build(terminal)
 | 
			
		||||
 | 
			
		||||
    processmanager = ProcessManagingEventHandler(  # Handles 'deploy' button clicks
 | 
			
		||||
    ))
 | 
			
		||||
    # Web shell backend
 | 
			
		||||
    terminal_eh = eh_factory.build(TerminalEventHandler())
 | 
			
		||||
    # Handles 'deploy' button clicks
 | 
			
		||||
    processmanager_eh = eh_factory.build(ProcessManagingEventHandler(
 | 
			
		||||
        log_tail=2000
 | 
			
		||||
    )
 | 
			
		||||
    processmanager_eh = eh_factory.build(processmanager)
 | 
			
		||||
 | 
			
		||||
    logmonitor = LogMonitoringEventHandler(  # Sends live logs of webservice process to frontend
 | 
			
		||||
    ))
 | 
			
		||||
    # Sends live logs of webservice process to frontend
 | 
			
		||||
    logmonitor_eh = eh_factory.build(LogMonitoringEventHandler(
 | 
			
		||||
        process_name='webservice',
 | 
			
		||||
        log_tail=2000
 | 
			
		||||
    )
 | 
			
		||||
    logmonitor_eh = eh_factory.build(logmonitor)
 | 
			
		||||
 | 
			
		||||
    snapshot = DirectorySnapshottingEventHandler(  # Manages filesystem snapshots of directories
 | 
			
		||||
    ))
 | 
			
		||||
    # Manages filesystem snapshots of directories
 | 
			
		||||
    snapshot_eh = eh_factory.build(DirectorySnapshottingEventHandler(
 | 
			
		||||
        directories=[
 | 
			
		||||
            TFWENV.IDE_WD,
 | 
			
		||||
            TFWENV.WEBSERVICE_DIR
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    snapshot_eh = eh_factory.build(snapshot)
 | 
			
		||||
    ))
 | 
			
		||||
    # Proxies frontend API calls to frontend
 | 
			
		||||
    frontend_eh = eh_factory.build(FrontendEventHandler())
 | 
			
		||||
 | 
			
		||||
    frontend = FrontendEventHandler()  # Proxies frontend API calls to frontend
 | 
			
		||||
    frontend_eh = eh_factory.build(frontend)
 | 
			
		||||
 | 
			
		||||
    # Your custom event handlers
 | 
			
		||||
    cenator = CenatorEventHandler()
 | 
			
		||||
    cenator_eh = eh_factory.build(cenator)
 | 
			
		||||
 | 
			
		||||
    message_fsm_steps = MessageFSMStepsEventHandler()
 | 
			
		||||
    # Replace these with your custom event handlers
 | 
			
		||||
    # Echoes executed commands to messages
 | 
			
		||||
    cenator_eh = eh_factory.build(CenatorEventHandler())
 | 
			
		||||
    # Echoes FSM steps
 | 
			
		||||
    message_fsm_steps_eh = eh_factory.build(
 | 
			
		||||
        message_fsm_steps,
 | 
			
		||||
        MessageFSMStepsEventHandler(),
 | 
			
		||||
        event_handler_type=FSMAwareEventHandler
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    commands = TestCommandsEventHandler(  # Catches special commands
 | 
			
		||||
    # Catches special commands
 | 
			
		||||
    commands_eh = eh_factory.build(TestCommandsEventHandler(
 | 
			
		||||
        bashrc=f'/home/{TAOENV.USER}/.bashrc'
 | 
			
		||||
    )
 | 
			
		||||
    commands_eh = eh_factory.build(commands)
 | 
			
		||||
    ))
 | 
			
		||||
 | 
			
		||||
    setup_signal_handlers()
 | 
			
		||||
    IOLoop.instance().start()
 | 
			
		||||
 
 | 
			
		||||
@@ -21,12 +21,10 @@ def main():
 | 
			
		||||
 | 
			
		||||
    eh_factory = EventHandlerFactory()
 | 
			
		||||
 | 
			
		||||
    json_pipe = PipeIOEventHandler(
 | 
			
		||||
    json_pipe_eh = eh_factory.build(PipeIOEventHandler(
 | 
			
		||||
        '/tmp/tfw_send',
 | 
			
		||||
        '/tmp/tfw_recv'
 | 
			
		||||
    )
 | 
			
		||||
    json_pipe_eh = eh_factory.build(json_pipe)
 | 
			
		||||
 | 
			
		||||
    ))
 | 
			
		||||
 | 
			
		||||
    setup_signal_handlers()
 | 
			
		||||
    IOLoop.instance().start()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user