diff sat/plugins/plugin_misc_file.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
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_file.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat/plugins/plugin_misc_file.py	Wed Jun 27 20:14:46 2018 +0200
@@ -20,6 +20,7 @@
 from sat.core.i18n import _, D_
 from sat.core.constants import Const as C
 from sat.core.log import getLogger
+
 log = getLogger(__name__)
 from sat.core import exceptions
 from sat.tools import xml_tools
@@ -37,38 +38,67 @@
     C.PI_MODES: C.PLUG_MODE_BOTH,
     C.PI_MAIN: "FilePlugin",
     C.PI_HANDLER: "no",
-    C.PI_DESCRIPTION: _("""File Tansfer Management:
-This plugin manage the various ways of sending a file, and choose the best one.""")
+    C.PI_DESCRIPTION: _(
+        """File Tansfer Management:
+This plugin manage the various ways of sending a file, and choose the best one."""
+    ),
 }
 
 
-SENDING = D_(u'Please select a file to send to {peer}')
-SENDING_TITLE = D_(u'File sending')
-CONFIRM = D_(u'{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?')
-CONFIRM_TITLE = D_(u'Confirm file transfer')
-CONFIRM_OVERWRITE = D_(u'File {} already exists, are you sure you want to overwrite ?')
-CONFIRM_OVERWRITE_TITLE = D_(u'File exists')
+SENDING = D_(u"Please select a file to send to {peer}")
+SENDING_TITLE = D_(u"File sending")
+CONFIRM = D_(
+    u'{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?'
+)
+CONFIRM_TITLE = D_(u"Confirm file transfer")
+CONFIRM_OVERWRITE = D_(u"File {} already exists, are you sure you want to overwrite ?")
+CONFIRM_OVERWRITE_TITLE = D_(u"File exists")
 SECURITY_LIMIT = 30
 
-PROGRESS_ID_KEY = 'progress_id'
+PROGRESS_ID_KEY = "progress_id"
 
 
 class FilePlugin(object):
-    File=stream.SatFile
+    File = stream.SatFile
 
     def __init__(self, host):
         log.info(_("plugin File initialization"))
         self.host = host
-        host.bridge.addMethod("fileSend", ".plugin", in_sign='ssssa{ss}s', out_sign='a{ss}', method=self._fileSend, async=True)
+        host.bridge.addMethod(
+            "fileSend",
+            ".plugin",
+            in_sign="ssssa{ss}s",
+            out_sign="a{ss}",
+            method=self._fileSend,
+            async=True,
+        )
         self._file_callbacks = []
-        host.importMenu((D_("Action"), D_("send file")), self._fileSendMenu, security_limit=10, help_string=D_("Send a file"), type_=C.MENU_SINGLE)
+        host.importMenu(
+            (D_("Action"), D_("send file")),
+            self._fileSendMenu,
+            security_limit=10,
+            help_string=D_("Send a file"),
+            type_=C.MENU_SINGLE,
+        )
 
-    def _fileSend(self, peer_jid_s, filepath, name="", file_desc="", extra=None, profile=C.PROF_KEY_NONE):
+    def _fileSend(
+        self,
+        peer_jid_s,
+        filepath,
+        name="",
+        file_desc="",
+        extra=None,
+        profile=C.PROF_KEY_NONE,
+    ):
         client = self.host.getClient(profile)
-        return self.fileSend(client, jid.JID(peer_jid_s), filepath, name or None, file_desc or None, extra)
+        return self.fileSend(
+            client, jid.JID(peer_jid_s), filepath, name or None, file_desc or None, extra
+        )
 
     @defer.inlineCallbacks
