143: Added errors.RequirementError exception; cmd.validate() now raises RequirementError if a required option is missing

This commit is contained in:
Jason Gerard DeRose 2008-08-13 05:14:12 +00:00
parent 47fed6c4c2
commit b4ad681f41
3 changed files with 43 additions and 16 deletions

View File

@ -64,11 +64,23 @@ class NormalizationError(ValidationError):
class RuleError(ValidationError): class RuleError(ValidationError):
"""
Raised when a required option was not provided.
"""
def __init__(self, name, value, rule, error): def __init__(self, name, value, rule, error):
self.rule = rule self.rule = rule
ValidationError.__init__(self, name, value, error) ValidationError.__init__(self, name, value, error)
class RequirementError(ValidationError):
"""
Raised when a required option was not provided.
"""
def __init__(self, name):
ValidationError.__init__(self, name, None,
'missing required value'
)
class SetError(IPAError): class SetError(IPAError):
msg = 'setting %r, but NameSpace does not allow attribute setting' msg = 'setting %r, but NameSpace does not allow attribute setting'

View File

@ -199,9 +199,13 @@ class cmd(plugable.Plugin):
def validate(self, **kw): def validate(self, **kw):
self.print_call('validate', kw, 1) self.print_call('validate', kw, 1)
for (key, value) in kw.items(): for opt in self.options:
if key in self.options: value = kw.get(opt.name, None)
self.options[key].validate(value) if value is None:
if opt.required:
raise errors.RequirementError(opt.name)
continue
opt.validate(value)
def execute(self, **kw): def execute(self, **kw):
self.print_call('execute', kw, 1) self.print_call('execute', kw, 1)

View File

@ -177,7 +177,7 @@ class test_cmd(ClassChecker):
class option0(my_option): class option0(my_option):
pass pass
class option1(my_option): class option1(my_option):
pass required = True
class example(self.cls): class example(self.cls):
option_classes = (option0, option1) option_classes = (option0, option1)
return example return example
@ -254,18 +254,30 @@ class test_cmd(ClassChecker):
Tests the `validate` method. Tests the `validate` method.
""" """
assert 'validate' in self.cls.__public__ # Public assert 'validate' in self.cls.__public__ # Public
sub = self.subcls() sub = self.subcls()
for name in ('option0', 'option1'):
okay = { # Check with valid args
name: name, okay = dict(
'another_option': 'some value', option0='option0',
} option1='option1',
fail = { another_option='some value',
name: 'whatever', )
'another_option': 'some value', sub.validate(**okay)
}
sub.validate(**okay) # Check with an invalid arg
raises(errors.RuleError, sub.validate, **fail) fail = dict(okay)
fail['option0'] = 'whatever'
raises(errors.RuleError, sub.validate, **fail)
# Check with a missing required arg
fail = dict(okay)
fail.pop('option1')
raises(errors.RequirementError, sub.validate, **fail)
# Check with missing *not* required arg
okay.pop('option0')
sub.validate(**okay)
def test_execute(self): def test_execute(self):
""" """
@ -274,7 +286,6 @@ class test_cmd(ClassChecker):
assert 'execute' in self.cls.__public__ # Public assert 'execute' in self.cls.__public__ # Public
def test_obj(): def test_obj():
cls = public.obj cls = public.obj
assert issubclass(cls, plugable.Plugin) assert issubclass(cls, plugable.Plugin)