From 7b2d0c0c4adf0b184bc0f9355ee90d672d0435e7 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 28 Dec 2015 21:40:40 +0100 Subject: [PATCH] remove some try/except --- tiramisu/option/baseoption.py | 43 ----- tiramisu/storage/dictionary/option.py | 242 ++++++++++++-------------- 2 files changed, 111 insertions(+), 174 deletions(-) diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index 0f25e91..be3138d 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -219,45 +219,6 @@ class BaseOption(Base): else: self._state_requires = new_value - def _impl_convert_callback(self, descr, load=False): - if self.__class__.__name__ == 'OptionDescription' or \ - isinstance(self, SymLinkOption): - return - if not load and self.impl_get_callback() == (None, {}): - self._state_callback = None - self._state_callback_params = {} - elif load and self._state_callback is None: - del(self._state_callback) - del(self._state_callback_params) - else: - if load: - callback = self._state_callback - callback_params = self._state_callback_params - else: - callback, callback_params = self.impl_get_callback() - self._state_callback_params = {} - cllbck_prms = {} - for key, values in callback_params.items(): - vls = [] - for value in values: - if isinstance(value, tuple) and value[0] is not None: - if load: - value = (descr.impl_get_opt_by_path(value[0]), - value[1]) - else: - value = (descr.impl_get_path_by_opt(value[0]), - value[1]) - vls.append(value) - cllbck_prms[key] = tuple(vls) - - if load: - del(self._state_callback) - del(self._state_callback_params) - self._set_callback(callback, cllbck_prms) - else: - self._state_callback = callback - self._state_callback_params = cllbck_prms - # serialize def _impl_getstate(self, descr): """the under the hood stuff that need to be done @@ -399,10 +360,6 @@ class Option(OnlyOption): Reminder: an Option object is **not** a container for the value. """ -# __slots__ = ('_multi', '_validator', '_default_multi', '_default', -# '_state_callback', '_callback', -# '_consistencies', '_warnings_only', '_master_slaves', -# '_state_consistencies', '__weakref__') __slots__ = tuple() _empty = '' diff --git a/tiramisu/storage/dictionary/option.py b/tiramisu/storage/dictionary/option.py index 2a3785a..50dc179 100644 --- a/tiramisu/storage/dictionary/option.py +++ b/tiramisu/storage/dictionary/option.py @@ -51,8 +51,7 @@ class StorageBase(object): '_choice_values_params', #other '_state_master_slaves', - '_state_callback', - '_state_callback_params', + '_state_val_call', '_state_requires', '_stated', '_state_consistencies', @@ -128,39 +127,28 @@ class StorageBase(object): :param key: the item string (ex: "help") """ - error = False dico = self._informations - if isinstance(dico, str) or isinstance(dico, unicode): + if isinstance(dico, tuple): + if key in dico[0]: + return dico[1][dico[0].index(key)] + elif isinstance(dico, str) or isinstance(dico, unicode): if key == 'doc': return dico - if default is not undefined: - return default - error = True - elif isinstance(dico, tuple): - try: - return dico[1][dico[0].index(key)] - except ValueError: - if default is not undefined: - return default - error = True - else: - # dict - if default is not undefined: - return self._informations.get(key, default) - try: - return self._informations[key] - except KeyError: # pragma: optional cover - error = True - if error: - raise ValueError(_("information's item not found: {0}").format( - key)) + elif isinstance(dico, dict): + if key in dico: + return dico[key] + if default is not undefined: + return default + raise ValueError(_("information's item not found: {0}").format( + key)) def _add_consistency(self, func, all_cons_opts, params): cons = (func, all_cons_opts, params) - try: - self._consistencies.append(cons) - except AttributeError: + consistencies = getattr(self, '_consistencies', None) + if consistencies is None: self._consistencies = [cons] + else: + consistencies.append(cons) def _del_consistency(self): self._consistencies.pop(-1) @@ -169,29 +157,21 @@ class StorageBase(object): return getattr(self, '_consistencies', static_tuple) def _set_callback(self, callback, callback_params): + val = getattr(self, '_val_call', (None,))[0] if callback_params is None or callback_params == {}: val_call = (callback,) else: val_call = tuple([callback, callback_params]) - try: - self._val_call = (self._val_call[0], val_call) - except AttributeError: - self._val_call = (None, val_call) + self._val_call = (val, val_call) def impl_get_callback(self): - default = (None, {}) - try: - call = self._val_call[1] - except (AttributeError, IndexError): - ret_call = default + call = getattr(self, '_val_call', (None, None))[1] + if call is None: + ret_call = (None, {}) + elif len(call) == 1: + ret_call = (call[0], {}) else: - if call is None: - ret_call = default - else: - if len(call) == 1: - ret_call = (call[0], default[1]) - else: - ret_call = call + ret_call = call return ret_call def impl_get_calc_properties(self): @@ -205,27 +185,56 @@ class StorageBase(object): val_call = (validator,) else: val_call = (validator, validator_params) - try: - self._val_call = (val_call, self._val_call[1]) - except (AttributeError, IndexError): - self._val_call = (val_call, None) + self._val_call = (val_call, None) def impl_get_validator(self): - default = (None, {}) - try: - val = self._val_call[0] - except (AttributeError, IndexError): - ret_val = default + val = getattr(self, '_val_call', (None,))[0] + if val is None: + ret_val = (None, {}) + elif len(val) == 1: + ret_val = (val[0], {}) else: - if val is None: - ret_val = default - else: - if len(val) == 1: - ret_val = (val[0], default[1]) - else: - ret_val = val + ret_val = val return ret_val + def _impl_convert_callback(self, descr, load=False): + if self.__class__.__name__ in ['OptionDescription', 'SymLinkOption']: + return + if not load and self.impl_get_callback() == (None, {}): + self._state_val_call = None + elif load and self._state_val_call is None: + del(self._state_val_call) + else: + if load: + val_call = self._state_val_call + else: + val_call = self._val_call + new_val_call = [] + for vals in val_call: + if vals is None or len(vals) == 1: + new_val_call.append(vals) + else: + prms = {} + val, params = vals + for key, values in params.items(): + vls = [] + for value in values: + if isinstance(value, tuple) and value[0] is not None: + if load: + value = (descr.impl_get_opt_by_path(value[0]), + value[1]) + else: + value = (descr.impl_get_path_by_opt(value[0]), + value[1]) + vls.append(value) + prms[key] = tuple(vls) + new_val_call.append((val, prms)) + if load: + del(self._state_val_call) + self._val_call = new_val_call + else: + self._state_val_call = new_val_call + def _get_id(self): return id(self) @@ -269,31 +278,26 @@ class StorageBase(object): self._state_informations = infos self._state_readonly = self.impl_is_readonly() else: - try: - self._informations = self._state_informations - del(self._state_informations) - except AttributeError: - pass + self._informations = self._state_informations + del(self._state_informations) if self._state_readonly: self._set_readonly(True) del(self._state_readonly) def _impl_convert_extra(self, descr, load=False): if not load: - try: - extra = self._extra - if isinstance(extra, tuple): - self._state_extra = {} - for idx, key in enumerate(extra[0]): - self._state_extra[key] = extra[1][idx] - except AttributeError: - pass + extra = getattr(self, '_extra', None) + if isinstance(extra, tuple): + self._state_extra = {} + for idx, key in enumerate(extra[0]): + self._state_extra[key] = extra[1][idx] + elif isinstance(extra, dict): + self._state_extra = extra else: - try: - self._extra = self._state_extra + extra = getattr(self, '_state_extra', None) + if extra is not None: + self._extra = extra del(self._state_extra) - except AttributeError: - pass def _impl_getattributes(self): slots = set() @@ -303,30 +307,19 @@ class StorageBase(object): return slots def impl_is_readonly(self): - try: - return not isinstance(self._informations, dict) - except AttributeError: - return False + return not isinstance(getattr(self, '_informations', dict()), dict) def impl_getname(self): return self._name def impl_is_multi(self): - _multi = getattr(self, '_multi', 1) - return _multi != 1 + return getattr(self, '_multi', 1) != 1 def impl_is_submulti(self): - try: - _multi = self._multi - except IndexError: - _multi = 1 - return _multi == 2 + return getattr(self, '_multi', 1) == 2 def impl_allow_empty_list(self): - try: - return self._allow_empty_list - except AttributeError: - return undefined + return getattr(self, '_allow_empty_list', undefined) def _get_extra(self, key): extra = self._extra @@ -336,31 +329,25 @@ class StorageBase(object): return extra[key] def _is_warnings_only(self): - try: - return self._warnings_only - except AttributeError: - return False + return getattr(self, '_warnings_only', False) def impl_getdefault(self): "accessing the default value" is_multi = self.impl_is_multi() - try: - ret = self._default + default = getattr(self, '_default', undefined) + if default is undefined: if is_multi: - return list(ret) - return ret - except AttributeError: + default = [] + else: + default = None + else: if is_multi: - return [] - return None + default = list(default) + return default def impl_getdefault_multi(self): "accessing the default value for a multi" - if self.impl_is_multi(): - try: - return self._default_multi - except (AttributeError, IndexError): - pass + return getattr(self, '_default_multi', None) def commit(self): pass @@ -384,18 +371,18 @@ class StorageOptionDescription(StorageBase): return getattr(self, '_cache_paths', None) is not None def impl_get_opt_by_path(self, path): - try: - return self._cache_paths[0][self._cache_paths[1].index(path)] - except ValueError: # pragma: optional cover + if getattr(self, '_cache_paths', None) is None: + raise ConfigError(_('use impl_get_opt_by_path only with root OptionDescription')) + if path not in self._cache_paths[1]: raise AttributeError(_('no option for path {0}').format(path)) + return self._cache_paths[0][self._cache_paths[1].index(path)] def impl_get_path_by_opt(self, opt): if getattr(self, '_cache_paths', None) is None: raise ConfigError(_('use impl_get_path_by_opt only with root OptionDescription')) - try: - return self._cache_paths[1][self._cache_paths[0].index(opt)] - except ValueError: # pragma: optional cover + if opt not in self._cache_paths[0]: raise AttributeError(_('no option {0} found').format(opt)) + return self._cache_paths[1][self._cache_paths[0].index(opt)] def impl_get_group_type(self): # pragma: optional cover return self._group_type @@ -529,30 +516,23 @@ class StorageOptionDescription(StorageBase): def _getattr(self, name, suffix=undefined, context=undefined, dyn=True): error = False if suffix is not undefined: - try: - if undefined in [suffix, context]: # pragma: optional cover - raise ConfigError(_("suffix and context needed if " - "it's a dyn option")) - if name.endswith(suffix): - oname = name[:-len(suffix)] - child = self._children[1][self._children[0].index(oname)] - return self._impl_get_dynchild(child, suffix) - else: - error = True - except ValueError: # pragma: optional cover + if undefined in [suffix, context]: # pragma: optional cover + raise ConfigError(_("suffix and context needed if " + "it's a dyn option")) + if name.endswith(suffix): + oname = name[:-len(suffix)] + child = self._children[1][self._children[0].index(oname)] + return self._impl_get_dynchild(child, suffix) + else: error = True else: - try: # pragma: optional cover - if name == '_readonly': - raise AttributeError(_("{0} instance has no attribute " - "'_readonly'").format( - self.__class__.__name__)) + if name in self._children[0]: child = self._children[1][self._children[0].index(name)] if dyn and child.impl_is_dynoptiondescription(): error = True else: return child - except ValueError: + else: child = self._impl_search_dynchild(name, context=context) if child != []: return child