diff src/memory/params.py @ 1220:f91e7028e2c3

memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes
author souliane <souliane@mailoo.org>
date Fri, 03 Oct 2014 12:27:43 +0200
parents 594fbdda4a87
children e6e0ea4dc835
line wrap: on
line diff
--- a/src/memory/params.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/src/memory/params.py	Fri Oct 03 12:27:43 2014 +0200
@@ -48,9 +48,9 @@
         <category name="Connection" label="%(category_connection)s">
             <param name="JabberID" value="name@example.org/SàT" type="string" />
             <param name="Password" value="" type="password" />
-            <param name="Priority" value="50" type="string" />
+            <param name="Priority" value="50" type="int" min="-128" max="127"/>
             <param name="%(force_server_param)s" value="" type="string" />
-            <param name="%(force_port_param)s" value="" type="string" />
+            <param name="%(force_port_param)s" value="" type="int" min="80" max="65535" />
             <param name="NewAccount" label="%(label_NewAccount)s" type="button" callback_id="registerNewAccount"/>
             <param name="autoconnect" label="%(label_autoconnect)s" value="true" type="bool" />
             <param name="autodisconnect" label="%(label_autodisconnect)s" value="false"  type="bool" />
@@ -331,6 +331,8 @@
             value_to_use = value if value is not None else node.getAttribute(attr)  # we use value (user defined) if it exist, else we use node's default value
             if node.getAttribute('type') == 'bool':
                 return value_to_use.lower() not in ('false', '0', 'no')
+            if node.getAttribute('type') == 'int':
+                return int(value_to_use)
             elif node.getAttribute('type') == 'list':
                 options = [option for option in node.childNodes if option.nodeName == 'option']
                 values = [option.getAttribute('value') for option in options]
@@ -398,6 +400,8 @@
         """ convert result to string, according to its type """
         if isinstance(result, bool):
             return "true" if result else "false"
+        elif isinstance(result, int):
+            return str(result)
         return result
 
     def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
@@ -729,7 +733,7 @@
         node = self._getParamNode(name, category, '@ALL@')
         if not node:
             log.error(_('Requesting an unknown parameter (%(category)s/%(name)s)')
-                  % {'category': category, 'name': name})
+                      % {'category': category, 'name': name})
             return defer.succeed(None)
 
         if not self.checkSecurityLimit(node[1], security_limit):
@@ -738,6 +742,21 @@
             return defer.succeed(None)
 
         type_ = node[1].getAttribute("type")
+        if type_ == 'int':
+            if not value:  # replace with the default value (which might also be '')
+                value = node[1].getAttribute("value")
+            else:
+                try:
+                    int(value)
+                except ValueError:
+                    log.debug(_("Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
+                                % {'param': name, 'cat': category}))
+                    return defer.succeed(None)
+                if node[1].hasAttribute("min"):
+                    value = str(max(int(value), int(node[1].getAttribute("min"))))
+                if node[1].hasAttribute("max"):
+                    value = str(min(int(value), int(node[1].getAttribute("max"))))
+
         log.info(_("Setting parameter (%(category)s, %(name)s) = %(value)s") %
                  {'category': category, 'name': name, 'value': value if type_ != 'password' else '********'})
 
@@ -789,7 +808,7 @@
 
         TODO: using during the dev but not anymore... remove if not needed
 
-        @param attr_type (str): the attribute type (string, text, password, bool, button, list)
+        @param attr_type (str): the attribute type (string, text, password, bool, int, button, list)
         @param node_type (str): keyword for filtering:
                                     @ALL@ search everywhere
                                     @GENERAL@ only search in general type