memory (params), tools (xml_tools), plugins, frontends: add "int" parameter type with "min" and "max" attributes

This commit is contained in:
souliane 2014-10-03 12:27:43 +02:00
commit ac59d061f4
8 changed files with 80 additions and 11 deletions

View file

@ -155,6 +155,24 @@ class PrimitivusBoolWidget(xmlui.BoolWidget, urwid.CheckBox, PrimitivusEvents):
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):

View file

@ -101,6 +101,10 @@ class BoolWidget(Widget):
"""
class IntWidget(Widget):
"""Input widget with require an integer"""
class ButtonWidget(Widget):
"""A clickable widget"""
@ -388,6 +392,9 @@ class XMLUIPanel(XMLUIBase):
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")]

View file

@ -127,6 +127,16 @@ class BoolWidget(EventWidget, ValueWidget, xmlui.BoolWidget, wx.CheckBox):
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

View file

@ -48,9 +48,9 @@ class Params(object):
<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 @@ class Params(object):
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 @@ class Params(object):
""" 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 @@ class Params(object):
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 @@ class Params(object):
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 @@ class Params(object):
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

View file

@ -51,7 +51,7 @@ class IMAP_server(object):
<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>

View file

@ -51,7 +51,7 @@ class SMTP_server(object):
<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>

View file

@ -444,14 +444,14 @@ class XEP_0065(object):
<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>

View file

@ -70,6 +70,9 @@ def _dataFormField2XMLUIData(field, read_only=False):
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 @@ class JidInputWidget(StringWidget):
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 @@ class BoolWidget(InputWidget):
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)