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

@ -31,7 +31,7 @@ from zope.interface import implements
from wokkel import disco, iwokkel
from base64 import b64decode,b64encode
from base64 import b64decode, b64encode
from hashlib import sha1
from sat.core import exceptions
from sat.memory.persistent import PersistentDict
@ -47,23 +47,24 @@ AVATAR_PATH = "avatars"
IQ_GET = '/iq[@type="get"]'
NS_VCARD = 'vcard-temp'
VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' #TODO: manage requests
VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' # TODO: manage requests
PRESENCE = '/presence'
NS_VCARD_UPDATE = 'vcard-temp:x:update'
VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]'
VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]'
PLUGIN_INFO = {
"name": "XEP 0054 Plugin",
"import_name": "XEP-0054",
"type": "XEP",
"protocols": ["XEP-0054", "XEP-0153"],
"dependencies": [],
"main": "XEP_0054",
"handler": "yes",
"description": _("""Implementation of vcard-temp""")
"name": "XEP 0054 Plugin",
"import_name": "XEP-0054",
"type": "XEP",
"protocols": ["XEP-0054", "XEP-0153"],
"dependencies": [],
"main": "XEP_0054",
"handler": "yes",
"description": _("""Implementation of vcard-temp""")
}
class XEP_0054(object):
#TODO: - check that nickname is ok
# - refactor the code/better use of Wokkel
@ -76,10 +77,10 @@ class XEP_0054(object):
if not os.path.exists(self.avatar_path):
os.makedirs(self.avatar_path)
self.avatars_cache = PersistentDict(NS_VCARD)
self.avatars_cache.load() #FIXME: resulting deferred must be correctly managed
self.avatars_cache.load() # FIXME: resulting deferred must be correctly managed
host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self.getCard)
host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile)
host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async = True)
host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async=True)
host.trigger.add("presence_available", self.presenceTrigger)
def getHandler(self, profile):
@ -144,9 +145,9 @@ class XEP_0054(object):
debug(_('Decoding binary'))
decoded = b64decode(str(elem))
image_hash = sha1(decoded).hexdigest()
filename = self.avatar_path+'/'+image_hash
filename = self.avatar_path + '/' + image_hash
if not os.path.exists(filename):
with open(filename,'wb') as file:
with open(filename, 'wb') as file:
file.write(decoded)
debug(_("file saved to %s") % image_hash)
else:
@ -156,7 +157,7 @@ class XEP_0054(object):
@inlineCallbacks
def vCard2Dict(self, vcard, target, profile):
"""Convert a VCard to a dict, and save binaries"""
debug (_("parsing vcard"))
debug(_("parsing vcard"))
dictionary = {}
for elem in vcard.elements():
@ -173,18 +174,18 @@ class XEP_0054(object):
dictionary['birthday'] = unicode(elem)
elif elem.name == 'PHOTO':
dictionary["avatar"] = yield threads.deferToThread(self.save_photo, elem)
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']
else:
self.update_cache(target, 'avatar', dictionary['avatar'], profile)
else:
info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
info(_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
returnValue(dictionary)
def vcard_ok(self, answer, profile):
"""Called after the first get IQ"""
debug (_("VCard found"))
debug(_("VCard found"))
if answer.firstChildElement().name == "vCard":
_jid, steam = self.host.getJidNStream(profile)
@ -195,13 +196,13 @@ class XEP_0054(object):
d = self.vCard2Dict(answer.firstChildElement(), from_jid, profile)
d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data, profile))
else:
error (_("FIXME: vCard not found as first child element"))
self.host.bridge.actionResult("SUPPRESS", answer['id'], {}, profile) #FIXME: maybe an error message would be better
error(_("FIXME: vCard not found as first child element"))
self.host.bridge.actionResult("SUPPRESS", answer['id'], {}, profile) # FIXME: maybe an error message would be better
def vcard_err(self, failure, profile):
"""Called when something is wrong with registration"""
error (_("Can't find VCard of %s") % failure.value.stanza['from'])
self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) #FIXME: maybe an error message would be better
error(_("Can't find VCard of %s") % failure.value.stanza['from'])
self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) # FIXME: maybe an error message would be better
def getCard(self, target_s, profile_key='@DEFAULT@'):
"""Ask server for VCard
@ -209,13 +210,13 @@ class XEP_0054(object):
@result: id to retrieve the profile"""
current_jid, xmlstream = self.host.getJidNStream(profile_key)
if not xmlstream:
error (_('Asking vcard for a non-existant or not connected profile'))
error(_('Asking vcard for a non-existant or not connected profile'))
return ""
profile = self.host.memory.getProfileName(profile_key)
to_jid = jid.JID(target_s)
debug(_("Asking for %s's VCard") % to_jid.userhost())
reg_request=IQ(xmlstream,'get')
reg_request["from"]=current_jid.full()
reg_request = IQ(xmlstream, 'get')
reg_request["from"] = current_jid.full()
reg_request["to"] = to_jid.userhost()
reg_request.addElement('vCard', NS_VCARD)
reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, callbackArgs=[profile], errbackArgs=[profile])
@ -226,9 +227,9 @@ class XEP_0054(object):
@param hash: SHA1 hash
@return full_path
"""
filename = self.avatar_path+'/'+avatar_hash
filename = self.avatar_path + '/' + avatar_hash
if not os.path.exists(filename):
error (_("Asking for an uncached avatar [%s]") % avatar_hash)
error(_("Asking for an uncached avatar [%s]") % avatar_hash)
return ""
return filename
@ -259,13 +260,13 @@ class XEP_0054(object):
if not client:
raise exceptions.NotConnectedProfileError(_('Trying to set avatar for a non-existant or not connected profile'))
vcard_set = IQ(client.xmlstream,'set')
vcard_set = IQ(client.xmlstream, 'set')
d = threads.deferToThread(self._buildSetAvatar, vcard_set, filepath)
def elementBuilt(result):
"""Called once the image is at the right size/format, and the vcard set element is build"""
set_avatar_elt, img_hash = result
self.avatars_cache[client.jid.userhost()] = img_hash # we need to update the hash, so we can send a new presence
self.avatars_cache[client.jid.userhost()] = img_hash # we need to update the hash, so we can send a new presence
# element with the right hash
return set_avatar_elt.send().addCallback(lambda ignore: client.presence.available())
@ -296,7 +297,7 @@ class XEP_0054_handler(XMPPHandler):
"""
from_jid = jid.JID(presence['from'])
#FIXME: wokkel's data_form should be used here
x_elem = filter (lambda x:x.name == "x", presence.elements())[0] #We only want the "x" element
x_elem = filter(lambda x: x.name == "x", presence.elements())[0] # We only want the "x" element
for elem in x_elem.elements():
if elem.name == 'photo':
_hash = str(elem)