Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- # # Copyright (C) 2010 John Garland <johnnybg+deluge@gmail.com> # # This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with # the additional special exception to link portions of this program with the OpenSSL library. # See LICENSE for more details. #
except ImportError: # twisted 8 from twisted.web.error import NoResource, ForbiddenResource
import deluge.ui.Win32IconImagePlugin assert deluge.ui.Win32IconImagePlugin # silence pyflakes else: PIL_INSTALLED = True
""" Represents a tracker's icon """ """ Initialises a new TrackerIcon object
:param filename: the filename of the icon :type filename: string """
""" Compares this TrackerIcon with another to determine if they're equal
:param other: the TrackerIcon to compare to :type other: TrackerIcon :returns: whether or not they're equal :rtype: boolean """ self.get_mimetype() == other.get_mimetype() and self.get_data() == other.get_data())
""" Returns the mimetype of this TrackerIcon's image
:returns: the mimetype of the image :rtype: string """
""" Returns the TrackerIcon's image data as a string
:returns: the image data :rtype: string """
""" Returns the TrackerIcon image's filename
:param full: an (optional) arg to indicate whether or not to return the full path :type full: boolean :returns: the path of the TrackerIcon's image :rtype: string """ return self.filename if full else os.path.basename(self.filename)
""" Set the cached icon data.
""" self.icon_cache = data
""" Returns the cached icon data.
""" return self.icon_cache
""" A TrackerIcon factory class """ """ Initialises a new TrackerIcons object
:param icon_dir: the (optional) directory of where to store the icons :type icon_dir: string :param no_icon: the (optional) path name of the icon to show when no icon can be fetched :type no_icon: string """
if icon != no_icon: host = icon_name_to_host(icon) try: self.icons[host] = TrackerIcon(os.path.join(self.dir, icon)) except KeyError: log.warning("invalid icon %s", icon) self.icons[None] = TrackerIcon(no_icon) else:
""" Returns True or False if the tracker icon for the given host exists or not.
:param host: the host for the TrackerIcon :type host: string :returns: True or False :rtype: bool """ return host.lower() in self.icons
""" Returns a TrackerIcon for the given tracker's host from the icon cache.
:param host: the host for the TrackerIcon :type host: string :returns: the TrackerIcon for the host :rtype: TrackerIcon """ host = host.lower() if host in self.icons: return self.icons[host] else: return None
""" Fetches (downloads) the icon for the given host. When the icon is downloaded a callback is fired on the the queue of callers to this function.
:param host: the host to obtain the TrackerIcon for :type host: string :returns: a Deferred which fires with the TrackerIcon for the given host :rtype: Deferred """ # We already have it, so let's return it # We're in the middle of getting it # Add ourselves to the waiting list d = defer.Deferred() self.pending[host].append(d) else: # We need to fetch it # Start callback chain errbackArgs=(host,)) callbackArgs=(host,)) callbackArgs=(host,), errbackArgs=(host,)) d.addCallback(self.resize_icon)
""" Downloads a tracker host's page If no url is provided, it bases the url on the host
:param host: the tracker host :type host: string :param url: the (optional) url of the host :type url: string :returns: the filename of the tracker host's page :rtype: Deferred """
""" Runs any download clean up functions
:param page: the page that finished downloading :type page: string :returns: the page that finished downloading :rtype: string """
""" Recovers from download error
:param f: the failure that occurred :type f: Failure :param host: the name of the host whose page failed to download :type host: string :returns: a Deferred if recovery was possible else the original failure :rtype: Deferred or Failure """ # Handle redirect errors errbackArgs=(host,))
def parse_html_page(self, page): """ Parses the html page for favicons
:param page: the page to parse :type page: string :returns: a Deferred which callbacks a list of available favicons (url, type) :rtype: Deferred """ except OSError as ex: log.warning("Couldn't remove temp file: %s", ex)
""" Runs any parse clean up functions
:param icons: the icons that were extracted from the page :type icons: list :param host: the host the icons are for :type host: string :returns: the icons that were extracted from the page :rtype: list """
""" Recovers from a parse error
:param f: the failure that occurred :type f: Failure :returns: a Deferred if recovery was possible else the original failure :rtype: Deferred or Failure """ log.debug("Error parsing page: %s", f.getErrorMessage()) return f
""" Downloads the first available icon from icons
:param icons: a list of icons :type icons: list :param host: the tracker's host name :type host: string :returns: a Deferred which fires with the downloaded icon's filename :rtype: Deferred """ force_filename=True) d.addErrback(self.on_download_icon_fail, host, icons)
def check_icon_is_valid(self, icon_name): """ Performs a sanity check on icon_name
:param icon_name: the name of the icon to check :type icon_name: string :returns: the name of the validated icon :rtype: string :raises: InvalidIconError """
try: Image.open(icon_name) except IOError as ex: raise InvalidIconError(ex) else: raise InvalidIconError("empty icon")
""" Runs any download cleanup functions
:param icon_name: the filename of the icon that finished downloading :type icon_name: string :param host: the host the icon completed to download for :type host: string :returns: the icon that finished downloading :rtype: TrackerIcon """
""" Recovers from a download error
:param f: the failure that occurred :type f: Failure :param host: the host the icon failed to download for :type host: string :param icons: the (optional) list of remaining icons :type icons: list :returns: a Deferred if recovery was possible else the original failure :rtype: Deferred or Failure """ # Handle redirect errors callbackArgs=(host,), errbackArgs=(host,)) d = self.download_icon(icons, host) # No icons, try favicon.ico as an act of desperation extension_to_mimetype("ico"))], host) callbackArgs=(host,), errbackArgs=(host,)) else: # No icons :( # Return the None Icon d = self.icons[None]
def resize_icon(self, icon): """ Resizes the given icon to be 16x16 pixels
:param icon: the icon to resize :type icon: TrackerIcon :returns: the resized icon :rtype: TrackerIcon """ if icon: filename = icon.get_filename() img = Image.open(filename) if img.size > (16, 16): new_filename = filename.rpartition(".")[0] + ".png" img = img.resize((16, 16), Image.ANTIALIAS) img.save(new_filename) if new_filename != filename: os.remove(filename) icon = TrackerIcon(new_filename) return icon
""" Stores the icon for the given host Callbacks any pending deferreds waiting on this icon
:param icon: the icon to store :type icon: TrackerIcon or None :param host: the host to store it for :type host: string :returns: the stored icon :rtype: TrackerIcon or None """ d.callback(icon)
""" Given a host, returns the URL to fetch
:param host: the tracker host :type host: string :returns: the url of the tracker :rtype: string """
# ------- HELPER CLASSES ------
""" A HTMLParser which extracts favicons from a HTML page """
except KeyError: pass else:
""" Returns a list of favicons extracted from the HTML page
:returns: a list of favicons :rtype: list """
# ------ HELPER FUNCTIONS ------
""" Given a URL, returns the host it belongs to
:param url: the URL in question :type url: string :returns: the host of the given URL :rtype: string """
""" Given a host, returns the appropriate icon name
:param host: the host in question :type host: string :param mimetype: the mimetype of the icon :type mimetype: string :returns: the icon's filename :rtype: string
"""
""" Given a host's icon name, returns the host name
:param icon: the icon name :type icon: string :returns: the host name :rtype: string """ return icon.rpartition(".")[0]
"image/gif": "gif", "image/jpeg": "jpg", "image/png": "png", "image/vnd.microsoft.icon": "ico", "image/x-icon": "ico", "gif": "image/gif", "jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "ico": "image/vnd.microsoft.icon", }
""" Given a mimetype, returns the appropriate filename extension
:param mimetype: the mimetype :type mimetype: string :returns: the filename extension for the given mimetype :rtype: string :raises KeyError: if given an invalid mimetype """
""" Given a filename extension, returns the appropriate mimetype
:param extension: the filename extension :type extension: string :returns: the mimetype for the given filename extension :rtype: string :raises KeyError: if given an invalid filename extension """
# ------ EXCEPTIONS ------
|