diff src/plugins/plugin_xep_0054.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents c6d8fc63b1db
children 7b4600ad73ad
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0054.py	Sat Apr 19 16:48:26 2014 +0200
+++ b/src/plugins/plugin_xep_0054.py	Sat Apr 19 19:19:19 2014 +0200
@@ -20,7 +20,8 @@
 
 from sat.core.i18n import _
 from sat.core.constants import Const as C
-from logging import debug, info, error
+from sat.core.log import getLogger
+log = getLogger(__name__)
 from twisted.internet import threads, defer
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber.xmlstream import IQ
@@ -72,7 +73,7 @@
     #      - get missing values
 
     def __init__(self, host):
-        info(_("Plugin XEP_0054 initialization"))
+        log.info(_("Plugin XEP_0054 initialization"))
         self.host = host
         self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH)
         if not os.path.exists(self.avatar_path):
@@ -141,24 +142,24 @@
         """Parse a <PHOTO> elem and save the picture"""
         for elem in photo_xml.elements():
             if elem.name == 'TYPE':
-                info(_('Photo of type [%s] found') % str(elem))
+                log.info(_('Photo of type [%s] found') % str(elem))
             if elem.name == 'BINVAL':
-                debug(_('Decoding binary'))
+                log.debug(_('Decoding binary'))
                 decoded = b64decode(str(elem))
                 image_hash = sha1(decoded).hexdigest()
                 filename = self.avatar_path + '/' + image_hash
                 if not os.path.exists(filename):
                     with open(filename, 'wb') as file:
                         file.write(decoded)
-                    debug(_("file saved to %s") % image_hash)
+                    log.debug(_("file saved to %s") % image_hash)
                 else:
-                    debug(_("file [%s] already in cache") % image_hash)
+                    log.debug(_("file [%s] already in cache") % image_hash)
                 return image_hash
 
     @defer.inlineCallbacks
     def vCard2Dict(self, vcard, target, profile):
         """Convert a VCard to a dict, and save binaries"""
-        debug(_("parsing vcard"))
+        log.debug(_("parsing vcard"))
         dictionary = {}
 
         for elem in vcard.elements():
@@ -180,13 +181,13 @@
                 else:
                     self.update_cache(target, 'avatar', dictionary['avatar'], profile)
             else:
-                info(_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
+                log.info(_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
 
         defer.returnValue(dictionary)
 
     def vcard_ok(self, answer, profile):
         """Called after the first get IQ"""
-        debug(_("VCard found"))
+        log.debug(_("VCard found"))
 
         if answer.firstChildElement().name == "vCard":
             _jid, steam = self.host.getJidNStream(profile)
@@ -197,13 +198,13 @@
             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"))
+            log.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"""
         if failure.value.stanza.hasAttribute("from"):
-            error(_("Can't find VCard of %s") % failure.value.stanza['from'])
+            log.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=C.PROF_KEY_NONE):
@@ -212,11 +213,11 @@
         @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'))
+            log.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())
+        log.debug(_("Asking for %s's VCard") % to_jid.userhost())
         reg_request = IQ(xmlstream, 'get')
         reg_request["from"] = current_jid.full()
         reg_request["to"] = to_jid.userhost()
@@ -231,7 +232,7 @@
         """
         filename = self.avatar_path + '/' + avatar_hash
         if not os.path.exists(filename):
-            error(_("Asking for an uncached avatar [%s]") % avatar_hash)
+            log.error(_("Asking for an uncached avatar [%s]") % avatar_hash)
             return ""
         return filename
 
@@ -303,5 +304,5 @@
                 _hash = str(elem)
                 old_avatar = self.plugin_parent.get_cache(from_jid, 'avatar', self.parent.profile)
                 if not old_avatar or old_avatar != _hash:
-                    debug(_('New avatar found, requesting vcard'))
+                    log.debug(_('New avatar found, requesting vcard'))
                     self.plugin_parent.getCard(from_jid.userhost(), self.parent.profile)