summaryrefslogtreecommitdiffstats
path: root/ipapython/install/typing.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/typing.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/typing.py')
-rw-r--r--ipapython/install/typing.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/ipapython/install/typing.py b/ipapython/install/typing.py
new file mode 100644
index 000000000..d86fc8f31
--- /dev/null
+++ b/ipapython/install/typing.py
@@ -0,0 +1,34 @@
+#
+# Copyright (C) 2016 FreeIPA Contributors see COPYING for license
+#
+
+import weakref
+
+import six
+
+_cache = weakref.WeakValueDictionary()
+
+
+class ListMeta(type):
+ def __getitem__(cls, key):
+ if not isinstance(key, type):
+ raise TypeError("Parameters to generic types must be types. "
+ "Got {!r}.".format(key))
+
+ t = ListMeta(
+ cls.__name__,
+ cls.__bases__,
+ {
+ '__parameters__': (key,),
+ '__init__': cls.__init__,
+ }
+ )
+
+ return _cache.get(key, t)
+
+
+class List(six.with_metaclass(ListMeta, list)):
+ __parameters__ = ()
+
+ def __init__(self, *_args, **_kwargs):
+ raise TypeError("Type List cannot be instantiated; use list() instead")