/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: Vincent Ladeuil
  • Date: 2008-05-05 18:16:46 UTC
  • mto: (487.1.1 gtk)
  • mto: This revision was merged to the branch mainline in revision 490.
  • Revision ID: v.ladeuil+lp@free.fr-20080505181646-n95l8ltw2u6jtr26
Fix bug #187283 fix replacing _() by _i18n().

* genpot.sh 
Remove duplication. Add the ability to specify the genrated pot
file on command-line for debugging purposes.

* po/olive-gtk.pot:
Regenerated.

* __init__.py, branch.py, branchview/treeview.py, checkout.py,
commit.py, conflicts.py, diff.py, errors.py, initialize.py,
merge.py, nautilus-bzr.py, olive/__init__.py, olive/add.py,
olive/bookmark.py, olive/guifiles.py, olive/info.py,
olive/menu.py, olive/mkdir.py, olive/move.py, olive/remove.py,
olive/rename.py, push.py, revbrowser.py, status.py, tags.py:
Replace all calls to _() by calls to _i18n(), the latter being
defined in __init__.py and imported in the other modules from
there. This fix the problem encountered countless times when
running bzr selftest and getting silly error messages about
boolean not being callables.

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 Queue
18
 
import urllib
19
 
import urllib2
20
 
import hashlib
21
 
import threading
22
 
 
23
 
 
24
 
class AvatarProvider(object):
25
 
    """Base class for Avatar providers.
26
 
 
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 """
32
 
        self.size = size
33
 
 
34
 
    def get_base_url(self):
35
 
        """Return the base URL of this provider.
36
 
        """
37
 
        raise NotImplementedError(self.get_base_url)
38
 
 
39
 
 
40
 
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
 
 
47
 
    def __init__(self, provider_method):
48
 
        """Constructor
49
 
 
50
 
        :param provider_method: Provider method that returns fields
51
 
                 to send with the request.
52
 
        """
53
 
        threading.Thread.__init__(self)
54
 
        self.__stop = threading.Event()
55
 
        self.__queue = Queue.Queue()
56
 
        self.__provider_method = provider_method
57
 
        self.__callback_method = None
58
 
        self.__error_method = None
59
 
 
60
 
    def stop(self):
61
 
        """ Stop this worker """
62
 
        self.__stop.set()
63
 
        while self.__queue.qsize() > 0:
64
 
            self.__queue.get_nowait()
65
 
            self.__queue.task_done()
66
 
        self.__queue.join()
67
 
 
68
 
    @property
69
 
    def is_running(self):
70
 
        return not self.__stop.is_set()
71
 
 
72
 
    def set_callback_method(self, method):
73
 
        """Fire the given callback method when treatment is finished."""
74
 
        self.__callback_method = method
75
 
 
76
 
    def set_error_method(self, method):
77
 
        """Fire the given callback when retrieving a avatar fails."""
78
 
        self.__error_method = method
79
 
 
80
 
    def queue(self, id_field):
81
 
        """Put in Queue the id_field to treat in the thread.
82
 
 
83
 
        This id_field is for example with Gravatar the email address.
84
 
        """
85
 
        if self.is_running:
86
 
            self.__queue.put(id_field)
87
 
            if not self.is_alive():
88
 
                self.start()
89
 
 
90
 
    def run(self):
91
 
        """Worker core code. """
92
 
        while self.is_running:
93
 
            try:
94
 
                id_field = self.__queue.get_nowait()
95
 
                # Call provider method to get fields to pass in the request
96
 
                url = self.__provider_method(id_field)
97
 
                # Execute the request
98
 
                try:
99
 
                    response = urllib2.urlopen(url)
100
 
                except urllib2.URLError, e:
101
 
                    if self.__error_method is not None:
102
 
                        self.__error_method(e)
103
 
                else:
104
 
                    # Fire the callback method
105
 
                    if not self.__callback_method is None:
106
 
                        self.__callback_method(response, id_field)
107
 
                self.__queue.task_done()
108
 
            except Queue.Empty:
109
 
                # There is no more work to do.
110
 
                pass
111
 
 
112
 
 
113
 
class AvatarProviderGravatar(AvatarProvider):
114
 
    """Gravatar provider."""
115
 
 
116
 
    def get_base_url(self):
117
 
        return "http://www.gravatar.com/avatar.php?"
118
 
 
119
 
    def gravatar_id_for_email(self, email):
120
 
        """Return a gravatar URL for an email address.."""
121
 
        return self.get_base_url() + \
122
 
                urllib.urlencode({
123
 
                    'gravatar_id': hashlib.md5(email.lower()).hexdigest(),
124
 
                    'size': str(self.size)
125
 
                })