cli: simplify parsing of arbitrary types

Add the 'constructor' type to IPAOption to allow parsing arbitrary
types.

When using this type, supply the 'constructor' attribute with the
constructor of the type.  The checker for the 'constructor' type
attempts to construct the data, returning if successful else raising
OptionValueError.

The 'knob' interface remains unchanged but now accepts arbitrary
constructors.

This feature subsumes the '_option_callback' mechanism, which has
been refactored away.

This feature also subsumes the "dn" type in IPAOption, but this
refactor is deferred.

Part of: https://pagure.io/freeipa/issue/6858

Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
This commit is contained in:
Fraser Tweedale authored and Pavel Vomacka committed 2017-10-04 10:09:18 +02:00
1 parent 7902fc9a06
commit c5afee964e
2 files changed
+18 -26

No files matched your search

+4 -24
View File
@@ -8,7 +8,6 @@ Command line support.
import collections
import enum
import functools
import logging
import optparse # pylint: disable=deprecated-module
import signal
@@ -105,17 +104,6 @@ def uninstall_tool(configurable_class, command_name, log_file_name,
)
def _option_callback(action, option, opt_str, value, parser, opt_type):
try:
value = opt_type(value)
except ValueError as e:
raise optparse.OptionValueError(
"option {0}: {1}".format(opt_str, e))
option.take_action(
action, option.dest, opt_str, value, parser.values, parser)
class ConfigureTool(admintool.AdminTool):
configurable_class = None
debug_option = False
@@ -186,24 +174,16 @@ class ConfigureTool(admintool.AdminTool):
kwargs['metavar'] = "{{{0}}}".format(
",".join(kwargs['choices']))
else:
kwargs['nargs'] = 1
kwargs['callback_args'] = (knob_scalar_type,)
kwargs['type'] = 'constructor'
kwargs['constructor'] = knob_scalar_type
kwargs['dest'] = name
if issubclass(knob_type, list):
if 'type' not in kwargs:
kwargs['action'] = 'callback'
kwargs['callback'] = (
functools.partial(_option_callback, 'append'))
elif kwargs['type'] is None:
if kwargs['type'] is None:
kwargs['action'] = 'append_const'
else:
kwargs['action'] = 'append'
else:
if 'type' not in kwargs:
kwargs['action'] = 'callback'
kwargs['callback'] = (
functools.partial(_option_callback, 'store'))
elif kwargs['type'] is None:
if kwargs['type'] is None:
kwargs['action'] = 'store_const'
else:
kwargs['action'] = 'store'