comparison src/plugins/plugin_exp_pipe.py @ 941:c6d8fc63b1db

core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
author Goffi <goffi@goffi.org>
date Fri, 28 Mar 2014 18:07:02 +0100
parents 1a759096ccbd
children 301b342c697a
comparison
equal deleted inserted replaced
940:92e41e7c7e00 941:c6d8fc63b1db
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from logging import debug, info, warning, error 22 from logging import debug, info, warning, error
23 from twisted.words.xish import domish 23 from twisted.words.xish import domish
24 from twisted.words.protocols.jabber import jid 24 from twisted.words.protocols.jabber import jid
25 from twisted.words.protocols.jabber import error as jab_error 25 from twisted.words.protocols.jabber import error as jab_error
26 import os
27 from twisted.internet import reactor 26 from twisted.internet import reactor
28 from sat.core.exceptions import ProfileNotInCacheError
29 27
30 from wokkel import data_form 28 from wokkel import data_form
31 29
32 IQ_SET = '/iq[@type="set"]' 30 IQ_SET = '/iq[@type="set"]'
33 PROFILE_NAME = "pipe-transfer" 31 PROFILE_NAME = "pipe-transfer"
80 @param si_el: domish.Element of the request 78 @param si_el: domish.Element of the request
81 @param profile: %(doc_profile)s""" 79 @param profile: %(doc_profile)s"""
82 info(_("EXP-PIPE file transfer requested")) 80 info(_("EXP-PIPE file transfer requested"))
83 debug(si_el.toXml()) 81 debug(si_el.toXml())
84 client = self.host.getClient(profile) 82 client = self.host.getClient(profile)
85 if not client:
86 raise ProfileNotInCacheError
87 pipe_elts = filter(lambda elt: elt.name == 'pipe', si_el.elements()) 83 pipe_elts = filter(lambda elt: elt.name == 'pipe', si_el.elements())
88 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el) 84 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el)
89 85
90 if not pipe_elts: 86 if not pipe_elts:
91 warning(_("No pipe element found")) 87 warning(_("No pipe element found"))
92 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile) 88 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
93 return 89 return
94 90
95 if feature_elts: 91 if feature_elts:
96 feature_el = feature_elts[0] 92 feature_el = feature_elts[0]
97 form = data_form.Form.fromElement(feature_el.firstChildElement()) 93 data_form.Form.fromElement(feature_el.firstChildElement())
98 try: 94 try:
99 stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method', self.managed_stream_m) 95 stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method', self.managed_stream_m)
100 except KeyError: 96 except KeyError:
101 warning(_("No stream method found")) 97 warning(_("No stream method found"))
102 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile) 98 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
120 """Called on confirmation answer 116 """Called on confirmation answer
121 @param sid: file transfer session id 117 @param sid: file transfer session id
122 @param accepted: True if file transfer is accepted 118 @param accepted: True if file transfer is accepted
123 @param frontend_data: data sent by frontend""" 119 @param frontend_data: data sent by frontend"""
124 client = self.host.getClient(profile) 120 client = self.host.getClient(profile)
125 if not client:
126 raise ProfileNotInCacheError
127 data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid] 121 data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid]
128 if accepted: 122 if accepted:
129 if timeout.active(): 123 if timeout.active():
130 timeout.cancel() 124 timeout.cancel()
131 try: 125 try:
157 151
158 def _transferSucceeded(self, sid, file_obj, stream_method, profile): 152 def _transferSucceeded(self, sid, file_obj, stream_method, profile):
159 """Called by the stream method when transfer successfuly finished 153 """Called by the stream method when transfer successfuly finished
160 @param id: stream id""" 154 @param id: stream id"""
161 client = self.host.getClient(profile) 155 client = self.host.getClient(profile)
162 if not client:
163 raise ProfileNotInCacheError
164 file_obj.close() 156 file_obj.close()
165 info(_('Transfer %s successfuly finished') % sid) 157 info(_('Transfer %s successfuly finished') % sid)
166 del(client._pipe_waiting_for_approval[sid]) 158 del(client._pipe_waiting_for_approval[sid])
167 159
168 def _transferFailed(self, sid, file_obj, stream_method, reason, profile): 160 def _transferFailed(self, sid, file_obj, stream_method, reason, profile):
169 """Called when something went wrong with the transfer 161 """Called when something went wrong with the transfer
170 @param id: stream id 162 @param id: stream id
171 @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR""" 163 @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR"""
172 client = self.host.getClient(profile) 164 client = self.host.getClient(profile)
173 if not client:
174 raise ProfileNotInCacheError
175 data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid] 165 data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid]
176 warning(_('Transfer %(id)s failed with stream method %(s_method)s') % {'id': sid, 166 warning(_('Transfer %(id)s failed with stream method %(s_method)s') % {'id': sid,
177 's_method': stream_method}) 167 's_method': stream_method})
178 filepath = file_obj.name 168 # filepath = file_obj.name
179 file_obj.close() 169 file_obj.close()
180 #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 170 #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
181 warning(_("All stream methods failed, can't transfer the file")) 171 warning(_("All stream methods failed, can't transfer the file"))
182 del(client._pipe_waiting_for_approval[sid]) 172 del(client._pipe_waiting_for_approval[sid])
183 173