bzr branch
http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
719.2.1
by zedtux
Added committers and authors avatars from Gravatar (urllib2 version) |
1 |
# Copyright (C) 2011 by Guillaume Hain (zedtux) <zedtux@zedroot.org>
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
719.2.5
by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file |
17 |
import Queue |
18 |
import urllib |
|
19 |
import urllib2 |
|
719.2.3
by zedtux
Renamed avatarprovider.py to avatarproviders.py |
20 |
import hashlib |
719.2.5
by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file |
21 |
import threading |
719.2.3
by zedtux
Renamed avatarprovider.py to avatarproviders.py |
22 |
|
719.2.1
by zedtux
Added committers and authors avatars from Gravatar (urllib2 version) |
23 |
class AvatarProvider(object): |
24 |
""" |
|
25 |
Master class for Avatar providers.
|
|
26 |
|
|
27 |
All AvatarProviderXxxx classes should inherite from this one
|
|
28 |
and override at least get_base_url.
|
|
29 |
"""
|
|
30 |
def __init__(self, size=80): |
|
31 |
""" Constructor """ |
|
719.2.2
by zedtux
Patched code following Jelmer's recommandations |
32 |
self.size = size |
719.2.3
by zedtux
Renamed avatarprovider.py to avatarproviders.py |
33 |
|
34 |
def get_base_url(self): |
|
35 |
""" |
|
36 |
Override this methode in your provider class in order to return
|
|
37 |
base url of your provider.
|
|
38 |
"""
|
|
39 |
raise NotImplementedError("You must implement the get_base_url method.") |
|
40 |
||
41 |
||
719.2.5
by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file |
42 |
class AvatarDownloaderWorker(threading.Thread): |
43 |
""" |
|
44 |
Threaded worker to retrieve avatar from a provider.
|
|
45 |
|
|
46 |
It create a persitante connection to the provider in order
|
|
47 |
to get avatars quickly through the same socket (urllib3).
|
|
48 |
"""
|
|
49 |
||
50 |
def __init__(self, provider_method): |
|
51 |
""" |
|
52 |
Constructor
|
|
53 |
|
|
54 |
provider_method: Provider method that return fields
|
|
55 |
to send with the request.
|
|
56 |
"""
|
|
57 |
threading.Thread.__init__(self) |
|
58 |
self.__stop = threading.Event() |
|
59 |
self.__queue = Queue.Queue() |
|
60 |
||
61 |
self.__provider_method = provider_method |
|
62 |
self.__end_thread = False |
|
63 |
||
64 |
def stop(self): |
|
65 |
""" Stop this worker """ |
|
66 |
self.__end_thread = True |
|
67 |
self.__stop.set() |
|
68 |
||
69 |
def set_callback_method(self, method): |
|
70 |
""" Fire the given callback method when treatment is finished """ |
|
71 |
self.__callback_method = method |
|
72 |
||
73 |
def queue(self, id_field): |
|
74 |
""" |
|
75 |
Put in Queue the id_field to treat in the thread.
|
|
76 |
This id_field is for example with Gravatar the email address.
|
|
77 |
"""
|
|
78 |
self.__queue.put(id_field) |
|
79 |
||
80 |
def run(self): |
|
81 |
""" Worker core code. """ |
|
82 |
while not self.__end_thread: |
|
83 |
id_field = self.__queue.get() |
|
84 |
# Call provider method to get fields to pass in the request
|
|
85 |
url = self.__provider_method(id_field) |
|
86 |
# Execute the request
|
|
87 |
response = urllib2.urlopen(url) |
|
88 |
# Fire the callback method
|
|
89 |
if not self.__callback_method is None: |
|
90 |
self.__callback_method(response, id_field) |
|
91 |
self.__queue.task_done() |
|
92 |
||
93 |
||
719.2.3
by zedtux
Renamed avatarprovider.py to avatarproviders.py |
94 |
class AvatarProviderGravatar(AvatarProvider): |
95 |
""" Gravatar provider """ |
|
96 |
||
97 |
def __init__(self): |
|
98 |
""" Constructor """ |
|
99 |
super(AvatarProviderGravatar, self).__init__() |
|
100 |
||
101 |
def get_base_url(self): |
|
102 |
return "http://www.gravatar.com/avatar.php?" |
|
103 |
||
104 |
def gravatar_id_for_email(self, email): |
|
105 |
""" Return a converted email address to a gravatar_id """ |
|
106 |
return self.get_base_url() + \ |
|
107 |
urllib.urlencode({ |
|
108 |
'gravatar_id':hashlib.md5(email.lower()).hexdigest(), |
|
109 |
'size':str(self.size) |
|
110 |
})
|