Ejemplo n.º 1
0
 def _next_command(self, state, buf=''):
     args = self.childs.keys()
     completions = [c + ' ' for c in args if c.startswith(buf)]
     completions = completions + [None]
     logger.debug('PossibleCompletions=>%s' % completions)
     if len(completions) > 2 and state == 0 and not buf:
         logger.debug("Showing all completions...")
         _print("Possible Completions:")
         for child in self.childs.keys():
             _print("  %s%s" % (child.ljust(16), self.childs[child].help))
         return None
     return completions[state]
Ejemplo n.º 2
0
 def _next_command(self, state, buf=''):
     args = self.childs.keys()
     completions = [c + ' ' for c in args if c.startswith(buf)]
     completions = completions + [None]
     logger.debug('PossibleCompletions=>%s' % completions)
     if len(completions) > 2 and state == 0 and not buf:
         logger.debug("Showing all completions...")
         _print("Possible Completions:")
         for child in self.childs.keys():
             _print("  %s%s" % (child.ljust(16), self.childs[child].help))
         return None
     return completions[state]
Ejemplo n.º 3
0
    def complete(self, line, buf, state, run=False, full_line=None):
        logger.debug('Walked to: %s' % self.name)
        if line:
            # Trying to walk to the next child or suggest the next one
            next_command = line[0]
            has_arg_completed = False
            if self.dynamic_args:
                if len(line) > 1:
                    logger.debug("Dynamic arg '%s' found" % line[0])
                    has_arg_completed = True
                    # Dynamic arg already filled, jump to next word
                    next_command = line[1]
                    line.pop(0)

            if next_command in self.childs.keys():
                if buf:
                    count = 0
                    for c in self.childs.keys():
                        if c.startswith(buf):
                            count += 1
                    if count > 1:
                        logger.debug(
                            "More than one candidate, not walking in %s" % buf)
                        return self._next_command(state, buf)
                logger.debug("Found %s in childs, walking." % next_command)
                # Already completed, walking
                cmd = self.childs[next_command]
                return cmd.complete(line[1:], buf, state, run, full_line)
            else:
                logger.debug('Not walking because %s was not found in childs' %
                             next_command)
                if has_arg_completed:
                    return self._next_command(state, next_command)

        logger.debug('Line=>%s, Buf=>%s, state=>%s' % (line, buf, state))

        if run:
            logger.debug('Executing %s' % self.name)
            return self.run(full_line.rstrip())

        logger.debug('Starting arg complete')

        # Checking if user already typed a valid arg without space at end
        if self.dynamic_args:
            if len(line) and buf and line[0] in self.args():
                readline.insert_text(" ")
                logger.debug("Inserted blank space")
            elif len(line) and not buf and line[0] not in self.args():
                logger.debug("Found an unknown arg, suggesting next command.")
                return self._next_command(state, buf)

        if buf == self.name:
            readline.insert_text(" ")
            logger.debug("Inserted blank space")

        if not self.dynamic_args:
            return self._next_command(state, buf)

        if self.dynamic_args:
            if (buf.strip() in self.args()):
                return self._next_command(state, buf)
            elif line and line[0] in self.args():
                return self._next_command(state, buf)
            else:
                return self._dynamic_args(state, buf)
        else:
            return self._next_command(state, buf)
Ejemplo n.º 4
0
 def _dynamic_args(self, state, buf=''):
     args = self.args()
     completions = [c for c in args if buf is None or c.startswith(buf)]
     completions = completions + [None]
     logger.debug("Returned dynamic args: %s" % completions)
     return completions[state]
