Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0363.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 | 412b99c29d83 |
children |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0363.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_xep_0363.py Sat Apr 08 13:54:42 2023 +0200 @@ -86,33 +86,33 @@ def __init__(self, host): log.info(_("plugin HTTP File Upload initialization")) self.host = host - host.bridge.addMethod( - "fileHTTPUpload", + host.bridge.add_method( + "file_http_upload", ".plugin", in_sign="sssbs", out_sign="", method=self._file_http_upload, ) - host.bridge.addMethod( - "fileHTTPUploadGetSlot", + host.bridge.add_method( + "file_http_upload_get_slot", ".plugin", in_sign="sisss", out_sign="(ssaa{ss})", - method=self._getSlot, + method=self._get_slot, async_=True, ) host.plugins["UPLOAD"].register( - "HTTP Upload", self.getHTTPUploadEntity, self.file_http_upload + "HTTP Upload", self.get_http_upload_entity, self.file_http_upload ) # list of callbacks used when a request is done to a component self.handlers = [] # XXX: there is not yet official short name, so we use "http_upload" - host.registerNamespace("http_upload", NS_HTTP_UPLOAD) + host.register_namespace("http_upload", NS_HTTP_UPLOAD) - def getHandler(self, client): + def get_handler(self, client): return XEP_0363_handler(self) - def registerHandler(self, callback, priority=0): + def register_handler(self, callback, priority=0): """Register a request handler @param callack: method to call when a request is done @@ -127,13 +127,13 @@ self.handlers.append(req_handler) self.handlers.sort(key=lambda handler: handler.priority, reverse=True) - def getFileTooLargeElt(self, max_size: int) -> domish.Element: + def get_file_too_large_elt(self, max_size: int) -> domish.Element: """Generate <file-too-large> app condition for errors""" file_too_large_elt = domish.Element((NS_HTTP_UPLOAD, "file-too-large")) file_too_large_elt.addElement("max-file-size", str(max_size)) return file_too_large_elt - async def getHTTPUploadEntity(self, client, upload_jid=None): + async def get_http_upload_entity(self, client, upload_jid=None): """Get HTTP upload capable entity upload_jid is checked, then its components @@ -144,7 +144,7 @@ try: entity = client.http_upload_service except AttributeError: - found_entities = await self.host.findFeaturesSet(client, (NS_HTTP_UPLOAD,)) + found_entities = await self.host.find_features_set(client, (NS_HTTP_UPLOAD,)) try: entity = client.http_upload_service = next(iter(found_entities)) except StopIteration: @@ -158,7 +158,7 @@ def _file_http_upload(self, filepath, filename="", upload_jid="", ignore_tls_errors=False, profile=C.PROF_KEY_NONE): assert os.path.isabs(filepath) and os.path.isfile(filepath) - client = self.host.getClient(profile) + client = self.host.get_client(profile) return defer.ensureDeferred(self.file_http_upload( client, filepath, @@ -204,7 +204,7 @@ triggers_no_cancel=True ) try: - slot = await self.getSlot( + slot = await self.get_slot( client, file_metadata["filename"], file_metadata["size"], upload_jid=upload_jid ) @@ -235,7 +235,7 @@ headers[name] = value - await self.host.trigger.asyncPoint( + await self.host.trigger.async_point( "XEP-0363_upload", client, extra, sat_file, file_producer, slot, triggers_no_cancel=True) @@ -259,34 +259,34 @@ """Called once file is successfully uploaded @param sat_file(SatFile): file used for the upload - should be closed, but it is needed to send the progressFinished signal + should be closed, but it is needed to send the progress_finished signal @param slot(Slot): put/get urls """ log.info(f"HTTP upload finished ({slot.get})") - sat_file.progressFinished({"url": slot.get}) + sat_file.progress_finished({"url": slot.get}) return slot.get def _upload_eb(self, failure_, sat_file): """Called on unsuccessful upload @param sat_file(SatFile): file used for the upload - should be closed, be is needed to send the progressError signal + should be closed, be is needed to send the progress_error signal """ try: wrapped_fail = failure_.value.reasons[0] except (AttributeError, IndexError) as e: log.warning(_("upload failed: {reason}").format(reason=e)) - sat_file.progressError(str(failure_)) + sat_file.progress_error(str(failure_)) else: if wrapped_fail.check(sat_web.SSLError): msg = "TLS validation error, can't connect to HTTPS server" else: msg = "can't upload file" log.warning(msg + ": " + str(wrapped_fail.value)) - sat_file.progressError(msg) + sat_file.progress_error(msg) raise failure_ - def _getSlot(self, filename, size, content_type, upload_jid, + def _get_slot(self, filename, size, content_type, upload_jid, profile_key=C.PROF_KEY_NONE): """Get an upload slot @@ -297,15 +297,15 @@ @param content_type(unicode, None): MIME type of the content empty string or None to guess automatically """ - client = self.host.getClient(profile_key) + client = self.host.get_client(profile_key) filename = filename.replace("/", "_") - d = defer.ensureDeferred(self.getSlot( + d = defer.ensureDeferred(self.get_slot( client, filename, size, content_type or None, jid.JID(upload_jid) or None )) d.addCallback(lambda slot: (slot.get, slot.put, slot.headers)) return d - async def getSlot(self, client, filename, size, content_type=None, upload_jid=None): + async def get_slot(self, client, filename, size, content_type=None, upload_jid=None): """Get a slot (i.e. download/upload links) @param filename(unicode): name to use for the upload @@ -327,8 +327,8 @@ try: upload_jid = client.http_upload_service except AttributeError: - found_entity = await self.getHTTPUploadEntity(client) - return await self.getSlot( + found_entity = await self.get_http_upload_entity(client) + return await self.get_slot( client, filename, size, content_type, found_entity) else: if upload_jid is None: @@ -374,11 +374,11 @@ # component - def onComponentRequest(self, iq_elt, client): + def on_component_request(self, iq_elt, client): iq_elt.handled=True - defer.ensureDeferred(self.handleComponentRequest(client, iq_elt)) + defer.ensureDeferred(self.handle_component_request(client, iq_elt)) - async def handleComponentRequest(self, client, iq_elt): + async def handle_component_request(self, client, iq_elt): try: request_elt = next(iq_elt.elements(NS_HTTP_UPLOAD, "request")) request = UploadRequest( @@ -395,7 +395,7 @@ for handler in self.handlers: try: - slot = await utils.asDeferred(handler.callback, client, request) + slot = await utils.as_deferred(handler.callback, client, request) except error.StanzaError as e: log.warning( "a stanza error has been raised while processing HTTP Upload of " @@ -436,7 +436,7 @@ if ((self.parent.is_component and PLUGIN_INFO[C.PI_IMPORT_NAME] in self.parent.enabled_features)): self.xmlstream.addObserver( - IQ_HTTP_UPLOAD_REQUEST, self.plugin_parent.onComponentRequest, + IQ_HTTP_UPLOAD_REQUEST, self.plugin_parent.on_component_request, client=self.parent )