Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0372.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 | 68a11b95a7d3 |
children | c23cad65ae99 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
60 class XEP_0372: | 60 class XEP_0372: |
61 namespace = NS_REFS | 61 namespace = NS_REFS |
62 | 62 |
63 def __init__(self, host): | 63 def __init__(self, host): |
64 log.info(_("References plugin initialization")) | 64 log.info(_("References plugin initialization")) |
65 host.registerNamespace("refs", NS_REFS) | 65 host.register_namespace("refs", NS_REFS) |
66 self.host = host | 66 self.host = host |
67 self._h = host.plugins["XEP-0334"] | 67 self._h = host.plugins["XEP-0334"] |
68 host.trigger.add("messageReceived", self._messageReceivedTrigger) | 68 host.trigger.add("messageReceived", self._message_received_trigger) |
69 host.bridge.addMethod( | 69 host.bridge.add_method( |
70 "referenceSend", | 70 "reference_send", |
71 ".plugin", | 71 ".plugin", |
72 in_sign="sssss", | 72 in_sign="sssss", |
73 out_sign="", | 73 out_sign="", |
74 method=self._sendReference, | 74 method=self._send_reference, |
75 async_=False, | 75 async_=False, |
76 ) | 76 ) |
77 | 77 |
78 def getHandler(self, client): | 78 def get_handler(self, client): |
79 return XEP_0372_Handler() | 79 return XEP_0372_Handler() |
80 | 80 |
81 def refElementToRefData( | 81 def ref_element_to_ref_data( |
82 self, | 82 self, |
83 reference_elt: domish.Element | 83 reference_elt: domish.Element |
84 ) -> Dict[str, Union[str, int, dict]]: | 84 ) -> Dict[str, Union[str, int, dict]]: |
85 ref_data: Dict[str, Union[str, int, dict]] = { | 85 ref_data: Dict[str, Union[str, int, dict]] = { |
86 "uri": reference_elt["uri"], | 86 "uri": reference_elt["uri"], |
87 "type": reference_elt["type"] | 87 "type": reference_elt["type"] |
88 } | 88 } |
89 | 89 |
90 if ref_data["uri"].startswith("xmpp:"): | 90 if ref_data["uri"].startswith("xmpp:"): |
91 ref_data["parsed_uri"] = xmpp_uri.parseXMPPUri(ref_data["uri"]) | 91 ref_data["parsed_uri"] = xmpp_uri.parse_xmpp_uri(ref_data["uri"]) |
92 | 92 |
93 for attr in ("begin", "end"): | 93 for attr in ("begin", "end"): |
94 try: | 94 try: |
95 ref_data[attr] = int(reference_elt[attr]) | 95 ref_data[attr] = int(reference_elt[attr]) |
96 except (KeyError, ValueError, TypeError): | 96 except (KeyError, ValueError, TypeError): |
98 | 98 |
99 anchor = reference_elt.getAttribute("anchor") | 99 anchor = reference_elt.getAttribute("anchor") |
100 if anchor is not None: | 100 if anchor is not None: |
101 ref_data["anchor"] = anchor | 101 ref_data["anchor"] = anchor |
102 if anchor.startswith("xmpp:"): | 102 if anchor.startswith("xmpp:"): |
103 ref_data["parsed_anchor"] = xmpp_uri.parseXMPPUri(anchor) | 103 ref_data["parsed_anchor"] = xmpp_uri.parse_xmpp_uri(anchor) |
104 return ref_data | 104 return ref_data |
105 | 105 |
106 async def _messageReceivedTrigger( | 106 async def _message_received_trigger( |
107 self, | 107 self, |
108 client: SatXMPPEntity, | 108 client: SatXMPPEntity, |
109 message_elt: domish.Element, | 109 message_elt: domish.Element, |
110 post_treat: defer.Deferred | 110 post_treat: defer.Deferred |
111 ) -> bool: | 111 ) -> bool: |
112 """Check if a direct invitation is in the message, and handle it""" | 112 """Check if a direct invitation is in the message, and handle it""" |
113 reference_elt = next(message_elt.elements(NS_REFS, "reference"), None) | 113 reference_elt = next(message_elt.elements(NS_REFS, "reference"), None) |
114 if reference_elt is None: | 114 if reference_elt is None: |
115 return True | 115 return True |
116 try: | 116 try: |
117 ref_data = self.refElementToRefData(reference_elt) | 117 ref_data = self.ref_element_to_ref_data(reference_elt) |
118 except KeyError: | 118 except KeyError: |
119 log.warning("invalid <reference> element: {reference_elt.toXml}") | 119 log.warning("invalid <reference> element: {reference_elt.toXml}") |
120 return True | 120 return True |
121 | 121 |
122 if not await self.host.trigger.asyncPoint( | 122 if not await self.host.trigger.async_point( |
123 "XEP-0372_ref_received", client, message_elt, ref_data | 123 "XEP-0372_ref_received", client, message_elt, ref_data |
124 ): | 124 ): |
125 return False | 125 return False |
126 return True | 126 return True |
127 | 127 |
128 def buildRefElement( | 128 def build_ref_element( |
129 self, | 129 self, |
130 uri: str, | 130 uri: str, |
131 type_: str = "mention", | 131 type_: str = "mention", |
132 begin: Optional[int] = None, | 132 begin: Optional[int] = None, |
133 end: Optional[int] = None, | 133 end: Optional[int] = None, |
146 reference_elt["end"] = str(end) | 146 reference_elt["end"] = str(end) |
147 if anchor is not None: | 147 if anchor is not None: |
148 reference_elt["anchor"] = anchor | 148 reference_elt["anchor"] = anchor |
149 return reference_elt | 149 return reference_elt |
150 | 150 |
151 def _sendReference( | 151 def _send_reference( |
152 self, | 152 self, |
153 recipient: str, | 153 recipient: str, |
154 anchor: str, | 154 anchor: str, |
155 type_: str, | 155 type_: str, |
156 extra_s: str, | 156 extra_s: str, |
157 profile_key: str | 157 profile_key: str |
158 ) -> defer.Deferred: | 158 ) -> defer.Deferred: |
159 recipient_jid = jid.JID(recipient) | 159 recipient_jid = jid.JID(recipient) |
160 client = self.host.getClient(profile_key) | 160 client = self.host.get_client(profile_key) |
161 extra: dict = data_format.deserialise(extra_s, default={}) | 161 extra: dict = data_format.deserialise(extra_s, default={}) |
162 self.sendReference( | 162 self.send_reference( |
163 client, | 163 client, |
164 uri=extra.get("uri"), | 164 uri=extra.get("uri"), |
165 type_=type_, | 165 type_=type_, |
166 anchor=anchor, | 166 anchor=anchor, |
167 to_jid=recipient_jid | 167 to_jid=recipient_jid |
168 ) | 168 ) |
169 | 169 |
170 def sendReference( | 170 def send_reference( |
171 self, | 171 self, |
172 client: "SatXMPPEntity", | 172 client: "SatXMPPEntity", |
173 uri: Optional[str] = None, | 173 uri: Optional[str] = None, |
174 type_: str = "mention", | 174 type_: str = "mention", |
175 begin: Optional[int] = None, | 175 begin: Optional[int] = None, |
198 if uri is None: | 198 if uri is None: |
199 if to_jid is None: | 199 if to_jid is None: |
200 raise exceptions.InternalError( | 200 raise exceptions.InternalError( |
201 '"to_jid" must be set if "uri is None"' | 201 '"to_jid" must be set if "uri is None"' |
202 ) | 202 ) |
203 uri = xmpp_uri.buildXMPPUri(path=to_jid.full()) | 203 uri = xmpp_uri.build_xmpp_uri(path=to_jid.full()) |
204 if message_elt is None: | 204 if message_elt is None: |
205 message_elt = domish.Element((None, "message")) | 205 message_elt = domish.Element((None, "message")) |
206 | 206 |
207 if to_jid is not None: | 207 if to_jid is not None: |
208 message_elt["to"] = to_jid.full() | 208 message_elt["to"] = to_jid.full() |
213 raise exceptions.InternalError( | 213 raise exceptions.InternalError( |
214 'invalid "to" attribute in given message element: ' | 214 'invalid "to" attribute in given message element: ' |
215 '{message_elt.toXml()}' | 215 '{message_elt.toXml()}' |
216 ) | 216 ) |
217 | 217 |
218 message_elt.addChild(self.buildRefElement(uri, type_, begin, end, anchor)) | 218 message_elt.addChild(self.build_ref_element(uri, type_, begin, end, anchor)) |
219 self._h.addHintElements(message_elt, [self._h.HINT_STORE]) | 219 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE]) |
220 client.send(message_elt) | 220 client.send(message_elt) |
221 | 221 |
222 | 222 |
223 @implementer(iwokkel.IDisco) | 223 @implementer(iwokkel.IDisco) |
224 class XEP_0372_Handler(XMPPHandler): | 224 class XEP_0372_Handler(XMPPHandler): |