Ejemplo n.º 5
0
    def walk(self, buf, state, run=False, full_line=None):
        # Current commands in line
        line = readline.get_line_buffer()
        line_commands = line.split()
        if run:
            line_commands = buf.split()
        logger.debug("Line=>%s" % line_commands)

        if not line and not buf and not run:
            completions = self.completions() + [None]
            return completions[state]

        # Traversing current command
        for cmd_name in line_commands:
            if self.is_child(cmd_name):
                logger.debug("Found existing command=>%s" % cmd_name)
                #if not line.endswith(' '):
                    #logger.debug("Inserting space to line buffer")
                    #readline.insert_text(" ")
                cmd = self.get_command(cmd_name)
                return cmd.complete(line_commands[1:], buf, state, run, full_line)

        # Needing completion
        logger.debug('buffer=> %s, state=>%s' % (buf, state))
        tokens = buf.split()
        logger.debug('tokens=>%s' % tokens)

        # Nothing was provided, running root completion
        completions = self.completions(tokens[0])
        completions += [None]
        logger.debug('completions=>%s' % completions)
        logger.debug('END COMPLETE')
        return completions[state]
Ejemplo n.º 6
0
    def complete(self, line, buf, state, run=False, full_line=None):
        logger.debug('Walked to: %s' % self.name)
        if line:
            # Trying to walk to the next child or suggest the next one
            next_command = line[0]
            has_arg_completed = False
            if self.dynamic_args:
                if len(line) > 1:
                    logger.debug("Dynamic arg '%s' found" % line[0])
                    has_arg_completed = True
                    # Dynamic arg already filled, jump to next word
                    next_command = line[1]
                    line.pop(0)

            if next_command in self.childs.keys():
                if buf:
                    count = 0
                    for c in self.childs.keys():
                        if c.startswith(buf):
                            count += 1
                    if count > 1 and len(line) == 1:
                        logger.debug("More than one candidate, not walking in %s" % buf)
                        return self._next_command(state, buf)
                logger.debug("Found %s in childs, walking." % next_command)
                # Already completed, walking
                cmd = self.childs[next_command]
                return cmd.complete(line[1:], buf, state, run, full_line)
            else:
                logger.debug('Not walking because %s was not found in childs' % next_command)
                if has_arg_completed:
                    return self._next_command(state, next_command)

        logger.debug('Line=>%s, Buf=>%s, state=>%s' % (line, buf, state))

        if run:
            logger.debug('Executing %s' % self.name)
            return self.run(full_line.rstrip())

        logger.debug('Starting arg complete')

        # Checking if user already typed a valid arg without space at end
        if self.dynamic_args:
            if len(line) and buf and line[0] in self.args():
                readline.insert_text(" ")
                logger.debug("Inserted blank space")
            elif len(line) and not buf and line[0] not in self.args():
                logger.debug("Found an unknown arg, suggesting next command.")
                return self._next_command(state, buf)

        if buf == self.name:
            readline.insert_text(" ")
            logger.debug("Inserted blank space")

        if not self.dynamic_args:
            return self._next_command(state, buf)

        if self.dynamic_args:
            if (buf.strip() in self.args()):
                return self._next_command(state, buf)
            elif line and line[0] in self.args():
                return self._next_command(state, buf)
            else:
                return self._dynamic_args(state, buf)
        else:
            return self._next_command(state, buf)
Ejemplo n.º 7
0
 def _dynamic_args(self, state, buf=''):
     args = self.args()
     completions = [c for c in args if buf is None or c.startswith(buf)]
     completions = completions + [None]
     logger.debug("Returned dynamic args: %s" % completions)
     return completions[state]
