summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-09-24 21:46:37 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-09-24 21:46:37 +0000
commit250a01b5b7ee81b19b5da80f1ef47f1ab9174a64 (patch)
tree4f2cf9c4ab46e8baf24d8f05b1351562c61958d1 /ipalib
parent566d5ea02a5dfdf6f0da0ce1a3f0bb656604c233 (diff)
downloadfreeipa-250a01b5b7ee81b19b5da80f1ef47f1ab9174a64.tar.gz
freeipa-250a01b5b7ee81b19b5da80f1ef47f1ab9174a64.tar.xz
freeipa-250a01b5b7ee81b19b5da80f1ef47f1ab9174a64.zip
348: If no keys are passed to DefaultFrom.__init__(), the keys from callback.func_code.co_varnames are used; updated DefaultFrom unit tests to test this usage
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/frontend.py20
-rw-r--r--ipalib/tests/test_frontend.py7
2 files changed, 19 insertions, 8 deletions
diff --git a/ipalib/frontend.py b/ipalib/frontend.py
index 7e22a9fa0..1e27d93b5 100644
--- a/ipalib/frontend.py
+++ b/ipalib/frontend.py
@@ -61,12 +61,16 @@ class DefaultFrom(plugable.ReadOnly):
:param callback: The callable to call when all ``keys`` are present.
:param keys: The keys used to map from keyword to position arguments.
"""
- assert callable(callback), 'not a callable: %r' % callback
- assert len(keys) > 0, 'must have at least one key'
- for key in keys:
- assert type(key) is str, 'not an str: %r' % key
+ if not callable(callback):
+ raise TypeError('callback must be callable; got %r' % callback)
self.callback = callback
- self.keys = keys
+ if len(keys) == 0:
+ self.keys = callback.func_code.co_varnames
+ else:
+ self.keys = keys
+ for key in self.keys:
+ if type(key) is not str:
+ raise_TypeError(key, str, 'keys')
lock(self)
def __call__(self, **kw):
@@ -77,11 +81,11 @@ class DefaultFrom(plugable.ReadOnly):
"""
vals = tuple(kw.get(k, None) for k in self.keys)
if None in vals:
- return None
+ return
try:
return self.callback(*vals)
- except Exception:
- return None
+ except StandardError:
+ pass
def parse_param_spec(spec):
diff --git a/ipalib/tests/test_frontend.py b/ipalib/tests/test_frontend.py
index 3d01ed116..fb818a4e7 100644
--- a/ipalib/tests/test_frontend.py
+++ b/ipalib/tests/test_frontend.py
@@ -89,6 +89,9 @@ class test_DefaultFrom(ClassChecker):
o = self.cls(callback, *keys)
assert read_only(o, 'callback') is callback
assert read_only(o, 'keys') == keys
+ lam = lambda first, last: first[0] + last
+ o = self.cls(lam)
+ assert read_only(o, 'keys') == ('first', 'last')
def test_call(self):
"""
@@ -109,6 +112,10 @@ class test_DefaultFrom(ClassChecker):
kw_copy = dict(kw)
del kw_copy[key]
assert o(**kw_copy) is None
+ o = self.cls(lambda first, last: first[0] + last)
+ assert o(first='john', last='doe') == 'jdoe'
+ assert o(first='', last='doe') is None
+ assert o(one='john', two='doe') is None
def test_parse_param_spec():