Exemplo n.º 1
0
    def powercmd_complete(text):
        from powercmd.utils import match_instance, get_available_instance_names
        from powercmd.match_string import match_string

        paren_idx = text.find('(')
        if paren_idx >= 0:
            # argument completion
            opt_name = text[:paren_idx]
            if opt_name == 'Option':
                ctor = Option
            else:
                ctor = match_instance(Option, text[:paren_idx],
                                      match_extra_cls=[OptionConstructor])
                if not ctor:
                    return []
                elif isinstance(ctor, OptionConstructor):
                    ctor = ctor.content_mapper

            sig = inspect.signature(ctor)

            # the '' is a hack to prevent Cmd from actually inserting the completion
            return ['', str(sig)]

        possible = get_available_instance_names(Option,
                                                match_extra_cls=[OptionConstructor],
                                                append_paren_to_callables=True)
        return match_string(text, possible)
Exemplo n.º 2
0
    def powercmd_complete(text):
        from powercmd.utils import match_instance, get_available_instance_names
        from powercmd.match_string import match_string

        paren_idx = text.find('(')
        if paren_idx >= 0:
            # argument completion
            opt_name = text[:paren_idx]
            if opt_name == 'Option':
                ctor = Option
            else:
                ctor = match_instance(Option, text[:paren_idx],
                                      match_extra_cls=[OptionConstructor])
                if not ctor:
                    return []

            if isinstance(ctor, OptionConstructor):
                ctor = ctor.content_mapper

            sig = inspect.signature(ctor)

            # the '' is a hack to prevent Cmd from actually inserting the completion
            return ['', str(sig)]

        possible =  get_available_instance_names(Option,
                                                 match_extra_cls=[OptionConstructor],
                                                 append_paren_to_callables=True)
        return match_string(text, possible)
Exemplo n.º 3
0
 def _complete_commands(self, incomplete_cmd: str) -> Sequence[Completion]:
     """
     Returns a sequence of command completions matching INCOMPLETE_CMD prefix.
     """
     matching_cmds = (self._cmds[cmd] for cmd in match_string(incomplete_cmd, self._cmds))
     yield from (Completion(cmd.name,
                            start_position=-len(incomplete_cmd),
                            display_meta=cmd.short_description)
                 for cmd in matching_cmds)
Exemplo n.º 4
0
    def powercmd_complete(text):
        """
        If a type has a static powercmd_complete method, it's called to get a
        list of possible autocompletions for given input TEXT.

        powercmd.match_string contains match_string and simple_match_string
        functions that may be used to filter a list of possibilities using
        various match strategies (like e.g. fuzzy search).
        """
        return match_string(text, ['first_completion', 'second_completion'])
Exemplo n.º 5
0
    def choose(self, short_cmd: str, verbose: bool = False) -> Command:
        """Returns a command handler that matches SHORT_CMD."""
        matches = match_string(short_cmd, self, verbose=verbose)

        if not matches:
            raise InvalidInput('no such command: %s' % (short_cmd, ))
        if len(matches) > 1:
            raise InvalidInput('ambigious command: %s (possible: %s)' %
                               (short_cmd, ' '.join(matches)))
        return self[matches[0]]
Exemplo n.º 6
0
 def _complete_enum(enum_hint: type,
                    incomplete_value: str):
     """
     Returns completions for an class derived from enum.Enum type.
     """
     matching_names = match_string(incomplete_value, (val.name for val in list(enum_hint)))
     matching_vals = (enum_hint[name] for name in matching_names)
     yield from (Completion(val.name,
                            start_position=-len(incomplete_value),
                            display_meta=str(val.value))
                 for val in matching_vals)
Exemplo n.º 7
0
 def _complete_params(cmd: Command, incomplete_param: str) -> Sequence[Completion]:
     """
     Returns a sequence of parameter name completions matching INCOMPLETE_PARAM
     prefix for given CMD.
     """
     matching_params = (cmd.parameters[param]
                        for param in match_string(incomplete_param, cmd.parameters))
     yield from (Completion(param.name,
                            start_position=-len(incomplete_param),
                            display_meta=str(param.type))
                 for param in matching_params)
