gpt4 book ai didi

python - 如何为 click 中的组操作创建异常(exception)

转载 作者:行者123 更新时间:2023-12-01 09:27:27 25 4
gpt4 key购买 nike

在某些情况下,我想为大多数点击命令运行一个通用函数 (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() 函数。

有没有一种方法可以装饰命令,以便我可以在组代码运行之前修改状态?或者另一种方法可以完成这个任务?

最佳答案

为了跟踪哪些命令绕过升级检查,我建议在绕过标记装饰器中将该状态存储在 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

关于python - 如何为 click 中的组操作创建异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50263363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com