comparison src/plugins/plugin_xep_0054.py @ 485:ee95ff721b68

plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
author Goffi <goffi@goffi.org>
date Thu, 16 Aug 2012 15:17:16 +0200
parents 2a072735e459
children 65ecbb473cbb
comparison
equal deleted inserted replaced
484:23cbdf0a0777 485:ee95ff721b68
20 """ 20 """
21 21
22 from logging import debug, info, error 22 from logging import debug, info, error
23 from twisted.words.xish import domish 23 from twisted.words.xish import domish
24 from twisted.internet import protocol, defer, threads, reactor 24 from twisted.internet import protocol, defer, threads, reactor
25 from twisted.internet.defer import inlineCallbacks, returnValue
25 from twisted.words.protocols.jabber import client, jid, xmlstream 26 from twisted.words.protocols.jabber import client, jid, xmlstream
26 from twisted.words.protocols.jabber import error as jab_error 27 from twisted.words.protocols.jabber import error as jab_error
27 from twisted.words.protocols.jabber.xmlstream import IQ 28 from twisted.words.protocols.jabber.xmlstream import IQ
28 import os.path 29 import os.path
29 30
126 debug(_("file saved to %s") % hash) 127 debug(_("file saved to %s") % hash)
127 else: 128 else:
128 debug(_("file [%s] already in cache") % hash) 129 debug(_("file [%s] already in cache") % hash)
129 return hash 130 return hash
130 131
131 @defer.deferredGenerator 132 @inlineCallbacks
132 def vCard2Dict(self, vcard, target, profile): 133 def vCard2Dict(self, vcard, target, profile):
133 """Convert a VCard to a dict, and save binaries""" 134 """Convert a VCard to a dict, and save binaries"""
134 debug (_("parsing vcard")) 135 debug (_("parsing vcard"))
135 dictionary = {} 136 dictionary = {}
136 d = defer.Deferred() 137 d = defer.Deferred()
146 elif elem.name == 'EMAIL': 147 elif elem.name == 'EMAIL':
147 dictionary['email'] = unicode(elem) 148 dictionary['email'] = unicode(elem)
148 elif elem.name == 'BDAY': 149 elif elem.name == 'BDAY':
149 dictionary['birthday'] = unicode(elem) 150 dictionary['birthday'] = unicode(elem)
150 elif elem.name == 'PHOTO': 151 elif elem.name == 'PHOTO':
151 d2 = defer.waitForDeferred( 152 dictionary["avatar"] = yield threads.deferToThread(self.save_photo, elem)
152 threads.deferToThread(self.save_photo, elem))
153 yield d2
154 dictionary["avatar"] = d2.getResult()
155 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem 153 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem
156 del dictionary['avatar'] 154 del dictionary['avatar']
157 else: 155 else:
158 self.update_cache(target, 'avatar', dictionary['avatar'], profile) 156 self.update_cache(target, 'avatar', dictionary['avatar'], profile)
159 else: 157 else:
160 info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name) 158 info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
161 159
162 yield dictionary 160 returnValue(dictionary)
163 161
164 def vcard_ok(self, answer, profile): 162 def vcard_ok(self, answer, profile):
165 """Called after the first get IQ""" 163 """Called after the first get IQ"""
166 debug (_("VCard found")) 164 debug (_("VCard found"))
167 165
168 if answer.firstChildElement().name == "vCard": 166 if answer.firstChildElement().name == "vCard":
169 d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]), profile) 167 d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]), profile)
170 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data)) 168 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data))
171 else: 169 else:
172 error (_("FIXME: vCard not found as first child element")) 170 error (_("FIXME: vCard not found as first child element"))
173 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be best 171 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be better
174 172
175 def vcard_err(self, failure): 173 def vcard_err(self, failure):
176 """Called when something is wrong with registration""" 174 """Called when something is wrong with registration"""
177 error (_("Can't find VCard of %s") % failure.value.stanza['from']) 175 error (_("Can't find VCard of %s") % failure.value.stanza['from'])
178 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be best 176 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be better
179 177
180 def getCard(self, target, profile_key='@DEFAULT@'): 178 def getCard(self, target, profile_key='@DEFAULT@'):
181 """Ask server for VCard 179 """Ask server for VCard
182 @param target: jid from which we want the VCard 180 @param target: jid from which we want the VCard
183 @result: id to retrieve the profile""" 181 @result: id to retrieve the profile"""