plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write

This commit is contained in:
Goffi 2015-11-17 19:51:52 +01:00
commit ed8d3db308

View file

@ -53,7 +53,7 @@ class SatFile(object):
"""A file-like object to have high level files manipulation"""
# TODO: manage "with" statement
def __init__(self, host, path, mode='r', uid=None, size=None, profile=C.PROF_KEY_NONE):
def __init__(self, host, path, mode='rb', uid=None, size=None, data_cb=None, profile=C.PROF_KEY_NONE):
"""
@param host: %(doc_host)s
@param path(str): path of the file to get
@ -62,11 +62,14 @@ class SatFile(object):
This uid will be used with self.host.progressGet
will be automaticaly generated if None
@param size(None, int): size of the file
@param data_cb(None, callable): method to call on each data read/write
mainly useful to do things like calculating hash
"""
self.host = host
self.uid = uid or unicode(uuid.uuid4())
self._file = open(path, mode)
self.size = size
self.data_cb = data_cb
self.profile = profile
self.host.registerProgressCb(self.uid, self.getProgress, profile)
self.host.bridge.progressStarted(self.uid, self.profile)
@ -85,9 +88,13 @@ class SatFile(object):
def write(self, buf):
self._file.write(buf)
if self.data_cb is not None:
return self.data_cb(buf)
def read(self, size=-1):
read = self._file.read(size)
if self.data_cb is not None and read:
self.data_cb(read)
return read
def seek(self, offset, whence=os.SEEK_SET):
@ -169,9 +176,10 @@ class FilePlugin(object):
transfer_data['file_obj'] = SatFile(
self.host,
file_path,
'w',
'wb',
uid=file_data[PROGRESS_ID_KEY],
size=file_data['size'],
data_cb = file_data.get('data_cb'),
profile=profile,
)
@ -232,9 +240,11 @@ class FilePlugin(object):
- name (unicode): name of the file to trasnsfer
the name must not be empty or contain a "/" character
- size (int): size of the file
- desc (unicode): description of the file
- progress_id (unicode): id to use for progression
It may content the key used in CONFIRM constant
It *MUST NOT* contain the "peer" key
It may contain:
- data_cb (callable): method called on each data read/write
"file_path" will be added to this dict once destination selected
"size_human" will also be added with human readable file size
@param profile: %(doc_profile)s