Merge branch 'master' of ssh://git.labs.libre-entreprise.org/gitroot/tiramisu
[tiramisu.git] / tiramisu / option.py
index 8b5eb68..b9960b6 100644 (file)
@@ -354,9 +354,6 @@ class Option(BaseOption):
         """
         if context is not None:
             descr = context.cfgimpl_get_description()
-        #option is also in all_cons_opts
-        if option not in all_cons_opts:
-            raise ConfigError(_('option not in all_cons_opts'))
 
         all_cons_vals = []
         for opt in all_cons_opts:
@@ -587,31 +584,15 @@ class Option(BaseOption):
                 consistencies = self._state_consistencies
             else:
                 consistencies = self._consistencies
-            if isinstance(consistencies, list):
-                new_value = []
-                for consistency in consistencies:
-                    values = []
-                    for obj in consistency[1]:
-                        if load:
-                            values.append(descr.impl_get_opt_by_path(obj))
-                        else:
-                            values.append(descr.impl_get_path_by_opt(obj))
-                    new_value.append((consistency[0], tuple(values)))
-
-            else:
-                new_value = {}
-                for key, _consistencies in consistencies.items():
-                    new_value[key] = []
-                    for key_cons, _cons in _consistencies:
-                        _list_cons = []
-                        for _con in _cons:
-                            if load:
-                                _list_cons.append(
-                                    descr.impl_get_opt_by_path(_con))
-                            else:
-                                _list_cons.append(
-                                    descr.impl_get_path_by_opt(_con))
-                        new_value[key].append((key_cons, tuple(_list_cons)))
+            new_value = []
+            for consistency in consistencies:
+                values = []
+                for obj in consistency[1]:
+                    if load:
+                        values.append(descr.impl_get_opt_by_path(obj))
+                    else:
+                        values.append(descr.impl_get_path_by_opt(obj))
+                new_value.append((consistency[0], tuple(values)))
             if load:
                 del(self._state_consistencies)
                 self._consistencies = new_value
@@ -663,7 +644,7 @@ class ChoiceOption(Option):
         return self._open_values
 
     def _validate(self, value):
-        if not self._open_values and not value in self._values:
+        if not self.impl_is_openvalues() and not value in self.impl_get_values():
             raise ValueError(_('value {0} is not permitted, '
                                'only {1} is allowed'
                                '').format(value, self._values))
@@ -783,9 +764,13 @@ class IPOption(Option):
     def _validate(self, value):
         # sometimes an ip term starts with a zero
         # but this does not fit in some case, for example bind does not like it
-        for val in value.split('.'):
-            if val.startswith("0") and len(val) > 1:
-                raise ValueError(_('invalid IP'))
+        try:
+            for val in value.split('.'):
+                if val.startswith("0") and len(val) > 1:
+                    raise ValueError(_('invalid IP'))
+        except AttributeError:
+            #if integer for example
+            raise ValueError(_('invalid IP'))
         # 'standard' validation
         try:
             IP('{0}/32'.format(value))
@@ -857,18 +842,23 @@ class PortOption(Option):
         if self._allow_range and ":" in str(value):
             value = str(value).split(':')
             if len(value) != 2:
-                raise ValueError('invalid part, range must have two values '
-                                 'only')
+                raise ValueError(_('invalid part, range must have two values '
+                                 'only'))
             if not value[0] < value[1]:
-                raise ValueError('invalid port, first port in range must be'
-                                 ' smaller than the second one')
+                raise ValueError(_('invalid port, first port in range must be'
+                                 ' smaller than the second one'))
         else:
             value = [value]
 
         for val in value:
+            try:
+                int(val)
+            except ValueError:
+                raise ValueError(_('invalid port'))
             if not self._min_value <= int(val) <= self._max_value:
-                raise ValueError('invalid port, must be an between {0} and {1}'
-                                 ''.format(self._min_value, self._max_value))
+                raise ValueError(_('invalid port, must be an between {0} '
+                                    'and {1}').format(self._min_value,
+                                                    self._max_value))
 
 
 class NetworkOption(Option):
@@ -924,19 +914,14 @@ class NetmaskOption(Option):
                     IP('{0}/{1}'.format(val_ipnetwork, val_netmask),
                         make_net=not make_net)
                 except ValueError:
-                    if not make_net:
-                        msg = _("invalid network {0} ({1}) "
-                                "with netmask {2},"
-                                " this network is an IP")
+                    pass
                 else:
                     if make_net:
                         msg = _("invalid IP {0} ({1}) with netmask {2},"
                                 " this IP is a network")
 
         except ValueError:
-            if make_net:
-                msg = _('invalid IP {0} ({1}) with netmask {2}')
-            else:
+            if not make_net:
                 msg = _('invalid network {0} ({1}) with netmask {2}')
         if msg is not None:
             raise ValueError(msg.format(val_ipnetwork, opts[1]._name,
@@ -1032,8 +1017,8 @@ class DomainnameOption(Option):
         if self._type == 'domainname' and not self._allow_without_dot and \
                 '.' not in value:
             raise ValueError(_("invalid domainname, must have dot"))
-            if len(value) > 255:
-                raise ValueError(_("invalid domainname's length (max 255)"))
+        if len(value) > 255:
+            raise ValueError(_("invalid domainname's length (max 255)"))
         if len(value) < 2:
             raise ValueError(_("invalid domainname's length (min 2)"))
         if not self._domain_re.search(value):
@@ -1310,13 +1295,10 @@ class OptionDescription(BaseOption):
         if consistencies is not None:
             for func, all_cons_opts in consistencies:
                 #all_cons_opts[0] is the option where func is set
-                ret = all_cons_opts[0]._launch_consistency(func, option,
-                                                           value,
-                                                           context, index,
-                                                           all_cons_opts)
-                if ret is False:
-                    return False
-        return True
+                all_cons_opts[0]._launch_consistency(func, option,
+                                                     value,
+                                                     context, index,
+                                                     all_cons_opts)
 
     def _impl_getstate(self, descr=None):
         """enables us to export into a dict