-    def fileSend(self, client, peer_jid, filepath, filename=None, file_desc=None, extra=None):
+    def fileSend(
+        self, client, peer_jid, filepath, filename=None, file_desc=None, extra=None
+    ):
         """Send a file using best available method
 
         @param peer_jid(jid.JID): jid of the destinee
@@ -81,22 +111,34 @@
         if not os.path.isfile(filepath):
             raise exceptions.DataError(u"The given path doesn't link to a file")
         if not filename:
-            filename = os.path.basename(filepath) or '_'
+            filename = os.path.basename(filepath) or "_"
         for namespace, callback, priority, method_name in self._file_callbacks:
             has_feature = yield self.host.hasFeature(client, namespace, peer_jid)
             if has_feature:
-                log.info(u"{name} method will be used to send the file".format(name=method_name))
-                progress_id = yield callback(client, peer_jid, filepath, filename, file_desc, extra)
-                defer.returnValue({'progress': progress_id})
+                log.info(
+                    u"{name} method will be used to send the file".format(
+                        name=method_name
+                    )
+                )
+                progress_id = yield callback(
+                    client, peer_jid, filepath, filename, file_desc, extra
+                )
+                defer.returnValue({"progress": progress_id})
         msg = u"Can't find any method to send file to {jid}".format(jid=peer_jid.full())
         log.warning(msg)
-        defer.returnValue({'xmlui': xml_tools.note(u"Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING).toXml()})
+        defer.returnValue(
+            {
+                "xmlui": xml_tools.note(
+                    u"Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING
+                ).toXml()
+            }
+        )
 
     def _onFileChoosed(self, client, peer_jid, data):
         cancelled = C.bool(data.get("cancelled", C.BOOL_FALSE))
         if cancelled:
             return
-        path=data['path']
+        path = data["path"]
         return self.fileSend(client, peer_jid, path)
 
     def _fileSendMenu(self, data, profile):
@@ -105,20 +147,28 @@
         @param profile: %(doc_profile)s
         """
         try:
-            jid_ = jid.JID(data['jid'])
+            jid_ = jid.JID(data["jid"])
         except RuntimeError:
             raise exceptions.DataError(_("Invalid JID"))
 
-        file_choosed_id = self.host.registerCallback(lambda data, profile: self._onFileChoosed(self.host.getClient(profile), jid_, data), with_data=True, one_shot=True)
+        file_choosed_id = self.host.registerCallback(
+            lambda data, profile: self._onFileChoosed(
+                self.host.getClient(profile), jid_, data
+            ),
+            with_data=True,
+            one_shot=True,
+        )
         xml_ui = xml_tools.XMLUI(
             C.XMLUI_DIALOG,
-            dialog_opt = {
+            dialog_opt={
                 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_FILE,
-                C.XMLUI_DATA_MESS: _(SENDING).format(peer=jid_.full())},
-            title = _(SENDING_TITLE),
-            submit_id = file_choosed_id)
+                C.XMLUI_DATA_MESS: _(SENDING).format(peer=jid_.full()),
+            },
+            title=_(SENDING_TITLE),
+            submit_id=file_choosed_id,
+        )
 
-        return {'xmlui': xml_ui.toXml()}
+        return {"xmlui": xml_ui.toXml()}
 
     def register(self, namespace, callback, priority=0, method_name=None):
         """Register a fileSending method
@@ -130,8 +180,12 @@
         """
         for data in self._file_callbacks:
             if namespace == data[0]:
-                raise exceptions.ConflictError(u'A method with this namespace is already registered')
-        self._file_callbacks.append((namespace, callback, priority, method_name or namespace))
+                raise exceptions.ConflictError(
+                    u"A method with this namespace is already registered"
+                )
+        self._file_callbacks.append(
+            (namespace, callback, priority, method_name or namespace)
+        )
         self._file_callbacks.sort(key=lambda data: data[2], reverse=True)
 
     def unregister(self, namespace):
@@ -148,29 +202,31 @@
         """create SatFile or FileStremaObject for the requested file and fill suitable data
         """
         if stream_object:
-            assert 'stream_object' not in transfer_data
-            transfer_data['stream_object'] = stream.FileStreamObject(
+            assert "stream_object" not in transfer_data
+            transfer_data["stream_object"] = stream.FileStreamObject(
                 self.host,
                 client,
                 file_path,
-                mode='wb',
+                mode="wb",
                 uid=file_data[PROGRESS_ID_KEY],
-                size=file_data['size'],
-                data_cb = file_data.get('data_cb'),
-                )
+                size=file_data["size"],
+                data_cb=file_data.get("data_cb"),
+            )
         else:
-            assert 'file_obj' not in transfer_data
-            transfer_data['file_obj'] = stream.SatFile(
+            assert "file_obj" not in transfer_data
+            transfer_data["file_obj"] = stream.SatFile(
                 self.host,
                 client,
                 file_path,
-                mode='wb',
+                mode="wb",
                 uid=file_data[PROGRESS_ID_KEY],
-                size=file_data['size'],
-                data_cb = file_data.get('data_cb'),
-                )
+                size=file_data["size"],
+                data_cb=file_data.get("data_cb"),
+            )
 
