summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-08-28 18:31:06 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-08-28 18:31:06 +0000
commita6ec94da601273719e44c69c4d7c23776ab30f3a (patch)
tree9da8b0eaea911aa7a7aa86e1957feb450e1e5828
parent283c6f8fcec6a4687fd2cc99326a7f2b33e4e8bf (diff)
downloadfreeipa-a6ec94da601273719e44c69c4d7c23776ab30f3a.tar.gz
freeipa-a6ec94da601273719e44c69c4d7c23776ab30f3a.tar.xz
freeipa-a6ec94da601273719e44c69c4d7c23776ab30f3a.zip
217: Started work on new Option2 class that is more declarative and doesn't require subclassing from Option
-rw-r--r--ipalib/public.py37
-rw-r--r--ipalib/tests/test_public.py29
2 files changed, 65 insertions, 1 deletions
diff --git a/ipalib/public.py b/ipalib/public.py
index 78d4a7a46..4c2a8dd2f 100644
--- a/ipalib/public.py
+++ b/ipalib/public.py
@@ -27,6 +27,7 @@ import inspect
import plugable
from plugable import lock
import errors
+import ipa_types
RULE_FLAG = 'validation_rule'
@@ -83,6 +84,42 @@ class DefaultFrom(plugable.ReadOnly):
return None
+class Option2(plugable.ReadOnly):
+ def __init__(self, name, doc, type_, required=False, multivalue=False,
+ default=None, default_from=None, normalize=None, rules=tuple()
+ ):
+ self.name = name
+ self.doc = doc
+ self.type = type_
+ self.required = required
+ self.multivalue = multivalue
+ self.default = default
+ self.default_from = default_from
+ self.__normalize = normalize
+ self.rules = (type_.validate,) + rules
+ lock(self)
+
+ def validate_scalar(self, value):
+ for rule in self.rules:
+ msg = rule(value)
+ if msg is not None:
+ raise errors.RuleError(
+ self.__class__.__name__,
+ value,
+ rule,
+ msg,
+ )
+
+ def validate(self, value):
+ if self.multivalue:
+ if type(value) is not tuple:
+ value = (value,)
+ for v in value:
+ self.validate_scalar(v)
+ else:
+ self.validate_scalar(value)
+
+
class Option(plugable.Plugin):
"""
The Option class represents a kw argument from a `Command`.
diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py
index 3ed1f5da2..5b5d1d4b5 100644
--- a/ipalib/tests/test_public.py
+++ b/ipalib/tests/test_public.py
@@ -22,7 +22,7 @@ Unit tests for `ipalib.public` module.
"""
from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker
-from ipalib import public, plugable, errors
+from ipalib import public, plugable, errors, ipa_types
def test_RULE_FLAG():
@@ -104,6 +104,33 @@ class test_DefaltFrom(ClassChecker):
assert o(**kw_copy) is None
+class test_Option2(ClassChecker):
+ """
+ Tests the `public.Option2` class.
+ """
+ _cls = public.Option2
+
+ def test_class(self):
+ assert self.cls.__bases__ == (plugable.ReadOnly,)
+
+ def test_init(self):
+ name = 'sn',
+ doc = 'Last Name',
+ type_ = ipa_types.Unicode()
+ o = self.cls(name, doc, type_)
+ assert o.__islocked__() is True
+ assert read_only(o, 'name') is name
+ assert read_only(o, 'doc') is doc
+ assert read_only(o, 'type') is type_
+ assert read_only(o, 'required') is False
+ assert read_only(o, 'multivalue') is False
+ assert read_only(o, 'default') is None
+ assert read_only(o, 'default_from') is None
+ assert read_only(o, 'rules') == (type_.validate,)
+
+
+
+
class test_Option(ClassChecker):
"""
Tests the `public.Option` class.