summaryrefslogtreecommitdiffstats
path: root/ipapython/install/cli.py
diff options
context:
space:
mode:
authorJan Cholasta <jcholast@redhat.com>2016-11-07 14:01:10 +0100
committerJan Cholasta <jcholast@redhat.com>2016-11-11 12:17:25 +0100
commita929ac333833a5cbf503d1fcbdee150658d933a4 (patch)
tree48ba4ca356525b8433507a17bee4ebbfd719b506 /ipapython/install/cli.py
parent9fd1981ae8abf720f5234b6049c9beabbb1f2211 (diff)
downloadfreeipa-a929ac333833a5cbf503d1fcbdee150658d933a4.tar.gz
freeipa-a929ac333833a5cbf503d1fcbdee150658d933a4.tar.xz
freeipa-a929ac333833a5cbf503d1fcbdee150658d933a4.zip
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 <mbasti@redhat.com>
Diffstat (limited to 'ipapython/install/cli.py')
-rw-r--r--ipapython/install/cli.py25
1 files changed, 16 insertions, 9 deletions
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'] = (