From 2842abdd46fbafb1037ff214ad7f0ff734724e04 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 6 Jan 2016 22:37:11 +0100 Subject: [PATCH] remove try/except --- tiramisu/config.py | 13 +++++++-- tiramisu/option/masterslave.py | 50 ++++++++++++++++++++++------------ tiramisu/value.py | 14 ++++++---- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/tiramisu/config.py b/tiramisu/config.py index 19b5fe3..2662ebb 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -70,12 +70,16 @@ class SubConfig(object): "remove cache (in context)" 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)""" path = path.split('.') for step in path[:-1]: 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] #def __hash__(self): @@ -254,7 +258,10 @@ class SubConfig(object): # for instance getattr(self, "creole.general.family.adresse_ip_eth0") if '.' in name: 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, validate=validate, _setting_properties=_setting_properties, diff --git a/tiramisu/option/masterslave.py b/tiramisu/option/masterslave.py index eff3ce9..603018b 100644 --- a/tiramisu/option/masterslave.py +++ b/tiramisu/option/masterslave.py @@ -126,7 +126,8 @@ class MasterSlaves(object): return self._getmaster(values, opt, path, validate, force_permissive, validate_properties, slave_path, - slave_value, self_properties, index) + slave_value, self_properties, index, + returns_raise) else: return self._getslave(values, opt, path, validate, force_permissive, trusted_cached_properties, @@ -135,12 +136,15 @@ class MasterSlaves(object): def _getmaster(self, values, opt, path, validate, force_permissive, 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, force_permissive=force_permissive, validate_properties=validate_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: masterlen = len(value) for slave in self.getslaves(opt): @@ -178,7 +182,9 @@ class MasterSlaves(object): masterp = master.impl_getpath(context) masterlen = self.get_length(values, opt, validate, undefined, 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) multi = values._get_multi(opt, path) #if masterlen is [], test properties (has no value, don't get any value) @@ -214,20 +220,24 @@ class MasterSlaves(object): masterlen=masterlen, from_masterslave=True, returns_raise=True) - if isinstance(value, PropertiesOptionError): - err = value - multi.append_properties_error(value) - elif isinstance(value, Exception): - raise value + if isinstance(value, Exception): + if isinstance(value, PropertiesOptionError): + err = value + multi.append_properties_error(value) + else: + return value else: multi.append(value, setitem=False, force=True, validate=validate) one_has_value = True if not one_has_value: #raise last err - raise err + if returns_raise: + return err + else: + raise err return multi - def validate(self, values, opt, value, path): + def validate(self, values, opt, value, path, returns_raise): if self.is_master(opt): masterlen = len(value) #for regen slave path @@ -237,13 +247,16 @@ class MasterSlaves(object): slavelen = values._p_.get_max_length(slave_path) self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt) else: - self.validate_slave_length(self.get_length(values, opt, - slave_path=path), len(value), + val_len = self.get_length(values, opt, slave_path=path, returns_raise=returns_raise) + if isinstance(val_len, Exception): + return val_len + self.validate_slave_length(val_len, + len(value), opt.impl_getname(), opt, setitem=True) def get_length(self, values, opt, validate=True, slave_path=undefined, slave_value=undefined, force_permissive=False, master=None, - masterp=None): + masterp=None, returns_raise=False): """get master len with slave option""" if master is None: master = self.getmaster(opt) @@ -251,9 +264,12 @@ class MasterSlaves(object): masterp = master.impl_getpath(values._getcontext()) if slave_value is undefined: slave_path = undefined - return len(self.getitem(values, master, masterp, validate, - force_permissive, None, True, slave_path, - slave_value)) + value = self.getitem(values, master, masterp, validate, + force_permissive, None, True, slave_path, + 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): if valuelen > masterlen or (valuelen < masterlen and setitem): # pragma: optional cover diff --git a/tiramisu/value.py b/tiramisu/value.py index f389837..0f5d283 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -257,8 +257,6 @@ class Values(object): index=index, self_properties=self_properties, returns_raise=returns_raise) - if isinstance(val, PropertiesOptionError): - return val else: val = self._get_validated_value(opt, path, validate, force_permissive, @@ -271,8 +269,11 @@ class Values(object): submulti_index=submulti_index, check_frozen=check_frozen, returns_raise=returns_raise) - if isinstance(val, PropertiesOptionError): + if isinstance(val, Exception): + if returns_raise: return val + else: + raise val # cache doesn't work with SubMulti yet if not isinstance(val, SubMulti) and 'cache' in setting_properties and \ 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, setting_properties=undefined, valid_masterslave=True, - not_raises=False): + not_raises=False, returns_raise=False): 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, False, check_frozen, @@ -868,7 +869,8 @@ class Multi(list): values = self._getcontext().cfgimpl_get_values() if not force: #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)