comparison 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
comparison
equal deleted inserted replaced
1219:16484ebb695b 1220:f91e7028e2c3
46 <param name="Password" value="" type="password" /> 46 <param name="Password" value="" type="password" />
47 </category> 47 </category>
48 <category name="Connection" label="%(category_connection)s"> 48 <category name="Connection" label="%(category_connection)s">
49 <param name="JabberID" value="name@example.org/SàT" type="string" /> 49 <param name="JabberID" value="name@example.org/SàT" type="string" />
50 <param name="Password" value="" type="password" /> 50 <param name="Password" value="" type="password" />
51 <param name="Priority" value="50" type="string" /> 51 <param name="Priority" value="50" type="int" min="-128" max="127"/>
52 <param name="%(force_server_param)s" value="" type="string" /> 52 <param name="%(force_server_param)s" value="" type="string" />
53 <param name="%(force_port_param)s" value="" type="string" /> 53 <param name="%(force_port_param)s" value="" type="int" min="80" max="65535" />
54 <param name="NewAccount" label="%(label_NewAccount)s" type="button" callback_id="registerNewAccount"/> 54 <param name="NewAccount" label="%(label_NewAccount)s" type="button" callback_id="registerNewAccount"/>
55 <param name="autoconnect" label="%(label_autoconnect)s" value="true" type="bool" /> 55 <param name="autoconnect" label="%(label_autoconnect)s" value="true" type="bool" />
56 <param name="autodisconnect" label="%(label_autodisconnect)s" value="false" type="bool" /> 56 <param name="autodisconnect" label="%(label_autodisconnect)s" value="false" type="bool" />
57 </category> 57 </category>
58 <category name="Misc" label="%(category_misc)s"> 58 <category name="Misc" label="%(category_misc)s">
329 """ 329 """
330 if attr == 'value': 330 if attr == 'value':
331 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 331 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
332 if node.getAttribute('type') == 'bool': 332 if node.getAttribute('type') == 'bool':
333 return value_to_use.lower() not in ('false', '0', 'no') 333 return value_to_use.lower() not in ('false', '0', 'no')
334 if node.getAttribute('type') == 'int':
335 return int(value_to_use)
334 elif node.getAttribute('type') == 'list': 336 elif node.getAttribute('type') == 'list':
335 options = [option for option in node.childNodes if option.nodeName == 'option'] 337 options = [option for option in node.childNodes if option.nodeName == 'option']
336 values = [option.getAttribute('value') for option in options] 338 values = [option.getAttribute('value') for option in options]
337 if value_to_use not in values: # value_to_use is probably empty 339 if value_to_use not in values: # value_to_use is probably empty
338 selected = [option for option in options if option.getAttribute('selected') == 'true'] 340 selected = [option for option in options if option.getAttribute('selected') == 'true']
396 398
397 def __type_to_string(self, result): 399 def __type_to_string(self, result):
398 """ convert result to string, according to its type """ 400 """ convert result to string, according to its type """
399 if isinstance(result, bool): 401 if isinstance(result, bool):
400 return "true" if result else "false" 402 return "true" if result else "false"
403 elif isinstance(result, int):
404 return str(result)
401 return result 405 return result
402 406
403 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): 407 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
404 """ Same as getParamA but for bridge: convert non string value to string """ 408 """ Same as getParamA but for bridge: convert non string value to string """
405 return self.__type_to_string(self.getParamA(name, category, attr, profile_key)) 409 return self.__type_to_string(self.getParamA(name, category, attr, profile_key))
727 raise exceptions.ProfileUnknownError 731 raise exceptions.ProfileUnknownError
728 732
729 node = self._getParamNode(name, category, '@ALL@') 733 node = self._getParamNode(name, category, '@ALL@')
730 if not node: 734 if not node:
731 log.error(_('Requesting an unknown parameter (%(category)s/%(name)s)') 735 log.error(_('Requesting an unknown parameter (%(category)s/%(name)s)')
732 % {'category': category, 'name': name}) 736 % {'category': category, 'name': name})
733 return defer.succeed(None) 737 return defer.succeed(None)
734 738
735 if not self.checkSecurityLimit(node[1], security_limit): 739 if not self.checkSecurityLimit(node[1], security_limit):
736 log.warning(_("Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!" 740 log.warning(_("Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!"
737 % {'param': name, 'cat': category})) 741 % {'param': name, 'cat': category}))
738 return defer.succeed(None) 742 return defer.succeed(None)
739 743
740 type_ = node[1].getAttribute("type") 744 type_ = node[1].getAttribute("type")
745 if type_ == 'int':
746 if not value: # replace with the default value (which might also be '')
747 value = node[1].getAttribute("value")
748 else:
749 try:
750 int(value)
751 except ValueError:
752 log.debug(_("Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
753 % {'param': name, 'cat': category}))
754 return defer.succeed(None)
755 if node[1].hasAttribute("min"):
756 value = str(max(int(value), int(node[1].getAttribute("min"))))
757 if node[1].hasAttribute("max"):
758 value = str(min(int(value), int(node[1].getAttribute("max"))))
759
741 log.info(_("Setting parameter (%(category)s, %(name)s) = %(value)s") % 760 log.info(_("Setting parameter (%(category)s, %(name)s) = %(value)s") %
742 {'category': category, 'name': name, 'value': value if type_ != 'password' else '********'}) 761 {'category': category, 'name': name, 'value': value if type_ != 'password' else '********'})
743 762
744 if node[0] == C.GENERAL: 763 if node[0] == C.GENERAL:
745 self.params_gen[(category, name)] = value 764 self.params_gen[(category, name)] = value
787 def _getNodesOfTypes(self, attr_type, node_type="@ALL@"): 806 def _getNodesOfTypes(self, attr_type, node_type="@ALL@"):
788 """Return all the nodes matching the given types. 807 """Return all the nodes matching the given types.
789 808
790 TODO: using during the dev but not anymore... remove if not needed 809 TODO: using during the dev but not anymore... remove if not needed
791 810
792 @param attr_type (str): the attribute type (string, text, password, bool, button, list) 811 @param attr_type (str): the attribute type (string, text, password, bool, int, button, list)
793 @param node_type (str): keyword for filtering: 812 @param node_type (str): keyword for filtering:
794 @ALL@ search everywhere 813 @ALL@ search everywhere
795 @GENERAL@ only search in general type 814 @GENERAL@ only search in general type
796 @INDIVIDUAL@ only search in individual type 815 @INDIVIDUAL@ only search in individual type
797 @return: dict{tuple: node}: a dict {key, value} where: 816 @return: dict{tuple: node}: a dict {key, value} where: