如何在单击中为组操作创建例外

时间:2018-05-09 23:19:06

标签: python python-click

我有一个案例,我想为大多数点击命令运行一个公共函数(check_upgrade()),但在某些情况下我不想运行它。而不是依赖于开发人员记住添加装饰器或函数调用以显式运行此检查,我宁愿在默认情况下运行它,然后有一个可以添加的装饰器(例如@bypass_upgrade_check)用于命令check_upgrade()不应该运行。

我希望有类似的东西:

class State(object):
    def __init__(self):
        self.bypass_upgrade_check = False

pass_state = click.make_pass_decorator(State, ensure=True)

def bypass_upgrade_check(func):
    @pass_state
    def wrapper(state, *args, **kwargs):
        state.bypass_upgrade_check = True
        func(*args, **kwargs)
    return wrapper 

@click.group()
@pass_state
def common(state):
    if not state.bypass_upgrade_check:
        check_upgrade()

@common.command()
def cmd1():
    # check_upgrade() runs here
    pass

@bypass_upgrade_check
@common.command()
def cmd2():
    # don't run check_upgrade() here
    pass

但这不起作用。它实际上并没有调用bypass_upgrade_check()函数。

有没有办法以这样的方式装饰命令,我可以在组代码运行之前修改状态?还是另一种完成这个的方法呢?

1 个答案:

答案 0 :(得分:1)

为了跟踪哪些命令绕过升级检查,我建议在旁路标记装饰器中将该状态存储在click.Command对象上。然后,如果您将click.Context传递给您的组,则可以查看命令对象以查看它是否标记为允许跳过升级,如:

代码:

def bypass_upgrade_check(func):
    setattr(func, 'do_upgrade_check', False)

@click.group()
@click.pass_context
def cli(ctx):
    sub_cmd = ctx.command.commands[ctx.invoked_subcommand]
    if getattr(sub_cmd, 'do_upgrade_check', True):
        check_upgrade()

测试代码:

import click

def check_upgrade():
    click.echo('Checking Upgrade!')

def bypass_upgrade_check(func):
    setattr(func, 'do_upgrade_check', False)

@click.group()
@click.pass_context
def cli(ctx):
    sub_cmd = ctx.command.commands[ctx.invoked_subcommand]
    if getattr(sub_cmd, 'do_upgrade_check', True):
        check_upgrade()

@cli.command()
def cmd1():
    # check_upgrade() runs here
    click.echo('cmd1')

@bypass_upgrade_check
@cli.command()
def cmd2():
    # don't run check_upgrade() here
    click.echo('cmd2')


if __name__ == "__main__":
    commands = (
        'cmd1',
        'cmd2',
    )

    import sys, time

    time.sleep(1)
    print('Click Version: {}'.format(click.__version__))
    print('Python Version: {}'.format(sys.version))
    for cmd in commands:
        try:
            time.sleep(0.1)
            print('-----------')
            print('> ' + cmd)
            time.sleep(0.1)
            cli(cmd.split())

        except BaseException as exc:
            if str(exc) != '0' and \
                    not isinstance(exc, (click.ClickException, SystemExit)):
                raise

结果:

Click Version: 6.7
Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
-----------
> cmd1
Checking Upgrade!
cmd1
-----------
> cmd2
cmd2