Example #1
0
def make_mpd_magic(command_name):
    """Turn an mpd function into a magic function."""
    def magic_func(self, args=''):
        args = args.split()
        if (mpd_command_dict[command_name][0] is not None and
                len(args) not in mpd_command_dict[command_name][0]):
            raise TypeError(_("%s takes one of %r arguments (%d given)") % (
                    command_name, mpd_command_dict[command_name][0], len(args)))

        # Convert them to correct type
        typ = mpd_command_dict[command_name][1]
        if typ is not None:
            index = 0
            while index < len(args):
                try:
                    args[index] = typ[index](args[index])
                except ValueError:
                    raise ValueError(_("%r is not %s") % (args[index], typ))
                index += 1

        if args:
            self.api.ex("_ret = getattr(_mpc, '%s')(*%r)" % (command_name, args))
        else:
            self.api.ex("_ret = getattr(_mpc, '%s')()" % command_name)

        if getattr(self.api.user_ns["config"], "ret"):
            return self.api.user_ns["_ret"]
    magic_func.__doc__ = mpd_command_dict[command_name][6]
    ip.expose_magic(command_name, magic_func)

    # Create aliases if there are any
    if config_data.has_section("alias"):
        for alias, command in config_data.items("alias"):
            if command_name == command:
                ip.expose_magic(alias, magic_func)
Example #2
0
def set_completer(command, completer_func):
    """Set completion for a command and its aliases."""
    ip.set_hook("complete_command", completer_func, str_key = command)
    # Aliases
    if config_data.has_section("alias"):
        for alias, mycommand in config_data.items("alias"):
            if command == mycommand:
                ip.set_hook("complete_command", completer_func, str_key = alias)