Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0260.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 | 3900626bc100 |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0260.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_xep_0260.py Sat Apr 08 13:54:42 2023 +0200 @@ -69,12 +69,12 @@ self._jingle_ibb = host.plugins["XEP-0261"] except KeyError: self._jingle_ibb = None - self._j.registerTransport(NS_JINGLE_S5B, self._j.TRANSPORT_STREAMING, self, 100) + self._j.register_transport(NS_JINGLE_S5B, self._j.TRANSPORT_STREAMING, self, 100) - def getHandler(self, client): + def get_handler(self, client): return XEP_0260_handler() - def _parseCandidates(self, transport_elt): + def _parse_candidates(self, transport_elt): """Parse <candidate> elements @param transport_elt(domish.Element): parent <transport> element @@ -96,7 +96,7 @@ # self._s5b.registerCandidate(candidate) return candidates - def _buildCandidates(self, session, candidates, sid, session_hash, client, mode=None): + def _build_candidates(self, session, candidates, sid, session_hash, client, mode=None): """Build <transport> element with candidates @param session(dict): jingle session data @@ -135,47 +135,47 @@ return transport_elt @defer.inlineCallbacks - def jingleSessionInit(self, client, session, content_name): + def jingle_session_init(self, client, session, content_name): content_data = session["contents"][content_name] transport_data = content_data["transport_data"] sid = transport_data["sid"] = str(uuid.uuid4()) - session_hash = transport_data["session_hash"] = self._s5b.getSessionHash( + session_hash = transport_data["session_hash"] = self._s5b.get_session_hash( session["local_jid"], session["peer_jid"], sid ) - transport_data["peer_session_hash"] = self._s5b.getSessionHash( + transport_data["peer_session_hash"] = self._s5b.get_session_hash( session["peer_jid"], session["local_jid"], sid ) # requester and target are inversed for peer candidates - transport_data["stream_d"] = self._s5b.registerHash(client, session_hash, None) - candidates = transport_data["candidates"] = yield self._s5b.getCandidates( + transport_data["stream_d"] = self._s5b.register_hash(client, session_hash, None) + candidates = transport_data["candidates"] = yield self._s5b.get_candidates( client, session["local_jid"]) mode = "tcp" # XXX: we only manage tcp for now - transport_elt = self._buildCandidates( + transport_elt = self._build_candidates( session, candidates, sid, session_hash, client, mode ) defer.returnValue(transport_elt) - def _proxyActivatedCb(self, iq_result_elt, client, candidate, session, content_name): + def _proxy_activated_cb(self, iq_result_elt, client, candidate, session, content_name): """Called when activation confirmation has been received from proxy cf XEP-0260 § 2.4 """ # now that the proxy is activated, we have to inform other peer - iq_elt, transport_elt = self._j.buildAction( + iq_elt, transport_elt = self._j.build_action( client, self._j.A_TRANSPORT_INFO, session, content_name ) activated_elt = transport_elt.addElement("activated") activated_elt["cid"] = candidate.id iq_elt.send() - def _proxyActivatedEb(self, stanza_error, client, candidate, session, content_name): + def _proxy_activated_eb(self, stanza_error, client, candidate, session, content_name): """Called when activation error has been received from proxy cf XEP-0260 § 2.4 """ # TODO: fallback to IBB # now that the proxy is activated, we have to inform other peer - iq_elt, transport_elt = self._j.buildAction( + iq_elt, transport_elt = self._j.build_action( client, self._j.A_TRANSPORT_INFO, session, content_name ) transport_elt.addElement("proxy-error") @@ -185,9 +185,9 @@ reason=stanza_error.value.condition ) ) - self.doFallback(session, content_name, client) + self.do_fallback(session, content_name, client) - def _foundPeerCandidate( + def _found_peer_candidate( self, candidate, session, transport_data, content_name, client ): """Called when the best candidate from other peer is found @@ -207,7 +207,7 @@ continue c.discard() del transport_data["peer_candidates"] - iq_elt, transport_elt = self._j.buildAction( + iq_elt, transport_elt = self._j.build_action( client, self._j.A_TRANSPORT_INFO, session, content_name ) if candidate is None: @@ -218,9 +218,9 @@ candidate_elt = transport_elt.addElement("candidate-used") candidate_elt["cid"] = candidate.id iq_elt.send() # TODO: check result stanza - self._checkCandidates(session, content_name, transport_data, client) + self._check_candidates(session, content_name, transport_data, client) - def _checkCandidates(self, session, content_name, transport_data, client): + def _check_candidates(self, session, content_name, transport_data, client): """Called when a candidate has been choosed if we have both candidates, we select one, or fallback to an other transport @@ -261,7 +261,7 @@ if choosed_candidate is None: log.warning("Socks5 negociation failed, we need to fallback to IBB") - self.doFallback(session, content_name, client) + self.do_fallback(session, content_name, client) else: if choosed_candidate == peer_best_candidate: # peer_best_candidate was choosed from the candidates we have sent @@ -288,7 +288,7 @@ # the stream transfer need to wait for proxy activation # (see XEP-0260 § 2.4) if our_candidate: - d = self._s5b.connectCandidate( + d = self._s5b.connect_candidate( client, choosed_candidate, transport_data["session_hash"] ) d.addCallback( @@ -298,7 +298,7 @@ ) args = [client, choosed_candidate, session, content_name] d.addCallbacks( - self._proxyActivatedCb, self._proxyActivatedEb, args, None, args + self._proxy_activated_cb, self._proxy_activated_eb, args, None, args ) else: # this Deferred will be called when we'll receive activation confirmation from other peer @@ -309,13 +309,13 @@ if content_data["senders"] == session["role"]: # we can now start the stream transfer (or start it after proxy activation) d.addCallback( - lambda __: choosed_candidate.startTransfer( + lambda __: choosed_candidate.start_transfer( transport_data["session_hash"] ) ) - d.addErrback(self._startEb, session, content_name, client) + d.addErrback(self._start_eb, session, content_name, client) - def _startEb(self, fail, session, content_name, client): + def _start_eb(self, fail, session, content_name, client): """Called when it's not possible to start the transfer Will try to fallback to IBB @@ -325,9 +325,9 @@ except AttributeError: reason = str(fail) log.warning("Cant start transfert, we'll try fallback method: {}".format(reason)) - self.doFallback(session, content_name, client) + self.do_fallback(session, content_name, client) - def _candidateInfo( + def _candidate_info( self, candidate_elt, session, content_name, transport_data, client ): """Called when best candidate has been received from peer (or if none is working) @@ -368,9 +368,9 @@ log.info("Other peer best candidate: {}".format(candidate)) del transport_data["candidates"] - self._checkCandidates(session, content_name, transport_data, client) + self._check_candidates(session, content_name, transport_data, client) - def _proxyActivationInfo( + def _proxy_activation_info( self, proxy_elt, session, content_name, transport_data, client ): """Called when proxy has been activated (or has sent an error) @@ -393,7 +393,7 @@ activation_d.errback(ProxyError()) @defer.inlineCallbacks - def jingleHandler(self, client, action, session, content_name, transport_elt): + def jingle_handler(self, client, action, session, content_name, transport_elt): content_data = session["contents"][content_name] transport_data = content_data["transport_data"] @@ -403,21 +403,21 @@ elif action == self._j.A_SESSION_ACCEPT: # initiator side, we select a candidate in the ones sent by responder assert "peer_candidates" not in transport_data - transport_data["peer_candidates"] = self._parseCandidates(transport_elt) + transport_data["peer_candidates"] = self._parse_candidates(transport_elt) elif action == self._j.A_START: session_hash = transport_data["session_hash"] peer_candidates = transport_data["peer_candidates"] stream_object = content_data["stream_object"] - self._s5b.associateStreamObject(client, session_hash, stream_object) + self._s5b.associate_stream_object(client, session_hash, stream_object) stream_d = transport_data.pop("stream_d") stream_d.chainDeferred(content_data["finished_d"]) peer_session_hash = transport_data["peer_session_hash"] - d = self._s5b.getBestCandidate( + d = self._s5b.get_best_candidate( client, peer_candidates, session_hash, peer_session_hash ) d.addCallback( - self._foundPeerCandidate, session, transport_data, content_name, client + self._found_peer_candidate, session, transport_data, content_name, client ) elif action == self._j.A_SESSION_INITIATE: @@ -425,27 +425,27 @@ # and we give our candidates assert "peer_candidates" not in transport_data sid = transport_data["sid"] = transport_elt["sid"] - session_hash = transport_data["session_hash"] = self._s5b.getSessionHash( + session_hash = transport_data["session_hash"] = self._s5b.get_session_hash( session["local_jid"], session["peer_jid"], sid ) peer_session_hash = transport_data[ "peer_session_hash" - ] = self._s5b.getSessionHash( + ] = self._s5b.get_session_hash( session["peer_jid"], session["local_jid"], sid ) # requester and target are inversed for peer candidates - peer_candidates = transport_data["peer_candidates"] = self._parseCandidates( + peer_candidates = transport_data["peer_candidates"] = self._parse_candidates( transport_elt ) stream_object = content_data["stream_object"] - stream_d = self._s5b.registerHash(client, session_hash, stream_object) + stream_d = self._s5b.register_hash(client, session_hash, stream_object) stream_d.chainDeferred(content_data["finished_d"]) - d = self._s5b.getBestCandidate( + d = self._s5b.get_best_candidate( client, peer_candidates, session_hash, peer_session_hash ) d.addCallback( - self._foundPeerCandidate, session, transport_data, content_name, client + self._found_peer_candidate, session, transport_data, content_name, client ) - candidates = yield self._s5b.getCandidates(client, session["local_jid"]) + candidates = yield self._s5b.get_candidates(client, session["local_jid"]) # we remove duplicate candidates candidates = [ candidate for candidate in candidates if candidate not in peer_candidates @@ -453,7 +453,7 @@ transport_data["candidates"] = candidates # we can now build a new <transport> element with our candidates - transport_elt = self._buildCandidates( + transport_elt = self._build_candidates( session, candidates, sid, session_hash, client ) @@ -462,8 +462,8 @@ candidate_elt = None for method, names in ( - (self._candidateInfo, ("candidate-used", "candidate-error")), - (self._proxyActivationInfo, ("activated", "proxy-error")), + (self._candidate_info, ("candidate-used", "candidate-error")), + (self._proxy_activation_info, ("activated", "proxy-error")), ): for name in names: try: @@ -483,21 +483,21 @@ elif action == self._j.A_DESTROY: # the transport is replaced (fallback ?), We need mainly to kill XEP-0065 session. # note that sid argument is not necessary for sessions created by this plugin - self._s5b.killSession(None, transport_data["session_hash"], None, client) + self._s5b.kill_session(None, transport_data["session_hash"], None, client) else: log.warning("FIXME: unmanaged action {}".format(action)) defer.returnValue(transport_elt) - def jingleTerminate(self, client, action, session, content_name, reason_elt): + def jingle_terminate(self, client, action, session, content_name, reason_elt): if reason_elt.decline: log.debug("Session declined, deleting S5B session") # we just need to clean the S5B session if it is declined content_data = session["contents"][content_name] transport_data = content_data["transport_data"] - self._s5b.killSession(None, transport_data["session_hash"], None, client) + self._s5b.kill_session(None, transport_data["session_hash"], None, client) - def _doFallback(self, feature_checked, session, content_name, client): + def _do_fallback(self, feature_checked, session, content_name, client): """Do the fallback, method called once feature is checked @param feature_checked(bool): True if other peer can do IBB @@ -508,11 +508,11 @@ ) self._j.terminate(client, self._j.REASON_CONNECTIVITY_ERROR, session) else: - self._j.transportReplace( + self._j.transport_replace( client, self._jingle_ibb.NAMESPACE, session, content_name ) - def doFallback(self, session, content_name, client): + def do_fallback(self, session, content_name, client): """Fallback to IBB transport, used in last resort @param session(dict): session data @@ -531,7 +531,7 @@ d = self.host.hasFeature( client, self._jingle_ibb.NAMESPACE, session["peer_jid"] ) - d.addCallback(self._doFallback, session, content_name, client) + d.addCallback(self._do_fallback, session, content_name, client) return d