Move argparse type extraction back into cli.py

This commit is contained in:
Peter Eckersley
2016-03-11 15:07:13 -08:00
parent 6f7f036e2c
commit 86ab35df4f
2 changed files with 17 additions and 10 deletions
+14
View File
@@ -283,6 +283,16 @@ def set_by_cli(var):
# static housekeeping var
set_by_cli.detector = None
def argparse_type(variable):
"Return our argparse type function for a config variable (default: str)"
# pylint: disable=protected-access
for action in helpful_parser.parser._actions:
if action.type is not None and action.dest == variable:
return action.type
return str
def read_file(filename, mode="rb"):
"""Returns the given file's contents.
@@ -304,6 +314,10 @@ def read_file(filename, mode="rb"):
def flag_default(name):
"""Default value for CLI flag."""
# XXX: this is an internal housekeeping notion of defaults before
# argparse has been set up; it is not accurate for all flags. Call it
# with caution. Plugin defaults are missing, and some things are using
# defaults defined in this file, not in constants.py :(
return constants.CLI_DEFAULTS[name]
+3 -10
View File
@@ -139,21 +139,14 @@ def _restore_plugin_configs(config, renewalparams):
for config_item, config_value in six.iteritems(renewalparams):
if config_item.startswith(plugin_prefix + "_") and not cli.set_by_cli(config_item):
# Values None, True, and False need to be treated specially,
# As they don't get parsed correctly based on type
# As their types aren't handled correctly by configobj
if config_value in ("None", "True", "False"):
# bool("False") == True
# pylint: disable=eval-used
setattr(config.namespace, config_item, eval(config_value))
continue
# If argparse has a type for this variable, use it:
# pylint: disable=protected-access
for action in cli.helpful_parser.parser._actions:
if action.type is not None and action.dest == config_item:
setattr(config.namespace, config_item,
action.type(config_value))
break
else:
setattr(config.namespace, config_item, str(config_value))
cast = cli.argparse_type(config_item)
setattr(config.namespace, config_item, cast(config_value))
def _restore_required_config_elements(config, renewalparams):