def __call__(self, argstring): args = [] param = None # Use callback-specified parameter parsers to generate param list from strings for param in self.params: result = param(argstring) argstring = result[-1] args.extend(result[:-1]) # Parse repeating final args while argstring: if param is None or not param.gobble: msg = f'{self.name} takes {len(self.params)} argument' if len(self.params) > 1: msg += 's' count = len(self.params) while argstring: _, argstring = getnextarg(argstring) count += 1 msg += f', but {count} were given' raise TypeError(msg) result = param(argstring) argstring = result[-1] args.extend(result[:-1]) # Call callback function with parsed parameters ret = self.callback(*args) return ret
def __call__(self, strargs): # First check subcommand if strargs: subcmd, subargs = getnextarg(strargs) subcmdobj = self.subcmds.get(subcmd.upper()) if subcmdobj: return subcmdobj(subargs) return super().__call__(strargs)
def process(): ''' Client-side stack processing. ''' # Process stack of commands for cmdline in Stack.commands(): success = True echotext = '' echoflags = bs.BS_OK # Get first argument from command line and check if it's a command cmd, argstring = argparser.getnextarg(cmdline) cmdu = cmd.upper() cmdobj = Command.cmddict.get(cmdu) # Proceed if a command object was found if cmdobj: try: # Call the command, passing the argument string success, echotext = cmdobj(argstring) if not success: if not argstring: echotext = echotext or cmdobj.brieftext() else: echoflags = bs.BS_FUNERR echotext = f'Syntax error: {echotext or cmdobj.brieftext()}' except Exception as e: success = False echoflags = bs.BS_ARGERR header = '' if not argstring else e.args[ 0] if e.args else 'Argument error.' echotext = f'{header}\nUsage:\n{cmdobj.brieftext()}' elif Stack.sender_rte is None: # If sender_id is None, this stack command originated from the gui. Send it on to the sim forward() # ------------------------------------------------------------------- # Command not found # ------------------------------------------------------------------- else: success = False echoflags = bs.BS_CMDERR if not argstring: echotext = f'Unknown command or aircraft: {cmd}' else: echotext = f'Unknown command: {cmd}' # Always return on command if echotext: bs.scr.echo(echotext, echoflags, Stack.sender_rte) # Clear the processed commands Stack.cmdstack.clear()
def __call__(self, argstring): args = [] param = None # Use callback-specified parameter parsers to generate param list from strings for param in self.params: result = param(argstring) argstring = result[-1] args.extend(result[:-1]) # Parse repeating final args while argstring: if param is None or not param.gobble: msg = f'{self.name} takes {len(self.params)} argument' if len(self.params) > 1: msg += 's' count = len(self.params) while argstring: _, argstring = getnextarg(argstring) count += 1 msg += f', but {count} were given' raise ArgumentError(msg) result = param(argstring) argstring = result[-1] args.extend(result[:-1]) # Call callback function with parsed parameters ret = self.callback(*args) # Always return a tuple with a success value and a message string if ret is None: return True, '' if isinstance(ret, (tuple, list)) and ret: if len(ret) > 1: # Assume that (success, echotext) is returned return ret[:2] ret = ret[0] return ret, ''
def process(): ''' Process and empty command stack. ''' global sender_rte # First check for commands in scenario file checkscen() # Process stack of commands for line, sender_rte in cmdstack: success = True echotext = '' echoflags = bs.BS_OK # Get first argument from command line and check if it's a command cmd, argstring = argparser.getnextarg(line) cmdu = cmd.upper() cmdobj = Command.cmddict.get(cmdu) # If no function is found for 'cmd', check if cmd is actually an aircraft id if not cmdobj and cmdu in bs.traf.id: cmd, argstring = argparser.getnextarg(argstring) argstring = cmdu + " " + argstring # When no other args are parsed, command is POS cmdu = cmd.upper() if cmd else 'POS' cmdobj = Command.cmddict.get(cmdu) # Proceed if a command object was found if cmdobj: try: # Call the command, passing the argument string result = cmdobj(argstring) if result is not None: if isinstance(result, tuple) and result: if len(result) > 1: echotext = result[1] success = result[0] else: # Assume result is a bool indicating the success of the function success = result if not success: if not argstring: echotext = echotext or cmdobj.brieftext() else: echoflags = bs.BS_FUNERR echotext = f'Syntax error: {echotext or cmdobj.brieftext()}' except Exception as e: success = False echoflags = bs.BS_ARGERR header = '' if not argstring else e.args[0] if e.args else 'Argument error.' echotext = f'{header}\nUsage:\n{cmdobj.brieftext()}' # ---------------------------------------------------------------------- # ZOOM command (or use ++++ or -- to zoom in or out) # ---------------------------------------------------------------------- elif cmdu[0] in ("+", "=", "-"): nplus = cmdu.count("+") + cmdu.count("=") # = equals + (same key) nmin = cmdu.count("-") bs.scr.zoom(math.sqrt(2) ** (nplus - nmin), absolute=False) cmdu = 'ZOOM' # ------------------------------------------------------------------- # Command not found # ------------------------------------------------------------------- else: echoflags = bs.BS_CMDERR if not argstring: echotext = f'Unknown command or aircraft: {cmd}' else: echotext = f'Unknown command: {cmd}' # Recording of actual validated commands if success: recorder.savecmd(cmdu, line) elif not sender_rte: echotext = f'{line}\n{echotext}' # Always return on command if echotext: bs.scr.echo(echotext, echoflags) # Clear the processed commands cmdstack.clear()
def process(from_pcall=None): ''' Sim-side stack processing. ''' # First check for commands in scenario file if from_pcall is None: checkscen() # Process stack of commands for cmdline in Stack.commands(from_pcall): success = True echotext = '' echoflags = bs.BS_OK # Get first argument from command line and check if it's a command cmd, argstring = argparser.getnextarg(cmdline) cmdu = cmd.upper() cmdobj = Command.cmddict.get(cmdu) # If no function is found for 'cmd', check if cmd is actually an aircraft id if not cmdobj and cmdu in bs.traf.id: cmd, argstring = argparser.getnextarg(argstring) argstring = cmdu + " " + argstring # When no other args are parsed, command is POS cmdu = cmd.upper() if cmd else 'POS' cmdobj = Command.cmddict.get(cmdu) # Proceed if a command object was found if cmdobj: try: # Call the command, passing the argument string success, echotext = cmdobj(argstring) if not success: if not argstring: echotext = echotext or cmdobj.brieftext() else: echoflags = bs.BS_FUNERR echotext = f'Syntax error: {echotext or cmdobj.brieftext()}' except ArgumentError as e: success = False echoflags = bs.BS_ARGERR header = '' if not argstring else e.args[0] if e.args else 'Argument error.' echotext = f'{header}\nUsage:\n{cmdobj.brieftext()}' except Exception as e: echoflags = bs.BS_FUNERR header = '' if not argstring else e.args[0] if e.args else 'Function error.' echotext = f'Error calling function implementation of {cmdu}: {header}\n' + \ 'Traceback printed to terminal.' traceback.print_exc() # ---------------------------------------------------------------------- # ZOOM command (or use ++++ or -- to zoom in or out) # ---------------------------------------------------------------------- elif cmdu[0] in ("+", "=", "-"): # = equals + (same key) nplus = cmdu.count("+") + cmdu.count("=") nmin = cmdu.count("-") bs.scr.zoom(math.sqrt(2) ** (nplus - nmin), absolute=False) cmdu = 'ZOOM' # ------------------------------------------------------------------- # Command not found # ------------------------------------------------------------------- elif Stack.sender_rte is None: # Command came from scenario file: assume it's a gui/client command and send it on forward() else: success = False echoflags = bs.BS_CMDERR if not argstring: echotext = f'Unknown command or aircraft: {cmd}' else: echotext = f'Unknown command: {cmd}' # Recording of actual validated commands if success: recorder.savecmd(cmdu, cmdline) elif not Stack.sender_rte: echotext = f'{cmdline}\n{echotext}' # Always return on command if echotext: bs.scr.echo(echotext, echoflags) # Clear the processed commands if from_pcall is None: Stack.clear()