Exemplo n.º 8
0
    def choose(self,
               short_cmd: str,
               verbose: bool = False) -> Command:
        """Returns a command handler that matches SHORT_CMD."""
        matches = match_string(short_cmd, self, verbose=verbose)

        if not matches:
            raise InvalidInput('no such command: %s' % (short_cmd,))
        elif len(matches) > 1:
            raise InvalidInput('ambigious command: %s (possible: %s)'
                               % (short_cmd, ' '.join(matches)))
        else:
            return self[matches[0]]
Exemplo n.º 9
0
    def _choose_cmd_handler(self,
                            cmds: Mapping[str, Callable],
                            short_cmd: str,
                            verbose: bool = False) -> Callable:
        """Returns a command handler that matches SHORT_CMD."""
        matches = match_string(short_cmd, cmds, verbose=verbose)

        if not matches:
            raise Cmd.SyntaxError('no such command: %s' % (short_cmd, ))
        elif len(matches) > 1:
            raise Cmd.SyntaxError('ambigious command: %s (possible: %s)' %
                                  (short_cmd, ' '.join(matches)))
        else:
            return cmds[matches[0]]
Exemplo n.º 10
0
Arquivo: cmd.py Projeto: dextero/nsh
    def _choose_cmd_handler(self,
                            cmds: Mapping[str, Callable],
                            short_cmd: str,
                            verbose: bool=False) -> Callable:
        """Returns a command handler that matches SHORT_CMD."""
        matches = match_string(short_cmd, cmds, verbose=verbose)

        if not matches:
            raise Cmd.SyntaxError('no such command: %s' % (short_cmd,))
        elif len(matches) > 1:
            raise Cmd.SyntaxError('ambigious command: %s (possible: %s)'
                                  % (short_cmd, ' '.join(matches)))
        else:
            return cmds[matches[0]]
Exemplo n.º 11
0
Arquivo: cmd.py Projeto: dextero/nsh
    def _complete_impl(self,
                       line: str,
                       possibilities: OrderedMapping[str, inspect.Parameter]) -> List[str]:
        """
        Returns the list of possible tab-completions for given LINE.
        """
        words = line.split(' ')
        if words and '=' in words[-1] and line[-1] not in string.whitespace:
            try:
                key, val = words[-1].split('=', maxsplit=1)

                try:
                    completer = self.get_completer(possibilities[key].annotation)
                    return list(completer(val))
                except AttributeError as e:
                    pass
            except ValueError as e:
                print(e)

        matches = match_string(words[-1], possibilities)
        return [x + '=' for x in matches]
Exemplo n.º 12
0
Arquivo: cmd.py Projeto: krwc/powercmd
    def _complete_impl(self,
                       line: str,
                       possibilities: OrderedMapping[str, inspect.Parameter]) -> List[str]:
        """
        Returns the list of possible tab-completions for given LINE.
        """
        words = line.split(' ')
        if words and '=' in words[-1] and line[-1] not in string.whitespace:
            try:
                key, val = words[-1].split('=', maxsplit=1)

                try:
                    completer = self.get_completer(possibilities[key].annotation)
                    return list(completer(val))
                except AttributeError as e:
                    pass
            except ValueError as e:
                print(e)

        matches = match_string(words[-1], possibilities)
        return [x + '=' for x in matches]
Exemplo n.º 13
0
    def powercmd_complete(text):
        from powercmd.utils import get_available_instance_names
        from powercmd.match_string import match_string

        possible = get_available_instance_names(ContentFormatOption)
        return match_string(text, possible)
Exemplo n.º 14
0
    def powercmd_complete(text):
        from powercmd.utils import get_available_instance_names
        from powercmd.match_string import match_string

        possible = get_available_instance_names(Code)
        return match_string(text, possible)
Exemplo n.º 15
0
 def powercmd_complete(text):
     possible = get_available_instance_names(ContentFormatOption)
     return match_string(text, possible)
Exemplo n.º 16
0
 def powercmd_complete(text):
     from powercmd.match_string import match_string
     return match_string(text, ResourceType.TYPES.keys())