Click-config文件产生错误

时间:2018-02-20 19:34:16

标签: python python-2.7 python-click

我正在使用click-configfile(Click的扩展名)让它直接从配置文件读取命令行参数:

class ConfigSectionSchema(object):
    """Describes all config sections of this configuration file."""

    @matches_section("POOL_CONFIG")   # Matches multiple sections
    class Pool(SectionSchema):
        pooluser      = Param(type=str)
        pool          = Param(type=str)
        poolpassword  = Param(type=str)


class ConfigFileProcessor(ConfigFileReader):
    config_files = ["xenbuilder.config"]
    config_section_schemas = [
        ConfigSectionSchema.Pool
    ]


CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor.read_config())

class ZenConnection(object):
    def __init__(self, pool, pooluser, poolpassword):
        self.pool = pool
        self.pooluser = pooluser
        self.poolpassword = poolpassword

# Pool Connection param args
@click.group()
@click.command(context_settings=CONTEXT_SETTINGS)
@click.option(
        '--pool',
        help='Pool Server to connect to'
)
@click.option(
        '--pooluser',
        help='Connecting as Pool User'
)
@click.option(
        '--poolpassword',
        hide_input=True,
        help='Password to authenticate in the Xen Pool'
)
@click.pass_context
def cli(ctx,pool,pooluser,poolpassword):
     """ BSD Xen VM Builder """
     ctx.obj = ZenConnection(pool, pooluser, poolpassword)

@click.pass_obj
def xen_session(ctx):
    logging.info('INFO: Establishing Connection...')
    try:
        session = XenAPI.Session(str(ctx.pool))
        session.xenapi.login_with_password(str(ctx.pooluser),str(ctx.poolpassword))
        print("Connection Successful!")
        logging.info('INFO: Connection Successful!!')
    except:
        logging.error("ERROR: Unexpected Error - ", sys.exc_info()[0])
        raise
    return session

我确保密切关注找到的here教程。但是,当我运行代码时,我不断得到这个堆栈跟踪:

Traceback (most recent call last):
  File "/usr/local/bin/bsdxenvmbuilder", line 9, in <module>
    load_entry_point('bsdxenvmbuilder==0.1', 'console_scripts', 'bsdxenvmbuilder')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 565, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2697, in load_entry_point
    return ep.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2370, in load
    return self.resolve()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2376, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/Users/ryankahil/Xen-Builder/bsdxenbuilder.py", line 52, in <module>
    @click.pass_context
  File "/Library/Python/2.7/site-packages/click/decorators.py", line 115, in decorator
    cmd = _make_command(f, name, attrs, cls)
  File "/Library/Python/2.7/site-packages/click/decorators.py", line 71, in _make_command
    raise TypeError('Attempted to convert a callback into a '
TypeError: Attempted to convert a callback into a command twice.

我知道错误是在你错误地使用装饰器的时候。但我不确定这是不正确的。知道click-configfile的人能否就此提供一些指导?

1 个答案:

答案 0 :(得分:2)

单击函数可以是一个组(一组命令)或一个命令,它不能同时是两者。所以问题在于这两行:

@click.group()
@click.command(context_settings=CONTEXT_SETTINGS)

您需要删除其中一个装饰器。