Ejemplo n.º 8
0
    def complete(self, line, buf, state, run=False, full_line=None):
        logger.debug('Walked to: %s' % self.name)
        if line and self.dynamic_args and len(line) > 1:
            logger.debug("Dynamic arg '%s' found" % line[0])
            has_arg_completed = True
            # Dynamic arg already filled, jump to next word
            next_command = line[1]
            line.pop(0)
        elif line:
            next_command = line[0]
            has_arg_completed = False

            candidates = self.get_candidates(next_command)
            # don't fail to run, or to complete, because an earlier command is a prefix of another
            # also don't autocomplete ambiguous commands that aren't at the end of the line
            if candidates and len(candidates) > 1 and buf and not run and len(
                    line) < 2:
                logger.debug("More than one candidate, not walking in %s: %s" %
                             (buf, candidates))
                return self._next_command(state, buf)
            elif candidates and next_command in candidates:
                logger.debug("Found %s in childs, walking." % next_command)
                cmd = candidates.pop(next_command)
                return cmd.complete(line[1:], buf, state, run, full_line)
            else:
                logger.debug('Not walking because %s was not found in childs' %
                             next_command)
                if has_arg_completed:
                    return self._next_command(state, next_command)

        logger.debug('Line=>%s, Buf=>%s, state=>%s' % (line, buf, state))

        if run:
            logger.debug('Executing %s' % self.name)
            return self.run(full_line.rstrip())

        logger.debug('Starting arg complete')

        # Checking if user already typed a valid arg without space at end
        if self.dynamic_args:
            if len(line) and buf and line[0] in self.args():
                readline.insert_text(" ")
                logger.debug("Inserted blank space")
            elif len(line) and not buf and line[0] not in self.args():
                logger.debug("Found an unknown arg, suggesting next command.")
                return self._next_command(state, buf)

        if buf == self.name:
            readline.insert_text(" ")
            logger.debug("Inserted blank space")

        if not self.dynamic_args:
            return self._next_command(state, buf)

        if self.dynamic_args:
            if (buf.strip() in self.args()):
                return self._next_command(state, buf)
            elif line and line[0] in self.args():
                return self._next_command(state, buf)
            else:
                return self._dynamic_args(state, buf)
        else:
            return self._next_command(state, buf)
Ejemplo n.º 9
0
    def walk(self, buf, state, run=False, full_line=None):
        # Current commands in line
        line = readline.get_line_buffer()
        line_commands = line.split()
        if run:
            line_commands = buf.split()
        logger.debug("Line=>%s" % line_commands)

        if not line and not buf and not run:
            completions = self.completions() + [None]
            return completions[state]

        # Traversing current command
        for cmd_name in line_commands:
            if self.is_child(cmd_name):
                logger.debug("Found existing command=>%s" % cmd_name)
                cmd = self.get_command(cmd_name)
                return cmd.complete(line_commands[1:], buf, state, run, full_line)

        if run:
            if buf != "?":
                print("Unknown Command: %s" % buf)
            self.print_childs_help()
            return
        # Needing completion
        logger.debug('buffer=> %s, state=>%s' % (buf, state))
        tokens = buf.split()
        logger.debug('tokens=>%s' % tokens)

        # Nothing was provided, running root completion
        completions = self.completions(tokens[0])
        completions += [None]
        logger.debug('completions=>%s' % completions)
        logger.debug('END COMPLETE')
        return completions[state]
Ejemplo n.º 10
0
    def walk(self, buf, state, run=False, full_line=None):
        # Current commands in line
        line = readline.get_line_buffer()
        line_commands = line.split()
        if run:
            line_commands = buf.split()
        logger.debug("Line=>%s" % line_commands)

        if not line and not buf and not run:
            completions = self.completions() + [None]
            return completions[state]

        # Traversing current command
        for cmd_name in line_commands:
            if self.is_child(cmd_name):
                logger.debug("Found existing command=>%s" % cmd_name)
                #if not line.endswith(' '):
                #logger.debug("Inserting space to line buffer")
                #readline.insert_text(" ")
                cmd = self.get_command(cmd_name)
                return cmd.complete(line_commands[1:], buf, state, run,
                                    full_line)

        # Needing completion
        logger.debug('buffer=> %s, state=>%s' % (buf, state))
        tokens = buf.split()
        logger.debug('tokens=>%s' % tokens)

        # Nothing was provided, running root completion
        completions = self.completions(tokens[0])
        completions += [None]
        logger.debug('completions=>%s' % completions)
        logger.debug('END COMPLETE')
        return completions[state]