Fix pep8 support in src/plugins.
This commit is contained in:
parent
e51a8a650b
commit
0dca274360
1 changed files with 1052 additions and 1079 deletions
|
|
@ -44,19 +44,20 @@ IBB_OPEN = IQ_SET + '/open[@xmlns="' + NS_IBB + '"]'
|
|||
IBB_CLOSE = IQ_SET + '/close[@xmlns="' + NS_IBB + '" and @sid="%s"]'
|
||||
IBB_IQ_DATA = IQ_SET + '/data[@xmlns="' + NS_IBB + '" and @sid="%s"]'
|
||||
IBB_MESSAGE_DATA = MESSAGE + '/data[@xmlns="' + NS_IBB + '" and @sid="%s"]'
|
||||
TIMEOUT = 60 #timeout for workflow
|
||||
TIMEOUT = 60 # timeout for workflow
|
||||
BLOCK_SIZE = 4096
|
||||
|
||||
PLUGIN_INFO = {
|
||||
"name": "In-Band Bytestream Plugin",
|
||||
"import_name": "XEP-0047",
|
||||
"type": "XEP",
|
||||
"protocols": ["XEP-0047"],
|
||||
"main": "XEP_0047",
|
||||
"handler": "yes",
|
||||
"description": _("""Implementation of In-Band Bytestreams""")
|
||||
"name": "In-Band Bytestream Plugin",
|
||||
"import_name": "XEP-0047",
|
||||
"type": "XEP",
|
||||
"protocols": ["XEP-0047"],
|
||||
"main": "XEP_0047",
|
||||
"handler": "yes",
|
||||
"description": _("""Implementation of In-Band Bytestreams""")
|
||||
}
|
||||
|
||||
|
||||
class XEP_0047(object):
|
||||
NAMESPACE = NS_IBB
|
||||
|
||||
|
|
@ -71,7 +72,7 @@ class XEP_0047(object):
|
|||
client = self.host.getClient(profile)
|
||||
if not client:
|
||||
raise ProfileNotInCacheError
|
||||
client.xep_0047_current_stream = {} #key: stream_id, value: data(dict)
|
||||
client.xep_0047_current_stream = {} # key: stream_id, value: data(dict)
|
||||
|
||||
def _timeOut(self, sid, profile):
|
||||
"""Delecte current_stream id, called after timeout
|
||||
|
|
@ -87,14 +88,14 @@ class XEP_0047(object):
|
|||
if not client:
|
||||
warning(_("Client no more in cache"))
|
||||
return
|
||||
if not client.xep_0047_current_stream.has_key(sid):
|
||||
if sid not in client.xep_0047_current_stream:
|
||||
warning(_("kill id called on a non existant id"))
|
||||
return
|
||||
if client.xep_0047_current_stream[sid].has_key("observer_cb"):
|
||||
if "observer_cb" in client.xep_0047_current_stream[sid]:
|
||||
client.xmlstream.removeObserver(client.xep_0047_current_stream[sid]["event_data"], client.xep_0047_current_stream[sid]["observer_cb"])
|
||||
if client.xep_0047_current_stream[sid]['timer'].active():
|
||||
client.xep_0047_current_stream[sid]['timer'].cancel()
|
||||
if client.xep_0047_current_stream[sid].has_key("size"):
|
||||
if "size" in client.xep_0047_current_stream[sid]:
|
||||
self.host.removeProgressCB(sid, profile)
|
||||
|
||||
file_obj = client.xep_0047_current_stream[sid]['file_obj']
|
||||
|
|
@ -145,7 +146,7 @@ class XEP_0047(object):
|
|||
|
||||
def streamOpening(self, IQ, profile):
|
||||
debug(_("IBB stream opening"))
|
||||
IQ.handled=True
|
||||
IQ.handled = True
|
||||
client = self.host.getClient(profile)
|
||||
if not client:
|
||||
raise ProfileNotInCacheError
|
||||
|
|
@ -153,7 +154,7 @@ class XEP_0047(object):
|
|||
block_size = open_elt.getAttribute('block-size')
|
||||
sid = open_elt.getAttribute('sid')
|
||||
stanza = open_elt.getAttribute('stanza', 'iq')
|
||||
if not sid or not block_size or int(block_size)>65535:
|
||||
if not sid or not block_size or int(block_size) > 65535:
|
||||
warning(_("malformed IBB transfer: %s" % IQ['id']))
|
||||
self.sendNotAcceptableError(IQ['id'], IQ['from'], client.xmlstream)
|
||||
return
|
||||
|
|
@ -173,12 +174,12 @@ class XEP_0047(object):
|
|||
client.xep_0047_current_stream[sid]["timer"].reset(TIMEOUT)
|
||||
|
||||
#we save the xmlstream, events and observer data to allow observer removal
|
||||
client.xep_0047_current_stream[sid]["event_data"] = event_data = (IBB_MESSAGE_DATA if stanza=='message' else IBB_IQ_DATA) % sid
|
||||
client.xep_0047_current_stream[sid]["observer_cb"] = observer_cb = self.messageData if stanza=='message' else self.iqData
|
||||
client.xep_0047_current_stream[sid]["event_data"] = event_data = (IBB_MESSAGE_DATA if stanza == 'message' else IBB_IQ_DATA) % sid
|
||||
client.xep_0047_current_stream[sid]["observer_cb"] = observer_cb = self.messageData if stanza == 'message' else self.iqData
|
||||
event_close = IBB_CLOSE % sid
|
||||
#we now set the stream observer to look after data packet
|
||||
client.xmlstream.addObserver(event_data, observer_cb, profile = profile)
|
||||
client.xmlstream.addOnetimeObserver(event_close, self.streamClosing, profile = profile)
|
||||
client.xmlstream.addObserver(event_data, observer_cb, profile=profile)
|
||||
client.xmlstream.addOnetimeObserver(event_close, self.streamClosing, profile=profile)
|
||||
#finally, we send the accept stanza
|
||||
result = domish.Element((None, 'iq'))
|
||||
result['type'] = 'result'
|
||||
|
|
@ -187,7 +188,7 @@ class XEP_0047(object):
|
|||
client.xmlstream.send(result)
|
||||
|
||||
def streamClosing(self, IQ, profile):
|
||||
IQ.handled=True
|
||||
IQ.handled = True
|
||||
client = self.host.getClient(profile)
|
||||
if not client:
|
||||
raise ProfileNotInCacheError
|
||||
|
|
@ -202,7 +203,7 @@ class XEP_0047(object):
|
|||
self._killId(sid, success=True, profile=profile)
|
||||
|
||||
def iqData(self, IQ, profile):
|
||||
IQ.handled=True
|
||||
IQ.handled = True
|
||||
client = self.host.getClient(profile)
|
||||
if not client:
|
||||
raise ProfileNotInCacheError
|
||||
|
|
@ -219,7 +220,7 @@ class XEP_0047(object):
|
|||
|
||||
def messageData(self, message_elt, profile):
|
||||
data_elt = message_elt.firstChildElement()
|
||||
sid = message_elt.getAttribute('id','')
|
||||
sid = message_elt.getAttribute('id', '')
|
||||
self._manageDataElt(message_elt, 'message', sid, jid.JID(message_elt['from']), profile)
|
||||
|
||||
def _manageDataElt(self, data_elt, stanza, sid, stanza_from_jid, profile):
|
||||
|
|
@ -239,14 +240,14 @@ class XEP_0047(object):
|
|||
|
||||
if stanza_from_jid != from_jid:
|
||||
warning(_("sended jid inconsistency (man in the middle attack attempt ?)"))
|
||||
if stanza=='iq':
|
||||
if stanza == 'iq':
|
||||
self.sendNotAcceptableError(sid, from_jid, client.xmlstream)
|
||||
return False
|
||||
|
||||
client.xep_0047_current_stream[sid]["seq"]+=1
|
||||
if int(data_elt.getAttribute("seq",-1)) != client.xep_0047_current_stream[sid]["seq"]:
|
||||
client.xep_0047_current_stream[sid]["seq"] += 1
|
||||
if int(data_elt.getAttribute("seq", -1)) != client.xep_0047_current_stream[sid]["seq"]:
|
||||
warning(_("Sequence error"))
|
||||
if stanza=='iq':
|
||||
if stanza == 'iq':
|
||||
self.sendNotAcceptableError(data_elt["id"], from_jid, client.xmlstream)
|
||||
return False
|
||||
|
||||
|
|
@ -259,7 +260,7 @@ class XEP_0047(object):
|
|||
except TypeError:
|
||||
#The base64 data is invalid
|
||||
warning(_("Invalid base64 data"))
|
||||
if stanza=='iq':
|
||||
if stanza == 'iq':
|
||||
self.sendNotAcceptableError(data_elt["id"], from_jid, client.xmlstream)
|
||||
return False
|
||||
return True
|
||||
|
|
@ -275,10 +276,10 @@ class XEP_0047(object):
|
|||
result['to'] = to_jid
|
||||
error_el = result.addElement('error')
|
||||
error_el['type'] = 'cancel'
|
||||
error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','not-acceptable'))
|
||||
error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas', 'not-acceptable'))
|
||||
xmlstream.send(result)
|
||||
|
||||
def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size = None, profile=None):
|
||||
def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size=None, profile=None):
|
||||
"""Launch the stream workflow
|
||||
@param file_obj: file_obj to send
|
||||
@param to_jid: JID of the recipient
|
||||
|
|
@ -290,7 +291,7 @@ class XEP_0047(object):
|
|||
client = self.host.getClient(profile)
|
||||
if not client:
|
||||
raise ProfileNotInCacheError
|
||||
if length != None:
|
||||
if length is not None:
|
||||
error(_('stream length not managed yet'))
|
||||
return
|
||||
data = client.xep_0047_current_stream[sid] = {}
|
||||
|
|
@ -303,10 +304,10 @@ class XEP_0047(object):
|
|||
if size:
|
||||
data["size"] = size
|
||||
self.host.registerProgressCB(sid, self.getProgress, profile)
|
||||
iq_elt = jabber_client.IQ(client.xmlstream,'set')
|
||||
iq_elt = jabber_client.IQ(client.xmlstream, 'set')
|
||||
iq_elt['from'] = client.jid.full()
|
||||
iq_elt['to'] = to_jid.full()
|
||||
open_elt = iq_elt.addElement('open',NS_IBB)
|
||||
open_elt = iq_elt.addElement('open', NS_IBB)
|
||||
open_elt['block-size'] = str(BLOCK_SIZE)
|
||||
open_elt['sid'] = sid
|
||||
open_elt['stanza'] = 'iq'
|
||||
|
|
@ -329,18 +330,18 @@ class XEP_0047(object):
|
|||
|
||||
buffer = data["file_obj"].read(data["block_size"])
|
||||
if buffer:
|
||||
next_iq_elt = jabber_client.IQ(client.xmlstream,'set')
|
||||
next_iq_elt = jabber_client.IQ(client.xmlstream, 'set')
|
||||
next_iq_elt['to'] = data["to"].full()
|
||||
data_elt = next_iq_elt.addElement('data', NS_IBB)
|
||||
data_elt['seq'] = str(seq)
|
||||
data_elt['sid'] = sid
|
||||
data_elt.addContent(base64.b64encode(buffer))
|
||||
next_iq_elt.addCallback(self.iqResult, sid, seq+1, length, profile)
|
||||
next_iq_elt.addCallback(self.iqResult, sid, seq + 1, length, profile)
|
||||
next_iq_elt.send()
|
||||
else:
|
||||
self.terminateStream(sid, profile=profile)
|
||||
|
||||
def terminateStream(self, sid, failure_reason = None, profile=None):
|
||||
def terminateStream(self, sid, failure_reason=None, profile=None):
|
||||
"""Terminate the stream session
|
||||
@param to_jid: recipient
|
||||
@param sid: Session id
|
||||
|
|
@ -353,9 +354,9 @@ class XEP_0047(object):
|
|||
if not client:
|
||||
raise ProfileNotInCacheError
|
||||
data = client.xep_0047_current_stream[sid]
|
||||
iq_elt = jabber_client.IQ(client.xmlstream,'set')
|
||||
iq_elt = jabber_client.IQ(client.xmlstream, 'set')
|
||||
iq_elt['to'] = data["to"].full()
|
||||
close_elt = iq_elt.addElement('close',NS_IBB)
|
||||
close_elt = iq_elt.addElement('close', NS_IBB)
|
||||
close_elt['sid'] = sid
|
||||
iq_elt.send()
|
||||
self.host.removeProgressCB(sid, profile)
|
||||
|
|
@ -364,14 +365,15 @@ class XEP_0047(object):
|
|||
else:
|
||||
self._killId(sid, True, profile=profile)
|
||||
|
||||
|
||||
class XEP_0047_handler(XMPPHandler):
|
||||
implements(iwokkel.IDisco)
|
||||
|
||||
def __init__(self,parent):
|
||||
def __init__(self, parent):
|
||||
self.plugin_parent = parent
|
||||
|
||||
def connectionInitialized(self):
|
||||
self.xmlstream.addObserver(IBB_OPEN, self.plugin_parent.streamOpening, profile = self.parent.profile)
|
||||
self.xmlstream.addObserver(IBB_OPEN, self.plugin_parent.streamOpening, profile=self.parent.profile)
|
||||
|
||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
||||
return [disco.DiscoFeature(NS_IBB)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue