2 回答
TA贡献1802条经验 获得超5个赞
装饰命令
定义一个上下文管理器装饰用
contextlib.ContextDecorator
使用
click.pass_context
装饰器 onmain()
,以便您可以探索单击上下文创建
db_context
上下文管理器的实例迭代为组定义的命令
main
使用ctx.command.commands
对于每个命令,使用上下文管理器修饰的相同回调替换原始回调(命令调用的函数)
db_context(cmd)
通过这种方式,您将以编程方式修改每个命令,使其行为类似于:
@main.command()
@db_context
def do_this_thing():
print('doing this thing')
但无需更改您的功能之外的代码main()。
有关工作示例,请参阅下面的代码:
import click
from contextlib import ContextDecorator
class Database_context(ContextDecorator):
"""Decorator context manager."""
def __init__(self, db_url):
self.db_url = db_url
def __enter__(self):
print(f'setup db connection: {self.db_url}')
def __exit__(self, type, value, traceback):
print('teardown db connection')
@click.group()
@click.option('--db', default='local')
@click.pass_context
def main(ctx, db):
print(f'running command against {db} database')
db_url = db # get_db_url(db)
# here come the mysterious part that makes all subcommands
# run inside the connection manager
db_context = Database_context(db_url) # Init context manager decorator
for name, cmd in ctx.command.commands.items(): # Iterate over main.commands
cmd.allow_extra_args = True # Seems to be required, not sure why
cmd.callback = db_context(cmd.callback) # Decorate command callback with context manager
@main.command()
def do_this_thing():
print('doing this thing')
@main.command()
def do_that_thing():
print('doing that thing')
if __name__ == "__main__":
main()
它执行您在问题中描述的内容,希望它能在实际代码中按预期工作。
使用 click.pass_context
下面的代码将让您了解如何使用click.pass_context.
import click
from contextlib import contextmanager
@contextmanager
def database_context(db_url):
try:
print(f'setup db connection: {db_url}')
yield
finally:
print('teardown db connection')
@click.group()
@click.option('--db',default='local')
@click.pass_context
def main(ctx, db):
ctx.ensure_object(dict)
print(f'running command against {db} database')
db_url = db #get_db_url(db)
# Initiate context manager
ctx.obj['context'] = database_context(db_url)
@main.command()
@click.pass_context
def do_this_thing(ctx):
with ctx.obj['context']:
print('doing this thing')
@main.command()
@click.pass_context
def do_that_thing(ctx):
with ctx.obj['context']:
print('doing that thing')
if __name__ == "__main__":
main(obj={})
避免显式with语句的另一种解决方案可能是使用 将上下文管理器作为装饰器传递contextlib.ContextDecorator,但使用进行设置可能会更复杂click。
TA贡献1817条经验 获得超6个赞
此用例在 Click from v8.0 中通过使用 ctx.with_resource(context_manager)
https://click.palletsprojects.com/en/8.0.x/api/#click.Context.with_resource
Click 高级文档中有一个工作示例
https://click.palletsprojects.com/en/8.0.x/advanced/#managing-resources
添加回答
举报