/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 avatarproviders.py

  • Committer: Jelmer Vernooij
  • Date: 2011-04-06 14:50:40 UTC
  • Revision ID: jelmer@samba.org-20110406145040-7u37k9hxkhleiqx2
Cleanups, fix compatibility with older versions of pygtk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import hashlib
21
21
import threading
22
22
 
 
23
 
23
24
class AvatarProvider(object):
24
 
    """
25
 
    Master class for Avatar providers.
26
 
    
 
25
    """Base class for Avatar providers.
 
26
 
27
27
    All AvatarProviderXxxx classes should inherite from this one
28
28
    and override at least get_base_url.
29
29
    """
30
30
    def __init__(self, size=80):
31
31
        """ Constructor """
32
32
        self.size = size
33
 
    
 
33
 
34
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.")
 
35
        """Return the base URL of this provider.
 
36
        """
 
37
        raise NotImplementedError(self.get_base_url)
40
38
 
41
39
 
42
40
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
 
    
 
41
    """Threaded worker to retrieve avatar from a provider.
 
42
 
 
43
    This creates a persistant connection to the provider in order
 
44
    to get avatars quickly through the same socket (urllib2).
 
45
    """
 
46
 
50
47
    def __init__(self, provider_method):
51
 
        """
52
 
        Constructor
 
48
        """Constructor
53
49
        
54
 
        provider_method: Provider method that return fields
55
 
                         to send with the request.
 
50
        :param provider_method: Provider method that returns fields
 
51
                 to send with the request.
56
52
        """
57
53
        threading.Thread.__init__(self)
58
54
        self.__stop = threading.Event()
59
55
        self.__queue = Queue.Queue()
60
 
        
 
56
 
61
57
        self.__provider_method = provider_method
62
58
        self.__end_thread = False
63
 
    
 
59
 
64
60
    def stop(self):
65
61
        """ Stop this worker """
66
62
        self.__end_thread = True
67
63
        self.__stop.set()
68
 
    
 
64
 
69
65
    def set_callback_method(self, method):
70
66
        """ Fire the given callback method when treatment is finished """
71
67
        self.__callback_method = method
72
 
    
 
68
 
73
69
    def queue(self, id_field):
74
 
        """
75
 
        Put in Queue the id_field to treat in the thread.
 
70
        """Put in Queue the id_field to treat in the thread.
 
71
 
76
72
        This id_field is for example with Gravatar the email address.
77
73
        """
78
74
        self.__queue.put(id_field)
79
 
    
 
75
 
80
76
    def run(self):
81
 
        """ Worker core code. """
 
77
        """Worker core code. """
82
78
        while not self.__end_thread:
83
79
            id_field = self.__queue.get()
84
80
            # Call provider method to get fields to pass in the request
92
88
 
93
89
 
94
90
class AvatarProviderGravatar(AvatarProvider):
95
 
    """ Gravatar provider """
96
 
    
97
 
    def __init__(self):
98
 
        """ Constructor """
99
 
        super(AvatarProviderGravatar, self).__init__()
100
 
    
 
91
    """Gravatar provider."""
 
92
 
101
93
    def get_base_url(self):
102
94
        return "http://www.gravatar.com/avatar.php?"
103
 
    
 
95
 
104
96
    def gravatar_id_for_email(self, email):
105
 
        """ Return a converted email address to a gravatar_id """
 
97
        """Return a gravatar URL for an email address.."""
106
98
        return self.get_base_url() + \
107
99
                urllib.urlencode({
108
100
                    'gravatar_id':hashlib.md5(email.lower()).hexdigest(),