Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0095.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 |
---|---|
53 def __init__(self, host): | 53 def __init__(self, host): |
54 log.info(_("Plugin XEP_0095 initialization")) | 54 log.info(_("Plugin XEP_0095 initialization")) |
55 self.host = host | 55 self.host = host |
56 self.si_profiles = {} # key: SI profile, value: callback | 56 self.si_profiles = {} # key: SI profile, value: callback |
57 | 57 |
58 def getHandler(self, client): | 58 def get_handler(self, client): |
59 return XEP_0095_handler(self) | 59 return XEP_0095_handler(self) |
60 | 60 |
61 def registerSIProfile(self, si_profile, callback): | 61 def register_si_profile(self, si_profile, callback): |
62 """Add a callback for a SI Profile | 62 """Add a callback for a SI Profile |
63 | 63 |
64 @param si_profile(unicode): SI profile name (e.g. file-transfer) | 64 @param si_profile(unicode): SI profile name (e.g. file-transfer) |
65 @param callback(callable): method to call when the profile name is asked | 65 @param callback(callable): method to call when the profile name is asked |
66 """ | 66 """ |
67 self.si_profiles[si_profile] = callback | 67 self.si_profiles[si_profile] = callback |
68 | 68 |
69 def unregisterSIProfile(self, si_profile): | 69 def unregister_si_profile(self, si_profile): |
70 try: | 70 try: |
71 del self.si_profiles[si_profile] | 71 del self.si_profiles[si_profile] |
72 except KeyError: | 72 except KeyError: |
73 log.error( | 73 log.error( |
74 "Trying to unregister SI profile [{}] which was not registered".format( | 74 "Trying to unregister SI profile [{}] which was not registered".format( |
75 si_profile | 75 si_profile |
76 ) | 76 ) |
77 ) | 77 ) |
78 | 78 |
79 def streamInit(self, iq_elt, client): | 79 def stream_init(self, iq_elt, client): |
80 """This method is called on stream initiation (XEP-0095 #3.2) | 80 """This method is called on stream initiation (XEP-0095 #3.2) |
81 | 81 |
82 @param iq_elt: IQ element | 82 @param iq_elt: IQ element |
83 """ | 83 """ |
84 log.info(_("XEP-0095 Stream initiation")) | 84 log.info(_("XEP-0095 Stream initiation")) |
115 if si_condition is not None: | 115 if si_condition is not None: |
116 iq_error_elt.error.addElement((NS_SI, si_condition)) | 116 iq_error_elt.error.addElement((NS_SI, si_condition)) |
117 | 117 |
118 client.send(iq_error_elt) | 118 client.send(iq_error_elt) |
119 | 119 |
120 def acceptStream(self, client, iq_elt, feature_elt, misc_elts=None): | 120 def accept_stream(self, client, iq_elt, feature_elt, misc_elts=None): |
121 """Send the accept stream initiation answer | 121 """Send the accept stream initiation answer |
122 | 122 |
123 @param iq_elt(domish.Element): initial SI request | 123 @param iq_elt(domish.Element): initial SI request |
124 @param feature_elt(domish.Element): 'feature' element containing stream method to use | 124 @param feature_elt(domish.Element): 'feature' element containing stream method to use |
125 @param misc_elts(list[domish.Element]): list of elements to add | 125 @param misc_elts(list[domish.Element]): list of elements to add |
132 si_elt.addChild(feature_elt) | 132 si_elt.addChild(feature_elt) |
133 for elt in misc_elts: | 133 for elt in misc_elts: |
134 si_elt.addChild(elt) | 134 si_elt.addChild(elt) |
135 client.send(result_elt) | 135 client.send(result_elt) |
136 | 136 |
137 def _parseOfferResult(self, iq_elt): | 137 def _parse_offer_result(self, iq_elt): |
138 try: | 138 try: |
139 si_elt = next(iq_elt.elements(NS_SI, "si")) | 139 si_elt = next(iq_elt.elements(NS_SI, "si")) |
140 except StopIteration: | 140 except StopIteration: |
141 log.warning("No <si/> element found in result while expected") | 141 log.warning("No <si/> element found in result while expected") |
142 raise exceptions.DataError | 142 raise exceptions.DataError |
143 return (iq_elt, si_elt) | 143 return (iq_elt, si_elt) |
144 | 144 |
145 def proposeStream( | 145 def propose_stream( |
146 self, | 146 self, |
147 client, | 147 client, |
148 to_jid, | 148 to_jid, |
149 si_profile, | 149 si_profile, |
150 feature_elt, | 150 feature_elt, |
176 for elt in misc_elts: | 176 for elt in misc_elts: |
177 si.addChild(elt) | 177 si.addChild(elt) |
178 si.addChild(feature_elt) | 178 si.addChild(feature_elt) |
179 | 179 |
180 offer_d = offer.send() | 180 offer_d = offer.send() |
181 offer_d.addCallback(self._parseOfferResult) | 181 offer_d.addCallback(self._parse_offer_result) |
182 return sid, offer_d | 182 return sid, offer_d |
183 | 183 |
184 | 184 |
185 @implementer(iwokkel.IDisco) | 185 @implementer(iwokkel.IDisco) |
186 class XEP_0095_handler(xmlstream.XMPPHandler): | 186 class XEP_0095_handler(xmlstream.XMPPHandler): |
189 self.plugin_parent = plugin_parent | 189 self.plugin_parent = plugin_parent |
190 self.host = plugin_parent.host | 190 self.host = plugin_parent.host |
191 | 191 |
192 def connectionInitialized(self): | 192 def connectionInitialized(self): |
193 self.xmlstream.addObserver( | 193 self.xmlstream.addObserver( |
194 SI_REQUEST, self.plugin_parent.streamInit, client=self.parent | 194 SI_REQUEST, self.plugin_parent.stream_init, client=self.parent |
195 ) | 195 ) |
196 | 196 |
197 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | 197 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
198 return [disco.DiscoFeature(NS_SI)] + [ | 198 return [disco.DiscoFeature(NS_SI)] + [ |
199 disco.DiscoFeature( | 199 disco.DiscoFeature( |