comparison src/plugins/plugin_misc_file.py @ 1619:3ec7511dbf28

plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
author Goffi <goffi@goffi.org>
date Tue, 17 Nov 2015 19:51:52 +0100
parents 4741e2f5eed2
children 63cef4dbf2a4
comparison
equal deleted inserted replaced
1618:0de5f210fe56 1619:3ec7511dbf28
51 51
52 class SatFile(object): 52 class SatFile(object):
53 """A file-like object to have high level files manipulation""" 53 """A file-like object to have high level files manipulation"""
54 # TODO: manage "with" statement 54 # TODO: manage "with" statement
55 55
56 def __init__(self, host, path, mode='r', uid=None, size=None, profile=C.PROF_KEY_NONE): 56 def __init__(self, host, path, mode='rb', uid=None, size=None, data_cb=None, profile=C.PROF_KEY_NONE):
57 """ 57 """
58 @param host: %(doc_host)s 58 @param host: %(doc_host)s
59 @param path(str): path of the file to get 59 @param path(str): path of the file to get
60 @param mode(str): same as for built-in "open" function 60 @param mode(str): same as for built-in "open" function
61 @param uid(unicode, None): unique id identifing this progressing element 61 @param uid(unicode, None): unique id identifing this progressing element
62 This uid will be used with self.host.progressGet 62 This uid will be used with self.host.progressGet
63 will be automaticaly generated if None 63 will be automaticaly generated if None
64 @param size(None, int): size of the file 64 @param size(None, int): size of the file
65 @param data_cb(None, callable): method to call on each data read/write
66 mainly useful to do things like calculating hash
65 """ 67 """
66 self.host = host 68 self.host = host
67 self.uid = uid or unicode(uuid.uuid4()) 69 self.uid = uid or unicode(uuid.uuid4())
68 self._file = open(path, mode) 70 self._file = open(path, mode)
69 self.size = size 71 self.size = size
72 self.data_cb = data_cb
70 self.profile = profile 73 self.profile = profile
71 self.host.registerProgressCb(self.uid, self.getProgress, profile) 74 self.host.registerProgressCb(self.uid, self.getProgress, profile)
72 self.host.bridge.progressStarted(self.uid, self.profile) 75 self.host.bridge.progressStarted(self.uid, self.profile)
73 76
74 def close(self): 77 def close(self):
83 def flush(self): 86 def flush(self):
84 self._file.flush() 87 self._file.flush()
85 88
86 def write(self, buf): 89 def write(self, buf):
87 self._file.write(buf) 90 self._file.write(buf)
91 if self.data_cb is not None:
92 return self.data_cb(buf)
88 93
89 def read(self, size=-1): 94 def read(self, size=-1):
90 read = self._file.read(size) 95 read = self._file.read(size)
96 if self.data_cb is not None and read:
97 self.data_cb(read)
91 return read 98 return read
92 99
93 def seek(self, offset, whence=os.SEEK_SET): 100 def seek(self, offset, whence=os.SEEK_SET):
94 self._file.seek(offset, whence) 101 self._file.seek(offset, whence)
95 102
167 def _openFileWrite(self, file_path, transfer_data, file_data, profile): 174 def _openFileWrite(self, file_path, transfer_data, file_data, profile):
168 assert 'file_obj' not in transfer_data 175 assert 'file_obj' not in transfer_data
169 transfer_data['file_obj'] = SatFile( 176 transfer_data['file_obj'] = SatFile(
170 self.host, 177 self.host,
171 file_path, 178 file_path,
172 'w', 179 'wb',
173 uid=file_data[PROGRESS_ID_KEY], 180 uid=file_data[PROGRESS_ID_KEY],
174 size=file_data['size'], 181 size=file_data['size'],
182 data_cb = file_data.get('data_cb'),
175 profile=profile, 183 profile=profile,
176 ) 184 )
177 185
178 def _gotConfirmation(self, data, peer_jid, transfer_data, file_data, profile): 186 def _gotConfirmation(self, data, peer_jid, transfer_data, file_data, profile):
179 """Called when the permission and dest path have been received 187 """Called when the permission and dest path have been received
230 It MUST contain the following keys: 238 It MUST contain the following keys:
231 - peer_jid (jid.JID): other peer jid 239 - peer_jid (jid.JID): other peer jid
232 - name (unicode): name of the file to trasnsfer 240 - name (unicode): name of the file to trasnsfer
233 the name must not be empty or contain a "/" character 241 the name must not be empty or contain a "/" character
234 - size (int): size of the file 242 - size (int): size of the file
243 - desc (unicode): description of the file
235 - progress_id (unicode): id to use for progression 244 - progress_id (unicode): id to use for progression
236 It may content the key used in CONFIRM constant
237 It *MUST NOT* contain the "peer" key 245 It *MUST NOT* contain the "peer" key
246 It may contain:
247 - data_cb (callable): method called on each data read/write
238 "file_path" will be added to this dict once destination selected 248 "file_path" will be added to this dict once destination selected
239 "size_human" will also be added with human readable file size 249 "size_human" will also be added with human readable file size
240 @param profile: %(doc_profile)s 250 @param profile: %(doc_profile)s
241 return (defer.Deferred): True if transfer is accepted 251 return (defer.Deferred): True if transfer is accepted
242 """ 252 """