diff options
| author | Jan Cholasta <jcholast@redhat.com> | 2016-11-07 14:01:10 +0100 |
|---|---|---|
| committer | Jan Cholasta <jcholast@redhat.com> | 2016-11-11 12:17:25 +0100 |
| commit | a929ac333833a5cbf503d1fcbdee150658d933a4 (patch) | |
| tree | 48ba4ca356525b8433507a17bee4ebbfd719b506 /ipapython/install/core.py | |
| parent | 9fd1981ae8abf720f5234b6049c9beabbb1f2211 (diff) | |
| download | freeipa-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/core.py')
| -rw-r--r-- | ipapython/install/core.py | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/ipapython/install/core.py b/ipapython/install/core.py index c3dc90846..111b71090 100644 --- a/ipapython/install/core.py +++ b/ipapython/install/core.py @@ -8,20 +8,25 @@ The framework core. import abc import collections +import enum import functools import itertools +import re import sys import six from ipapython.ipa_log_manager import root_logger +from ipapython.ipautil import CheckedIPAddress -from . import util +from . import util, typing from .util import from_ __all__ = ['InvalidStateError', 'KnobValueError', 'Property', 'Knob', 'Configurable', 'Group', 'Component', 'Composite'] +NoneType = type(None) + # Configurable states _VALIDATE_PENDING = 'VALIDATE_PENDING' _VALIDATE_RUNNING = 'VALIDATE_RUNNING' @@ -145,11 +150,15 @@ def knob(type_or_base, default=_missing, sensitive=_missing, deprecated=_missing, description=_missing, cli_positional=_missing, cli_name=_missing, cli_short_name=_missing, cli_aliases=_missing, cli_metavar=_missing): + if type_or_base is None: + type_or_base = NoneType + + assert isinstance(type_or_base, type) + class_dict = {} class_dict['_order'] = next(_counter) - if (not isinstance(type_or_base, type) or - not issubclass(type_or_base, KnobBase)): + if not issubclass(type_or_base, KnobBase): class_dict['type'] = type_or_base type_or_base = KnobBase @@ -179,6 +188,27 @@ def Knob(type_or_base, default=_missing, sensitive=_missing, deprecated=_missing, description=_missing, cli_positional=_missing, cli_name=_missing, cli_short_name=_missing, cli_aliases=_missing, cli_metavar=_missing): + if isinstance(type_or_base, tuple): + assert type_or_base[0] is list + scalar_type = type_or_base[1] + else: + scalar_type = type_or_base + + if scalar_type is bool: + scalar_type = NoneType + elif scalar_type == 'ip': + scalar_type = CheckedIPAddress + elif isinstance(scalar_type, set): + scalar_type = type( + 'Enum', + (enum.Enum,), + {re.sub(r'[^0-9A-Za-z_]', '', n): n for n in scalar_type}) + + if isinstance(type_or_base, tuple): + type_or_base = typing.List[scalar_type] + else: + type_or_base = scalar_type + return knob(type_or_base, default=default, sensitive=sensitive, |
