python - 单击配置文件产生错误

标签 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 的任何人都可以提供一些指导吗?

最佳答案

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

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

您需要删除这些装饰器之一。

关于python - 单击配置文件产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48892906/

相关文章:

python-2.7 - python protobuf 安装为 windows

python - 多个单词选项

python - 如何将接触的多边形与 geopandas 合并

python - 如何让 Python 看到 librt?

python - 使用 shell 脚本中的选项调用 python 函数

Python 检查列表中的项目是否空闲

python - 在 Python 中的 Else 之后使用 IF

python - 将 Flask 的 Click CLI 与应用工厂模式结合使用

python - Python Click 提示是否有任何预填充选项?

python 无法编码(marshal) <class 'decimal.Decimal' > 对象