mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
217: Started work on new Option2 class that is more declarative and doesn't require subclassing from Option
This commit is contained in:
@@ -27,6 +27,7 @@ import inspect
|
|||||||
import plugable
|
import plugable
|
||||||
from plugable import lock
|
from plugable import lock
|
||||||
import errors
|
import errors
|
||||||
|
import ipa_types
|
||||||
|
|
||||||
|
|
||||||
RULE_FLAG = 'validation_rule'
|
RULE_FLAG = 'validation_rule'
|
||||||
@@ -83,6 +84,42 @@ class DefaultFrom(plugable.ReadOnly):
|
|||||||
return None
|
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):
|
class Option(plugable.Plugin):
|
||||||
"""
|
"""
|
||||||
The Option class represents a kw argument from a `Command`.
|
The Option class represents a kw argument from a `Command`.
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ Unit tests for `ipalib.public` module.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker
|
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():
|
def test_RULE_FLAG():
|
||||||
@@ -104,6 +104,33 @@ class test_DefaltFrom(ClassChecker):
|
|||||||
assert o(**kw_copy) is None
|
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):
|
class test_Option(ClassChecker):
|
||||||
"""
|
"""
|
||||||
Tests the `public.Option` class.
|
Tests the `public.Option` class.
|
||||||
|
|||||||
Reference in New Issue
Block a user