Mercurial > libervia-backend
comparison sat/memory/params.py @ 3040:fee60f17ebac
jp: jp asyncio port:
/!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\
This patch implements the port of jp to asyncio, so it is now correctly using the bridge
asynchronously, and it can be used with bridges like `pb`. This also simplify the code,
notably for things which were previously implemented with many callbacks (like pagination
with RSM).
During the process, some behaviours have been modified/fixed, in jp and backends, check
diff for details.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Sep 2019 08:56:41 +0200 |
parents | ab2696e34d29 |
children | eec0c25c796b |
comparison
equal
deleted
inserted
replaced
3039:a1bc34f90fa5 | 3040:fee60f17ebac |
---|---|
527 ) | 527 ) |
528 return password | 528 return password |
529 | 529 |
530 return d.addCallback(gotPlainPassword) | 530 return d.addCallback(gotPlainPassword) |
531 | 531 |
532 def __type_to_string(self, result): | 532 def _type_to_str(self, result): |
533 """ convert result to string, according to its type """ | 533 """Convert result to string, according to its type """ |
534 if isinstance(result, bool): | 534 if isinstance(result, bool): |
535 return "true" if result else "false" | 535 return C.boolConst(result) |
536 elif isinstance(result, int): | 536 elif isinstance(result, (list, set, tuple)): |
537 return ', '.join(self._type_to_str(r) for r in result) | |
538 else: | |
537 return str(result) | 539 return str(result) |
538 return result | |
539 | 540 |
540 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): | 541 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): |
541 """ Same as getParamA but for bridge: convert non string value to string """ | 542 """ Same as getParamA but for bridge: convert non string value to string """ |
542 return self.__type_to_string( | 543 return self._type_to_str( |
543 self.getParamA(name, category, attr, profile_key=profile_key) | 544 self.getParamA(name, category, attr, profile_key=profile_key) |
544 ) | 545 ) |
545 | 546 |
546 def getParamA( | 547 def getParamA( |
547 self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE | 548 self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE |
597 if value is None and attr == "value" and not use_default: | 598 if value is None and attr == "value" and not use_default: |
598 return value | 599 return value |
599 return self._getAttr(node[1], attr, value) | 600 return self._getAttr(node[1], attr, value) |
600 | 601 |
601 def asyncGetStringParamA( | 602 def asyncGetStringParamA( |
602 self, | 603 self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, |
603 name, | 604 profile_key=C.PROF_KEY_NONE): |
604 category, | |
605 attr="value", | |
606 security_limit=C.NO_SECURITY_LIMIT, | |
607 profile_key=C.PROF_KEY_NONE, | |
608 ): | |
609 d = self.asyncGetParamA(name, category, attr, security_limit, profile_key) | 605 d = self.asyncGetParamA(name, category, attr, security_limit, profile_key) |
610 d.addCallback(self.__type_to_string) | 606 d.addCallback(self._type_to_str) |
611 return d | 607 return d |
612 | 608 |
613 def asyncGetParamA( | 609 def asyncGetParamA( |
614 self, | 610 self, |
615 name, | 611 name, |
955 name = cat.getAttribute("name") | 951 name = cat.getAttribute("name") |
956 if name not in categories: | 952 if name not in categories: |
957 categories.append(cat.getAttribute("name")) | 953 categories.append(cat.getAttribute("name")) |
958 return categories | 954 return categories |
959 | 955 |
960 def setParam( | 956 def setParam(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT, |
961 self, | 957 profile_key=C.PROF_KEY_NONE): |
962 name, | |
963 value, | |
964 category, | |
965 security_limit=C.NO_SECURITY_LIMIT, | |
966 profile_key=C.PROF_KEY_NONE, | |
967 ): | |
968 """Set a parameter, return None if the parameter is not in param xml. | 958 """Set a parameter, return None if the parameter is not in param xml. |
969 | 959 |
970 Parameter of type 'password' that are not the SàT profile password are | 960 Parameter of type 'password' that are not the SàT profile password are |
971 stored encrypted (if not empty). The profile password is stored hashed | 961 stored encrypted (if not empty). The profile password is stored hashed |
972 (if not empty). | 962 (if not empty). |
992 % {"category": category, "name": name} | 982 % {"category": category, "name": name} |
993 ) | 983 ) |
994 return defer.succeed(None) | 984 return defer.succeed(None) |
995 | 985 |
996 if not self.checkSecurityLimit(node[1], security_limit): | 986 if not self.checkSecurityLimit(node[1], security_limit): |
997 log.warning( | 987 msg = _( |
998 _( | 988 f"{profile!r} is trying to set parameter {name!r} in category " |
999 "Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!" | 989 f"{category!r} without authorization!!!") |
1000 % {"param": name, "cat": category} | 990 log.warning(msg) |
1001 ) | 991 raise exceptions.PermissionError(msg) |
1002 ) | |
1003 return defer.succeed(None) | |
1004 | 992 |
1005 type_ = node[1].getAttribute("type") | 993 type_ = node[1].getAttribute("type") |
1006 if type_ == "int": | 994 if type_ == "int": |
1007 if not value: # replace with the default value (which might also be '') | 995 if not value: # replace with the default value (which might also be '') |
1008 value = node[1].getAttribute("value") | 996 value = node[1].getAttribute("value") |
1009 else: | 997 else: |
1010 try: | 998 try: |
1011 int(value) | 999 int(value) |
1012 except ValueError: | 1000 except ValueError: |
1013 log.debug( | 1001 log.warning(_( |
1014 _( | 1002 f"Trying to set parameter {name!r} in category {category!r} with" |
1015 "Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value" | 1003 f"an non-integer value" |
1016 % {"param": name, "cat": category} | 1004 )) |
1017 ) | |
1018 ) | |
1019 return defer.succeed(None) | 1005 return defer.succeed(None) |
1020 if node[1].hasAttribute("constraint"): | 1006 if node[1].hasAttribute("constraint"): |
1021 constraint = node[1].getAttribute("constraint") | 1007 constraint = node[1].getAttribute("constraint") |
1022 try: | 1008 try: |
1023 min_, max_ = [int(limit) for limit in constraint.split(";")] | 1009 min_, max_ = [int(limit) for limit in constraint.split(";")] |