changeset 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 (2014-10-03)
parents 16484ebb695b
children 5e5661ab5c81
files frontends/src/primitivus/xmlui.py frontends/src/tools/xmlui.py frontends/src/wix/xmlui.py src/memory/params.py src/plugins/plugin_misc_imap.py src/plugins/plugin_misc_smtp.py src/plugins/plugin_xep_0065.py src/tools/xml_tools.py
diffstat 8 files changed, 80 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/xmlui.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/frontends/src/primitivus/xmlui.py	Fri Oct 03 12:27:43 2014 +0200
@@ -155,6 +155,24 @@
         return "true" if self.get_state() else "false"
 
 
+class PrimitivusIntWidget(xmlui.IntWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
+
+    def __init__(self, _xmlui_parent, value, read_only=False):
+        sat_widgets.AdvancedEdit.__init__(self, edit_text=value)
+        self.read_only = read_only
+
+    def selectable(self):
+        if self.read_only:
+            return False
+        return super(PrimitivusIntWidget, self).selectable()
+
+    def _xmluiSetValue(self, value):
+        self.set_edit_text(value)
+
+    def _xmluiGetValue(self):
+        return self.get_edit_text()
+
+
 class PrimitivusButtonWidget(xmlui.ButtonWidget, sat_widgets.CustomButton, PrimitivusEvents):
 
     def __init__(self, _xmlui_parent, value, click_callback):
--- a/frontends/src/tools/xmlui.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/frontends/src/tools/xmlui.py	Fri Oct 03 12:27:43 2014 +0200
@@ -101,6 +101,10 @@
     """
 
 
+class IntWidget(Widget):
+    """Input widget with require an integer"""
+
+
 class ButtonWidget(Widget):
     """A clickable widget"""
 
@@ -388,6 +392,9 @@
                 elif type_=="bool":
                     ctrl = self.widget_factory.createBoolWidget(_xmlui_parent, value==C.BOOL_TRUE, self._isAttrSet("read_only", node))
                     self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
+                elif type_ == "int":
+                    ctrl = self.widget_factory.createIntWidget(_xmlui_parent, value, self._isAttrSet("read_only", node))
+                    self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
                 elif type_ == "list":
                     style = [] if node.getAttribute("multi") == 'yes' else ['single']
                     _options = [(option.getAttribute("value"), option.getAttribute("label")) for option in node.getElementsByTagName("option")]
--- a/frontends/src/wix/xmlui.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/frontends/src/wix/xmlui.py	Fri Oct 03 12:27:43 2014 +0200
@@ -127,6 +127,16 @@
         return "true" if self.GetValue() else "false"
 
 
+# TODO: use wx.SpinCtrl instead of wx.TextCtrl
+class IntWidget(EventWidget, ValueWidget, xmlui.IntWidget, wx.TextCtrl):
+    _xmlui_change_event = wx.EVT_TEXT
+
+    def __init__(self, _xmlui_parent, value, read_only=False):
+        style = wx.TE_READONLY if read_only else 0
+        wx.TextCtrl.__init__(self, _xmlui_parent, -1, value, style=style)
+        self._xmlui_proportion = 1
+
+
 class ButtonWidget(EventWidget, WixWidget, xmlui.ButtonWidget, wx.Button):
     _xmlui_change_event = wx.EVT_BUTTON
 
--- 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
--- a/src/plugins/plugin_misc_imap.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/src/plugins/plugin_misc_imap.py	Fri Oct 03 12:27:43 2014 +0200
@@ -51,7 +51,7 @@
     <params>
     <general>
     <category name="Mail Server">
-        <param name="IMAP Port" value="10143" type="string" />
+        <param name="IMAP Port" value="10143" type="int" min="143" max="65535" />
     </category>
     </general>
     </params>
--- a/src/plugins/plugin_misc_smtp.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/src/plugins/plugin_misc_smtp.py	Fri Oct 03 12:27:43 2014 +0200
@@ -51,7 +51,7 @@
     <params>
     <general>
     <category name="Mail Server">
-        <param name="SMTP Port" value="10125" type="string" />
+        <param name="SMTP Port" value="10125" type="int" min="25" max="65535" />
     </category>
     </general>
     </params>
--- a/src/plugins/plugin_xep_0065.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/src/plugins/plugin_xep_0065.py	Fri Oct 03 12:27:43 2014 +0200
@@ -444,14 +444,14 @@
     <general>
     <category name="File Transfer">
         <param name="IP" value='0.0.0.0' default_cb='yes' type="string" />
-        <param name="Port" value="28915" type="string" />
+        <param name="Port" value="28915" type="int" min="80" max="65535" />
     </category>
     </general>
     <individual>
     <category name="File Transfer">
         <param name="Proxy" value="" type="string" />
         <param name="Proxy host" value="" type="string" />
-        <param name="Proxy port" value="" type="string" />
+        <param name="Proxy port" value="" type="int" min="80" max="65535" />
     </category>
     </individual>
     </params>
--- a/src/tools/xml_tools.py	Mon Sep 22 22:25:44 2014 +0200
+++ b/src/tools/xml_tools.py	Fri Oct 03 12:27:43 2014 +0200
@@ -70,6 +70,9 @@
         if widget_args[0] is None:
             widget_args[0] = 'false'
         widget_kwargs['read_only'] = read_only
+    elif field.fieldType == 'integer':
+        widget_type = "integer"
+        widget_kwargs['read_only'] = read_only
     elif field.fieldType == 'list-single':
         widget_type = "list"
         widget_kwargs["options"] = [(option.value, option.label or option.value) for option in field.options]
@@ -696,6 +699,18 @@
     type = 'jid_input'
 
 
+# TODO handle min and max values
+class IntWidget(StringWidget):
+    type = 'int'
+
+    def __init__(self, xmlui, value=0, name=None, parent=None, read_only=False):
+        try:
+            int(value)
+        except ValueError:
+            raise exceptions.DataError(_("Value must be an integer"))
+        super(IntWidget, self).__init__(xmlui, value, name, parent, read_only=read_only)
+
+
 class BoolWidget(InputWidget):
     type = 'bool'
 
@@ -703,10 +718,10 @@
         if isinstance(value, bool):
             value = 'true' if value else 'false'
         elif value == '0':
-            value='false'
+            value = 'false'
         elif value == '1':
-            value='true'
-        if not value in ('true', 'false'):
+            value = 'true'
+        if value not in ('true', 'false'):
             raise exceptions.DataError(_("Value must be 0, 1, false or true"))
         super(BoolWidget, self).__init__(xmlui, name, parent, read_only=read_only)
         self.elem.setAttribute('value', value)