comparison src/memory/params.py @ 1534:a5e0393a06cd

plugin ip, params: plugin IP discovery, first draft: - a dedicated plugin now manage IP discovery, for now it uses external website, but it should implement XEP-0279 soon - a permission is requested to user for calling an external website. Once the permission is granted (it's global), it is not asked anymore. - the ip is discovered on demand, and cache is kept for the session. - memory.params.getParamA has a new parameter use_default, which allow to get None when a parameter is not set, instead of the default value. This allow to detected when we need to do the first permission request. memory.getParamA don't have this parameter.
author Goffi <goffi@goffi.org>
date Tue, 29 Sep 2015 17:54:22 +0200
parents fbe86b5d156f
children 42285d993e68
comparison
equal deleted inserted replaced
1533:d749922300d0 1534:a5e0393a06cd
346 /!\ This method would return encrypted password values. 346 /!\ This method would return encrypted password values.
347 347
348 @param node: XML param node 348 @param node: XML param node
349 @param attr: name of the attribute to get (e.g.: 'value' or 'type') 349 @param attr: name of the attribute to get (e.g.: 'value' or 'type')
350 @param value: user defined value 350 @param value: user defined value
351 @return: str 351 @return: value (can be str, bool, int, list, None)
352 """ 352 """
353 if attr == 'value': 353 if attr == 'value':
354 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 354 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
355 if node.getAttribute('type') == 'bool': 355 if node.getAttribute('type') == 'bool':
356 return C.bool(value_to_use) 356 return C.bool(value_to_use)
442 442
443 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): 443 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
444 """ Same as getParamA but for bridge: convert non string value to string """ 444 """ Same as getParamA but for bridge: convert non string value to string """
445 return self.__type_to_string(self.getParamA(name, category, attr, profile_key)) 445 return self.__type_to_string(self.getParamA(name, category, attr, profile_key))
446 446
447 def getParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): 447 def getParamA(self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE):
448 """Helper method to get a specific attribute. 448 """Helper method to get a specific attribute.
449 449
450 /!\ This method would return encrypted password values, 450 /!\ This method would return encrypted password values,
451 to get the plain values you have to use _asyncGetParamA. 451 to get the plain values you have to use _asyncGetParamA.
452 @param name: name of the parameter 452 @param name: name of the parameter
453 @param category: category of the parameter 453 @param category: category of the parameter
454 @param attr: name of the attribute (default: "value") 454 @param attr: name of the attribute (default: "value")
455 @parm use_default(bool): if True and attr=='value', return default value if not set
456 else return None if not set
455 @param profile: owner of the param (@ALL@ for everyone) 457 @param profile: owner of the param (@ALL@ for everyone)
456 @return: attribute 458 @return: attribute
457 """ 459 """
458 #FIXME: looks really dirty and buggy, need to be reviewed/refactored 460 #FIXME: looks really dirty and buggy, need to be reviewed/refactored
459 node = self._getParamNode(name, category) 461 node = self._getParamNode(name, category)
464 if attr == 'value' and node[1].getAttribute('type') == 'password': 466 if attr == 'value' and node[1].getAttribute('type') == 'password':
465 raise exceptions.InternalError('To retrieve password values, use asyncGetParamA instead of getParamA') 467 raise exceptions.InternalError('To retrieve password values, use asyncGetParamA instead of getParamA')
466 468
467 if node[0] == C.GENERAL: 469 if node[0] == C.GENERAL:
468 value = self._getParam(category, name, C.GENERAL) 470 value = self._getParam(category, name, C.GENERAL)
471 if value is None and attr=='value' and not use_default:
472 return value
469 return self._getAttr(node[1], attr, value) 473 return self._getAttr(node[1], attr, value)
470 474
471 assert node[0] == C.INDIVIDUAL 475 assert node[0] == C.INDIVIDUAL
472 476
473 profile = self.getProfileName(profile_key) 477 profile = self.getProfileName(profile_key)
479 log.error(_('Requesting synchronous param for not connected profile')) 483 log.error(_('Requesting synchronous param for not connected profile'))
480 raise exceptions.NotConnectedProfileError(profile) 484 raise exceptions.NotConnectedProfileError(profile)
481 485
482 if attr == "value": 486 if attr == "value":
483 value = self._getParam(category, name, profile=profile) 487 value = self._getParam(category, name, profile=profile)
488 if value is None and attr=='value' and not use_default:
489 return value
484 return self._getAttr(node[1], attr, value) 490 return self._getAttr(node[1], attr, value)
485 491
486 def asyncGetStringParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE): 492 def asyncGetStringParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
487 d = self.asyncGetParamA(name, category, attr, security_limit, profile_key) 493 d = self.asyncGetParamA(name, category, attr, security_limit, profile_key)
488 d.addCallback(self.__type_to_string) 494 d.addCallback(self.__type_to_string)
493 499
494 @param name: name of the parameter 500 @param name: name of the parameter
495 @param category: category of the parameter 501 @param category: category of the parameter
496 @param attr: name of the attribute (default: "value") 502 @param attr: name of the attribute (default: "value")
497 @param profile: owner of the param (@ALL@ for everyone) 503 @param profile: owner of the param (@ALL@ for everyone)
498 @return: Deferred 504 @return (defer.Deferred): parameter value, with corresponding type (bool, int, list, etc)
499 """ 505 """
500 node = self._getParamNode(name, category) 506 node = self._getParamNode(name, category)
501 if not node: 507 if not node:
502 log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category}) 508 log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
503 raise ValueError("Requested param doesn't exist") 509 raise ValueError("Requested param doesn't exist")
767 @param category (str): the parameter category 773 @param category (str): the parameter category
768 @param security_limit (int) 774 @param security_limit (int)
769 @param profile_key (str): %(doc_profile_key)s 775 @param profile_key (str): %(doc_profile_key)s
770 @return: a deferred None value when everything is done 776 @return: a deferred None value when everything is done
771 """ 777 """
778 # FIXME: setParam should accept the right type for value, not only str !
772 if profile_key != C.PROF_KEY_NONE: 779 if profile_key != C.PROF_KEY_NONE:
773 profile = self.getProfileName(profile_key) 780 profile = self.getProfileName(profile_key)
774 if not profile: 781 if not profile:
775 log.error(_(u'Trying to set parameter for an unknown profile')) 782 log.error(_(u'Trying to set parameter for an unknown profile'))
776 raise exceptions.ProfileUnknownError 783 raise exceptions.ProfileUnknownError