comparison sat/tools/stream.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 0dd79c6cc1d2
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
35 35
36 log = getLogger(__name__) 36 log = getLogger(__name__)
37 37
38 38
39 class IStreamProducer(interface.Interface): 39 class IStreamProducer(interface.Interface):
40 def startStream(consumer): 40 def start_stream(consumer):
41 """start producing the stream 41 """start producing the stream
42 42
43 @return (D): deferred fired when stream is finished 43 @return (D): deferred fired when stream is finished
44 """ 44 """
45 pass 45 pass
66 """ 66 """
67 @param host: %(doc_host)s 67 @param host: %(doc_host)s
68 @param path(Path, str): path to the file to get or write to 68 @param path(Path, str): path to the file to get or write to
69 @param mode(str): same as for built-in "open" function 69 @param mode(str): same as for built-in "open" function
70 @param uid(unicode, None): unique id identifing this progressing element 70 @param uid(unicode, None): unique id identifing this progressing element
71 This uid will be used with self.host.progressGet 71 This uid will be used with self.host.progress_get
72 will be automaticaly generated if None 72 will be automaticaly generated if None
73 @param size(None, int): size of the file (when known in advance) 73 @param size(None, int): size of the file (when known in advance)
74 @param data_cb(None, callable): method to call on each data read/write 74 @param data_cb(None, callable): method to call on each data read/write
75 can be used to do processing like calculating hash. 75 can be used to do processing like calculating hash.
76 if data_cb return a non None value, it will be used instead of the 76 if data_cb return a non None value, it will be used instead of the
77 data read/to write 77 data read/to write
78 @param auto_end_signals(bool): if True, progressFinished and progressError signals 78 @param auto_end_signals(bool): if True, progress_finished and progress_error signals
79 are automatically sent. 79 are automatically sent.
80 if False, you'll have to call self.progressFinished and self.progressError 80 if False, you'll have to call self.progress_finished and self.progress_error
81 yourself. 81 yourself.
82 progressStarted signal is always sent automatically 82 progress_started signal is always sent automatically
83 @param check_size_with_read(bool): if True, size will be checked using number of 83 @param check_size_with_read(bool): if True, size will be checked using number of
84 bytes read or written. This is useful when data_cb modifiy len of file. 84 bytes read or written. This is useful when data_cb modifiy len of file.
85 @param pre_close_cb: 85 @param pre_close_cb:
86 """ 86 """
87 self.host = host 87 self.host = host
90 self._file = open(path, mode) 90 self._file = open(path, mode)
91 self.size = size 91 self.size = size
92 self.data_cb = data_cb 92 self.data_cb = data_cb
93 self.auto_end_signals = auto_end_signals 93 self.auto_end_signals = auto_end_signals
94 self.pre_close_cb = pre_close_cb 94 self.pre_close_cb = pre_close_cb
95 metadata = self.getProgressMetadata() 95 metadata = self.get_progress_metadata()
96 self.host.registerProgressCb( 96 self.host.register_progress_cb(
97 self.uid, self.getProgress, metadata, profile=client.profile 97 self.uid, self.get_progress, metadata, profile=client.profile
98 ) 98 )
99 self.host.bridge.progressStarted(self.uid, metadata, client.profile) 99 self.host.bridge.progress_started(self.uid, metadata, client.profile)
100 100
101 self._transfer_count = 0 if check_size_with_read else None 101 self._transfer_count = 0 if check_size_with_read else None
102 102
103 @property 103 @property
104 def check_size_with_read(self): 104 def check_size_with_read(self):
109 if value and self._transfer_count is None: 109 if value and self._transfer_count is None:
110 self._transfer_count = 0 110 self._transfer_count = 0
111 else: 111 else:
112 self._transfer_count = None 112 self._transfer_count = None
113 113
114 def checkSize(self): 114 def check_size(self):
115 """Check that current size correspond to given size 115 """Check that current size correspond to given size
116 116
117 must be used when the transfer is supposed to be finished 117 must be used when the transfer is supposed to be finished
118 @return (bool): True if the position is the same as given size 118 @return (bool): True if the position is the same as given size
119 @raise exceptions.NotFound: size has not be specified 119 @raise exceptions.NotFound: size has not be specified
140 return # avoid double close (which is allowed) error 140 return # avoid double close (which is allowed) error
141 if self.pre_close_cb is not None: 141 if self.pre_close_cb is not None:
142 self.pre_close_cb() 142 self.pre_close_cb()
143 if error is None: 143 if error is None:
144 try: 144 try:
145 size_ok = self.checkSize() 145 size_ok = self.check_size()
146 except exceptions.NotFound: 146 except exceptions.NotFound:
147 size_ok = True 147 size_ok = True
148 if not size_ok: 148 if not size_ok:
149 error = "declared and actual size mismatch" 149 error = "declared and actual size mismatch"
150 log.warning(error) 150 log.warning(error)
152 152
153 self._file.close() 153 self._file.close()
154 154
155 if self.auto_end_signals: 155 if self.auto_end_signals:
156 if error is None: 156 if error is None:
157 self.progressFinished(progress_metadata) 157 self.progress_finished(progress_metadata)
158 else: 158 else:
159 assert progress_metadata is None 159 assert progress_metadata is None
160 self.progressError(error) 160 self.progress_error(error)
161 161
162 self.host.removeProgressCb(self.uid, self.profile) 162 self.host.remove_progress_cb(self.uid, self.profile)
163 if error is not None: 163 if error is not None:
164 log.error(f"file {self._file} closed with an error: {error}") 164 log.error(f"file {self._file} closed with an error: {error}")
165 165
166 @property 166 @property
167 def closed(self): 167 def closed(self):
168 return self._file.closed 168 return self._file.closed
169 169
170 def progressFinished(self, metadata=None): 170 def progress_finished(self, metadata=None):
171 if metadata is None: 171 if metadata is None:
172 metadata = {} 172 metadata = {}
173 self.host.bridge.progressFinished(self.uid, metadata, self.profile) 173 self.host.bridge.progress_finished(self.uid, metadata, self.profile)
174 174
175 def progressError(self, error): 175 def progress_error(self, error):
176 self.host.bridge.progressError(self.uid, error, self.profile) 176 self.host.bridge.progress_error(self.uid, error, self.profile)
177 177
178 def flush(self): 178 def flush(self):
179 self._file.flush() 179 self._file.flush()
180 180
181 def write(self, buf): 181 def write(self, buf):
205 205
206 @property 206 @property
207 def mode(self): 207 def mode(self):
208 return self._file.mode 208 return self._file.mode
209 209
210 def getProgressMetadata(self): 210 def get_progress_metadata(self):
211 """Return progression metadata as given to progressStarted 211 """Return progression metadata as given to progress_started
212 212
213 @return (dict): metadata (check bridge for documentation) 213 @return (dict): metadata (check bridge for documentation)
214 """ 214 """
215 metadata = {"type": C.META_TYPE_FILE} 215 metadata = {"type": C.META_TYPE_FILE}
216 216
228 228
229 metadata["name"] = self._file.name 229 metadata["name"] = self._file.name
230 230
231 return metadata 231 return metadata
232 232
233 def getProgress(self, progress_id, profile): 233 def get_progress(self, progress_id, profile):
234 ret = {"position": self._file.tell()} 234 ret = {"position": self._file.tell()}
235 if self.size: 235 if self.size:
236 ret["size"] = self.size 236 ret["size"] = self.size
237 return ret 237 return ret
238 238
250 self.file_obj = SatFile(host, client, path, **kwargs) 250 self.file_obj = SatFile(host, client, path, **kwargs)
251 251
252 def registerProducer(self, producer, streaming): 252 def registerProducer(self, producer, streaming):
253 pass 253 pass
254 254
255 def startStream(self, consumer): 255 def start_stream(self, consumer):
256 return self.beginFileTransfer(self.file_obj, consumer) 256 return self.beginFileTransfer(self.file_obj, consumer)
257 257
258 def write(self, data): 258 def write(self, data):
259 self.file_obj.write(data) 259 self.file_obj.write(data)
260 260