add cons_lower + DomainnameOption
authorEmmanuel Garette <egarette@cadoles.com>
Tue, 16 Apr 2013 07:34:00 +0000 (09:34 +0200)
committerEmmanuel Garette <egarette@cadoles.com>
Tue, 16 Apr 2013 07:34:00 +0000 (09:34 +0200)
tiramisu/option.py

index c837934..88dd9e3 100644 (file)
@@ -184,8 +184,8 @@ class Option(BaseInformation):
         else:
             if not isinstance(value, list):
                 raise ValueError(_("invalid value {0} "
-                                    "for option {1} which must be a list"
-                                    "").format(value, self._name))
+                                   "for option {1} which must be a list"
+                                   "").format(value, self._name))
             for index in range(0, len(value)):
                 val = value[index]
                 # None allows the reset of the value
@@ -246,6 +246,16 @@ class Option(BaseInformation):
     def is_multi(self):
         return self._multi
 
+    def add_consistency(self, func, opts):
+        pass
+        if self._consistencies is None:
+            self._consistencies = []
+        if self not in opts:
+            opts = list(opts)
+            opts.append(self)
+            opts = tuple(opts)
+        self._consistencies.append(('cons_{}'.format(func), opts))
+
     def cons_not_equal(self, opt, value, context, index, opts):
         values = [value]
         descr = context.cfgimpl_get_description()
@@ -259,15 +269,12 @@ class Option(BaseInformation):
                     values.append(val)
         return True
 
-    def add_consistency(self, func, opts):
-        pass
-        if self._consistencies is None:
-            self._consistencies = []
-        if self not in opts:
-            opts = list(opts)
-            opts.append(self)
-            opts = tuple(opts)
-        self._consistencies.append(('cons_{}'.format(func), opts))
+    def cons_lower(self, value):
+        try:
+            return value.islower()
+        except AttributeError:
+            #no "islower" attribute
+            return False
 
 
 class ChoiceOption(Option):
@@ -465,6 +472,54 @@ class NetmaskOption(Option):
             return False
 
 
+class DomainnameOption(Option):
+    __slots__ = ('opt_type', '_type', '_allow_ip')
+    opt_type = 'domainname'
+    #allow_ip
+
+    def __init__(self, name, doc, default=None, default_multi=None,
+                 requires=None, multi=False, callback=None,
+                 callback_params=None, validator=None, validator_args=None,
+                 properties=None, allow_ip=False, type_='domainname'):
+        #netbios: for MS domain
+        #hostname: to identify the device
+        #domainname:
+        #fqdn: with tld, not supported yet
+        super(NetmaskOption, self).__init__(name, doc, default=default,
+                                            default_multi=default_multi,
+                                            callback=callback,
+                                            callback_params=callback_params,
+                                            requires=requires,
+                                            multi=multi,
+                                            validator=validator,
+                                            validator_args=validator_args,
+                                            properties=properties)
+        if type_ not in ['netbios', 'hostname', 'domainname']:
+            raise ValueError(_('unknown type_ {0} for hostname').format(type_))
+        self._type = type_
+        self._allow_ip = allow_ip
+
+    def _validate(self, value):
+        if self._allow_ip is True:
+            try:
+                IP('{0}/32'.format(value))
+                return True
+            except ValueError:
+                pass
+        if self._type == 'netbios':
+            length = 15
+            extrachar = ''
+        elif self._type == 'hostname':
+            length = 63
+            extrachar = ''
+        elif self._type == 'domainname':
+            length = 255
+            extrachar = '\.'
+        regexp = r'^[a-zA-Z]([a-zA-Z\d-{0}]{{,{1}}})*[a-zA-Z\d]$'.format(
+            extrachar, length - 2)
+        return re.match(regexp, value) is not None
+
+
 class OptionDescription(BaseInformation):
     """Config's schema (organisation, group) and container of Options"""
     __slots__ = ('_name', '_requires', '_cache_paths', '_group_type',