Skip to content
Snippets Groups Projects
Commit 639e1b79 authored by Theophile GENTILHOMME's avatar Theophile GENTILHOMME
Browse files

Move function and fix problem when reuired option

parent b8a5a810
No related branches found
No related tags found
1 merge request!86Dump config file
This commit is part of merge request !86. Comments created here will be created in the context of that merge request.
...@@ -6,11 +6,9 @@ ...@@ -6,11 +6,9 @@
import imp import imp
import pkgutil import pkgutil
import time
from os.path import isfile from os.path import isfile
import logging import logging
import pkg_resources import pkg_resources
import click
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -214,34 +212,3 @@ def mod_to_context(mod): ...@@ -214,34 +212,3 @@ def mod_to_context(mod):
return {k: v for k, v in mod.__dict__.items() return {k: v for k, v in mod.__dict__.items()
if not (k.startswith('__') and k.endswith('__'))} if not (k.startswith('__') and k.endswith('__'))}
def dump_config(params, ctx):
""" Generate configuration file from parameters and context
Parameters
----------
params : :any:`list`
List of parameters. For example, params attributes of click.Option.
ctx : dict
Click context dictionary.
"""
config_file = open(ctx.params.get('dump_config'), 'w')
logger.debug("Generating configuration file `%s'...", config_file)
config_file.write('## Configuration file automatically generated at %s '
'for %s.\n\n\n' % (time.strftime("%d/%m/%Y"),
ctx.command_path))
for param in params:
if param.name not in ctx.params or param.name == 'dump_config':
continue
if not isinstance(param, click.Option):
continue
config_file.write('## %s.\n' % param.help)
config_file.write(
'## Option: %s [default: %s]\n' % (
', '.join(param.opts), str(param.default)
)
)
config_file.write('# %s = %s\n\n' % (param.name,
str(ctx.params[param.name])))
config_file.write('\n\n\n')
from ..log import set_verbosity_level from ..log import set_verbosity_level
from ..config import load, mod_to_context, dump_config from ..config import load, mod_to_context
import time
import click import click
import logging import logging
...@@ -145,6 +146,41 @@ def verbosity_option(**kwargs): ...@@ -145,6 +146,41 @@ def verbosity_option(**kwargs):
return custom_verbosity_option return custom_verbosity_option
def dump_config(command, params, ctx):
""" Generate configuration file from parameters and context
Parameters
----------
params : :any:`list`
List of parameters. For example, params attributes of click.Option.
ctx : dict
Click context dictionary.
"""
with open(ctx.params.get('dump_config'), 'w') as config_file:
logger.debug("Generating configuration file `%s'...", config_file)
config_file.write('## Configuration file automatically generated at %s '
'for %s.\n\n\n' % (time.strftime("%d/%m/%Y"),
ctx.command_path))
if command.help is not None:
config_file.write("'''" + command.help + "'''\n\n\n")
for param in params:
if param.name not in ctx.params or param.name == 'dump_config':
continue
if not isinstance(param, click.Option):
continue
if param.help is not None:
config_file.write('## %s.\n' % param.help)
config_file.write(
'## Option: %s [default: %s]\n' % (
', '.join(param.opts), str(param.default)
)
)
config_file.write('# %s = %s\n\n' % (param.name,
str(ctx.params[param.name])))
config_file.write('\n\n\n')
class ConfigCommand(click.Command): class ConfigCommand(click.Command):
"""A click.Command that can take options both form command line options and """A click.Command that can take options both form command line options and
configuration files. In order to use this class, you have to use the configuration files. In order to use this class, you have to use the
...@@ -177,6 +213,8 @@ class ConfigCommand(click.Command): ...@@ -177,6 +213,8 @@ class ConfigCommand(click.Command):
help="Name of the config file to be generated")(self) help="Name of the config file to be generated")(self)
def invoke(self, ctx): def invoke(self, ctx):
if ctx.params.get('dump_config') is not None:
return dump_config(self, self.params, ctx)
config_files = ctx.params[self.config_argument_name.lower()] config_files = ctx.params[self.config_argument_name.lower()]
# load and normalize context from config files # load and normalize context from config files
config_context = load( config_context = load(
...@@ -203,8 +241,6 @@ class ConfigCommand(click.Command): ...@@ -203,8 +241,6 @@ class ConfigCommand(click.Command):
finally: finally:
# make sure to set this back to False for future invocations # make sure to set this back to False for future invocations
param.required = False param.required = False
if ctx.params.get('dump_config') is not None:
dump_config(self.params, ctx)
return super(ConfigCommand, self).invoke(ctx) return super(ConfigCommand, self).invoke(ctx)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment