summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-09-24 17:55:29 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-09-24 17:55:29 +0000
commit11a07008b896ac995755b2f2a90e6089ca1344a5 (patch)
tree44588862549fbcdb1f44a126b58fda3e23a4fe20 /ipalib
parentac7bdf8790a6b18eccc72af5dc2c5b59133546cd (diff)
downloadfreeipa-11a07008b896ac995755b2f2a90e6089ca1344a5.tar.gz
freeipa-11a07008b896ac995755b2f2a90e6089ca1344a5.tar.xz
freeipa-11a07008b896ac995755b2f2a90e6089ca1344a5.zip
339: Added parse_param_spec() function and corresponding unit tests
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/frontend.py32
-rw-r--r--ipalib/tests/test_frontend.py12
2 files changed, 44 insertions, 0 deletions
diff --git a/ipalib/frontend.py b/ipalib/frontend.py
index 82d966436..1ff620236 100644
--- a/ipalib/frontend.py
+++ b/ipalib/frontend.py
@@ -84,6 +84,38 @@ class DefaultFrom(plugable.ReadOnly):
return None
+def parse_param_spec(spec):
+ """
+ Parse param spec to get name, required, and multivalue.
+
+ The ``spec`` string determines the param name, whether the param is
+ required, and whether the param is multivalue according the following
+ syntax:
+
+ name => required=True, multivalue=False
+ name? => required=False, multivalue=False
+ name+ => required=True, multivalue=True
+ name* => required=False, multivalue=True
+
+ :param spec: A spec string.
+ """
+ if type(spec) is not str:
+ raise_TypeError(spec, str, 'spec')
+ if len(spec) < 2:
+ raise ValueError(
+ 'param spec must be at least 2 characters; got %r' % spec
+ )
+ _map = {
+ '?': dict(required=False, multivalue=False),
+ '*': dict(required=False, multivalue=True),
+ '+': dict(required=True, multivalue=True),
+ }
+ end = spec[-1]
+ if end in _map:
+ return (spec[:-1], _map[end])
+ return (spec, dict(required=True, multivalue=False))
+
+
class Param(plugable.ReadOnly):
__nones = (None, '', tuple(), [])
diff --git a/ipalib/tests/test_frontend.py b/ipalib/tests/test_frontend.py
index 58d5fd930..1fe4e376c 100644
--- a/ipalib/tests/test_frontend.py
+++ b/ipalib/tests/test_frontend.py
@@ -111,6 +111,18 @@ class test_DefaultFrom(ClassChecker):
assert o(**kw_copy) is None
+def test_parse_param_spec():
+ """
+ Test the `frontend.parse_param_spec` function.
+ """
+ f = frontend.parse_param_spec
+
+ assert f('name') == ('name', dict(required=True, multivalue=False))
+ assert f('name?') == ('name', dict(required=False, multivalue=False))
+ assert f('name*') == ('name', dict(required=False, multivalue=True))
+ assert f('name+') == ('name', dict(required=True, multivalue=True))
+
+
class test_Param(ClassChecker):
"""
Test the `frontend.Param` class.