47
47
def __init__(self, provider_method):
50
50
:param provider_method: Provider method that returns fields
51
51
to send with the request.
53
53
threading.Thread.__init__(self)
54
54
self.__stop = threading.Event()
55
55
self.__queue = Queue.Queue()
56
57
self.__provider_method = provider_method
57
self.__callback_method = None
58
self.__error_method = None
58
self.__end_thread = False
61
61
""" Stop this worker """
62
self.__end_thread = True
63
while self.__queue.qsize() > 0:
64
self.__queue.get_nowait()
65
self.__queue.task_done()
70
return not self.__stop.is_set()
72
65
def set_callback_method(self, method):
73
"""Fire the given callback method when treatment is finished."""
66
""" Fire the given callback method when treatment is finished """
74
67
self.__callback_method = method
76
def set_error_method(self, method):
77
"""Fire the given callback when retrieving a avatar fails."""
78
self.__error_method = method
80
69
def queue(self, id_field):
81
70
"""Put in Queue the id_field to treat in the thread.
83
72
This id_field is for example with Gravatar the email address.
86
self.__queue.put(id_field)
87
if not self.is_alive():
74
self.__queue.put(id_field)
91
77
"""Worker core code. """
92
while self.is_running:
94
id_field = self.__queue.get_nowait()
95
# Call provider method to get fields to pass in the request
96
url = self.__provider_method(id_field)
99
response = urllib2.urlopen(url)
100
except urllib2.URLError, e:
101
if self.__error_method is not None:
102
self.__error_method(e)
104
# Fire the callback method
105
if not self.__callback_method is None:
106
self.__callback_method(response, id_field)
107
self.__queue.task_done()
109
# There is no more work to do.
78
while not self.__end_thread:
79
id_field = self.__queue.get()
80
# Call provider method to get fields to pass in the request
81
url = self.__provider_method(id_field)
83
response = urllib2.urlopen(url)
84
# Fire the callback method
85
if not self.__callback_method is None:
86
self.__callback_method(response, id_field)
87
self.__queue.task_done()
113
90
class AvatarProviderGravatar(AvatarProvider):