remove some try/except

This commit is contained in:
Emmanuel Garette 2015-12-28 21:40:40 +01:00
parent 14489c3ef5
commit 7b2d0c0c4a
2 changed files with 111 additions and 174 deletions

View File

@ -219,45 +219,6 @@ class BaseOption(Base):
else: else:
self._state_requires = new_value 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 # serialize
def _impl_getstate(self, descr): def _impl_getstate(self, descr):
"""the under the hood stuff that need to be done """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. 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() __slots__ = tuple()
_empty = '' _empty = ''

View File

@ -51,8 +51,7 @@ class StorageBase(object):
'_choice_values_params', '_choice_values_params',
#other #other
'_state_master_slaves', '_state_master_slaves',
'_state_callback', '_state_val_call',
'_state_callback_params',
'_state_requires', '_state_requires',
'_stated', '_stated',
'_state_consistencies', '_state_consistencies',
@ -128,39 +127,28 @@ class StorageBase(object):
:param key: the item string (ex: "help") :param key: the item string (ex: "help")
""" """
error = False
dico = self._informations 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': if key == 'doc':
return dico return dico
elif isinstance(dico, dict):
if key in dico:
return dico[key]
if default is not undefined: if default is not undefined:
return default 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( raise ValueError(_("information's item not found: {0}").format(
key)) key))
def _add_consistency(self, func, all_cons_opts, params): def _add_consistency(self, func, all_cons_opts, params):
cons = (func, all_cons_opts, params) cons = (func, all_cons_opts, params)
try: consistencies = getattr(self, '_consistencies', None)
self._consistencies.append(cons) if consistencies is None:
except AttributeError:
self._consistencies = [cons] self._consistencies = [cons]
else:
consistencies.append(cons)
def _del_consistency(self): def _del_consistency(self):
self._consistencies.pop(-1) self._consistencies.pop(-1)
@ -169,27 +157,19 @@ class StorageBase(object):
return getattr(self, '_consistencies', static_tuple) return getattr(self, '_consistencies', static_tuple)
def _set_callback(self, callback, callback_params): def _set_callback(self, callback, callback_params):
val = getattr(self, '_val_call', (None,))[0]
if callback_params is None or callback_params == {}: if callback_params is None or callback_params == {}:
val_call = (callback,) val_call = (callback,)
else: else:
val_call = tuple([callback, callback_params]) val_call = tuple([callback, callback_params])
try: self._val_call = (val, val_call)
self._val_call = (self._val_call[0], val_call)
except AttributeError:
self._val_call = (None, val_call)
def impl_get_callback(self): def impl_get_callback(self):
default = (None, {}) call = getattr(self, '_val_call', (None, None))[1]
try:
call = self._val_call[1]
except (AttributeError, IndexError):
ret_call = default
else:
if call is None: if call is None:
ret_call = default ret_call = (None, {})
else: elif len(call) == 1:
if len(call) == 1: ret_call = (call[0], {})
ret_call = (call[0], default[1])
else: else:
ret_call = call ret_call = call
return ret_call return ret_call
@ -205,27 +185,56 @@ class StorageBase(object):
val_call = (validator,) val_call = (validator,)
else: else:
val_call = (validator, validator_params) 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): def impl_get_validator(self):
default = (None, {}) val = getattr(self, '_val_call', (None,))[0]
try:
val = self._val_call[0]
except (AttributeError, IndexError):
ret_val = default
else:
if val is None: if val is None:
ret_val = default ret_val = (None, {})
else: elif len(val) == 1:
if len(val) == 1: ret_val = (val[0], {})
ret_val = (val[0], default[1])
else: else:
ret_val = val ret_val = val
return ret_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): def _get_id(self):
return id(self) return id(self)
@ -269,31 +278,26 @@ class StorageBase(object):
self._state_informations = infos self._state_informations = infos
self._state_readonly = self.impl_is_readonly() self._state_readonly = self.impl_is_readonly()
else: else:
try:
self._informations = self._state_informations self._informations = self._state_informations
del(self._state_informations) del(self._state_informations)
except AttributeError:
pass
if self._state_readonly: if self._state_readonly:
self._set_readonly(True) self._set_readonly(True)
del(self._state_readonly) del(self._state_readonly)
def _impl_convert_extra(self, descr, load=False): def _impl_convert_extra(self, descr, load=False):
if not load: if not load:
try: extra = getattr(self, '_extra', None)
extra = self._extra
if isinstance(extra, tuple): if isinstance(extra, tuple):
self._state_extra = {} self._state_extra = {}
for idx, key in enumerate(extra[0]): for idx, key in enumerate(extra[0]):
self._state_extra[key] = extra[1][idx] self._state_extra[key] = extra[1][idx]
except AttributeError: elif isinstance(extra, dict):
pass self._state_extra = extra
else: else:
try: extra = getattr(self, '_state_extra', None)
self._extra = self._state_extra if extra is not None:
self._extra = extra
del(self._state_extra) del(self._state_extra)
except AttributeError:
pass
def _impl_getattributes(self): def _impl_getattributes(self):
slots = set() slots = set()
@ -303,30 +307,19 @@ class StorageBase(object):
return slots return slots
def impl_is_readonly(self): def impl_is_readonly(self):
try: return not isinstance(getattr(self, '_informations', dict()), dict)
return not isinstance(self._informations, dict)
except AttributeError:
return False
def impl_getname(self): def impl_getname(self):
return self._name return self._name
def impl_is_multi(self): def impl_is_multi(self):
_multi = getattr(self, '_multi', 1) return getattr(self, '_multi', 1) != 1
return _multi != 1
def impl_is_submulti(self): def impl_is_submulti(self):
try: return getattr(self, '_multi', 1) == 2
_multi = self._multi
except IndexError:
_multi = 1
return _multi == 2
def impl_allow_empty_list(self): def impl_allow_empty_list(self):
try: return getattr(self, '_allow_empty_list', undefined)
return self._allow_empty_list
except AttributeError:
return undefined
def _get_extra(self, key): def _get_extra(self, key):
extra = self._extra extra = self._extra
@ -336,31 +329,25 @@ class StorageBase(object):
return extra[key] return extra[key]
def _is_warnings_only(self): def _is_warnings_only(self):
try: return getattr(self, '_warnings_only', False)
return self._warnings_only
except AttributeError:
return False
def impl_getdefault(self): def impl_getdefault(self):
"accessing the default value" "accessing the default value"
is_multi = self.impl_is_multi() is_multi = self.impl_is_multi()
try: default = getattr(self, '_default', undefined)
ret = self._default if default is undefined:
if is_multi: if is_multi:
return list(ret) default = []
return ret else:
except AttributeError: default = None
else:
if is_multi: if is_multi:
return [] default = list(default)
return None return default
def impl_getdefault_multi(self): def impl_getdefault_multi(self):
"accessing the default value for a multi" "accessing the default value for a multi"
if self.impl_is_multi(): return getattr(self, '_default_multi', None)
try:
return self._default_multi
except (AttributeError, IndexError):
pass
def commit(self): def commit(self):
pass pass
@ -384,18 +371,18 @@ class StorageOptionDescription(StorageBase):
return getattr(self, '_cache_paths', None) is not None return getattr(self, '_cache_paths', None) is not None
def impl_get_opt_by_path(self, path): def impl_get_opt_by_path(self, path):
try: if getattr(self, '_cache_paths', None) is None:
return self._cache_paths[0][self._cache_paths[1].index(path)] raise ConfigError(_('use impl_get_opt_by_path only with root OptionDescription'))
except ValueError: # pragma: optional cover if path not in self._cache_paths[1]:
raise AttributeError(_('no option for path {0}').format(path)) 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): def impl_get_path_by_opt(self, opt):
if getattr(self, '_cache_paths', None) is None: if getattr(self, '_cache_paths', None) is None:
raise ConfigError(_('use impl_get_path_by_opt only with root OptionDescription')) raise ConfigError(_('use impl_get_path_by_opt only with root OptionDescription'))
try: if opt not in self._cache_paths[0]:
return self._cache_paths[1][self._cache_paths[0].index(opt)]
except ValueError: # pragma: optional cover
raise AttributeError(_('no option {0} found').format(opt)) 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 def impl_get_group_type(self): # pragma: optional cover
return self._group_type return self._group_type
@ -529,7 +516,6 @@ class StorageOptionDescription(StorageBase):
def _getattr(self, name, suffix=undefined, context=undefined, dyn=True): def _getattr(self, name, suffix=undefined, context=undefined, dyn=True):
error = False error = False
if suffix is not undefined: if suffix is not undefined:
try:
if undefined in [suffix, context]: # pragma: optional cover if undefined in [suffix, context]: # pragma: optional cover
raise ConfigError(_("suffix and context needed if " raise ConfigError(_("suffix and context needed if "
"it's a dyn option")) "it's a dyn option"))
@ -539,20 +525,14 @@ class StorageOptionDescription(StorageBase):
return self._impl_get_dynchild(child, suffix) return self._impl_get_dynchild(child, suffix)
else: else:
error = True error = True
except ValueError: # pragma: optional cover
error = True
else: else:
try: # pragma: optional cover if name in self._children[0]:
if name == '_readonly':
raise AttributeError(_("{0} instance has no attribute "
"'_readonly'").format(
self.__class__.__name__))
child = self._children[1][self._children[0].index(name)] child = self._children[1][self._children[0].index(name)]
if dyn and child.impl_is_dynoptiondescription(): if dyn and child.impl_is_dynoptiondescription():
error = True error = True
else: else:
return child return child
except ValueError: else:
child = self._impl_search_dynchild(name, context=context) child = self._impl_search_dynchild(name, context=context)
if child != []: if child != []:
return child return child