Fix pep8 support in src/plugins.

This commit is contained in:
Emmanuel Gil Peyrot 2013-01-18 17:55:35 +01:00
commit 0dca274360

View file

@ -23,7 +23,7 @@ from logging import debug, info, warning, error
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
@ -34,16 +34,17 @@ PROFILE_NAME = "pipe-transfer"
PROFILE = "http://jabber.org/protocol/si/profile/" + PROFILE_NAME
PLUGIN_INFO = {
"name": "Pipe Plugin",
"import_name": "EXP-PIPE",
"type": "EXP",
"protocols": ["EXP-PIPE"],
"dependencies": ["XEP-0020", "XEP-0095", "XEP-0065", "XEP-0047"],
"main": "Exp_Pipe",
"handler": "no",
"description": _("""Implementation of SI Pipe Transfer""")
"name": "Pipe Plugin",
"import_name": "EXP-PIPE",
"type": "EXP",
"protocols": ["EXP-PIPE"],
"dependencies": ["XEP-0020", "XEP-0095", "XEP-0065", "XEP-0047"],
"main": "Exp_Pipe",
"handler": "no",
"description": _("""Implementation of SI Pipe Transfer""")
}
class Exp_Pipe(object):
"""This is a modified version of XEP-0096 to work with named pipes instead of files"""
@ -51,13 +52,13 @@ class Exp_Pipe(object):
info(_("Plugin Pipe 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("pipeOut", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.pipeOut)
def profileConnected(self, profile):
client = self.host.getClient(profile)
client._pipe_waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout,
client._pipe_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 +79,7 @@ class Exp_Pipe(object):
@param si_mime_type: Mime type of the pipe (or default "application/octet-stream" if unknown)
@param si_el: domish.Element of the request
@param profile: %(doc_profile)s"""
info (_("EXP-PIPE file transfer requested"))
info(_("EXP-PIPE file transfer requested"))
debug(si_el.toXml())
client = self.host.getClient(profile)
if not client:
@ -95,7 +96,7 @@ class Exp_Pipe(object):
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)
@ -110,12 +111,11 @@ class Exp_Pipe(object):
return
#if we are here, the transfer can start, we just need user's agreement
data={ "id": iq_id, "from":from_jid }
data = {"id": iq_id, "from": from_jid}
client._pipe_waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id), stream_method, [], profile]
self.host.askConfirmation(si_id, "PIPE_TRANSFER", data, self.confirmationCB, profile)
def confirmationCB(self, sid, accepted, frontend_data, profile):
"""Called on confirmation answer
@param sid: file transfer session id
@ -146,13 +146,13 @@ class Exp_Pipe(object):
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")))
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._pipe_waiting_for_approval[sid])
def _transferSucceeded(self, sid, file_obj, stream_method, profile):
@ -173,8 +173,8 @@ class Exp_Pipe(object):
if not client:
raise ProfileNotInCacheError
data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid]
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid,
's_method': stream_method })
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % {'id': sid,
's_method': stream_method})
filepath = file_obj.name
file_obj.close()
#TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session
@ -211,15 +211,15 @@ class Exp_Pipe(object):
return
if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
#fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
#file_obj = os.fdopen(fd, 'r')
file_obj = open(filepath, 'r') #XXX: we have to be sure that filepath is well opened, as reading can block it
self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile)
#fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
#file_obj = os.fdopen(fd, 'r')
file_obj = open(filepath, 'r') # XXX: we have to be sure that filepath is well opened, as reading can block it
self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile)
elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
#fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
#file_obj = os.fdopen(fd, 'r')
file_obj = open(filepath, 'r') #XXX: we have to be sure that filepath is well opened, as reading can block it
self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile)
#fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
#file_obj = os.fdopen(fd, 'r')
file_obj = open(filepath, 'r') # XXX: we have to be sure that filepath is well opened, as reading can block it
self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile)
else:
warning(_("Invalid stream method received"))
@ -242,7 +242,7 @@ class Exp_Pipe(object):
pipe_elt = domish.Element((PROFILE, 'pipe'))
pipe_transfer_elts.append(pipe_elt)
sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, pipe_transfer_elts, profile_key = profile)
sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, pipe_transfer_elts, profile_key=profile)
offer.addCallback(self.pipeCb, filepath, sid, profile)
return sid
@ -252,5 +252,4 @@ class Exp_Pipe(object):
def sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
file_obj.close()
warning(_('Transfer %(id)s failed with stream method %(s_method)s %(profile)s') % { 'id': sid, "s_method": stream_method, "profile": profile })
warning(_('Transfer %(id)s failed with stream method %(s_method)s %(profile)s') % {'id': sid, "s_method": stream_method, "profile": profile})