# HG changeset patch # User souliane # Date 1412332063 -7200 # Node ID f91e7028e2c3105c13d1737f1b4c412f038763ec # Parent 16484ebb695bb1364753ef7818dbd0707e3deb98 memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes diff -r 16484ebb695b -r f91e7028e2c3 frontends/src/primitivus/xmlui.py --- 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): diff -r 16484ebb695b -r f91e7028e2c3 frontends/src/tools/xmlui.py --- 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")] diff -r 16484ebb695b -r f91e7028e2c3 frontends/src/wix/xmlui.py --- 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 diff -r 16484ebb695b -r f91e7028e2c3 src/memory/params.py --- 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 @@ - + - + @@ -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 diff -r 16484ebb695b -r f91e7028e2c3 src/plugins/plugin_misc_imap.py --- 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 @@ - + diff -r 16484ebb695b -r f91e7028e2c3 src/plugins/plugin_misc_smtp.py --- 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 @@ - + diff -r 16484ebb695b -r f91e7028e2c3 src/plugins/plugin_xep_0065.py --- 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 @@ - + - + diff -r 16484ebb695b -r f91e7028e2c3 src/tools/xml_tools.py --- 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)