diff sat/plugins/plugin_misc_download.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 0ff265725489
children
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_download.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_misc_download.py	Sat Apr 08 13:54:42 2023 +0200
@@ -54,20 +54,20 @@
     def __init__(self, host):
         log.info(_("plugin Download initialization"))
         self.host = host
-        host.bridge.addMethod(
-            "fileDownload",
+        host.bridge.add_method(
+            "file_download",
             ".plugin",
             in_sign="ssss",
             out_sign="s",
-            method=self._fileDownload,
+            method=self._file_download,
             async_=True,
         )
-        host.bridge.addMethod(
-            "fileDownloadComplete",
+        host.bridge.add_method(
+            "file_download_complete",
             ".plugin",
             in_sign="ssss",
             out_sign="s",
-            method=self._fileDownloadComplete,
+            method=self._file_download_complete,
             async_=True,
         )
         self._download_callbacks = {}
@@ -75,11 +75,11 @@
         self.register_scheme('http', self.download_http)
         self.register_scheme('https', self.download_http)
 
-    def _fileDownload(
+    def _file_download(
             self, attachment_s: str, dest_path: str, extra_s: str, profile: str
     ) -> defer.Deferred:
         d = defer.ensureDeferred(self.file_download(
-            self.host.getClient(profile),
+            self.host.get_client(profile),
             data_format.deserialise(attachment_s),
             Path(dest_path),
             data_format.deserialise(extra_s)
@@ -118,11 +118,11 @@
         else:
             return {"progress": progress_id}
 
-    def _fileDownloadComplete(
+    def _file_download_complete(
             self, attachment_s: str, dest_path: str, extra_s: str, profile: str
     ) -> defer.Deferred:
         d = defer.ensureDeferred(self.file_download_complete(
-            self.host.getClient(profile),
+            self.host.get_client(profile),
             data_format.deserialise(attachment_s),
             Path(dest_path),
             data_format.deserialise(extra_s)
@@ -168,7 +168,7 @@
             # we hash the URL to have an unique identifier, and avoid double download
             url_hash = hashlib.sha256(uri_parsed.geturl().encode()).hexdigest()
             cache_uid = f"{stem}_{url_hash}"
-            cache_data = client.cache.getMetadata(cache_uid)
+            cache_data = client.cache.get_metadata(cache_uid)
             if cache_data is not None:
                 # file is already in cache, we return it
                 download_d = defer.succeed(cache_data['path'])
@@ -176,14 +176,14 @@
             else:
                 # the file is not in cache
                 unique_name = '.'.join([cache_uid] + suffixes)
-                with client.cache.cacheData(
+                with client.cache.cache_data(
                     "DOWNLOAD", cache_uid, filename=unique_name) as f:
                     # we close the file and only use its name, the file will be opened
                     # by the registered callback
                     dest_path = Path(f.name)
 
         # should we check certificates?
-        check_certificate = self.host.memory.getParamA(
+        check_certificate = self.host.memory.param_get_a(
             "check_certificate", "Connection", profile_key=client.profile)
         if not check_certificate:
             extra['ignore_tls_errors'] = True
@@ -203,7 +203,7 @@
                     "Can't download URI {uri}: {reason}").format(
                     uri=uri, reason=e))
                 if cache_uid is not None:
-                    client.cache.removeFromCache(cache_uid)
+                    client.cache.remove_from_cache(cache_uid)
                 elif dest_path.exists():
                     dest_path.unlink()
                 raise e