changeset 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 0de5f210fe56
children 4dd07d026214
files src/plugins/plugin_misc_file.py
diffstat 1 files changed, 13 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_file.py	Tue Nov 17 19:48:19 2015 +0100
+++ b/src/plugins/plugin_misc_file.py	Tue Nov 17 19:51:52 2015 +0100
@@ -53,7 +53,7 @@
     """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 @@
             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 @@
 
     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 @@
         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 @@
                 - 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