fetch tracker icons in thread

This commit is contained in:
Martijn Voncken 2008-10-21 18:36:53 +00:00
commit 7359ddb879
3 changed files with 61 additions and 33 deletions

View file

@ -1,15 +1,39 @@
import time
import gobject
import os
from deluge.tracker_icons import TrackerIcons from deluge.tracker_icons import TrackerIcons
from deluge.common import get_default_config_dir
def test(): def del_old():
filename = os.path.join(get_default_config_dir("trackers"),"legaltorrents.com.ico")
if os.path.exists(filename):
os.remove(filename)
def test_get():
del_old()
trackericons = TrackerIcons() trackericons = TrackerIcons()
print trackericons.get("unknown2")
print trackericons._fetch_icon("unknown?") #exception, Returns None
print trackericons._fetch_icon("deluge-torrent.org") #404 , returns False
print trackericons._fetch_icon("legaltorrents.com")
print trackericons.get("thepiratebay.com")
print trackericons.get("unknown2") #exception, returns None
print trackericons.get("legaltorrents.com") #logs cached
print trackericons.get("google.com") print trackericons.get("google.com")
print trackericons.get("legaltorrents.com")
time.sleep(1.0)
print trackericons.get("legaltorrents.com")
def callback1(value):
print "callback1:", value
return False
def test_async():
#test is broken :(,. but filtertreeview works.
del_old()
trackericons = TrackerIcons()
trackericons.get_async("legaltorrents.com",callback1)
print "here"
gobject.MainLoop()
test_get()
#test_async()
test()

View file

@ -31,6 +31,9 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
import threading
import gobject
from urllib import urlopen from urllib import urlopen
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.common import get_default_config_dir, get_pixmap from deluge.common import get_default_config_dir, get_pixmap
@ -53,15 +56,14 @@ class TrackerIcons(object):
#load image-names in cache-dir #load image-names in cache-dir
for icon in os.listdir(self.image_dir): for icon in os.listdir(self.image_dir):
if icon.endswith(".ico"): if icon.endswith(".ico"):
self.add_icon(icon[:-4], os.path.join(self.image_dir, icon)) self.images[icon[:-4]] = os.path.join(self.image_dir, icon)
def _fetch_icon_thread(self, tracker_host, callback):
def _fetch_icon(self, tracker_host):
""" """
gets new icon from the internet. gets new icon from the internet.
used by get(). used by get().
calls add_icon() calls callback on sucess
returns True or False assumes dicts,urllib and logging are threadsafe.
""" """
try: try:
host_name = RENAMES.get(tracker_host, tracker_host) host_name = RENAMES.get(tracker_host, tracker_host)
@ -75,7 +77,6 @@ class TrackerIcons(object):
raise Exception("No data") raise Exception("No data")
except Exception, e: except Exception, e:
log.debug("%s %s %s" % (tracker_host, e, e.message)) log.debug("%s %s %s" % (tracker_host, e, e.message))
self.add_icon(tracker_host, None)
return False return False
filename = os.path.join(get_default_config_dir("trackers"),"%s.ico" % tracker_host) filename = os.path.join(get_default_config_dir("trackers"),"%s.ico" % tracker_host)
@ -83,23 +84,21 @@ class TrackerIcons(object):
f = open(filename,"wb") f = open(filename,"wb")
f.write(icon_data) f.write(icon_data)
f.close() f.close()
self.add_icon(tracker_host, filename)
return True
def add_icon(self, tracker_host, filename):
self.images[tracker_host] = filename self.images[tracker_host] = filename
if callback:
gobject.idle_add(callback, filename)
def get_async(self, tracker_host, callback):
threading.Thread(
target=self. _fetch_icon_thread,
args=(tracker_host, callback)).start()
def get(self, tracker_host): def get(self, tracker_host):
""" """
use this method to get the filename of an icon. returns None if the icon is not fetched(yet) or not fond.
""" """
if not tracker_host in self.images: if not tracker_host in self.images:
self._fetch_icon(tracker_host) self.images[tracker_host] = None
else: self.get_async(tracker_host, None)
log.debug("cached tracker icon:%s" % tracker_host)
return self.images[tracker_host] return self.images[tracker_host]

View file

@ -231,15 +231,20 @@ class FilterTreeView(component.Component):
return gtk.gdk.pixbuf_new_from_file(deluge.common.get_pixmap("%s16.png" % pix)) return gtk.gdk.pixbuf_new_from_file(deluge.common.get_pixmap("%s16.png" % pix))
elif cat == "tracker_host": elif cat == "tracker_host":
ico = self.tracker_icons.get(value) self.tracker_icons.get_async(value, lambda filename: self.set_row_image(cat, value, filename))
if ico:
try: #assume we could get trashed images here..
return gtk.gdk.pixbuf_new_from_file(ico)
except:
log.debug(e.message)
return None return None
def set_row_image(self, cat, value, filename):
try: #assume we could get trashed images here..
pix = gtk.gdk.pixbuf_new_from_file(filename)
row = self.filters[(cat, value)]
self.treestore.set_value(row, 4, pix)
except Exception, e:
log.debug(e.message)
return False
def on_selection_changed(self, selection): def on_selection_changed(self, selection):
try: try:
(model, row) = self.label_view.get_selection().get_selected() (model, row) = self.label_view.get_selection().get_selected()