/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to avatardownloaderworker.py

  • Committer: zedtux
  • Date: 2011-03-11 14:59:04 UTC
  • mto: This revision was merged to the branch mainline in revision 723.
  • Revision ID: zedtux@zedroot.org-20110311145904-g0cq6drvweriy9hz
Added committers and authors avatars from Gravatar (urllib2 version)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
17
import urllib2
 
18
import Queue
 
19
import threading
 
20
 
 
21
class AvatarDownloaderWorker(threading.Thread):
 
22
    """
 
23
    Threaded worker to retrieve avatar from a provider.
 
24
    
 
25
    It create a persitante connection to the provider in order
 
26
    to get avatars quickly through the same socket (urllib3).
 
27
    """
 
28
    
 
29
    def __init__(self, provider_method):
 
30
        """
 
31
        Constructor
 
32
        
 
33
        Initialize reusable connection from urllib3.
 
34
        base_url:        Base of the provider url to use for each requests.
 
35
        provider_method: Provider method that return fields
 
36
                         to send with the request.
 
37
        """
 
38
        threading.Thread.__init__(self)
 
39
        self.__stop = threading.Event()
 
40
        self.__queue = Queue.Queue()
 
41
        
 
42
        self.__provider_method = provider_method
 
43
        self.__end_thread = False
 
44
    
 
45
    def stop(self):
 
46
        """ Stop this worker """
 
47
        self.__end_thread = True
 
48
        self.__stop.set()
 
49
    
 
50
    def set_callback_method(self, method):
 
51
        """ Fire the given callback method when treatment is finished """
 
52
        self.__callback_method = method
 
53
    
 
54
    def queue(self, id_field):
 
55
        """
 
56
        Put in Queue the id_field to treat in the thread.
 
57
        This id_field is for example with Gravatar the email address.
 
58
        """
 
59
        self.__queue.put(id_field)
 
60
    
 
61
    def run(self):
 
62
        """ Worker core code. """
 
63
        while not self.__end_thread:
 
64
            id_field = self.__queue.get()
 
65
            # Call provider method to get fields to pass in the request
 
66
            url = self.__provider_method(id_field)
 
67
            # Execute the request
 
68
            response = urllib2.urlopen(url)
 
69
            # Fire the callback method
 
70
            if not self.__callback_method is None:
 
71
                self.__callback_method(response, id_field)
 
72
            self.__queue.task_done()