comparison 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
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
52 class DownloadPlugin(object): 52 class DownloadPlugin(object):
53 53
54 def __init__(self, host): 54 def __init__(self, host):
55 log.info(_("plugin Download initialization")) 55 log.info(_("plugin Download initialization"))
56 self.host = host 56 self.host = host
57 host.bridge.addMethod( 57 host.bridge.add_method(
58 "fileDownload", 58 "file_download",
59 ".plugin", 59 ".plugin",
60 in_sign="ssss", 60 in_sign="ssss",
61 out_sign="s", 61 out_sign="s",
62 method=self._fileDownload, 62 method=self._file_download,
63 async_=True, 63 async_=True,
64 ) 64 )
65 host.bridge.addMethod( 65 host.bridge.add_method(
66 "fileDownloadComplete", 66 "file_download_complete",
67 ".plugin", 67 ".plugin",
68 in_sign="ssss", 68 in_sign="ssss",
69 out_sign="s", 69 out_sign="s",
70 method=self._fileDownloadComplete, 70 method=self._file_download_complete,
71 async_=True, 71 async_=True,
72 ) 72 )
73 self._download_callbacks = {} 73 self._download_callbacks = {}
74 self._scheme_callbacks = {} 74 self._scheme_callbacks = {}
75 self.register_scheme('http', self.download_http) 75 self.register_scheme('http', self.download_http)
76 self.register_scheme('https', self.download_http) 76 self.register_scheme('https', self.download_http)
77 77
78 def _fileDownload( 78 def _file_download(
79 self, attachment_s: str, dest_path: str, extra_s: str, profile: str 79 self, attachment_s: str, dest_path: str, extra_s: str, profile: str
80 ) -> defer.Deferred: 80 ) -> defer.Deferred:
81 d = defer.ensureDeferred(self.file_download( 81 d = defer.ensureDeferred(self.file_download(
82 self.host.getClient(profile), 82 self.host.get_client(profile),
83 data_format.deserialise(attachment_s), 83 data_format.deserialise(attachment_s),
84 Path(dest_path), 84 Path(dest_path),
85 data_format.deserialise(extra_s) 85 data_format.deserialise(extra_s)
86 )) 86 ))
87 d.addCallback(lambda ret: data_format.serialise(ret)) 87 d.addCallback(lambda ret: data_format.serialise(ret))
116 ).toXml() 116 ).toXml()
117 } 117 }
118 else: 118 else:
119 return {"progress": progress_id} 119 return {"progress": progress_id}
120 120
121 def _fileDownloadComplete( 121 def _file_download_complete(
122 self, attachment_s: str, dest_path: str, extra_s: str, profile: str 122 self, attachment_s: str, dest_path: str, extra_s: str, profile: str
123 ) -> defer.Deferred: 123 ) -> defer.Deferred:
124 d = defer.ensureDeferred(self.file_download_complete( 124 d = defer.ensureDeferred(self.file_download_complete(
125 self.host.getClient(profile), 125 self.host.get_client(profile),
126 data_format.deserialise(attachment_s), 126 data_format.deserialise(attachment_s),
127 Path(dest_path), 127 Path(dest_path),
128 data_format.deserialise(extra_s) 128 data_format.deserialise(extra_s)
129 )) 129 ))
130 d.addCallback(lambda path: str(path)) 130 d.addCallback(lambda path: str(path))
166 # suffixes, but we still want to handle suffixes like "tar.gz". 166 # suffixes, but we still want to handle suffixes like "tar.gz".
167 stem, *suffixes = filename.rsplit('.', 2) 167 stem, *suffixes = filename.rsplit('.', 2)
168 # we hash the URL to have an unique identifier, and avoid double download 168 # we hash the URL to have an unique identifier, and avoid double download
169 url_hash = hashlib.sha256(uri_parsed.geturl().encode()).hexdigest() 169 url_hash = hashlib.sha256(uri_parsed.geturl().encode()).hexdigest()
170 cache_uid = f"{stem}_{url_hash}" 170 cache_uid = f"{stem}_{url_hash}"
171 cache_data = client.cache.getMetadata(cache_uid) 171 cache_data = client.cache.get_metadata(cache_uid)
172 if cache_data is not None: 172 if cache_data is not None:
173 # file is already in cache, we return it 173 # file is already in cache, we return it
174 download_d = defer.succeed(cache_data['path']) 174 download_d = defer.succeed(cache_data['path'])
175 return '', download_d 175 return '', download_d
176 else: 176 else:
177 # the file is not in cache 177 # the file is not in cache
178 unique_name = '.'.join([cache_uid] + suffixes) 178 unique_name = '.'.join([cache_uid] + suffixes)
179 with client.cache.cacheData( 179 with client.cache.cache_data(
180 "DOWNLOAD", cache_uid, filename=unique_name) as f: 180 "DOWNLOAD", cache_uid, filename=unique_name) as f:
181 # we close the file and only use its name, the file will be opened 181 # we close the file and only use its name, the file will be opened
182 # by the registered callback 182 # by the registered callback
183 dest_path = Path(f.name) 183 dest_path = Path(f.name)
184 184
185 # should we check certificates? 185 # should we check certificates?
186 check_certificate = self.host.memory.getParamA( 186 check_certificate = self.host.memory.param_get_a(
187 "check_certificate", "Connection", profile_key=client.profile) 187 "check_certificate", "Connection", profile_key=client.profile)
188 if not check_certificate: 188 if not check_certificate:
189 extra['ignore_tls_errors'] = True 189 extra['ignore_tls_errors'] = True
190 log.warning( 190 log.warning(
191 _("certificate check disabled for download, this is dangerous!")) 191 _("certificate check disabled for download, this is dangerous!"))
201 except Exception as e: 201 except Exception as e:
202 log.warning(_( 202 log.warning(_(
203 "Can't download URI {uri}: {reason}").format( 203 "Can't download URI {uri}: {reason}").format(
204 uri=uri, reason=e)) 204 uri=uri, reason=e))
205 if cache_uid is not None: 205 if cache_uid is not None:
206 client.cache.removeFromCache(cache_uid) 206 client.cache.remove_from_cache(cache_uid)
207 elif dest_path.exists(): 207 elif dest_path.exists():
208 dest_path.unlink() 208 dest_path.unlink()
209 raise e 209 raise e
210 download_d.addCallback(lambda __: dest_path) 210 download_d.addCallback(lambda __: dest_path)
211 return progress_id, download_d 211 return progress_id, download_d