Search code examples
pythonpython-2.7python-click

Click-config file produces error


I am using click-configfile (an extension of Click) to have it read command line args straight from a config file:

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

I am making sure to follow as closely to the tutorial found here. However, when I run the code, I constantly get this stack trace:

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.

I know that error is when you are using a decorator incorrectly. But I am not sure how it is incorrect. Can anyone with knowledge of click-configfile be able to provide some guidance on this?


Solution

  • A click function can either be a group (a collection of commands) or a command, it cannot be both. So the problem is in these two lines:

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

    You will need to remove one of these decorators.