summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-08-26 19:02:24 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-08-26 19:02:24 +0000
commit0e47948695680047490ebdc55ca3fa4f7347640f (patch)
treeb03a17f1340f3c89a5fe4fb6bd64fae73df47f35
parent0755c218ffdedafbeb9b0a19750704205b4f0b65 (diff)
downloadfreeipa.git-0e47948695680047490ebdc55ca3fa4f7347640f.tar.gz
freeipa.git-0e47948695680047490ebdc55ca3fa4f7347640f.tar.xz
freeipa.git-0e47948695680047490ebdc55ca3fa4f7347640f.zip
197: Added new public.Option.get_default() method that calls Option.default_from() if it's a DefaultFrom instance, and otherwise returns Option.default (the static default value)
-rw-r--r--ipalib/public.py25
-rw-r--r--ipalib/tests/test_public.py19
2 files changed, 29 insertions, 15 deletions
diff --git a/ipalib/public.py b/ipalib/public.py
index bb592d2f..9e999879 100644
--- a/ipalib/public.py
+++ b/ipalib/public.py
@@ -90,14 +90,19 @@ class Option(plugable.Plugin):
__public__ = frozenset((
'normalize',
- 'default',
+ 'get_default',
'validate',
- 'required',
'type',
+ 'required',
+ 'default',
+ 'default_from',
))
__rules = None
type = unicode
required = False
+ default = None
+ default_from = None
+
def normalize(self, value):
"""
@@ -159,16 +164,12 @@ class Option(plugable.Plugin):
if is_rule(attr):
yield attr
- def default(self, **kw):
- """
- Returns a default or auto-completed value for this Option. If no
- default is available, this method should return None.
-
- All the keywords are passed so it's possible to build an
- auto-completed value from other Options values, e.g., build 'initials'
- from 'givenname' + 'sn'.
- """
- return None
+ def get_default(self, **kw):
+ if type(self.default_from) is DefaultFrom:
+ default = self.default_from(**kw)
+ if default is not None:
+ return default
+ return self.default
class Command(plugable.Plugin):
diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py
index d809b074..609ac33e 100644
--- a/ipalib/tests/test_public.py
+++ b/ipalib/tests/test_public.py
@@ -190,12 +190,25 @@ class test_Option(ClassChecker):
rules = tuple(get_rule(i) for i in xrange(3))
assert o.rules == rules
- def test_default(self):
+ def test_get_default(self):
"""
- Tests the `public.Option.default` method.
+ Tests the `public.Option.get_default` method.
"""
+ assert 'get_default' in self.cls.__public__
assert 'default' in self.cls.__public__
- assert self.cls().default() is None
+ assert 'default_from' in self.cls.__public__
+ assert self.cls().get_default() is None
+ class subclass(self.cls):
+ default = 3
+ default_from = public.DefaultFrom(
+ lambda a,b: a * b,
+ 'key0', 'key1'
+ )
+ o = subclass()
+ assert o.get_default() == 3
+ assert o.get_default(key0=2, key1=5) == 10
+ assert o.get_default(key0=7) == 3
+
class test_Command(ClassChecker):