From a929ac333833a5cbf503d1fcbdee150658d933a4 Mon Sep 17 00:00:00 2001 From: Jan Cholasta Date: Mon, 7 Nov 2016 14:01:10 +0100 Subject: install: use standard Python classes to declare knob types Use type(None) rather than bool to define knobs which are represented as command line flags. This allows declaring both "--option" and "--option={0,1}"-style command line options. Use enum.Enum subclasses instead of set literals to declare enumerations. Use typing.List[T] instead of (list, T) to declare lists. (Note that a minimal reimplementation of typing.List is used instead of the Python 2 backport of the typing module due to non-technical reasons.) Use CheckedIPAddress instead of 'ip' and 'ip-local' to declare IP addresses. https://fedorahosted.org/freeipa/ticket/6392 Reviewed-By: Martin Basti --- ipapython/install/cli.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'ipapython/install/cli.py') diff --git a/ipapython/install/cli.py b/ipapython/install/cli.py index a91afe5fd..65fc9529f 100644 --- a/ipapython/install/cli.py +++ b/ipapython/install/cli.py @@ -7,6 +7,7 @@ Command line support. """ import collections +import enum import functools import optparse import signal @@ -14,7 +15,7 @@ import signal import six from ipapython import admintool, ipa_log_manager -from ipapython.ipautil import private_ccache +from ipapython.ipautil import CheckedIPAddress, private_ccache from . import core, common @@ -23,6 +24,8 @@ __all__ = ['install_tool', 'uninstall_tool'] if six.PY3: long = int +NoneType = type(None) + def _get_usage(configurable_class): usage = '%prog [options]' @@ -143,13 +146,17 @@ class ConfigureTool(admintool.AdminTool): parser, "{0} options".format(group_cls.description)) knob_type = knob_cls.type - if isinstance(knob_type, tuple): - knob_scalar_type = knob_type[1] + if issubclass(knob_type, list): + try: + # typing.List[X].__parameters__ == (X,) + knob_scalar_type = knob_type.__parameters__[0] + except AttributeError: + knob_scalar_type = str else: knob_scalar_type = knob_type kwargs = dict() - if knob_scalar_type is bool: + if knob_scalar_type is NoneType: kwargs['type'] = None kwargs['const'] = True kwargs['default'] = False @@ -159,16 +166,16 @@ class ConfigureTool(admintool.AdminTool): kwargs['type'] = 'int' elif knob_scalar_type is long: kwargs['type'] = 'long' - elif knob_scalar_type in ('ip', 'ip-local'): - kwargs['type'] = knob_scalar_type - elif isinstance(knob_scalar_type, set): + elif knob_scalar_type is CheckedIPAddress: + kwargs['type'] = 'ip' + elif issubclass(knob_scalar_type, enum.Enum): kwargs['type'] = 'choice' - kwargs['choices'] = list(knob_scalar_type) + kwargs['choices'] = [i.value for i in knob_scalar_type] else: kwargs['nargs'] = 1 kwargs['callback_args'] = (knob_scalar_type,) kwargs['dest'] = name - if isinstance(knob_type, tuple): + if issubclass(knob_type, list): if 'type' not in kwargs: kwargs['action'] = 'callback' kwargs['callback'] = ( -- cgit