Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0163.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 | 5d72dc52ee4a |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
51 def __init__(self, host): | 51 def __init__(self, host): |
52 log.info(_("PEP plugin initialization")) | 52 log.info(_("PEP plugin initialization")) |
53 self.host = host | 53 self.host = host |
54 self.pep_events = set() | 54 self.pep_events = set() |
55 self.pep_out_cb = {} | 55 self.pep_out_cb = {} |
56 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger) | 56 host.trigger.add("PubSub Disco Info", self.diso_info_trigger) |
57 host.bridge.addMethod( | 57 host.bridge.add_method( |
58 "PEPSend", | 58 "pep_send", |
59 ".plugin", | 59 ".plugin", |
60 in_sign="sa{ss}s", | 60 in_sign="sa{ss}s", |
61 out_sign="", | 61 out_sign="", |
62 method=self.PEPSend, | 62 method=self.pep_send, |
63 async_=True, | 63 async_=True, |
64 ) # args: type(MOOD, TUNE, etc), data, profile_key; | 64 ) # args: type(MOOD, TUNE, etc), data, profile_key; |
65 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood) | 65 self.add_pep_event("MOOD", NS_USER_MOOD, self.user_mood_cb, self.send_mood) |
66 | 66 |
67 def disoInfoTrigger(self, disco_info, profile): | 67 def diso_info_trigger(self, disco_info, profile): |
68 """Add info from managed PEP | 68 """Add info from managed PEP |
69 | 69 |
70 @param disco_info: list of disco feature as returned by PubSub, | 70 @param disco_info: list of disco feature as returned by PubSub, |
71 will be filled with PEP features | 71 will be filled with PEP features |
72 @param profile: profile we are handling | 72 @param profile: profile we are handling |
73 """ | 73 """ |
74 disco_info.extend(list(map(disco.DiscoFeature, self.pep_events))) | 74 disco_info.extend(list(map(disco.DiscoFeature, self.pep_events))) |
75 return True | 75 return True |
76 | 76 |
77 def addPEPEvent( | 77 def add_pep_event( |
78 self, | 78 self, |
79 event_type: Optional[str], | 79 event_type: Optional[str], |
80 node: str, | 80 node: str, |
81 in_callback: Callable, | 81 in_callback: Callable, |
82 out_callback: Optional[Callable] = None, | 82 out_callback: Optional[Callable] = None, |
91 for User Mood) | 91 for User Mood) |
92 @param in_callback: method to call when this event occur | 92 @param in_callback: method to call when this event occur |
93 the callable will be called with (itemsEvent, profile) as arguments | 93 the callable will be called with (itemsEvent, profile) as arguments |
94 @param out_callback: method to call when we want to publish this | 94 @param out_callback: method to call when we want to publish this |
95 event (must return a deferred) | 95 event (must return a deferred) |
96 the callable will be called when sendPEPEvent is called | 96 the callable will be called when send_pep_event is called |
97 @param notify: add autosubscribe (+notify) if True | 97 @param notify: add autosubscribe (+notify) if True |
98 """ | 98 """ |
99 if event_type and out_callback: | 99 if event_type and out_callback: |
100 event_type = event_type.upper() | 100 event_type = event_type.upper() |
101 if event_type in self.pep_out_cb: | 101 if event_type in self.pep_out_cb: |
105 self.pep_out_cb[event_type] = out_callback | 105 self.pep_out_cb[event_type] = out_callback |
106 self.pep_events.add(node) | 106 self.pep_events.add(node) |
107 if notify: | 107 if notify: |
108 self.pep_events.add(node + "+notify") | 108 self.pep_events.add(node + "+notify") |
109 | 109 |
110 def filterPEPEvent(client, itemsEvent): | 110 def filter_pep_event(client, itemsEvent): |
111 """Ignore messages which are not coming from PEP (i.e. a bare jid) | 111 """Ignore messages which are not coming from PEP (i.e. a bare jid) |
112 | 112 |
113 @param itemsEvent(pubsub.ItemsEvent): pubsub event | 113 @param itemsEvent(pubsub.ItemsEvent): pubsub event |
114 """ | 114 """ |
115 if not itemsEvent.sender.user or itemsEvent.sender.resource: | 115 if not itemsEvent.sender.user or itemsEvent.sender.resource: |
119 ) | 119 ) |
120 ) | 120 ) |
121 return | 121 return |
122 in_callback(itemsEvent, client.profile) | 122 in_callback(itemsEvent, client.profile) |
123 | 123 |
124 self.host.plugins["XEP-0060"].addManagedNode(node, items_cb=filterPEPEvent) | 124 self.host.plugins["XEP-0060"].add_managed_node(node, items_cb=filter_pep_event) |
125 | 125 |
126 def sendPEPEvent(self, node, data, profile): | 126 def send_pep_event(self, node, data, profile): |
127 """Publish the event data | 127 """Publish the event data |
128 | 128 |
129 @param node(unicode): node namespace | 129 @param node(unicode): node namespace |
130 @param data: domish.Element to use as payload | 130 @param data: domish.Element to use as payload |
131 @param profile: profile which send the data | 131 @param profile: profile which send the data |
132 """ | 132 """ |
133 client = self.host.getClient(profile) | 133 client = self.host.get_client(profile) |
134 item = pubsub.Item(payload=data) | 134 item = pubsub.Item(payload=data) |
135 return self.host.plugins["XEP-0060"].publish(client, None, node, [item]) | 135 return self.host.plugins["XEP-0060"].publish(client, None, node, [item]) |
136 | 136 |
137 def PEPSend(self, event_type, data, profile_key=C.PROF_KEY_NONE): | 137 def pep_send(self, event_type, data, profile_key=C.PROF_KEY_NONE): |
138 """Send personal event after checking the data is alright | 138 """Send personal event after checking the data is alright |
139 | 139 |
140 @param event_type: type of event (eg: MOOD, TUNE), | 140 @param event_type: type of event (eg: MOOD, TUNE), |
141 must be in self.pep_out_cb.keys() | 141 must be in self.pep_out_cb.keys() |
142 @param data: dict of {string:string} of event_type dependant data | 142 @param data: dict of {string:string} of event_type dependant data |
143 @param profile_key: profile who send the event | 143 @param profile_key: profile who send the event |
144 """ | 144 """ |
145 profile = self.host.memory.getProfileName(profile_key) | 145 profile = self.host.memory.get_profile_name(profile_key) |
146 if not profile: | 146 if not profile: |
147 log.error( | 147 log.error( |
148 _("Trying to send personal event with an unknown profile key [%s]") | 148 _("Trying to send personal event with an unknown profile key [%s]") |
149 % profile_key | 149 % profile_key |
150 ) | 150 ) |
152 if not event_type in list(self.pep_out_cb.keys()): | 152 if not event_type in list(self.pep_out_cb.keys()): |
153 log.error(_("Trying to send personal event for an unknown type")) | 153 log.error(_("Trying to send personal event for an unknown type")) |
154 raise exceptions.DataError("Type unknown") | 154 raise exceptions.DataError("Type unknown") |
155 return self.pep_out_cb[event_type](data, profile) | 155 return self.pep_out_cb[event_type](data, profile) |
156 | 156 |
157 def userMoodCB(self, itemsEvent, profile): | 157 def user_mood_cb(self, itemsEvent, profile): |
158 if not itemsEvent.items: | 158 if not itemsEvent.items: |
159 log.debug(_("No item found")) | 159 log.debug(_("No item found")) |
160 return | 160 return |
161 try: | 161 try: |
162 mood_elt = [ | 162 mood_elt = [ |
167 return | 167 return |
168 mood = Mood.fromXml(mood_elt) | 168 mood = Mood.fromXml(mood_elt) |
169 if not mood: | 169 if not mood: |
170 log.debug(_("No mood found")) | 170 log.debug(_("No mood found")) |
171 return | 171 return |
172 self.host.bridge.psEvent( | 172 self.host.bridge.ps_event( |
173 C.PS_PEP, | 173 C.PS_PEP, |
174 itemsEvent.sender.full(), | 174 itemsEvent.sender.full(), |
175 itemsEvent.nodeIdentifier, | 175 itemsEvent.nodeIdentifier, |
176 "MOOD", | 176 "MOOD", |
177 data_format.serialise({"mood": mood.value or "", "text": mood.text or ""}), | 177 data_format.serialise({"mood": mood.value or "", "text": mood.text or ""}), |
178 profile, | 178 profile, |
179 ) | 179 ) |
180 | 180 |
181 def sendMood(self, data, profile): | 181 def send_mood(self, data, profile): |
182 """Send XEP-0107's User Mood | 182 """Send XEP-0107's User Mood |
183 | 183 |
184 @param data: must include mood and text | 184 @param data: must include mood and text |
185 @param profile: profile which send the mood""" | 185 @param profile: profile which send the mood""" |
186 try: | 186 try: |
187 value = data["mood"].lower() | 187 value = data["mood"].lower() |
188 text = data["text"] if "text" in data else "" | 188 text = data["text"] if "text" in data else "" |
189 except KeyError: | 189 except KeyError: |
190 raise exceptions.DataError("Mood data must contain at least 'mood' key") | 190 raise exceptions.DataError("Mood data must contain at least 'mood' key") |
191 mood = UserMood(value, text) | 191 mood = UserMood(value, text) |
192 return self.sendPEPEvent(NS_USER_MOOD, mood, profile) | 192 return self.send_pep_event(NS_USER_MOOD, mood, profile) |
193 | 193 |
194 | 194 |
195 class UserMood(Mood, domish.Element): | 195 class UserMood(Mood, domish.Element): |
196 """Improved wokkel Mood which is also a domish.Element""" | 196 """Improved wokkel Mood which is also a domish.Element""" |
197 | 197 |