14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
23
class AvatarProvider(object):
36
39
raise NotImplementedError("You must implement the get_base_url method.")
42
class AvatarDownloaderWorker(threading.Thread):
44
Threaded worker to retrieve avatar from a provider.
46
It create a persitante connection to the provider in order
47
to get avatars quickly through the same socket (urllib3).
50
def __init__(self, provider_method):
54
provider_method: Provider method that return fields
55
to send with the request.
57
threading.Thread.__init__(self)
58
self.__stop = threading.Event()
59
self.__queue = Queue.Queue()
61
self.__provider_method = provider_method
62
self.__end_thread = False
65
""" Stop this worker """
66
self.__end_thread = True
69
def set_callback_method(self, method):
70
""" Fire the given callback method when treatment is finished """
71
self.__callback_method = method
73
def queue(self, id_field):
75
Put in Queue the id_field to treat in the thread.
76
This id_field is for example with Gravatar the email address.
78
self.__queue.put(id_field)
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)
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()
39
94
class AvatarProviderGravatar(AvatarProvider):
40
95
""" Gravatar provider """