Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0199.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 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
46 class XEP_0199(object): | 46 class XEP_0199(object): |
47 | 47 |
48 def __init__(self, host): | 48 def __init__(self, host): |
49 log.info(_("XMPP Ping plugin initialization")) | 49 log.info(_("XMPP Ping plugin initialization")) |
50 self.host = host | 50 self.host = host |
51 host.bridge.addMethod( | 51 host.bridge.add_method( |
52 "ping", ".plugin", in_sign='ss', out_sign='d', method=self._ping, async_=True) | 52 "ping", ".plugin", in_sign='ss', out_sign='d', method=self._ping, async_=True) |
53 try: | 53 try: |
54 self.text_cmds = self.host.plugins[C.TEXT_CMDS] | 54 self.text_cmds = self.host.plugins[C.TEXT_CMDS] |
55 except KeyError: | 55 except KeyError: |
56 log.info(_("Text commands not available")) | 56 log.info(_("Text commands not available")) |
57 else: | 57 else: |
58 self.text_cmds.registerTextCommands(self) | 58 self.text_cmds.register_text_commands(self) |
59 | 59 |
60 def getHandler(self, client): | 60 def get_handler(self, client): |
61 return XEP_0199_handler(self) | 61 return XEP_0199_handler(self) |
62 | 62 |
63 def _pingRaiseIfFailure(self, pong): | 63 def _ping_raise_if_failure(self, pong): |
64 """If ping didn't succeed, raise the failure, else return pong delay""" | 64 """If ping didn't succeed, raise the failure, else return pong delay""" |
65 if pong[0] != "PONG": | 65 if pong[0] != "PONG": |
66 raise pong[0] | 66 raise pong[0] |
67 return pong[1] | 67 return pong[1] |
68 | 68 |
69 def _ping(self, jid_s, profile): | 69 def _ping(self, jid_s, profile): |
70 client = self.host.getClient(profile) | 70 client = self.host.get_client(profile) |
71 entity_jid = jid.JID(jid_s) | 71 entity_jid = jid.JID(jid_s) |
72 d = self.ping(client, entity_jid) | 72 d = self.ping(client, entity_jid) |
73 d.addCallback(self._pingRaiseIfFailure) | 73 d.addCallback(self._ping_raise_if_failure) |
74 return d | 74 return d |
75 | 75 |
76 def _pingCb(self, iq_result, send_time): | 76 def _ping_cb(self, iq_result, send_time): |
77 receive_time = time.time() | 77 receive_time = time.time() |
78 return ("PONG", receive_time - send_time) | 78 return ("PONG", receive_time - send_time) |
79 | 79 |
80 def _pingEb(self, failure_, send_time): | 80 def _ping_eb(self, failure_, send_time): |
81 receive_time = time.time() | 81 receive_time = time.time() |
82 return (failure_.value, receive_time - send_time) | 82 return (failure_.value, receive_time - send_time) |
83 | 83 |
84 def ping(self, client, entity_jid): | 84 def ping(self, client, entity_jid): |
85 """Ping an XMPP entity | 85 """Ping an XMPP entity |
92 iq_elt = client.IQ("get") | 92 iq_elt = client.IQ("get") |
93 iq_elt["to"] = entity_jid.full() | 93 iq_elt["to"] = entity_jid.full() |
94 iq_elt.addElement((NS_PING, "ping")) | 94 iq_elt.addElement((NS_PING, "ping")) |
95 d = iq_elt.send() | 95 d = iq_elt.send() |
96 send_time = time.time() | 96 send_time = time.time() |
97 d.addCallback(self._pingCb, send_time) | 97 d.addCallback(self._ping_cb, send_time) |
98 d.addErrback(self._pingEb, send_time) | 98 d.addErrback(self._ping_eb, send_time) |
99 return d | 99 return d |
100 | 100 |
101 def _cmd_ping_fb(self, pong, client, mess_data): | 101 def _cmd_ping_fb(self, pong, client, mess_data): |
102 """Send feedback to client when pong data is received""" | 102 """Send feedback to client when pong data is received""" |
103 txt_cmd = self.host.plugins[C.TEXT_CMDS] | 103 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
104 | 104 |
105 if pong[0] == "PONG": | 105 if pong[0] == "PONG": |
106 txt_cmd.feedBack(client, "PONG ({time} s)".format(time=pong[1]), mess_data) | 106 txt_cmd.feed_back(client, "PONG ({time} s)".format(time=pong[1]), mess_data) |
107 else: | 107 else: |
108 txt_cmd.feedBack( | 108 txt_cmd.feed_back( |
109 client, _("ping error ({err_msg}). Response time: {time} s") | 109 client, _("ping error ({err_msg}). Response time: {time} s") |
110 .format(err_msg=pong[0], time=pong[1]), mess_data) | 110 .format(err_msg=pong[0], time=pong[1]), mess_data) |
111 | 111 |
112 def cmd_ping(self, client, mess_data): | 112 def cmd_ping(self, client, mess_data): |
113 """ping an entity | 113 """ping an entity |
118 if mess_data["unparsed"].strip(): | 118 if mess_data["unparsed"].strip(): |
119 try: | 119 try: |
120 entity_jid = jid.JID(mess_data["unparsed"].strip()) | 120 entity_jid = jid.JID(mess_data["unparsed"].strip()) |
121 except RuntimeError: | 121 except RuntimeError: |
122 txt_cmd = self.host.plugins[C.TEXT_CMDS] | 122 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
123 txt_cmd.feedBack(client, _('Invalid jid: "{entity_jid}"').format( | 123 txt_cmd.feed_back(client, _('Invalid jid: "{entity_jid}"').format( |
124 entity_jid=mess_data["unparsed"].strip()), mess_data) | 124 entity_jid=mess_data["unparsed"].strip()), mess_data) |
125 return False | 125 return False |
126 else: | 126 else: |
127 entity_jid = mess_data["to"] | 127 entity_jid = mess_data["to"] |
128 d = self.ping(client, entity_jid) | 128 d = self.ping(client, entity_jid) |
129 d.addCallback(self._cmd_ping_fb, client, mess_data) | 129 d.addCallback(self._cmd_ping_fb, client, mess_data) |
130 | 130 |
131 return False | 131 return False |
132 | 132 |
133 def onPingRequest(self, iq_elt, client): | 133 def on_ping_request(self, iq_elt, client): |
134 log.info(_("XMPP PING received from {from_jid} [{profile}]").format( | 134 log.info(_("XMPP PING received from {from_jid} [{profile}]").format( |
135 from_jid=iq_elt["from"], profile=client.profile)) | 135 from_jid=iq_elt["from"], profile=client.profile)) |
136 iq_elt.handled = True | 136 iq_elt.handled = True |
137 iq_result_elt = xmlstream.toResponse(iq_elt, "result") | 137 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
138 client.send(iq_result_elt) | 138 client.send(iq_result_elt) |
144 def __init__(self, plugin_parent): | 144 def __init__(self, plugin_parent): |
145 self.plugin_parent = plugin_parent | 145 self.plugin_parent = plugin_parent |
146 | 146 |
147 def connectionInitialized(self): | 147 def connectionInitialized(self): |
148 self.xmlstream.addObserver( | 148 self.xmlstream.addObserver( |
149 PING_REQUEST, self.plugin_parent.onPingRequest, client=self.parent | 149 PING_REQUEST, self.plugin_parent.on_ping_request, client=self.parent |
150 ) | 150 ) |
151 | 151 |
152 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | 152 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
153 return [disco.DiscoFeature(NS_PING)] | 153 return [disco.DiscoFeature(NS_PING)] |
154 | 154 |