Mercurial > libervia-backend
diff libervia/backend/memory/params.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children | a8bceb29b1ff |
line wrap: on
line diff
--- a/libervia/backend/memory/params.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/memory/params.py Wed Jun 19 18:44:57 2024 +0200 @@ -227,15 +227,13 @@ if not default: log.info(_("No default profile, returning first one")) try: - default = self.host.memory.memory_data[ - "Profile_default" - ] = self.storage.get_profiles_list()[0] + default = self.host.memory.memory_data["Profile_default"] = ( + self.storage.get_profiles_list()[0] + ) except IndexError: log.info(_("No profile exist yet")) raise exceptions.ProfileUnknownError(profile_key) - return ( - default - ) # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists + return default # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists elif profile_key == C.PROF_KEY_NONE: raise exceptions.ProfileNotSetError elif return_profile_keys and profile_key in [C.PROF_KEY_ALL]: @@ -284,8 +282,8 @@ to_remove.append(cat_node) continue to_remove_count = ( - 0 - ) # count the params to be removed from current category + 0 # count the params to be removed from current category + ) for node in cat_node.childNodes: if node.nodeName != "param" or not self.check_security_limit( node, security_limit @@ -382,9 +380,7 @@ node = self._get_param_node(name, category, "@ALL@") if not node: log.error( - _( - "Requested param [%(name)s] in category [%(category)s] doesn't exist !" - ) + _("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {"name": name, "category": category} ) return @@ -526,16 +522,18 @@ return defer.succeed(password) def _type_to_str(self, result): - """Convert result to string, according to its type """ + """Convert result to string, according to its type""" if isinstance(result, bool): return C.bool_const(result) elif isinstance(result, (list, set, tuple)): - return ', '.join(self._type_to_str(r) for r in result) + return ", ".join(self._type_to_str(r) for r in result) else: return str(result) - def get_string_param_a(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): - """ Same as param_get_a but for bridge: convert non string value to string """ + def get_string_param_a( + self, name, category, attr="value", profile_key=C.PROF_KEY_NONE + ): + """Same as param_get_a but for bridge: convert non string value to string""" return self._type_to_str( self.param_get_a(name, category, attr, profile_key=profile_key) ) @@ -560,9 +558,7 @@ node = self._get_param_node(name, category) if not node: log.error( - _( - "Requested param [%(name)s] in category [%(category)s] doesn't exist !" - ) + _("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {"name": name, "category": category} ) raise exceptions.NotFound @@ -596,10 +592,16 @@ return self._get_attr(node[1], attr, value) async def async_get_string_param_a( - self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, - profile=C.PROF_KEY_NONE): + self, + name, + category, + attr="value", + security_limit=C.NO_SECURITY_LIMIT, + profile=C.PROF_KEY_NONE, + ): value = await self.param_get_a_async( - name, category, attr, security_limit, profile_key=profile) + name, category, attr, security_limit, profile_key=profile + ) return self._type_to_str(value) def param_get_a_async( @@ -621,9 +623,7 @@ node = self._get_param_node(name, category) if not node: log.error( - _( - "Requested param [%(name)s] in category [%(category)s] doesn't exist !" - ) + _("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {"name": name, "category": category} ) raise ValueError("Requested param doesn't exist") @@ -662,14 +662,19 @@ ) def _get_params_values_from_category( - self, category, security_limit, app, extra_s, profile_key): + self, category, security_limit, app, extra_s, profile_key + ): client = self.host.get_client(profile_key) extra = data_format.deserialise(extra_s) - return defer.ensureDeferred(self.get_params_values_from_category( - client, category, security_limit, app, extra)) + return defer.ensureDeferred( + self.get_params_values_from_category( + client, category, security_limit, app, extra + ) + ) async def get_params_values_from_category( - self, client, category, security_limit, app='', extra=None): + self, client, category, security_limit, app="", extra=None + ): """Get all parameters "attribute" for a category @param category(unicode): the desired category @@ -697,8 +702,11 @@ ) continue value = await self.async_get_string_param_a( - name, category, security_limit=security_limit, - profile=client.profile) + name, + category, + security_limit=security_limit, + profile=client.profile, + ) ret[name] = value break @@ -751,9 +759,11 @@ def check_node(node): """Check the node against security_limit, app and extra""" - return (self.check_security_limit(node, security_limit) - and self.check_app(node, app) - and self.check_extra(node, extra)) + return ( + self.check_security_limit(node, security_limit) + and self.check_app(node, app) + and self.check_extra(node, extra) + ) if profile in self.params: profile_cache = self.params[profile] @@ -828,9 +838,7 @@ pass elif dest_params[name].getAttribute("type") == "jids_list": jids = profile_value.split("\t") - for jid_elt in dest_params[name].getElementsByTagName( - "jid" - ): + for jid_elt in dest_params[name].getElementsByTagName("jid"): dest_params[name].removeChild( jid_elt ) # remove all default @@ -866,7 +874,6 @@ return prof_xml - def _get_params_ui(self, security_limit, app, extra_s, profile_key): client = self.host.get_client(profile_key) extra = data_format.deserialise(extra_s) @@ -940,8 +947,14 @@ categories.append(cat.getAttribute("name")) return categories - def param_set(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT, - profile_key=C.PROF_KEY_NONE): + def param_set( + self, + name, + value, + category, + security_limit=C.NO_SECURITY_LIMIT, + profile_key=C.PROF_KEY_NONE, + ): """Set a parameter, return None if the parameter is not in param xml. Parameter of type 'password' that are not the SàT profile password are @@ -973,11 +986,8 @@ if not self.check_security_limit(node[1], security_limit): msg = _( "{profile!r} is trying to set parameter {name!r} in category " - "{category!r} without authorization!!!").format( - profile=repr(profile), - name=repr(name), - category=repr(category) - ) + "{category!r} without authorization!!!" + ).format(profile=repr(profile), name=repr(name), category=repr(category)) log.warning(msg) raise exceptions.PermissionError(msg) @@ -989,13 +999,12 @@ try: int(value) except ValueError: - log.warning(_( - "Trying to set parameter {name} in category {category} with" - "an non-integer value" - ).format( - name=repr(name), - category=repr(category) - )) + log.warning( + _( + "Trying to set parameter {name} in category {category} with" + "an non-integer value" + ).format(name=repr(name), category=repr(category)) + ) return defer.succeed(None) if node[1].hasAttribute("constraint"): constraint = node[1].getAttribute("constraint") @@ -1135,11 +1144,11 @@ @param app: name of the frontend requesting the parameters, or '' to get all parameters @return: True if node doesn't match category/name of extra['ignore'] list """ - ignore_list = extra.get('ignore') + ignore_list = extra.get("ignore") if not ignore_list: return True - category = node.parentNode.getAttribute('name') - name = node.getAttribute('name') + category = node.parentNode.getAttribute("name") + name = node.getAttribute("name") ignore = [category, name] in ignore_list if ignore: log.debug(f"Ignoring parameter {category}/{name} as requested") @@ -1164,10 +1173,10 @@ selected = 'selected="true"' selected_found = True else: - selected = '' + selected = "" str_list.append( - f'<option value={quoteattr(value)} label={quoteattr(label)} {selected}/>' + f"<option value={quoteattr(value)} label={quoteattr(label)} {selected}/>" ) if not selected_found: raise ValueError(f"selected value ({selected}) not found in options") - return '\n'.join(str_list) + return "\n".join(str_list)