/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (C) 2011 by Guillaume Hain (zedtux) <zedtux@zedroot.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import Queue
import urllib
import urllib2
import hashlib
import threading

class AvatarProvider(object):
    """
    Master class for Avatar providers.
    
    All AvatarProviderXxxx classes should inherite from this one
    and override at least get_base_url.
    """
    def __init__(self, size=80):
        """ Constructor """
        self.size = size
    
    def get_base_url(self):
        """
        Override this methode in your provider class in order to return
        base url of your provider.
        """
        raise NotImplementedError("You must implement the get_base_url method.")


class AvatarDownloaderWorker(threading.Thread):
    """
    Threaded worker to retrieve avatar from a provider.
    
    It create a persitante connection to the provider in order
    to get avatars quickly through the same socket (urllib3).
    """
    
    def __init__(self, provider_method):
        """
        Constructor
        
        provider_method: Provider method that return fields
                         to send with the request.
        """
        threading.Thread.__init__(self)
        self.__stop = threading.Event()
        self.__queue = Queue.Queue()
        
        self.__provider_method = provider_method
        self.__end_thread = False
    
    def stop(self):
        """ Stop this worker """
        self.__end_thread = True
        self.__stop.set()
    
    def set_callback_method(self, method):
        """ Fire the given callback method when treatment is finished """
        self.__callback_method = method
    
    def queue(self, id_field):
        """
        Put in Queue the id_field to treat in the thread.
        This id_field is for example with Gravatar the email address.
        """
        self.__queue.put(id_field)
    
    def run(self):
        """ Worker core code. """
        while not self.__end_thread:
            id_field = self.__queue.get()
            # Call provider method to get fields to pass in the request
            url = self.__provider_method(id_field)
            # Execute the request
            response = urllib2.urlopen(url)
            # Fire the callback method
            if not self.__callback_method is None:
                self.__callback_method(response, id_field)
            self.__queue.task_done()


class AvatarProviderGravatar(AvatarProvider):
    """ Gravatar provider """
    
    def __init__(self):
        """ Constructor """
        super(AvatarProviderGravatar, self).__init__()
    
    def get_base_url(self):
        return "http://www.gravatar.com/avatar.php?"
    
    def gravatar_id_for_email(self, email):
        """ Return a converted email address to a gravatar_id """
        return self.get_base_url() + \
                urllib.urlencode({
                    'gravatar_id':hashlib.md5(email.lower()).hexdigest(),
                    'size':str(self.size)
                })