/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-03-23 09:49:44 UTC
  • mfrom: (723.1.1 isearch)
  • Revision ID: jelmer@samba.org-20110323094944-7n5h1vif3xpbze3p
Merge support for interactive substring search in bzr viz and annotate.

Show diffs side-by-side

added added

removed removed

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