mirror of
				https://github.com/avatao-content/test-tutorial-framework
				synced 2025-11-04 12:12:55 +00:00 
			
		
		
		
	Refactor command implementation boilerplate into Command class
This commit is contained in:
		@@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					from re import match
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from tornado.ioloop import IOLoop
 | 
					from tornado.ioloop import IOLoop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor
 | 
					from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor
 | 
				
			||||||
@@ -13,28 +15,53 @@ def cenator(history):
 | 
				
			|||||||
    MessageSender().send('JOHN CENA', 'You\'ve executed "{}"'.format(history[-1]))
 | 
					    MessageSender().send('JOHN CENA', 'You\'ve executed "{}"'.format(history[-1]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def selectdir(history):
 | 
					class Commands:
 | 
				
			||||||
 | 
					    def __init__(self):
 | 
				
			||||||
 | 
					        self.command_impls = {self._parse_command_name(fun): getattr(self, fun) for fun in dir(self)
 | 
				
			||||||
 | 
					                              if callable(getattr(self, fun))
 | 
				
			||||||
 | 
					                              and self._is_command_implementation(fun)}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _is_command_implementation(method_name):
 | 
				
			||||||
 | 
					        return bool(Commands._match_command_regex(method_name))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _parse_command_name(method_name):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
        cmd = history[-1].split()
 | 
					            return Commands._match_command_regex(method_name).groups()[0]
 | 
				
			||||||
        if cmd[0] == 'selectdir':
 | 
					        except AttributeError:
 | 
				
			||||||
 | 
					            return ''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _match_command_regex(string):
 | 
				
			||||||
 | 
					        command_impl_regex = r'^_command_impl_(.+)$'
 | 
				
			||||||
 | 
					        return match(command_impl_regex, string)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def callback(self, history):
 | 
				
			||||||
 | 
					        parts = history[-1].split()
 | 
				
			||||||
 | 
					        command = parts[0]
 | 
				
			||||||
 | 
					        if command in self.command_impls.keys():
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                self.command_impls[command](*parts[1:])
 | 
				
			||||||
 | 
					            except IndexError:
 | 
				
			||||||
 | 
					                LOG.debug('Command "%s" failed!', command)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _command_impl_selectdir(self, *args):
 | 
				
			||||||
        TFWServerConnector().send_to_eventhandler('webide',
 | 
					        TFWServerConnector().send_to_eventhandler('webide',
 | 
				
			||||||
                                                  {'data': {'command': 'selectdir',
 | 
					                                                  {'data': {'command': 'selectdir',
 | 
				
			||||||
                                                                'directory': cmd[1]}})
 | 
					                                                            'directory': args[0]}})
 | 
				
			||||||
    except IndexError:
 | 
					 | 
				
			||||||
        LOG.exception('Selectdir failed!')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _command_impl_trigger(self, *args):
 | 
				
			||||||
 | 
					        TFWServerConnector().send('selectdir_needs_no_key',
 | 
				
			||||||
 | 
					                                  {'trigger': args[0]})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def toggle_next(history):
 | 
					    def _command_impl_togglenext(self, *args):
 | 
				
			||||||
    toggle_next.button_state = not toggle_next.button_state
 | 
					        if not hasattr(self, 'togglenext_visible'):
 | 
				
			||||||
    try:
 | 
					            self.togglenext_visible = True
 | 
				
			||||||
        cmd = history[-1].split()
 | 
					 | 
				
			||||||
        if cmd[0] == 'togglenext':
 | 
					 | 
				
			||||||
        TFWServerConnector().send('messagecontrol',
 | 
					        TFWServerConnector().send('messagecontrol',
 | 
				
			||||||
                                  {'data': {'command': 'showbutton',
 | 
					                                  {'data': {'command': 'showbutton',
 | 
				
			||||||
                                                'next_visibility': toggle_next.button_state}})
 | 
					                                            'next_visibility': self.togglenext_visible}})
 | 
				
			||||||
    except IndexError:
 | 
					        self.togglenext_visible = not self.togglenext_visible
 | 
				
			||||||
        LOG.exception('Togglenext failed!')
 | 
					 | 
				
			||||||
toggle_next.button_state = False
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
@@ -43,8 +70,8 @@ if __name__ == '__main__':
 | 
				
			|||||||
                             directory=TFWENV.WEBIDE_WD, exclude=['*.pyc'])
 | 
					                             directory=TFWENV.WEBIDE_WD, exclude=['*.pyc'])
 | 
				
			||||||
    terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE))
 | 
					    terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE))
 | 
				
			||||||
    terminado.historymonitor.subscribe_callback(cenator)
 | 
					    terminado.historymonitor.subscribe_callback(cenator)
 | 
				
			||||||
    terminado.historymonitor.subscribe_callback(selectdir)
 | 
					    commands = Commands()
 | 
				
			||||||
    terminado.historymonitor.subscribe_callback(toggle_next)
 | 
					    terminado.historymonitor.subscribe_callback(commands.callback)
 | 
				
			||||||
    processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)
 | 
					    processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    eventhandlers = {ide, terminado, processmanager}
 | 
					    eventhandlers = {ide, terminado, processmanager}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user