如何在单击中为组操作创建例外
How do I create an exception to a group action in 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()
函数。
有没有一种方法可以修饰命令,使我可以修改组代码 运行 之前的状态?还是另一种完全可以实现此目的的方法?
为了跟踪哪些命令绕过升级检查,我建议在绕过标记装饰器中将该状态存储在 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
我有一个案例,我想 运行 一个通用函数 (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