示例#1
0
    def get_command(self, ctx, name):
        """Get the command to be run

        >>> mc = MultiCommand()
        >>> cmd = mc.get_command(None, 'add')
        >>> cmd.name, cmd.help
        ('cli', 'Add...')
        >>> mc.get_command(None, 'this command does not exist')
        """
        namespace = {}
        try:
            script = self.scripts[name]
        except KeyError:
            return None
        if script['python']:
            with open(script['path']) as f:
                code = compile(f.read(), script['path'], 'exec')
                eval(code, namespace, namespace)
            if 'cli' in namespace.keys():
                return namespace['cli']
        # If it gets here, it means that it is an external script
        from papis.commands.external import external_cli as cli
        from papis.commands.external import get_command_help
        cli.context_settings['obj'] = script
        cli.help = get_command_help(script['path'])
        cli.short_help = cli.help
        return cli
示例#2
0
    def get_command(self, ctx, name):
        """Get the command to be run

        >>> mc = MultiCommand()
        >>> cmd = mc.get_command(None, 'add')
        >>> cmd.name, cmd.help
        ('add', 'Add...')
        >>> mc.get_command(None, 'this command does not exist')
        """
        try:
            script = self.scripts[name]
        except KeyError:
            colorama.init()  # general: colorama has to be activated
            self.logger.error('{c.Fore.RED}{c.Style.BRIGHT}{c.Back.BLACK}'
                              'Did you mean {0}?'
                              '{c.Style.RESET_ALL}'.format(' or '.join(
                                  difflib.get_close_matches(name,
                                                            self.scripts,
                                                            n=2)),
                                                           c=colorama))
            return None
        if script['plugin']:
            return script['plugin']
        # If it gets here, it means that it is an external script
        from papis.commands.external import external_cli as cli
        from papis.commands.external import get_command_help
        cli.context_settings['obj'] = script
        cli.help = get_command_help(script['path'])
        cli.name = script["command_name"]
        cli.short_help = cli.help
        return cli
示例#3
0
文件: default.py 项目: papis/papis
    def get_command(self, ctx: click.core.Context,
                    name: str) -> Optional[click.core.Command]:
        """Get the command to be run

        >>> mc = MultiCommand()
        >>> cmd = mc.get_command(None, 'add')
        >>> cmd.name, cmd.help
        ('add', 'Add...')
        >>> mc.get_command(None, 'this command does not exist')
        """
        try:
            script = self.scripts[name]
        except KeyError:
            import difflib
            matches = list(
                map(str, difflib.get_close_matches(name, self.scripts, n=2)))

            import colorama
            self.logger.error(
                "{c.Fore.RED}{c.Style.BRIGHT}{c.Back.BLACK}"
                "Command '{name}' is unknown! Did you mean '{matches}'?"
                "{c.Style.RESET_ALL}".format(c=colorama,
                                             name=name,
                                             matches="' or '".join(matches)))

            # return the match if there was only one match
            if len(matches) == 1:
                self.logger.warning("I suppose you meant: '%s'", matches[0])
                script = self.scripts[matches[0]]
            else:
                return None

        if script.plugin is not None:
            return script.plugin

        # If it gets here, it means that it is an external script
        import copy
        from papis.commands.external import external_cli
        cli = copy.copy(external_cli)

        from papis.commands.external import get_command_help
        cli.context_settings['obj'] = script
        if script.path is not None:
            cli.help = get_command_help(script.path)
        cli.name = script.command_name
        cli.short_help = cli.help
        return cli
示例#4
0
文件: default.py 项目: zlangley/papis
    def get_command(self, ctx, name):
        """Get the command to be run

        >>> mc = MultiCommand()
        >>> cmd = mc.get_command(None, 'add')
        >>> cmd.name, cmd.help
        ('add', 'Add...')
        >>> mc.get_command(None, 'this command does not exist')
        """
        try:
            script = self.scripts[name]
        except KeyError:
            return None
        if script['plugin']:
            return script['plugin']
        # If it gets here, it means that it is an external script
        from papis.commands.external import external_cli as cli
        from papis.commands.external import get_command_help
        cli.context_settings['obj'] = script
        cli.help = get_command_help(script['path'])
        cli.name = script["command_name"]
        cli.short_help = cli.help
        return cli