remove some try/except

This commit is contained in:
Emmanuel Garette 2015-12-28 22:00:46 +01:00
parent 7b2d0c0c4a
commit 729db2ceec
2 changed files with 33 additions and 46 deletions

View File

@ -17,6 +17,7 @@
# ____________________________________________________________ # ____________________________________________________________
from ..util import Cache from ..util import Cache
from ...setting import undefined from ...setting import undefined
from ...i18n import _
class Values(Cache): class Values(Cache):
@ -50,9 +51,9 @@ class Values(Cache):
lst[idx] = value lst[idx] = value
else: else:
if nb == 1: if nb == 1:
try: if index in lst[idx]:
vidx = lst[idx].index(index) vidx = lst[idx].index(index)
except ValueError: else:
vidx = None vidx = None
if vidx is None: if vidx is None:
lst[idx].append(value) lst[idx].append(value)
@ -60,9 +61,9 @@ class Values(Cache):
lst[idx][vidx] = value lst[idx][vidx] = value
values.append(tuple(lst)) values.append(tuple(lst))
return vidx return vidx
try: if path in self._values[0]:
idx = self._values[0].index(path) idx = self._values[0].index(path)
except ValueError: else:
idx = None idx = None
vidx = _setvalue_info(0, idx, path, vidx) vidx = _setvalue_info(0, idx, path, vidx)
vidx = _setvalue_info(1, idx, index, vidx) vidx = _setvalue_info(1, idx, index, vidx)
@ -90,6 +91,7 @@ class Values(Cache):
lst.pop(idx) lst.pop(idx)
values.append(tuple(lst)) values.append(tuple(lst))
values = [] values = []
if path in self._values[0]:
idx = self._values[0].index(path) idx = self._values[0].index(path)
_resetvalue(0) _resetvalue(0)
_resetvalue(1) _resetvalue(1)
@ -124,9 +126,9 @@ class Values(Cache):
self._values = tuple(values) self._values = tuple(values)
def get_max_length(self, path): def get_max_length(self, path):
try: if path in self._values[0]:
idx = self._values[0].index(path) idx = self._values[0].index(path)
except ValueError: else:
return 0 return 0
return max(self._values[1][idx]) + 1 return max(self._values[1][idx]) + 1
@ -155,11 +157,8 @@ class Values(Cache):
""" """
_values == ((path1, path2), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2), ((idx1_1, idx1_2), None)) _values == ((path1, path2), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2), ((idx1_1, idx1_2), None))
""" """
try: if path in self._values[0]:
idx = self._values[0].index(path) idx = self._values[0].index(path)
except ValueError:
value = None
else:
if isinstance(self._values[1][idx], list): if isinstance(self._values[1][idx], list):
if index is None: if index is None:
raise ValueError('index is mandatory') raise ValueError('index is mandatory')
@ -173,10 +172,10 @@ class Values(Cache):
value = self._values[nb][idx][index] value = self._values[nb][idx][index]
else: else:
if index is not None: if index is not None:
try: if index in self._values[1][idx]:
subidx = self._values[1][idx].index(index) subidx = self._values[1][idx].index(index)
value = self._values[nb][idx][subidx] value = self._values[nb][idx][subidx]
except ValueError: else:
value = None value = None
else: else:
value = [] value = []
@ -185,6 +184,8 @@ class Values(Cache):
value.append(self._values[nb][idx][self._values[1][idx].index(i)]) value.append(self._values[nb][idx][self._values[1][idx].index(i)])
else: else:
value.append(undefined) value.append(undefined)
else:
value = None
return value return value
def set_information(self, key, value): def set_information(self, key, value):
@ -196,12 +197,13 @@ class Values(Cache):
""" """
self._informations[key] = value self._informations[key] = value
def get_information(self, key): def get_information(self, key, default):
"""retrieves one information's item """retrieves one information's item
:param key: the item string (ex: "help") :param key: the item string (ex: "help")
""" """
if key in self._informations: value = self._informations.get(key, default)
return self._informations[key] if value is undefined:
else: # pragma: optional cover raise ValueError(_("information's item"
raise ValueError("not found") " not found: {0}").format(key))
return value

View File

@ -93,9 +93,9 @@ class Values(object):
if value == []: if value == []:
value = opt.impl_getdefault_multi() value = opt.impl_getdefault_multi()
else: else:
try: if len(value) > index:
value = value[index] value = value[index]
except IndexError: else:
value = opt.impl_getdefault_multi() value = opt.impl_getdefault_multi()
return value return value
@ -116,12 +116,10 @@ class Values(object):
else: else:
value = self._p_.getvalue(path) value = self._p_.getvalue(path)
if index is not None: if index is not None:
try: if len(value) > index:
return value[index] return value[index]
except IndexError:
#value is smaller than expected #value is smaller than expected
#so return default value #so return default value
pass
else: else:
return value return value
return self._getdefaultvalue(opt, path, with_meta, index, submulti_index) return self._getdefaultvalue(opt, path, with_meta, index, submulti_index)
@ -400,10 +398,7 @@ class Values(object):
value[idx] = list(val) value[idx] = list(val)
owner = context.cfgimpl_get_settings().getowner() owner = context.cfgimpl_get_settings().getowner()
if opt.impl_is_master_slaves('slave') and index is None: if opt.impl_is_master_slaves('slave') and index is None:
try:
self._p_.resetvalue(path) self._p_.resetvalue(path)
except ValueError:
pass
for idx, val in enumerate(value): for idx, val in enumerate(value):
self._p_.setvalue(path, val, owner, idx) self._p_.setvalue(path, val, owner, idx)
else: else:
@ -545,14 +540,7 @@ class Values(object):
:param key: the item string (ex: "help") :param key: the item string (ex: "help")
""" """
try: return self._p_.get_information(key, default)
return self._p_.get_information(key)
except ValueError: # pragma: optional cover
if default is not undefined:
return default
else:
raise ValueError(_("information's item"
" not found: {0}").format(key))
def mandatory_warnings(self, force_permissive=False, validate=True): def mandatory_warnings(self, force_permissive=False, validate=True):
"""convenience function to trace Options that are mandatory and """convenience function to trace Options that are mandatory and
@ -804,10 +792,7 @@ class Multi(list):
if self.opt.impl_is_master_slaves(): if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot extend multi option {0} if master or " raise SlaveError(_("cannot extend multi option {0} if master or "
"slave").format(self.opt.impl_getname())) "slave").format(self.opt.impl_getname()))
try: index = getattr(self, '_index', None)
index = self._index
except:
index = None
context = self._getcontext() context = self._getcontext()
setting = context.cfgimpl_get_settings() setting = context.cfgimpl_get_settings()
setting_properties = setting._getproperties(read_write=False) setting_properties = setting._getproperties(read_write=False)