diff src/plugins/plugin_xep_0096.py @ 594:e629371a28d3

Fix pep8 support in src/plugins.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 84a6e83157c2
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0096.py	Mon Jan 21 00:59:50 2013 +0100
+++ b/src/plugins/plugin_xep_0096.py	Fri Jan 18 17:55:35 2013 +0100
@@ -23,11 +23,10 @@
 from twisted.words.xish import domish
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber import error as jab_error
-import os, os.path
+import os
 from twisted.internet import reactor
 from sat.core.exceptions import ProfileNotInCacheError
 
-
 from wokkel import data_form
 
 IQ_SET = '/iq[@type="set"]'
@@ -35,29 +34,30 @@
 PROFILE = "http://jabber.org/protocol/si/profile/" + PROFILE_NAME
 
 PLUGIN_INFO = {
-"name": "XEP 0096 Plugin",
-"import_name": "XEP-0096",
-"type": "XEP",
-"protocols": ["XEP-0096"],
-"dependencies": ["XEP-0020", "XEP-0095", "XEP-0065", "XEP-0047"],
-"main": "XEP_0096",
-"handler": "no",
-"description": _("""Implementation of SI File Transfer""")
+    "name": "XEP 0096 Plugin",
+    "import_name": "XEP-0096",
+    "type": "XEP",
+    "protocols": ["XEP-0096"],
+    "dependencies": ["XEP-0020", "XEP-0095", "XEP-0065", "XEP-0047"],
+    "main": "XEP_0096",
+    "handler": "no",
+    "description": _("""Implementation of SI File Transfer""")
 }
 
+
 class XEP_0096(object):
 
     def __init__(self, host):
         info(_("Plugin XEP_0096 initialization"))
         self.host = host
         self.managed_stream_m = [self.host.plugins["XEP-0065"].NAMESPACE,
-                                 self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed
+                                 self.host.plugins["XEP-0047"].NAMESPACE]  # Stream methods managed
         self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest)
         host.bridge.addMethod("sendFile", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.sendFile)
 
     def profileConnected(self, profile):
         client = self.host.getClient(profile)
-        client._xep_0096_waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout,
+        client._xep_0096_waiting_for_approval = {}  # key = id, value = [transfer data, IdelayedCall Reactor timeout,
                                         # current stream method, [failed stream methods], profile]
 
     def _kill_id(self, approval_id, profile):
@@ -78,7 +78,7 @@
         @param si_mime_type: Mime type of the file (or default "application/octet-stream" if unknown)
         @param si_el: domish.Element of the request
         @param profile: %(doc_profile)s"""
-        info (_("XEP-0096 file transfer requested"))
+        info(_("XEP-0096 file transfer requested"))
         debug(si_el.toXml())
         client = self.host.getClient(profile)
         if not client:
@@ -98,7 +98,7 @@
             file_size = file_el["size"]
             file_date = file_el.getAttribute("date", "")
             file_hash = file_el.getAttribute("hash", "")
-            info (_("File proposed: name=[%(name)s] size=%(size)s") % {'name':filename, 'size':file_size})
+            info(_("File proposed: name=[%(name)s] size=%(size)s") % {'name': filename, 'size': file_size})
             for file_child_el in file_el.elements():
                 if file_child_el.name == "desc":
                     file_desc = unicode(file_child_el)
@@ -113,7 +113,7 @@
             feature_el = feature_elts[0]
             form = data_form.Form.fromElement(feature_el.firstChildElement())
             try:
-                stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method',self.managed_stream_m)
+                stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method', self.managed_stream_m)
             except KeyError:
                 warning(_("No stream method found"))
                 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
@@ -128,13 +128,12 @@
             return
 
         #if we are here, the transfer can start, we just need user's agreement
-        data={ "filename":filename, "id": iq_id, "from":from_jid, "size":file_size, "date":file_date, "hash":file_hash, "desc":file_desc, "can_range": str(can_range) }
+        data = {"filename": filename, "id": iq_id, "from": from_jid, "size": file_size, "date": file_date, "hash": file_hash, "desc": file_desc, "can_range": str(can_range)}
         client._xep_0096_waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id, profile), stream_method, []]
 
         self.host.askConfirmation(si_id, "FILE_TRANSFER", data, self.confirmationCB, profile)
 
-
-    def _getFileObject(self, dest_path, can_range = False):
+    def _getFileObject(self, dest_path, can_range=False):
         """Open file, put file pointer to the end if the file if needed
         @param dest_path: path of the destination file
         @param can_range: True if the file pointer can be moved
