prefs: apply flake8 suggestions
[git-cola.git] / cola / gravatar.py
blob44b017c2062c530b2b2567fdc31dfe97b77f3cad
1 from __future__ import division, absolute_import, unicode_literals
2 import time
3 import hashlib
5 from cola import sipcompat
6 sipcompat.initialize()
8 from PyQt4 import QtGui
9 from PyQt4 import QtCore
10 from PyQt4 import QtNetwork
11 from PyQt4.QtCore import SIGNAL
13 from cola import core
14 from cola import icons
15 from cola.compat import ustr, parse
16 from cola.widgets import defs
19 class Gravatar(object):
21 @staticmethod
22 def url_for_email(email, imgsize):
23 email_hash = core.decode(hashlib.md5(core.encode(email)).hexdigest())
24 # Python2.6 requires byte strings for urllib2.quote() so we have
25 # to force
26 default_url = 'https://git-cola.github.io/images/git-64x64.jpg'
27 encoded_url = parse.quote(core.encode(default_url), core.encode(''))
28 query = '?s=%d&d=%s' % (imgsize, core.decode(encoded_url))
29 url = 'https://gravatar.com/avatar/' + email_hash + query
30 return url
33 class GravatarLabel(QtGui.QLabel):
35 def __init__(self, parent=None):
36 QtGui.QLabel.__init__(self, parent)
38 self.email = None
39 self.response = None
40 self.timeout = 0
41 self.imgsize = defs.medium_icon
42 self.pixmaps = {}
44 self.network = QtNetwork.QNetworkAccessManager()
45 self.connect(self.network,
46 SIGNAL('finished(QNetworkReply*)'),
47 self.network_finished)
49 def set_email(self, email):
50 if email in self.pixmaps:
51 self.setPixmap(self.pixmaps[email])
52 return
53 if (self.timeout > 0 and
54 (int(time.time()) - self.timeout) < (5 * 60)):
55 self.set_pixmap_from_response()
56 return
57 if email == self.email and self.response is not None:
58 self.set_pixmap_from_response()
59 return
60 self.email = email
61 self.request(email)
63 def request(self, email):
64 url = Gravatar.url_for_email(email, self.imgsize)
65 self.network.get(QtNetwork.QNetworkRequest(QtCore.QUrl(url)))
67 def default_pixmap_as_bytes(self):
68 xres = self.imgsize
69 pixmap = icons.cola().pixmap(xres)
70 byte_array = QtCore.QByteArray()
71 buf = QtCore.QBuffer(byte_array)
72 buf.open(QtCore.QIODevice.WriteOnly)
73 pixmap.save(buf, 'PNG')
74 buf.close()
75 return byte_array
77 def network_finished(self, reply):
78 email = self.email
80 header = QtCore.QByteArray('Location')
81 location = ustr(reply.rawHeader(header)).strip()
82 if location:
83 request_location = Gravatar.url_for_email(self.email, self.imgsize)
84 relocated = location != request_location
85 else:
86 relocated = False
88 if reply.error() == QtNetwork.QNetworkReply.NoError:
89 if relocated:
90 # We could do get_url(parse.unquote(location)) to
91 # download the default image.
92 # Save bandwidth by using a pixmap.
93 self.response = self.default_pixmap_as_bytes()
94 else:
95 self.response = reply.readAll()
96 self.timeout = 0
97 else:
98 self.response = self.default_pixmap_as_bytes()
99 self.timeout = int(time.time())
101 pixmap = self.set_pixmap_from_response()
103 # If the email has not changed (e.g. no other requests)
104 # then we know that this pixmap corresponds to this specific
105 # email address. We can't blindly trust self.email else
106 # we may add cache entries for thee wrong email address.
107 url = Gravatar.url_for_email(email, self.imgsize)
108 if url == reply.url().toString():
109 self.pixmaps[email] = pixmap
111 def set_pixmap_from_response(self):
112 pixmap = QtGui.QPixmap()
113 pixmap.loadFromData(self.response)
114 self.setPixmap(pixmap)
115 return pixmap