summaryrefslogtreecommitdiffstats
path: root/ipalib/public.py
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-08-08 06:18:12 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-08-08 06:18:12 +0000
commitb3fc5f9a41685f40da0702f860e6182182783150 (patch)
tree907645ea09c9c79ae956d13ddb18afbf5389b3bb /ipalib/public.py
parent9ee10d383dc03649c3358ab7a296872fac528ada (diff)
downloadfreeipa.git-b3fc5f9a41685f40da0702f860e6182182783150.tar.gz
freeipa.git-b3fc5f9a41685f40da0702f860e6182182783150.tar.xz
freeipa.git-b3fc5f9a41685f40da0702f860e6182182783150.zip
79: More work on option and cmd
Diffstat (limited to 'ipalib/public.py')
-rw-r--r--ipalib/public.py50
1 files changed, 31 insertions, 19 deletions
diff --git a/ipalib/public.py b/ipalib/public.py
index f6bad7ec..071d905d 100644
--- a/ipalib/public.py
+++ b/ipalib/public.py
@@ -39,6 +39,10 @@ def is_rule(obj):
class option(object):
+ """
+ The option class represents a kw argument from a command.
+ """
+
__public__ = frozenset((
'normalize',
'validate',
@@ -64,6 +68,21 @@ class option(object):
self.__class__.__name__, value, self.type
)
+ def validate(self, value):
+ """
+ Calls each validation rule and if any rule fails, raises RuleError,
+ which is a subclass of ValidationError.
+ """
+ for rule in self.rules:
+ msg = rule(value)
+ if msg is not None:
+ raise errors.RuleError(
+ self.__class__.__name__,
+ value,
+ rule,
+ msg,
+ )
+
def __get_rules(self):
"""
Returns the tuple of rule methods used for input validation. This
@@ -91,21 +110,12 @@ class option(object):
if is_rule(attr):
yield attr
- def validate(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 default(self, **kw):
+ """
+ Returns a default or auto-completed value for this option. If no
+ default is available, this method should return None.
+ """
+ return None
class cmd(plugable.Plugin):
@@ -146,7 +156,6 @@ class cmd(plugable.Plugin):
return self.__opt
opt = property(__get_opt)
-
def normalize_iter(self, kw):
for (key, value) in kw.items():
if key in self.options:
@@ -164,9 +173,12 @@ class cmd(plugable.Plugin):
if key in self.options:
self.options.validate(value)
-
-
-
+ def default(self, **kw):
+ for opt in self.options:
+ if opt.name not in kw:
+ value = opt.default(**kw)
+ if value is not None:
+ kw[opt.name] = value
def __call__(self, **kw):
(args, kw) = self.normalize(*args, **kw)