/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
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
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
17
import Queue
18
import urllib
19
import urllib2
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
20
import hashlib
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
21
import threading
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
22
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
23
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
24
class AvatarProvider(object):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
25
    """Base class for Avatar providers.
26
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
27
    All AvatarProviderXxxx classes should inherite from this one
28
    and override at least get_base_url.
29
    """
30
    def __init__(self, size=80):
31
        """ Constructor """
719.2.2 by zedtux
Patched code following Jelmer's recommandations
32
        self.size = size
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
33
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
34
    def get_base_url(self):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
35
        """Return the base URL of this provider.
36
        """
37
        raise NotImplementedError(self.get_base_url)
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
38
39
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
40
class AvatarDownloaderWorker(threading.Thread):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
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
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
47
    def __init__(self, provider_method):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
48
        """Constructor
734.1.32 by Curtis Hovey
Lint fixes while looking at thread issues.
49
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
50
        :param provider_method: Provider method that returns fields
51
                 to send with the request.
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
52
        """
53
        threading.Thread.__init__(self)
54
        self.__stop = threading.Event()
55
        self.__queue = Queue.Queue()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
56
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
57
        self.__provider_method = provider_method
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
58
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
59
    def stop(self):
60
        """ Stop this worker """
61
        self.__stop.set()
734.1.35 by Curtis Hovey
Remove unneeded exception handlers.
62
        while self.__queue.qsize() > 0:
63
            self.__queue.get_nowait()
64
            self.__queue.task_done()
734.1.34 by Curtis Hovey
Ensure the avatar threads are stopped when the widget is destroyed.
65
        self.__queue.join()
66
67
    @property
68
    def is_running(self):
69
        return not self.__stop.is_set()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
70
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
71
    def set_callback_method(self, method):
72
        """ Fire the given callback method when treatment is finished """
73
        self.__callback_method = method
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
74
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
75
    def queue(self, id_field):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
76
        """Put in Queue the id_field to treat in the thread.
77
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
78
        This id_field is for example with Gravatar the email address.
79
        """
734.1.34 by Curtis Hovey
Ensure the avatar threads are stopped when the widget is destroyed.
80
        if self.is_running:
734.1.36 by Curtis Hovey
Do not exit the run method until stop is set.
81
            self.__queue.put(id_field)
734.1.34 by Curtis Hovey
Ensure the avatar threads are stopped when the widget is destroyed.
82
            if not self.is_alive():
83
                self.start()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
84
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
85
    def run(self):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
86
        """Worker core code. """
734.1.36 by Curtis Hovey
Do not exit the run method until stop is set.
87
        while self.is_running:
88
            try:
734.1.34 by Curtis Hovey
Ensure the avatar threads are stopped when the widget is destroyed.
89
                id_field = self.__queue.get_nowait()
90
                # Call provider method to get fields to pass in the request
91
                url = self.__provider_method(id_field)
92
                # Execute the request
93
                response = urllib2.urlopen(url)
94
                # Fire the callback method
95
                if not self.__callback_method is None:
96
                    self.__callback_method(response, id_field)
734.1.35 by Curtis Hovey
Remove unneeded exception handlers.
97
                self.__queue.task_done()
734.1.36 by Curtis Hovey
Do not exit the run method until stop is set.
98
            except Queue.Empty:
99
                # There is no more work to do.
100
                pass
719.2.5 by zedtux
Moved AvatarDownloaderWorker class into avatarsbox.py file
101
102
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
103
class AvatarProviderGravatar(AvatarProvider):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
104
    """Gravatar provider."""
105
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
106
    def get_base_url(self):
107
        return "http://www.gravatar.com/avatar.php?"
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
108
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
109
    def gravatar_id_for_email(self, email):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
110
        """Return a gravatar URL for an email address.."""
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
111
        return self.get_base_url() + \
112
                urllib.urlencode({
734.1.32 by Curtis Hovey
Lint fixes while looking at thread issues.
113
                    'gravatar_id': hashlib.md5(email.lower()).hexdigest(),
114
                    'size': str(self.size)
719.2.3 by zedtux
Renamed avatarprovider.py to avatarproviders.py
115
                })