def process_help( self, request ): """process 'help' command return rendered help via request object. **Args** | request (object): request object """ help = [] help.append( "Known commands:" ) # iterate over all commands defined in self.commands help.extend( hQUtils.renderHelp( sorted(self.commands.keys()), self.commands ) ) request.send( '\n'.join( help ) )
def process(self, requestStr, request, logger, server): """parse requst string and process command **Args** | requestStr (string): request as string | request (object): request object | logger (hQLogger): logger instance. | server (hQBaseServer): server instance which got this request """ self.writeLog = logger self.server = server # parse request string. request string could be: # help # help COMMAND_STR # COMMAND # where COMMAND_STR is either a full name of a command and an incomplete command string m = re.match( 'help ?(.*)', requestStr ) if m: # request string is # help # help COMMAND_STR command = m.group(1) if command=="": # no command has been given # return all commands help = [] help.append( "Known commands:" ) # iterate over all commands defined in self.commands help.extend( hQUtils.renderHelp( sorted(self.commands.keys()), self.commands ) ) request.send( '\n'.join( help ) ) else: # check if COMMAND_STR is known by server, i.e. is present in self.commands # match COMMAND_STR against each hQCommand.name try: # find first matching command key = next( key for key,c in self.commands.iteritems() if command==c.name ) cmd = self.commands[ key ] response = [ "help for '"+cmd.name+"':" ] response.append( "--------------------" ) response.append( "full command: {c}".format(c=cmd.get_command_str() ) ) response.append( "" ) response.append( cmd.get_fullhelp() ) request.send( '\n'.join( response ) ) except StopIteration: # COMMAND_STR ist not known. find all commands which begin with COMMAND_STR matching_commands = [ c.name for key,c in self.commands.iteritems() if c.name.startswith( command ) ] if matching_commands: response = [] response.append( "command '" + command + "' is unknown!" ) response.append( "" ) response.append( "similar commands:" ) for c in matching_commands: response.append( " " + c ) request.send( '\n'.join( response ) ) else: request.send( 'no matching command.' ) else: # request string does not beginn with 'help' # find matching command and execute associate function try: cmd = next( cmd for cmd_str,cmd in self.commands.iteritems() if cmd.match( requestStr ) ) except: self.writeLog("unknown command.", logCategory='request_processing') request.send("what do you want?") return # command was found. call associated function try: # call associated function (defined as method of hQBaseRequestProcessor or server # specific processor) with the function arguments self._call_fct( cmd, requestStr, request ) except: self.writeLog("error while processing request.", logCategory='request_processing') print traceback.print_exc() request.send("Request could not be processed.") return