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] group_names = [res[0] for res in result]
assert group_names == ['general', 'interface1'] 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 True
return False 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): def __getattr__(self, name):
return self._getattr(name) return self._getattr(name)
@ -299,15 +309,7 @@ class Config(object):
raise ConfigError('invalid calculated value returned' raise ConfigError('invalid calculated value returned'
' for option {0}'.format(name)) ' for option {0}'.format(name))
self._cfgimpl_values[name] = _result self._cfgimpl_values[name] = _result
self._test_mandatory(name, opt_or_descr)
# 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))
# frozen and force default # frozen and force default
if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze(): if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze():
return opt_or_descr.getdefault() return opt_or_descr.getdefault()
@ -577,11 +579,11 @@ class Config(object):
groups = [group_type] groups = [group_type]
for child in self._cfgimpl_descr._children: for child in self._cfgimpl_descr._children:
if isinstance(child, OptionDescription): if isinstance(child, OptionDescription):
try: try:
if child.get_group_type() in groups: if child.get_group_type() in groups:
yield child._name, getattr(self, child._name) yield child._name, getattr(self, child._name)
except: except:
pass # hidden, disabled option pass # hidden, disabled option
# ______________________________________________________________________ # ______________________________________________________________________
def __str__(self, indent=""): def __str__(self, indent=""):
"Config's string representation" "Config's string representation"
@ -742,7 +744,15 @@ def mandatory_warnings(config):
config._cfgimpl_get_toplevel()._cfgimpl_mandatory = True config._cfgimpl_get_toplevel()._cfgimpl_mandatory = True
for path in config._cfgimpl_descr.getpaths(include_groups=True): for path in config._cfgimpl_descr.getpaths(include_groups=True):
try: 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: except MandatoryError:
yield path yield path
except PropertiesOptionError: except PropertiesOptionError: