繁体   English   中英

如何在单击选项中使用 python-click 参数(检查文件中是否存在变量)?

[英]How can I use an python-click argument from within a click option (check if variable exists in file)?

我想传递一个文件(netCDF 数据文件)作为第一个参数和一个选项 (-v) 来指示要从此文件读取的变量到我的脚本。

我想我需要一个自定义回调来评估这个或这些变量是否包含在文件中。 但是,我一直在弄清楚如何从我的回调方法中访问参数。

import click
import xarray as xr

def validate_variable(ctx, param, value):
    """Check that requested variable(s) are in netCDF file """

    # HOW TO ACCESS ARGUMENT 'tile' ???

    with xr.open_dataset(ctx.tile) as ds:
        for v in value:
            if v not in ds.data_vars:
                raise click.BadParameter('Var %s not in file %s.' % (v, ctx.tile))

EPILOG = 'my script ...'

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

@click.command(context_settings=CONTEXT_SETTINGS, epilog=EPILOG)

@click.pass_context
@click.option('--variable', '-v', multiple=True, metavar='VAR', 
                callback=validate_variable,
                help='variable to render')
@click.argument('tile', type=click.File())
def cli(tile, variable):
    main(tile, variable)

您在回调中尝试执行此操作时遇到问题,因为调用回调的顺序并不能保证您需要的所有参数在调用回调时都已被评估。

您可以执行此操作的一种方法是覆盖click.Command.make_context() ,并在上下文构建完成后进行验证。

自定义命令类

这个函数被传递一个验证器函数并返回一个自定义的click.Command类。

def command_validate(validator):

    class CustomClass(click.Command):

        def make_context(self, *args, **kwargs):
            ctx = super(CustomClass, self).make_context(*args, **kwargs)
            validator(ctx)
            return ctx

    return CustomClass

验证器功能

验证器函数被传递一个上下文,并且可以在验证失败时引发click.BadParameter异常。

def validate_variables(ctx):
    """Check that requested variable(s) are in netCDF file """

    value = ctx.params['variable']
    tile = ctx.params['tile']
    with xr.open_dataset(tile) as ds:
        for v in value:
            if v not in ds.data_vars:
                raise click.BadParameter(
                    'Var %s not in file %s.' % (v, tile), ctx)

使用自定义类:

要使用自定义类,请传递command_validate验证器函数,并将返回值作为cls传递给命令装饰器,例如:

@click.command(cls=command_validate(validate_variables), ...OtherOptions...)
...Other config...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM