test mandatory

This commit is contained in:
gwen 2012-11-06 15:19:36 +01:00
parent 3bd9d2c45b
commit bfb5045753
2 changed files with 39 additions and 20 deletions

View File

@ -68,3 +68,12 @@ def test_iter_on_groups():
group_names = [res[0] for res in result]
assert group_names == ['general', 'interface1']
def test_iter_on_empty_group():
config = Config(OptionDescription("name", "descr", [] ))
result = list(config.iter_groups())
assert result == []
for i in config.iter_groups():
pass
for i in config:
pass
assert [] == list(config)

View File

@ -209,6 +209,16 @@ class Config(object):
return True
return False
def _test_mandatory(self, path, opt):
# mandatory options
homeconfig = self._cfgimpl_get_toplevel()
mandatory = homeconfig._cfgimpl_mandatory
if opt.is_mandatory() and mandatory:
if self._is_empty(opt) and \
opt.is_empty_by_default():
raise MandatoryError("option: {0} is mandatory "
"and shall have a value".format(path))
def __getattr__(self, name):
return self._getattr(name)
@ -299,15 +309,7 @@ class Config(object):
raise ConfigError('invalid calculated value returned'
' for option {0}'.format(name))
self._cfgimpl_values[name] = _result
# mandatory options
homeconfig = self._cfgimpl_get_toplevel()
mandatory = homeconfig._cfgimpl_mandatory
if opt_or_descr.is_mandatory() and mandatory:
if self._is_empty(opt_or_descr) and \
opt_or_descr.is_empty_by_default():
raise MandatoryError("option: {0} is mandatory "
"and shall have a value".format(name))
self._test_mandatory(name, opt_or_descr)
# frozen and force default
if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze():
return opt_or_descr.getdefault()
@ -577,11 +579,11 @@ class Config(object):
groups = [group_type]
for child in self._cfgimpl_descr._children:
if isinstance(child, OptionDescription):
try:
if child.get_group_type() in groups:
yield child._name, getattr(self, child._name)
except:
pass # hidden, disabled option
try:
if child.get_group_type() in groups:
yield child._name, getattr(self, child._name)
except:
pass # hidden, disabled option
# ______________________________________________________________________
def __str__(self, indent=""):
"Config's string representation"
@ -742,7 +744,15 @@ def mandatory_warnings(config):
config._cfgimpl_get_toplevel()._cfgimpl_mandatory = True
for path in config._cfgimpl_descr.getpaths(include_groups=True):
try:
value = getattr(config, path)
if '.' in path:
homeconfig, path = config._cfgimpl_get_home_by_path(path)
else:
homeconfig = config
opt = getattr(homeconfig._cfgimpl_descr, path)
#for PropertiesOptionError
homeconfig._validate(path, opt)
if not isinstance(opt, OptionDescription):
homeconfig._test_mandatory(path, opt)
except MandatoryError:
yield path
except PropertiesOptionError: