Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0334.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 | 13a2403774d4 |
children | c23cad65ae99 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
46 C.PI_DESCRIPTION: D_("""Implementation of Message Processing Hints"""), | 46 C.PI_DESCRIPTION: D_("""Implementation of Message Processing Hints"""), |
47 C.PI_USAGE: dedent( | 47 C.PI_USAGE: dedent( |
48 D_( | 48 D_( |
49 """\ | 49 """\ |
50 Frontends can use HINT_* constants in mess_data['extra'] in a serialized 'hints' dict. | 50 Frontends can use HINT_* constants in mess_data['extra'] in a serialized 'hints' dict. |
51 Internal plugins can use directly addHint([HINT_* constant]). | 51 Internal plugins can use directly add_hint([HINT_* constant]). |
52 Will set mess_data['extra']['history'] to 'skipped' when no store is requested and message is not saved in history.""" | 52 Will set mess_data['extra']['history'] to 'skipped' when no store is requested and message is not saved in history.""" |
53 ) | 53 ) |
54 ), | 54 ), |
55 } | 55 } |
56 | 56 |
65 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE) | 65 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE) |
66 | 66 |
67 def __init__(self, host): | 67 def __init__(self, host): |
68 log.info(_("Message Processing Hints plugin initialization")) | 68 log.info(_("Message Processing Hints plugin initialization")) |
69 self.host = host | 69 self.host = host |
70 host.trigger.add("sendMessage", self.sendMessageTrigger) | 70 host.trigger.add("sendMessage", self.send_message_trigger) |
71 host.trigger.add("messageReceived", self.messageReceivedTrigger, priority=-1000) | 71 host.trigger.add("messageReceived", self.message_received_trigger, priority=-1000) |
72 | 72 |
73 def getHandler(self, client): | 73 def get_handler(self, client): |
74 return XEP_0334_handler() | 74 return XEP_0334_handler() |
75 | 75 |
76 def addHint(self, mess_data, hint): | 76 def add_hint(self, mess_data, hint): |
77 if hint == self.HINT_NO_COPY and not mess_data["to"].resource: | 77 if hint == self.HINT_NO_COPY and not mess_data["to"].resource: |
78 log.error( | 78 log.error( |
79 "{hint} can only be used with full jids! Ignoring it.".format(hint=hint) | 79 "{hint} can only be used with full jids! Ignoring it.".format(hint=hint) |
80 ) | 80 ) |
81 return | 81 return |
83 if hint in self.HINTS: | 83 if hint in self.HINTS: |
84 hints.add(hint) | 84 hints.add(hint) |
85 else: | 85 else: |
86 log.error("Unknown hint: {}".format(hint)) | 86 log.error("Unknown hint: {}".format(hint)) |
87 | 87 |
88 def addHintElements(self, message_elt: domish.Element, hints: Iterable[str]) -> None: | 88 def add_hint_elements(self, message_elt: domish.Element, hints: Iterable[str]) -> None: |
89 """Add hints elements to message stanza | 89 """Add hints elements to message stanza |
90 | 90 |
91 @param message_elt: stanza where hints must be added | 91 @param message_elt: stanza where hints must be added |
92 @param hints: hints to add | 92 @param hints: hints to add |
93 """ | 93 """ |
95 if not list(message_elt.elements(NS_HINTS, hint)): | 95 if not list(message_elt.elements(NS_HINTS, hint)): |
96 message_elt.addElement((NS_HINTS, hint)) | 96 message_elt.addElement((NS_HINTS, hint)) |
97 else: | 97 else: |
98 log.debug('Not adding {hint!r} hint: it is already present in <message>') | 98 log.debug('Not adding {hint!r} hint: it is already present in <message>') |
99 | 99 |
100 def _sendPostXmlTreatment(self, mess_data): | 100 def _send_post_xml_treatment(self, mess_data): |
101 if "hints" in mess_data: | 101 if "hints" in mess_data: |
102 self.addHintElements(mess_data["xml"], mess_data["hints"]) | 102 self.add_hint_elements(mess_data["xml"], mess_data["hints"]) |
103 return mess_data | 103 return mess_data |
104 | 104 |
105 def sendMessageTrigger( | 105 def send_message_trigger( |
106 self, client, mess_data, pre_xml_treatments, post_xml_treatments | 106 self, client, mess_data, pre_xml_treatments, post_xml_treatments |
107 ): | 107 ): |
108 """Add the hints element to the message to be sent""" | 108 """Add the hints element to the message to be sent""" |
109 if "hints" in mess_data["extra"]: | 109 if "hints" in mess_data["extra"]: |
110 for hint in data_format.dict2iter("hints", mess_data["extra"], pop=True): | 110 for hint in data_format.dict2iter("hints", mess_data["extra"], pop=True): |
111 self.addHint(hint) | 111 self.add_hint(hint) |
112 | 112 |
113 post_xml_treatments.addCallback(self._sendPostXmlTreatment) | 113 post_xml_treatments.addCallback(self._send_post_xml_treatment) |
114 return True | 114 return True |
115 | 115 |
116 def _receivedSkipHistory(self, mess_data): | 116 def _received_skip_history(self, mess_data): |
117 mess_data["history"] = C.HISTORY_SKIP | 117 mess_data["history"] = C.HISTORY_SKIP |
118 return mess_data | 118 return mess_data |
119 | 119 |
120 def messageReceivedTrigger(self, client, message_elt, post_treat): | 120 def message_received_trigger(self, client, message_elt, post_treat): |
121 """Check for hints in the received message""" | 121 """Check for hints in the received message""" |
122 for elt in message_elt.elements(): | 122 for elt in message_elt.elements(): |
123 if elt.uri == NS_HINTS and elt.name in ( | 123 if elt.uri == NS_HINTS and elt.name in ( |
124 self.HINT_NO_PERMANENT_STORE, | 124 self.HINT_NO_PERMANENT_STORE, |
125 self.HINT_NO_STORE, | 125 self.HINT_NO_STORE, |
126 ): | 126 ): |
127 log.debug("history will be skipped for this message, as requested") | 127 log.debug("history will be skipped for this message, as requested") |
128 post_treat.addCallback(self._receivedSkipHistory) | 128 post_treat.addCallback(self._received_skip_history) |
129 break | 129 break |
130 return True | 130 return True |
131 | 131 |
132 | 132 |
133 @implementer(iwokkel.IDisco) | 133 @implementer(iwokkel.IDisco) |