Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0055.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | be6d91572633 |
children |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0055.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_xep_0055.py Sat Apr 08 13:54:42 2023 +0200 @@ -73,49 +73,49 @@ # default search services (config file + hard-coded lists) self.services = [ jid.JID(entry) - for entry in host.memory.getConfig( + for entry in host.memory.config_get( CONFIG_SECTION, CONFIG_SERVICE_LIST, DEFAULT_SERVICE_LIST ) ] - host.bridge.addMethod( - "searchGetFieldsUI", + host.bridge.add_method( + "search_fields_ui_get", ".plugin", in_sign="ss", out_sign="s", - method=self._getFieldsUI, + method=self._get_fields_ui, async_=True, ) - host.bridge.addMethod( - "searchRequest", + host.bridge.add_method( + "search_request", ".plugin", in_sign="sa{ss}s", out_sign="s", - method=self._searchRequest, + method=self._search_request, async_=True, ) - self.__search_menu_id = host.registerCallback(self._getMainUI, with_data=True) - host.importMenu( + self.__search_menu_id = host.register_callback(self._get_main_ui, with_data=True) + host.import_menu( (D_("Contacts"), D_("Search directory")), - self._getMainUI, + self._get_main_ui, security_limit=1, help_string=D_("Search user directory"), ) - def _getHostServices(self, profile): + def _get_host_services(self, profile): """Return the jabber search services associated to the user host. @param profile (unicode): %(doc_profile)s @return: list[jid.JID] """ - client = self.host.getClient(profile) - d = self.host.findFeaturesSet(client, [NS_SEARCH]) + client = self.host.get_client(profile) + d = self.host.find_features_set(client, [NS_SEARCH]) return d.addCallback(lambda set_: list(set_)) ## Main search UI (menu item callback) ## - def _getMainUI(self, raw_data, profile): + def _get_main_ui(self, raw_data, profile): """Get the XMLUI for selecting a service and searching the directory. @param raw_data (dict): data received from the frontend @@ -123,10 +123,10 @@ @return: a deferred XMLUI string representation """ # check if the user's server offers some search services - d = self._getHostServices(profile) - return d.addCallback(lambda services: self.getMainUI(services, raw_data, profile)) + d = self._get_host_services(profile) + return d.addCallback(lambda services: self.get_main_ui(services, raw_data, profile)) - def getMainUI(self, services, raw_data, profile): + def get_main_ui(self, services, raw_data, profile): """Get the XMLUI for selecting a service and searching the directory. @param services (list[jid.JID]): search services offered by the user server @@ -136,7 +136,7 @@ """ # extend services offered by user's server with the default services services.extend([service for service in self.services if service not in services]) - data = xml_tools.XMLUIResult2DataFormResult(raw_data) + data = xml_tools.xmlui_result_2_data_form_result(raw_data) main_ui = xml_tools.XMLUI( C.XMLUI_WINDOW, container="tabs", @@ -144,13 +144,13 @@ submit_id=self.__search_menu_id, ) - d = self._addSimpleSearchUI(services, main_ui, data, profile) + d = self._add_simple_search_ui(services, main_ui, data, profile) d.addCallback( - lambda __: self._addAdvancedSearchUI(services, main_ui, data, profile) + lambda __: self._add_advanced_search_ui(services, main_ui, data, profile) ) return d.addCallback(lambda __: {"xmlui": main_ui.toXml()}) - def _addSimpleSearchUI(self, services, main_ui, data, profile): + def _add_simple_search_ui(self, services, main_ui, data, profile): """Add to the main UI a tab for the simple search. Display a single input field and search on the main service (it actually does one search per search field and then compile the results). @@ -176,13 +176,13 @@ ) ) - sub_cont = main_ui.main_container.addTab( + sub_cont = main_ui.main_container.add_tab( "simple_search", label=_("Simple search"), container=xml_tools.VerticalContainer, ) - main_ui.changeContainer(sub_cont.append(xml_tools.PairsContainer(main_ui))) - xml_tools.dataForm2Widgets(main_ui, form) + main_ui.change_container(sub_cont.append(xml_tools.PairsContainer(main_ui))) + xml_tools.data_form_2_widgets(main_ui, form) # FIXME: add colspan attribute to divider? (we are in a PairsContainer) main_ui.addDivider("blank") @@ -197,19 +197,19 @@ } if simple_data: log.debug("Simple search with %s on %s" % (simple_data, service_jid)) - sub_cont.parent.setSelected(True) - main_ui.changeContainer(sub_cont.append(xml_tools.VerticalContainer(main_ui))) + sub_cont.parent.set_selected(True) + main_ui.change_container(sub_cont.append(xml_tools.VerticalContainer(main_ui))) main_ui.addDivider("dash") - d = self.searchRequest(service_jid, simple_data, profile) + d = self.search_request(service_jid, simple_data, profile) d.addCallbacks( - lambda elt: self._displaySearchResult(main_ui, elt), + lambda elt: self._display_search_result(main_ui, elt), lambda failure: main_ui.addText(failure.getErrorMessage()), ) return d return defer.succeed(None) - def _addAdvancedSearchUI(self, services, main_ui, data, profile): + def _add_advanced_search_ui(self, services, main_ui, data, profile): """Add to the main UI a tab for the advanced search. Display a service selector and allow to search on all the fields that are implemented by the selected service. @@ -221,7 +221,7 @@ @return: a __ Deferred """ - sub_cont = main_ui.main_container.addTab( + sub_cont = main_ui.main_container.add_tab( "advanced_search", label=_("Advanced search"), container=xml_tools.VerticalContainer, @@ -230,7 +230,7 @@ if "service_jid_extra" in data: # refresh button has been pushed, select the tab - sub_cont.parent.setSelected(True) + sub_cont.parent.set_selected(True) # get the selected service service_jid_s = data.get("service_jid_extra", "") if not service_jid_s: @@ -242,7 +242,7 @@ if service_jid_s not in services_s: services_s.append(service_jid_s) - main_ui.changeContainer(sub_cont.append(xml_tools.PairsContainer(main_ui))) + main_ui.change_container(sub_cont.append(xml_tools.PairsContainer(main_ui))) main_ui.addLabel(_("Search on")) main_ui.addList("service_jid", options=services_s, selected=service_jid_s) main_ui.addLabel(_("Other service")) @@ -262,17 +262,17 @@ main_ui.addDivider("dash") main_ui.addDivider("dash") - main_ui.changeContainer(sub_cont.append(xml_tools.VerticalContainer(main_ui))) + main_ui.change_container(sub_cont.append(xml_tools.VerticalContainer(main_ui))) service_jid = jid.JID(service_jid_s) - d = self.getFieldsUI(service_jid, profile) + d = self.get_fields_ui(service_jid, profile) d.addCallbacks( - self._addAdvancedForm, + self._add_advanced_form, lambda failure: main_ui.addText(failure.getErrorMessage()), [service_jid, main_ui, sub_cont, data, profile], ) return d - def _addAdvancedForm(self, form_elt, service_jid, main_ui, sub_cont, data, profile): + def _add_advanced_form(self, form_elt, service_jid, main_ui, sub_cont, data, profile): """Add the search form and the search results (if there is some to display). @param form_elt (domish.Element): form element listing the fields @@ -288,7 +288,7 @@ adv_fields = [field.var for field in field_list if field.var] adv_data = {key: value for key, value in data.items() if key in adv_fields} - xml_tools.dataForm2Widgets(main_ui, data_form.Form.fromElement(form_elt)) + xml_tools.data_form_2_widgets(main_ui, data_form.Form.fromElement(form_elt)) # refill the submitted values # FIXME: wokkel's data_form.Form.fromElement doesn't parse the values, so we do it directly in XMLUI for now @@ -309,26 +309,26 @@ if adv_data: # display the search results log.debug("Advanced search with %s on %s" % (adv_data, service_jid)) - sub_cont.parent.setSelected(True) - main_ui.changeContainer(sub_cont.append(xml_tools.VerticalContainer(main_ui))) + sub_cont.parent.set_selected(True) + main_ui.change_container(sub_cont.append(xml_tools.VerticalContainer(main_ui))) main_ui.addDivider("dash") - d = self.searchRequest(service_jid, adv_data, profile) + d = self.search_request(service_jid, adv_data, profile) d.addCallbacks( - lambda elt: self._displaySearchResult(main_ui, elt), + lambda elt: self._display_search_result(main_ui, elt), lambda failure: main_ui.addText(failure.getErrorMessage()), ) return d return defer.succeed(None) - def _displaySearchResult(self, main_ui, elt): + def _display_search_result(self, main_ui, elt): """Display the search results. @param main_ui (XMLUI): the main XMLUI instance @param elt (domish.Element): form result element """ if [child for child in elt.children if child.name == "item"]: - headers, xmlui_data = xml_tools.dataFormEltResult2XMLUIData(elt) + headers, xmlui_data = xml_tools.data_form_elt_result_2_xmlui_data(elt) if "jid" in headers: # use XMLUI JidsListWidget to display the results values = {} for i in range(len(xmlui_data)): @@ -341,41 +341,41 @@ main_ui.addJidsList(jids=values["jid"], name=D_("Search results")) # TODO: also display the values other than JID else: - xml_tools.XMLUIData2AdvancedList(main_ui, headers, xmlui_data) + xml_tools.xmlui_data_2_advanced_list(main_ui, headers, xmlui_data) else: main_ui.addText(D_("The search gave no result")) ## Retrieve the search fields ## - def _getFieldsUI(self, to_jid_s, profile_key): + def _get_fields_ui(self, to_jid_s, profile_key): """Ask a service to send us the list of the form fields it manages. @param to_jid_s (unicode): XEP-0055 compliant search entity @param profile_key (unicode): %(doc_profile_key)s @return: a deferred XMLUI instance """ - d = self.getFieldsUI(jid.JID(to_jid_s), profile_key) - d.addCallback(lambda form: xml_tools.dataFormEltResult2XMLUI(form).toXml()) + d = self.get_fields_ui(jid.JID(to_jid_s), profile_key) + d.addCallback(lambda form: xml_tools.data_form_elt_result_2_xmlui(form).toXml()) return d - def getFieldsUI(self, to_jid, profile_key): + def get_fields_ui(self, to_jid, profile_key): """Ask a service to send us the list of the form fields it manages. @param to_jid (jid.JID): XEP-0055 compliant search entity @param profile_key (unicode): %(doc_profile_key)s @return: a deferred domish.Element """ - client = self.host.getClient(profile_key) + client = self.host.get_client(profile_key) fields_request = IQ(client.xmlstream, "get") fields_request["from"] = client.jid.full() fields_request["to"] = to_jid.full() fields_request.addElement("query", NS_SEARCH) d = fields_request.send(to_jid.full()) - d.addCallbacks(self._getFieldsUICb, self._getFieldsUIEb) + d.addCallbacks(self._get_fields_ui_cb, self._get_fields_ui_eb) return d - def _getFieldsUICb(self, answer): - """Callback for self.getFieldsUI. + def _get_fields_ui_cb(self, answer): + """Callback for self.get_fields_ui. @param answer (domish.Element): search query element @return: domish.Element @@ -394,8 +394,8 @@ ) return form_elt - def _getFieldsUIEb(self, failure): - """Errback to self.getFieldsUI. + def _get_fields_ui_eb(self, failure): + """Errback to self.get_fields_ui. @param failure (defer.failure.Failure): twisted failure @raise: the unchanged defer.failure.Failure @@ -405,35 +405,35 @@ ## Do the search ## - def _searchRequest(self, to_jid_s, search_data, profile_key): + def _search_request(self, to_jid_s, search_data, profile_key): """Actually do a search, according to filled data. @param to_jid_s (unicode): XEP-0055 compliant search entity - @param search_data (dict): filled data, corresponding to the form obtained in getFieldsUI + @param search_data (dict): filled data, corresponding to the form obtained in get_fields_ui @param profile_key (unicode): %(doc_profile_key)s @return: a deferred XMLUI string representation """ - d = self.searchRequest(jid.JID(to_jid_s), search_data, profile_key) - d.addCallback(lambda form: xml_tools.dataFormEltResult2XMLUI(form).toXml()) + d = self.search_request(jid.JID(to_jid_s), search_data, profile_key) + d.addCallback(lambda form: xml_tools.data_form_elt_result_2_xmlui(form).toXml()) return d - def searchRequest(self, to_jid, search_data, profile_key): + def search_request(self, to_jid, search_data, profile_key): """Actually do a search, according to filled data. @param to_jid (jid.JID): XEP-0055 compliant search entity - @param search_data (dict): filled data, corresponding to the form obtained in getFieldsUI + @param search_data (dict): filled data, corresponding to the form obtained in get_fields_ui @param profile_key (unicode): %(doc_profile_key)s @return: a deferred domish.Element """ if FIELD_SINGLE in search_data: value = search_data[FIELD_SINGLE] - d = self.getFieldsUI(to_jid, profile_key) + d = self.get_fields_ui(to_jid, profile_key) d.addCallback( - lambda elt: self.searchRequestMulti(to_jid, value, elt, profile_key) + lambda elt: self.search_request_multi(to_jid, value, elt, profile_key) ) return d - client = self.host.getClient(profile_key) + client = self.host.get_client(profile_key) search_request = IQ(client.xmlstream, "set") search_request["from"] = client.jid.full() search_request["to"] = to_jid.full() @@ -443,10 +443,10 @@ query_elt.addChild(x_form.toElement()) # TODO: XEP-0059 could be used here (with the needed new method attributes) d = search_request.send(to_jid.full()) - d.addCallbacks(self._searchOk, self._searchErr) + d.addCallbacks(self._search_ok, self._search_err) return d - def searchRequestMulti(self, to_jid, value, form_elt, profile_key): + def search_request_multi(self, to_jid, value, form_elt, profile_key): """Search for a value simultaneously in all fields, returns the results compilation. @param to_jid (jid.JID): XEP-0055 compliant search entity @@ -459,7 +459,7 @@ d_list = [] for field in [field.var for field in form.fieldList if field.var]: - d_list.append(self.searchRequest(to_jid, {field: value}, profile_key)) + d_list.append(self.search_request(to_jid, {field: value}, profile_key)) def cb(result): # return the results compiled in one domish element result_elt = None @@ -481,8 +481,8 @@ return defer.DeferredList(d_list).addCallback(cb) - def _searchOk(self, answer): - """Callback for self.searchRequest. + def _search_ok(self, answer): + """Callback for self.search_request. @param answer (domish.Element): search query element @return: domish.Element @@ -501,8 +501,8 @@ ) return form_elt - def _searchErr(self, failure): - """Errback to self.searchRequest. + def _search_err(self, failure): + """Errback to self.search_request. @param failure (defer.failure.Failure): twisted failure @raise: the unchanged defer.failure.Failure