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

Add function dump_config

parent a934e17a
No related branches found
No related tags found
1 merge request!86Dump config file
...@@ -5,10 +5,12 @@ ...@@ -5,10 +5,12 @@
''' '''
import imp import imp
import pkg_resources
import pkgutil import pkgutil
import time
from os.path import isfile from os.path import isfile
import logging import logging
import pkg_resources
import click
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -211,3 +213,25 @@ def mod_to_context(mod): ...@@ -211,3 +213,25 @@ 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):
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 from ..config import load, mod_to_context, dump_config
import time
import click import click
import logging import logging
...@@ -205,30 +204,10 @@ class ConfigCommand(click.Command): ...@@ -205,30 +204,10 @@ class ConfigCommand(click.Command):
# 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: if ctx.params.get('dump_config') is not None:
self.dump_config(ctx) dump_config(self.params, ctx)
return super(ConfigCommand, self).invoke(ctx) return super(ConfigCommand, self).invoke(ctx)
def dump_config(self, ctx):
config_file = open(ctx.params.get('dump_config'), 'w')
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 self.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')
class ResourceOption(click.Option): class ResourceOption(click.Option):
"""A click.Option that is aware if the user actually provided this option """A click.Option that is aware if the user actually provided this option
......
...@@ -58,6 +58,7 @@ Configuration ...@@ -58,6 +58,7 @@ Configuration
bob.extension.rc_config.ENVNAME bob.extension.rc_config.ENVNAME
bob.extension.rc_config.RCFILENAME bob.extension.rc_config.RCFILENAME
bob.extension.config.load bob.extension.config.load
bob.extension.config.dump_config
Stacked Processors Stacked Processors
^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment