comparison sat/tools/stream.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
30 30
31 log = getLogger(__name__) 31 log = getLogger(__name__)
32 32
33 33
34 class IStreamProducer(interface.Interface): 34 class IStreamProducer(interface.Interface):
35
36 def startStream(consumer): 35 def startStream(consumer):
37 """start producing the stream 36 """start producing the stream
38 37
39 @return (D): deferred fired when stream is finished 38 @return (D): deferred fired when stream is finished
40 """ 39 """
41 40
42 41
43 class SatFile(object): 42 class SatFile(object):
44 """A file-like object to have high level files manipulation""" 43 """A file-like object to have high level files manipulation"""
44
45 # TODO: manage "with" statement 45 # TODO: manage "with" statement
46 46
47 def __init__(self, host, client, path, mode='rb', uid=None, size=None, data_cb=None, auto_end_signals=True): 47 def __init__(
48 self,
49 host,
50 client,
51 path,
52 mode="rb",
53 uid=None,
54 size=None,
55 data_cb=None,
56 auto_end_signals=True,
57 ):
48 """ 58 """
49 @param host: %(doc_host)s 59 @param host: %(doc_host)s
50 @param path(str): path of the file to get 60 @param path(str): path of the file to get
51 @param mode(str): same as for built-in "open" function 61 @param mode(str): same as for built-in "open" function
52 @param uid(unicode, None): unique id identifing this progressing element 62 @param uid(unicode, None): unique id identifing this progressing element
65 self._file = open(path, mode) 75 self._file = open(path, mode)
66 self.size = size 76 self.size = size
67 self.data_cb = data_cb 77 self.data_cb = data_cb
68 self.auto_end_signals = auto_end_signals 78 self.auto_end_signals = auto_end_signals
69 metadata = self.getProgressMetadata() 79 metadata = self.getProgressMetadata()
70 self.host.registerProgressCb(self.uid, self.getProgress, metadata, profile=client.profile) 80 self.host.registerProgressCb(
81 self.uid, self.getProgress, metadata, profile=client.profile
82 )
71 self.host.bridge.progressStarted(self.uid, metadata, client.profile) 83 self.host.bridge.progressStarted(self.uid, metadata, client.profile)
72 84
73 def checkSize(self): 85 def checkSize(self):
74 """Check that current size correspond to given size 86 """Check that current size correspond to given size
75 87
89 @param error(None, unicode): set to an error message if progress was not successful 101 @param error(None, unicode): set to an error message if progress was not successful
90 mutually exclusive with progress_metadata 102 mutually exclusive with progress_metadata
91 error can happen even if error is None, if current size differ from given size 103 error can happen even if error is None, if current size differ from given size
92 """ 104 """
93 if self._file.closed: 105 if self._file.closed:
94 return # avoid double close (which is allowed) error 106 return # avoid double close (which is allowed) error
95 if error is None: 107 if error is None:
96 try: 108 try:
97 size_ok = self.checkSize() 109 size_ok = self.checkSize()
98 except exceptions.NotFound: 110 except exceptions.NotFound:
99 size_ok = True 111 size_ok = True
100 if not size_ok: 112 if not size_ok:
101 error = u'declared and actual size mismatch' 113 error = u"declared and actual size mismatch"
102 log.warning(error) 114 log.warning(error)
103 progress_metadata = None 115 progress_metadata = None
104 116
105 self._file.close() 117 self._file.close()
106 118
147 def getProgressMetadata(self): 159 def getProgressMetadata(self):
148 """Return progression metadata as given to progressStarted 160 """Return progression metadata as given to progressStarted
149 161
150 @return (dict): metadata (check bridge for documentation) 162 @return (dict): metadata (check bridge for documentation)
151 """ 163 """
152 metadata = {'type': C.META_TYPE_FILE} 164 metadata = {"type": C.META_TYPE_FILE}
153 165
154 mode = self._file.mode 166 mode = self._file.mode
155 if '+' in mode: 167 if "+" in mode:
156 pass # we have no direction in read/write modes 168 pass # we have no direction in read/write modes
157 elif mode in ('r', 'rb'): 169 elif mode in ("r", "rb"):
158 metadata['direction'] = 'out' 170 metadata["direction"] = "out"
159 elif mode in ('w', 'wb'): 171 elif mode in ("w", "wb"):
160 metadata['direction'] = 'in' 172 metadata["direction"] = "in"
161 elif 'U' in mode: 173 elif "U" in mode:
162 metadata['direction'] = 'out' 174 metadata["direction"] = "out"
163 else: 175 else:
164 raise exceptions.InternalError 176 raise exceptions.InternalError
165 177
166 metadata['name'] = self._file.name 178 metadata["name"] = self._file.name
167 179
168 return metadata 180 return metadata
169 181
170 def getProgress(self, progress_id, profile): 182 def getProgress(self, progress_id, profile):
171 ret = {'position': self._file.tell()} 183 ret = {"position": self._file.tell()}
172 if self.size: 184 if self.size:
173 ret['size'] = self.size 185 ret["size"] = self.size
174 return ret 186 return ret
175 187
176 188
177 @interface.implementer(IStreamProducer) 189 @interface.implementer(IStreamProducer)
178 @interface.implementer(interfaces.IConsumer) 190 @interface.implementer(interfaces.IConsumer)
179 class FileStreamObject(basic.FileSender): 191 class FileStreamObject(basic.FileSender):
180
181 def __init__(self, host, client, path, **kwargs): 192 def __init__(self, host, client, path, **kwargs):
182 """ 193 """
183 194
184 A SatFile will be created and put in self.file_obj 195 A SatFile will be created and put in self.file_obj
185 @param path(unicode): path to the file 196 @param path(unicode): path to the file