def apply_commandline(self, cmdline): """ interprets a command line string i.e., splits it into separate command strings, instanciates :class:`Commands <alot.commands.Command>` accordingly and applies then in sequence. :param cmdline: command line to interpret :type cmdline: str """ # remove initial spaces cmdline = cmdline.lstrip() # we pass Commands one by one to `self.apply_command`. # To properly call them in sequence, even if they trigger asyncronous # code (return Deferreds), these applications happen in individual # callback functions which are then used as callback chain to some # trivial Deferred that immediately calls its first callback. This way, # one callback may return a Deferred and thus postpone the application # of the next callback (and thus Command-application) def apply_this_command(ignored, cmdstring): logging.debug('%s command string: "%s"' % (self.mode, str(cmdstring))) #logging.debug('CMDSEQ: apply %s' % str(cmdstring)) # translate cmdstring into :class:`Command` #try: cmd = commandfactory(cmdstring, self.mode) #except CommandParseError, e: # self.notify(e.message, priority='error') # return # store cmdline for use with 'repeat' command if cmd.repeatable: self.last_commandline = cmdline return self.apply_command(cmd) # we initialize a deferred which is already triggered # so that the first callbacks will be called immediately d = defer.succeed(None) # split commandline if necessary for cmdstring in split_commandline(cmdline): d.addCallback(apply_this_command, cmdstring) # add sequence-wide error handler def errorHandler(failure): if failure.check(CommandParseError): self.notify(failure.getErrorMessage(), priority='error') elif failure.check(CommandCanceled): self.notify("operation cancelled", priority='error') else: logging.error(failure.getTraceback()) errmsg = failure.getErrorMessage() if errmsg: msg = "%s\n(check the log for details)" self.notify(msg % errmsg, priority='error') d.addErrback(errorHandler) return d
def get_context(self, line, pos): """ computes start and end position of substring of line that is the command string under given position """ commands = split_commandline(line) + [''] i = 0 start = 0 end = len(commands[i]) while pos > end: i += 1 start = end + 1 end += 1 + len(commands[i]) return start, end
def apply(self, ui): # split commandline if necessary for cmdstring in split_commandline(self.cmdline): logging.debug('CMDSEQ: apply %s' % str(cmdstring)) # translate cmdstring into :class:`Command` try: cmd = commandfactory(cmdstring, ui.mode) # store cmdline for use with 'repeat' command if cmd.repeatable: ui.last_commandline = self.cmdline.lstrip() except CommandParseError, e: ui.notify(e.message, priority='error') return yield ui.apply_command(cmd)
def apply(self, ui): # split commandline if necessary for cmdstring in split_commandline(self.cmdline): logging.debug('CMDSEQ: apply %s' % str(cmdstring)) # translate cmdstring into :class:`Command` try: cmd = commandfactory(cmdstring, ui.mode) # store cmdline for use with 'repeat' command if cmd.repeatable: ui.last_commandline = self.cmdline.lstrip() except CommandParseError as e: ui.notify(e.message, priority='error') return yield ui.apply_command(cmd)
def apply_commandline(self, cmdline): """ Interprets a command line string and applies the resulting (sequence of) :class:`Commands <alot.commands.Command>`. :param cmdline: command line to interpret :type cmdline: str """ # split commandline if necessary cmd = None cmdlist = split_commandline(cmdline) if len(cmdlist) == 1: try: # translate cmdstring into :class:`Command` cmd = commandfactory(cmdlist[0], self.mode) except CommandParseError, e: self.notify(e.message, priority='error') return
def _test(self, base, expected): """Shared helper to reduce some boilerplate.""" actual = helper.split_commandline(base) self.assertListEqual(actual, expected)