@@ -175,7 +174,7 @@
                 return
 
             #we can send the iq result
-            feature_elt = self.host.plugins["XEP-0020"].chooseOption({'stream-method':stream_method})
+            feature_elt = self.host.plugins["XEP-0020"].chooseOption({'stream-method': stream_method})
             misc_elts = []
             misc_elts.append(domish.Element((PROFILE, "file")))
             if can_range:
@@ -185,8 +184,8 @@
                 misc_elts.append(range_elt)
             self.host.plugins["XEP-0095"].acceptStream(data["id"], data['from'], feature_elt, misc_elts, profile)
         else:
-            debug (_("Transfer [%s] refused"), sid)
-            self.host.plugins["XEP-0095"].sendRejectedError (data["id"], data['from'], profile=profile)
+            debug(_("Transfer [%s] refused"), sid)
+            self.host.plugins["XEP-0095"].sendRejectedError(data["id"], data['from'], profile=profile)
             del(client._xep_0096_waiting_for_approval[sid])
 
     def _transferSucceeded(self, sid, file_obj, stream_method, profile):
@@ -207,9 +206,10 @@
         if not client:
             raise ProfileNotInCacheError
         data, timeout, stream_method, failed_methods = client._xep_0096_waiting_for_approval[sid]
-        warning(_('Transfer %(id)s failed with stream method %(s_method)s: %(reason)s') % { 'id': sid,
-                                                                                 's_method': stream_method,
-                                                                                 'reason': reason})
+        warning(_('Transfer %(id)s failed with stream method %(s_method)s: %(reason)s') % {
+            'id': sid,
+            's_method': stream_method,
+            'reason': reason})
         filepath = file_obj.name
         file_obj.close()
         os.remove(filepath)
@@ -255,15 +255,15 @@
             range_length = range_elt.getAttribute("length")
 
         if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
-           file_obj = open(filepath, 'r')
-           if range_offset:
-               file_obj.seek(range_offset)
-           self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
+            file_obj = open(filepath, 'r')
+            if range_offset:
+                file_obj.seek(range_offset)
+            self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
         elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
-           file_obj = open(filepath, 'r')
-           if range_offset:
-               file_obj.seek(range_offset)
-           self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
+            file_obj = open(filepath, 'r')
+            if range_offset:
+                file_obj.seek(range_offset)
+            self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, range_length, self.sendSuccessCb, self.sendFailureCb, size, profile)
         else:
             warning(_("Invalid stream method received"))
 
@@ -286,13 +286,13 @@
 
         statinfo = os.stat(filepath)
         file_elt = domish.Element((PROFILE, 'file'))
-        file_elt['name']=os.path.basename(filepath)
-        size = file_elt['size']=str(statinfo.st_size)
+        file_elt['name'] = os.path.basename(filepath)
+        size = file_elt['size'] = str(statinfo.st_size)
         file_transfer_elts.append(file_elt)
 
-        file_transfer_elts.append(domish.Element((None,'range')))
+        file_transfer_elts.append(domish.Element((None, 'range')))
 
-        sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, file_transfer_elts, profile_key = profile)
+        sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, file_transfer_elts, profile_key=profile)
         offer.addCallback(self.fileCb, filepath, sid, size, profile)
         return sid
 
@@ -302,4 +302,4 @@
 
     def sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
         file_obj.close()
-        warning(_('Transfer %(id)s failed with stream method %(s_method)s: %(reason)s [%(profile)s') % { 'id': sid, "s_method": stream_method, 'reason': reason, 'profile': profile })
+        warning(_('Transfer %(id)s failed with stream method %(s_method)s: %(reason)s [%(profile)s') % {'id': sid, "s_method": stream_method, 'reason': reason, 'profile': profile})