plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks

This commit is contained in:
Goffi 2012-08-16 15:17:16 +02:00
commit f6cdc6383d

View file

@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from logging import debug, info, error from logging import debug, info, error
from twisted.words.xish import domish from twisted.words.xish import domish
from twisted.internet import protocol, defer, threads, reactor from twisted.internet import protocol, defer, threads, reactor
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.words.protocols.jabber import client, jid, xmlstream from twisted.words.protocols.jabber import client, jid, xmlstream
from twisted.words.protocols.jabber import error as jab_error from twisted.words.protocols.jabber import error as jab_error
from twisted.words.protocols.jabber.xmlstream import IQ from twisted.words.protocols.jabber.xmlstream import IQ
@ -128,7 +129,7 @@ class XEP_0054():
debug(_("file [%s] already in cache") % hash) debug(_("file [%s] already in cache") % hash)
return hash return hash
@defer.deferredGenerator @inlineCallbacks
def vCard2Dict(self, vcard, target, profile): def vCard2Dict(self, vcard, target, profile):
"""Convert a VCard to a dict, and save binaries""" """Convert a VCard to a dict, and save binaries"""
debug (_("parsing vcard")) debug (_("parsing vcard"))
@ -148,10 +149,7 @@ class XEP_0054():
elif elem.name == 'BDAY': elif elem.name == 'BDAY':
dictionary['birthday'] = unicode(elem) dictionary['birthday'] = unicode(elem)
elif elem.name == 'PHOTO': elif elem.name == 'PHOTO':
d2 = defer.waitForDeferred( dictionary["avatar"] = yield threads.deferToThread(self.save_photo, elem)
threads.deferToThread(self.save_photo, elem))
yield d2
dictionary["avatar"] = d2.getResult()
if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem
del dictionary['avatar'] del dictionary['avatar']
else: else:
@ -159,7 +157,7 @@ class XEP_0054():
else: else:
info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name) info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
yield dictionary returnValue(dictionary)
def vcard_ok(self, answer, profile): def vcard_ok(self, answer, profile):
"""Called after the first get IQ""" """Called after the first get IQ"""
@ -170,12 +168,12 @@ class XEP_0054():
d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data)) d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data))
else: else:
error (_("FIXME: vCard not found as first child element")) error (_("FIXME: vCard not found as first child element"))
self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be best self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be better
def vcard_err(self, failure): def vcard_err(self, failure):
"""Called when something is wrong with registration""" """Called when something is wrong with registration"""
error (_("Can't find VCard of %s") % failure.value.stanza['from']) error (_("Can't find VCard of %s") % failure.value.stanza['from'])
self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be best self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be better
def getCard(self, target, profile_key='@DEFAULT@'): def getCard(self, target, profile_key='@DEFAULT@'):
"""Ask server for VCard """Ask server for VCard