remove some try/except
authorEmmanuel Garette <egarette@cadoles.com>
Mon, 28 Dec 2015 21:00:46 +0000 (22:00 +0100)
committerEmmanuel Garette <egarette@cadoles.com>
Mon, 28 Dec 2015 21:00:46 +0000 (22:00 +0100)
tiramisu/storage/dictionary/value.py
tiramisu/value.py

index d7892c6..97085dc 100644 (file)
@@ -17,6 +17,7 @@
 # ____________________________________________________________
 from ..util import Cache
 from ...setting import undefined
+from ...i18n import _
 
 
 class Values(Cache):
@@ -50,9 +51,9 @@ class Values(Cache):
                     lst[idx] = value
                 else:
                     if nb == 1:
-                        try:
+                        if index in lst[idx]:
                             vidx = lst[idx].index(index)
-                        except ValueError:
+                        else:
                             vidx = None
                     if vidx is None:
                         lst[idx].append(value)
@@ -60,9 +61,9 @@ class Values(Cache):
                         lst[idx][vidx] = value
             values.append(tuple(lst))
             return vidx
-        try:
+        if path in self._values[0]:
             idx = self._values[0].index(path)
-        except ValueError:
+        else:
             idx = None
         vidx = _setvalue_info(0, idx, path, vidx)
         vidx = _setvalue_info(1, idx, index, vidx)
@@ -90,12 +91,13 @@ class Values(Cache):
             lst.pop(idx)
             values.append(tuple(lst))
         values = []
-        idx = self._values[0].index(path)
-        _resetvalue(0)
-        _resetvalue(1)
-        _resetvalue(2)
-        _resetvalue(3)
-        self._values = tuple(values)
+        if path in self._values[0]:
+            idx = self._values[0].index(path)
+            _resetvalue(0)
+            _resetvalue(1)
+            _resetvalue(2)
+            _resetvalue(3)
+            self._values = tuple(values)
 
     def get_modified_values(self):
         """return all values in a dictionary
@@ -124,9 +126,9 @@ class Values(Cache):
         self._values = tuple(values)
 
     def get_max_length(self, path):
-        try:
+        if path in self._values[0]:
             idx = self._values[0].index(path)
-        except ValueError:
+        else:
             return 0
         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))
         """
-        try:
+        if path in self._values[0]:
             idx = self._values[0].index(path)
-        except ValueError:
-            value = None
-        else:
             if isinstance(self._values[1][idx], list):
                 if index is None:
                     raise ValueError('index is mandatory')
@@ -173,10 +172,10 @@ class Values(Cache):
                     value = self._values[nb][idx][index]
             else:
                 if index is not None:
-                    try:
+                    if index in self._values[1][idx]:
                         subidx = self._values[1][idx].index(index)
                         value = self._values[nb][idx][subidx]
-                    except ValueError:
+                    else:
                         value = None
                 else:
                     value = []
@@ -185,6 +184,8 @@ class Values(Cache):
                             value.append(self._values[nb][idx][self._values[1][idx].index(i)])
                         else:
                             value.append(undefined)
+        else:
+            value = None
         return value
 
     def set_information(self, key, value):
@@ -196,12 +197,13 @@ class Values(Cache):
         """
         self._informations[key] = value
 
-    def get_information(self, key):
+    def get_information(self, key, default):
         """retrieves one information's item
 
         :param key: the item string (ex: "help")
         """
-        if key in self._informations:
-            return self._informations[key]
-        else:  # pragma: optional cover
-            raise ValueError("not found")
+        value = self._informations.get(key, default)
+        if value is undefined:
+            raise ValueError(_("information's item"
+                               " not found: {0}").format(key))
+        return value
index 6c2c42f..fbb680b 100644 (file)
@@ -93,9 +93,9 @@ class Values(object):
             if value == []:
                 value = opt.impl_getdefault_multi()
             else:
-                try:
+                if len(value) > index:
                     value = value[index]
-                except IndexError:
+                else:
                     value = opt.impl_getdefault_multi()
         return value
 
@@ -116,12 +116,10 @@ class Values(object):
             else:
                 value = self._p_.getvalue(path)
                 if index is not None:
-                    try:
+                    if len(value) > index:
                         return value[index]
-                    except IndexError:
-                        #value is smaller than expected
-                        #so return default value
-                        pass
+                    #value is smaller than expected
+                    #so return default value
                 else:
                     return value
         return self._getdefaultvalue(opt, path, with_meta, index, submulti_index)
@@ -400,10 +398,7 @@ class Values(object):
                         value[idx] = list(val)
         owner = context.cfgimpl_get_settings().getowner()
         if opt.impl_is_master_slaves('slave') and index is None:
-            try:
-                self._p_.resetvalue(path)
-            except ValueError:
-                pass
+            self._p_.resetvalue(path)
             for idx, val in enumerate(value):
                 self._p_.setvalue(path, val, owner, idx)
         else:
@@ -545,14 +540,7 @@ class Values(object):
 
         :param key: the item string (ex: "help")
         """
-        try:
-            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))
+        return self._p_.get_information(key, default)
 
     def mandatory_warnings(self, force_permissive=False, validate=True):
         """convenience function to trace Options that are mandatory and
@@ -804,10 +792,7 @@ class Multi(list):
         if self.opt.impl_is_master_slaves():
             raise SlaveError(_("cannot extend multi option {0} if master or "
                                "slave").format(self.opt.impl_getname()))
-        try:
-            index = self._index
-        except:
-            index = None
+        index = getattr(self, '_index', None)
         context = self._getcontext()
         setting = context.cfgimpl_get_settings()
         setting_properties = setting._getproperties(read_write=False)