def readline_completer(self, text, state):
        """
        A completer for the readline library
        """
        if state == 0:
            # New completion, reset the list of matches and the display hook
            self._readline_matches = []
            try:
                readline.set_completion_display_matches_hook(None)
            except AttributeError:
                pass

            # Get the full line
            full_line = readline.get_line_buffer()
            begin_idx = readline.get_begidx()

            # Parse arguments as best as we can
            try:
                arguments = shlex.split(full_line)
            except ValueError:
                arguments = full_line.split()

            # Extract the command (maybe with its namespace)
            command = arguments.pop(0)

            if begin_idx > 0:
                # We're completing after the command (and maybe some args)
                try:
                    # Find the command
                    ns, command = self._shell.get_ns_command(command)
                except ValueError:
                    # Ambiguous command: ignore
                    return None

                # Use the completer associated to the command, if any
                try:
                    configuration = self._shell.get_command_completers(
                        ns, command
                    )
                    if configuration is not None:
                        self._readline_matches = completion_hints(
                            configuration,
                            self.__get_ps1(),
                            self.__session,
                            self._context,
                            text,
                            arguments,
                        )
                except KeyError:
                    # Unknown command
                    pass

            elif "." in command:
                # Completing the command, and a name space is given
                namespace, prefix = text.split(".", 2)
                commands = self._shell.get_commands(namespace)

                # Filter methods according to the prefix
                self._readline_matches = [
                    "{0}.{1}".format(namespace, command)
                    for command in commands
                    if command.startswith(prefix)
                ]
            else:
                # Completing a command or namespace
                prefix = command

                # Default commands goes first...
                possibilities = [
                    "{0} ".format(command)
                    for command in self._shell.get_commands(None)
                    if command.startswith(prefix)
                ]

                # ... then name spaces
                namespaces = self._shell.get_namespaces()
                possibilities.extend(
                    "{0}.".format(namespace)
                    for namespace in namespaces
                    if namespace.startswith(prefix)
                )

                # ... then commands in those name spaces
                possibilities.extend(
                    "{0} ".format(command)
                    for namespace in namespaces
                    if namespace is not None
                    for command in self._shell.get_commands(namespace)
                    if command.startswith(prefix)
                )

                # Filter methods according to the prefix
                self._readline_matches = possibilities

            if not self._readline_matches:
                return None

            # Return the first possibility
            return self._readline_matches[0]

        elif state < len(self._readline_matches):
            # Next try
            return self._readline_matches[state]

        return None
def readline_completer ( self , text , state ) : if state == 0 : self . _readline_matches = [ ] try : readline . set_completion_display_matches_hook ( None ) except AttributeError : pass full_line = readline . get_line_buffer ( ) begin_idx = readline . get_begidx ( ) try : arguments = shlex . split ( full_line ) except ValueError : arguments = full_line . split ( ) command = arguments . pop ( 0 ) if begin_idx > 0 : try : ns , command = self . _shell . get_ns_command ( command ) except ValueError : return None try : configuration = self . _shell . get_command_completers ( ns , command ) if configuration is not None : self . _readline_matches = completion_hints ( configuration , self . __get_ps1 ( ) , self . __session , self . _context , text , arguments , ) except KeyError : pass elif "." in command : namespace , prefix = text . split ( "." , 2 ) commands = self . _shell . get_commands ( namespace ) self . _readline_matches = [ "{0}.{1}" . format ( namespace , command ) for command in commands if command . startswith ( prefix ) ] else : prefix = command possibilities = [ "{0} " . format ( command ) for command in self . _shell . get_commands ( None ) if command . startswith ( prefix ) ] namespaces = self . _shell . get_namespaces ( ) possibilities . extend ( "{0}." . format ( namespace ) for namespace in namespaces if namespace . startswith ( prefix ) ) possibilities . extend ( "{0} " . format ( command ) for namespace in namespaces if namespace is not None for command in self . _shell . get_commands ( namespace ) if command . startswith ( prefix ) ) self . _readline_matches = possibilities if not self . _readline_matches : return None return self . _readline_matches [ 0 ] elif state < len ( self . _readline_matches ) : return self . _readline_matches [ state ] return None
A completer for the readline library