-    def _gotConfirmation(self, data, client, peer_jid, transfer_data, file_data, stream_object):
+    def _gotConfirmation(
+        self, data, client, peer_jid, transfer_data, file_data, stream_object
+    ):
         """Called when the permission and dest path have been received
 
         @param peer_jid(jid.JID): jid of the file sender
@@ -181,17 +237,20 @@
             False if user wants to cancel
             if file exists ask confirmation and call again self._getDestDir if needed
         """
-        if data.get('cancelled', False):
+        if data.get("cancelled", False):
             return False
-        path = data['path']
-        file_data['file_path'] = file_path = os.path.join(path, file_data['name'])
-        log.debug(u'destination file path set to {}'.format(file_path))
+        path = data["path"]
+        file_data["file_path"] = file_path = os.path.join(path, file_data["name"])
+        log.debug(u"destination file path set to {}".format(file_path))
 
         # we manage case where file already exists
         if os.path.exists(file_path):
+
             def check_overwrite(overwrite):
                 if overwrite:
-                    self.openFileWrite(client, file_path, transfer_data, file_data, stream_object)
+                    self.openFileWrite(
+                        client, file_path, transfer_data, file_data, stream_object
+                    )
                     return True
                 else:
                     return self.getDestDir(client, peer_jid, transfer_data, file_data)
@@ -200,12 +259,14 @@
                 self.host,
                 _(CONFIRM_OVERWRITE).format(file_path),
                 _(CONFIRM_OVERWRITE_TITLE),
-                action_extra={'meta_from_jid': peer_jid.full(),
-                              'meta_type': C.META_TYPE_OVERWRITE,
-                              'meta_progress_id': file_data[PROGRESS_ID_KEY]
-                             },
+                action_extra={
+                    "meta_from_jid": peer_jid.full(),
+                    "meta_type": C.META_TYPE_OVERWRITE,
+                    "meta_progress_id": file_data[PROGRESS_ID_KEY],
+                },
                 security_limit=SECURITY_LIMIT,
-                profile=client.profile)
+                profile=client.profile,
+            )
             exists_d.addCallback(check_overwrite)
             return exists_d
 
@@ -239,24 +300,38 @@
             a stream.FileStreamObject will be used
         return (defer.Deferred): True if transfer is accepted
         """
-        cont,ret_value = self.host.trigger.returnPoint("FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object)
+        cont, ret_value = self.host.trigger.returnPoint(
+            "FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object
+        )
         if not cont:
             return ret_value
-        filename = file_data['name']
-        assert filename and not '/' in filename
+        filename = file_data["name"]
+        assert filename and not "/" in filename
         assert PROGRESS_ID_KEY in file_data
         # human readable size
-        file_data['size_human'] = u'{:.6n} Mio'.format(float(file_data['size'])/(1024**2))
-        d = xml_tools.deferDialog(self.host,
+        file_data["size_human"] = u"{:.6n} Mio".format(
+            float(file_data["size"]) / (1024 ** 2)
+        )
+        d = xml_tools.deferDialog(
+            self.host,
             _(CONFIRM).format(peer=peer_jid.full(), **file_data),
             _(CONFIRM_TITLE),
             type_=C.XMLUI_DIALOG_FILE,
             options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
-            action_extra={'meta_from_jid': peer_jid.full(),
-                          'meta_type': C.META_TYPE_FILE,
-                          'meta_progress_id': file_data[PROGRESS_ID_KEY]
-                         },
+            action_extra={
+                "meta_from_jid": peer_jid.full(),
+                "meta_type": C.META_TYPE_FILE,
+                "meta_progress_id": file_data[PROGRESS_ID_KEY],
+            },
             security_limit=SECURITY_LIMIT,
-            profile=client.profile)
-        d.addCallback(self._gotConfirmation, client, peer_jid, transfer_data, file_data, stream_object)
+            profile=client.profile,
+        )
+        d.addCallback(
+            self._gotConfirmation,
+            client,
+            peer_jid,
+            transfer_data,
+            file_data,
+            stream_object,
+        )
         return d