remove try/except

This commit is contained in:
Emmanuel Garette 2016-01-06 22:37:11 +01:00
parent b521c459ee
commit 2842abdd46
3 changed files with 51 additions and 26 deletions

View File

@ -70,12 +70,16 @@ class SubConfig(object):
"remove cache (in context)" "remove cache (in context)"
self._cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only) # pragma: optional cover self._cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only) # pragma: optional cover
def cfgimpl_get_home_by_path(self, path, force_permissive=False): def cfgimpl_get_home_by_path(self, path, force_permissive=False,
returns_raise=False):
""":returns: tuple (config, name)""" """:returns: tuple (config, name)"""
path = path.split('.') path = path.split('.')
for step in path[:-1]: for step in path[:-1]:
self = self.getattr(step, self = self.getattr(step,
force_permissive=force_permissive) force_permissive=force_permissive,
returns_raise=returns_raise)
if isinstance(self, Exception):
return self, None
return self, path[-1] return self, path[-1]
#def __hash__(self): #def __hash__(self):
@ -254,7 +258,10 @@ class SubConfig(object):
# for instance getattr(self, "creole.general.family.adresse_ip_eth0") # for instance getattr(self, "creole.general.family.adresse_ip_eth0")
if '.' in name: if '.' in name:
homeconfig, name = self.cfgimpl_get_home_by_path( homeconfig, name = self.cfgimpl_get_home_by_path(
name, force_permissive=force_permissive) name, force_permissive=force_permissive,
returns_raise=returns_raise)
if isinstance(homeconfig, Exception):
return homeconfig
return homeconfig.getattr(name, force_permissive=force_permissive, return homeconfig.getattr(name, force_permissive=force_permissive,
validate=validate, validate=validate,
_setting_properties=_setting_properties, _setting_properties=_setting_properties,

View File

@ -126,7 +126,8 @@ class MasterSlaves(object):
return self._getmaster(values, opt, path, validate, return self._getmaster(values, opt, path, validate,
force_permissive, force_permissive,
validate_properties, slave_path, validate_properties, slave_path,
slave_value, self_properties, index) slave_value, self_properties, index,
returns_raise)
else: else:
return self._getslave(values, opt, path, validate, return self._getslave(values, opt, path, validate,
force_permissive, trusted_cached_properties, force_permissive, trusted_cached_properties,
@ -135,12 +136,15 @@ class MasterSlaves(object):
def _getmaster(self, values, opt, path, validate, force_permissive, def _getmaster(self, values, opt, path, validate, force_permissive,
validate_properties, c_slave_path, validate_properties, c_slave_path,
c_slave_value, self_properties, index): c_slave_value, self_properties, index, returns_raise):
value = values._get_cached_value(opt, path=path, validate=validate, value = values._get_cached_value(opt, path=path, validate=validate,
force_permissive=force_permissive, force_permissive=force_permissive,
validate_properties=validate_properties, validate_properties=validate_properties,
self_properties=self_properties, self_properties=self_properties,
from_masterslave=True, index=index) from_masterslave=True, index=index,
returns_raise=True)
if isinstance(value, Exception):
return value
if index is None and validate is True: if index is None and validate is True:
masterlen = len(value) masterlen = len(value)
for slave in self.getslaves(opt): for slave in self.getslaves(opt):
@ -178,7 +182,9 @@ class MasterSlaves(object):
masterp = master.impl_getpath(context) masterp = master.impl_getpath(context)
masterlen = self.get_length(values, opt, validate, undefined, masterlen = self.get_length(values, opt, validate, undefined,
undefined, force_permissive, undefined, force_permissive,
master=master) master=master, returns_raise=returns_raise)
if isinstance(masterlen, Exception):
return masterlen
master_is_meta = values._is_meta(master, masterp) master_is_meta = values._is_meta(master, masterp)
multi = values._get_multi(opt, path) multi = values._get_multi(opt, path)
#if masterlen is [], test properties (has no value, don't get any value) #if masterlen is [], test properties (has no value, don't get any value)
@ -214,20 +220,24 @@ class MasterSlaves(object):
masterlen=masterlen, masterlen=masterlen,
from_masterslave=True, from_masterslave=True,
returns_raise=True) returns_raise=True)
if isinstance(value, Exception):
if isinstance(value, PropertiesOptionError): if isinstance(value, PropertiesOptionError):
err = value err = value
multi.append_properties_error(value) multi.append_properties_error(value)
elif isinstance(value, Exception): else:
raise value return value
else: else:
multi.append(value, setitem=False, force=True, validate=validate) multi.append(value, setitem=False, force=True, validate=validate)
one_has_value = True one_has_value = True
if not one_has_value: if not one_has_value:
#raise last err #raise last err
if returns_raise:
return err
else:
raise err raise err
return multi return multi
def validate(self, values, opt, value, path): def validate(self, values, opt, value, path, returns_raise):
if self.is_master(opt): if self.is_master(opt):
masterlen = len(value) masterlen = len(value)
#for regen slave path #for regen slave path
@ -237,13 +247,16 @@ class MasterSlaves(object):
slavelen = values._p_.get_max_length(slave_path) slavelen = values._p_.get_max_length(slave_path)
self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt) self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
else: else:
self.validate_slave_length(self.get_length(values, opt, val_len = self.get_length(values, opt, slave_path=path, returns_raise=returns_raise)
slave_path=path), len(value), if isinstance(val_len, Exception):
return val_len
self.validate_slave_length(val_len,
len(value),
opt.impl_getname(), opt, setitem=True) opt.impl_getname(), opt, setitem=True)
def get_length(self, values, opt, validate=True, slave_path=undefined, def get_length(self, values, opt, validate=True, slave_path=undefined,
slave_value=undefined, force_permissive=False, master=None, slave_value=undefined, force_permissive=False, master=None,
masterp=None): masterp=None, returns_raise=False):
"""get master len with slave option""" """get master len with slave option"""
if master is None: if master is None:
master = self.getmaster(opt) master = self.getmaster(opt)
@ -251,9 +264,12 @@ class MasterSlaves(object):
masterp = master.impl_getpath(values._getcontext()) masterp = master.impl_getpath(values._getcontext())
if slave_value is undefined: if slave_value is undefined:
slave_path = undefined slave_path = undefined
return len(self.getitem(values, master, masterp, validate, value = self.getitem(values, master, masterp, validate,
force_permissive, None, True, slave_path, force_permissive, None, True, slave_path,
slave_value)) slave_value, returns_raise=returns_raise)
if isinstance(value, Exception):
return value
return len(value)
def validate_slave_length(self, masterlen, valuelen, name, opt, setitem=False): def validate_slave_length(self, masterlen, valuelen, name, opt, setitem=False):
if valuelen > masterlen or (valuelen < masterlen and setitem): # pragma: optional cover if valuelen > masterlen or (valuelen < masterlen and setitem): # pragma: optional cover

View File

@ -257,8 +257,6 @@ class Values(object):
index=index, index=index,
self_properties=self_properties, self_properties=self_properties,
returns_raise=returns_raise) returns_raise=returns_raise)
if isinstance(val, PropertiesOptionError):
return val
else: else:
val = self._get_validated_value(opt, path, validate, val = self._get_validated_value(opt, path, validate,
force_permissive, force_permissive,
@ -271,8 +269,11 @@ class Values(object):
submulti_index=submulti_index, submulti_index=submulti_index,
check_frozen=check_frozen, check_frozen=check_frozen,
returns_raise=returns_raise) returns_raise=returns_raise)
if isinstance(val, PropertiesOptionError): if isinstance(val, Exception):
if returns_raise:
return val return val
else:
raise val
# cache doesn't work with SubMulti yet # cache doesn't work with SubMulti yet
if not isinstance(val, SubMulti) and 'cache' in setting_properties and \ if not isinstance(val, SubMulti) and 'cache' in setting_properties and \
validate and validate_properties and force_permissive is False \ validate and validate_properties and force_permissive is False \
@ -427,9 +428,9 @@ class Values(object):
def validate(self, opt, value, path, check_frozen=True, force_permissive=False, def validate(self, opt, value, path, check_frozen=True, force_permissive=False,
setting_properties=undefined, valid_masterslave=True, setting_properties=undefined, valid_masterslave=True,
not_raises=False): not_raises=False, returns_raise=False):
if valid_masterslave and opt.impl_is_master_slaves(): if valid_masterslave and opt.impl_is_master_slaves():
opt.impl_get_master_slaves().validate(self, opt, value, path) opt.impl_get_master_slaves().validate(self, opt, value, path, returns_raise)
props = self._getcontext().cfgimpl_get_settings().validate_properties(opt, props = self._getcontext().cfgimpl_get_settings().validate_properties(opt,
False, False,
check_frozen, check_frozen,
@ -868,7 +869,8 @@ class Multi(list):
values = self._getcontext().cfgimpl_get_values() values = self._getcontext().cfgimpl_get_values()
if not force: if not force:
#FIXME could get properties an pass it #FIXME could get properties an pass it
values.validate(self.opt, self, self.path, valid_masterslave=False) values.validate(self.opt, self, self.path,
valid_masterslave=False)
values._setvalue(self.opt, self.path, self) values._setvalue(self.opt, self.path, self)