Module: deluge
Branch: 1.3-stable
Commit: 6bb4559d18d9541fa4abcb1611c7996afbc4bcf6

Author: John Garland <[email protected]>
Date:   Sat May  8 16:24:16 2010 +1000

Make host_to_url support redirection and add another test

---

 deluge/ui/tracker_icons.py  |   36 ++++++++++++++++++++----------------
 tests/publicbt.ico          |  Bin 0 -> 2550 bytes
 tests/test_tracker_icons.py |    7 +++++++
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py
index e9c23ab..d4823e5 100644
--- a/deluge/ui/tracker_icons.py
+++ b/deluge/ui/tracker_icons.py
@@ -152,6 +152,7 @@ class TrackerIcons(Component):
             self.icons[None] = None
 
         self.pending = {}
+        self.redirects = {}
 
     def get(self, host):
         """
@@ -202,7 +203,7 @@ class TrackerIcons(Component):
         :rtype: Deferred
         """
         if not url:
-            url = host_to_url(host)
+            url = self.host_to_url(host)
         log.debug("Downloading %s", url)
         return download_file(url, mkstemp()[1], force_filename=True)
 
@@ -235,7 +236,8 @@ class TrackerIcons(Component):
         d = f
         if f.check(error.PageRedirect):
             # Handle redirect errors
-            location = urljoin(host_to_url(host), error_msg.split(" to ")[1])
+            location = urljoin(self.host_to_url(host), error_msg.split(" to 
")[1])
+            self.redirects[host] = url_to_host(location)
             d = self.download_page(host, url=location)
             d.addCallbacks(self.on_download_page_complete, 
self.on_download_page_fail,
                            errbackArgs=(host,))
@@ -275,7 +277,7 @@ class TrackerIcons(Component):
         :rtype: list
         """
         log.debug("Got icons for %s: %s", host, icons)
-        url = host_to_url(host)
+        url = self.host_to_url(host)
         icons = [(urljoin(url, icon), mimetype) for icon, mimetype in icons]
         log.debug("Icon urls: %s", icons)
         return icons
@@ -345,7 +347,7 @@ class TrackerIcons(Component):
         d = f
         if f.check(error.PageRedirect):
             # Handle redirect errors
-            location = urljoin(host_to_url(host), error_msg.split(" to ")[1])
+            location = urljoin(self.host_to_url(host), error_msg.split(" to 
")[1])
             d = self.download_icon([(location, 
extension_to_mimetype(location.rpartition('.')[2]))] + icons, host)
             if not icons:
                 d.addCallbacks(self.on_download_icon_complete, 
self.on_download_icon_fail,
@@ -354,7 +356,7 @@ class TrackerIcons(Component):
             d = self.download_icon(icons, host)
         elif f.check(IndexError, HTMLParseError):
             # No icons, try favicon.ico as an act of desperation
-            d = self.download_icon([(urljoin(host_to_url(host), 
"favicon.ico"), extension_to_mimetype("ico"))], host)
+            d = self.download_icon([(urljoin(self.host_to_url(host), 
"favicon.ico"), extension_to_mimetype("ico"))], host)
             d.addCallbacks(self.on_download_icon_complete, 
self.on_download_icon_fail,
                            callbackArgs=(host,), errbackArgs=(host,))
         else:
@@ -404,6 +406,19 @@ class TrackerIcons(Component):
         del self.pending[host]
         return icon
 
+    def host_to_url(self, host):
+        """
+        Given a host, returns the URL to fetch
+
+        :param host: the tracker host
+        :type host: string
+        :returns: the url of the tracker
+        :rtype: string
+        """
+        if host in self.redirects:
+            host = self.redirects[host]
+        return "http://%s/"; % host
+
 ################################ HELPER CLASSES ###############################
 
 class FaviconParser(HTMLParser):
@@ -450,17 +465,6 @@ class FaviconParser(HTMLParser):
 
 ############################### HELPER FUNCTIONS ##############################
 
-def host_to_url(host):
-    """
-    Given a host, returns the URL to fetch
-
-    :param host: the tracker host
-    :type host: string
-    :returns: the url of the tracker
-    :rtype: string
-    """
-    return "http://%s/"; % host
-
 def url_to_host(url):
     """
     Given a URL, returns the host it belongs to
diff --git a/tests/publicbt.ico b/tests/publicbt.ico
new file mode 100644
index 0000000..54b10a8
Binary files /dev/null and b/tests/publicbt.ico differ
diff --git a/tests/test_tracker_icons.py b/tests/test_tracker_icons.py
index 5ff7241..f96e07b 100644
--- a/tests/test_tracker_icons.py
+++ b/tests/test_tracker_icons.py
@@ -52,3 +52,10 @@ class TrackerIconsTestCase(unittest.TestCase):
         d.addCallback(self.assertNotIdentical, None)
         d.addCallback(self.assertEquals, icon)
         return d
+
+    def test_get_publicbt_ico(self):
+        icon = TrackerIcon("../publicbt.ico")
+        d = icons.get("publicbt.org")
+        d.addCallback(self.assertNotIdentical, None)
+        d.addCallback(self.assertEquals, icon)
+        return d

-- 
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en.

Reply via email to