From 91e1e3f74c7407c08eee0f750a1338cd2a78d93b Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sun, 11 Dec 2011 16:48:42 +0000 Subject: [PATCH] Changed Handler.monitored_downloads from a dict to a set The old system was intended to allow multiple callers to share a single download. But that should never happen in practice because higher-level code will handle it, and even if it does we need some per-caller information (e.g. "hint"). --- zeroinstall/0launch-gui/gui.py | 2 +- zeroinstall/0launch-gui/iface_browser.py | 2 +- zeroinstall/0launch-gui/mainwindow.py | 2 +- zeroinstall/injector/fetch.py | 14 ++++------- zeroinstall/injector/handler.py | 40 +++++++++++--------------------- 5 files changed, 22 insertions(+), 38 deletions(-) diff --git a/zeroinstall/0launch-gui/gui.py b/zeroinstall/0launch-gui/gui.py index 44f615f..3b57d25 100644 --- a/zeroinstall/0launch-gui/gui.py +++ b/zeroinstall/0launch-gui/gui.py @@ -21,7 +21,7 @@ class GUIHandler(handler.Handler): return False def abort_all_downloads(self): - for dl in self.monitored_downloads.values(): + for dl in self.monitored_downloads: dl.abort() def downloads_changed(self): diff --git a/zeroinstall/0launch-gui/iface_browser.py b/zeroinstall/0launch-gui/iface_browser.py index c913f69..6a03897 100644 --- a/zeroinstall/0launch-gui/iface_browser.py +++ b/zeroinstall/0launch-gui/iface_browser.py @@ -446,7 +446,7 @@ class InterfaceBrowser: # A download may be for a feed, an interface or an implementation. # Create the reverse mapping (item -> download) hints = {} - for dl in self.policy.handler.monitored_downloads.values(): + for dl in self.policy.handler.monitored_downloads: if dl.hint: if dl.hint not in hints: hints[dl.hint] = [] diff --git a/zeroinstall/0launch-gui/mainwindow.py b/zeroinstall/0launch-gui/mainwindow.py index aeb5f92..c094356 100644 --- a/zeroinstall/0launch-gui/mainwindow.py +++ b/zeroinstall/0launch-gui/mainwindow.py @@ -155,7 +155,7 @@ class MainWindow: done = total = self.policy.handler.total_bytes_downloaded # Completed downloads n_downloads = self.policy.handler.n_completed_downloads # Now add downloads in progress... - for x in monitored_downloads.values(): + for x in monitored_downloads: if x.status != download.download_fetching: continue n_downloads += 1 if x.expected_size: diff --git a/zeroinstall/injector/fetch.py b/zeroinstall/injector/fetch.py index c959cfe..983d46a 100644 --- a/zeroinstall/injector/fetch.py +++ b/zeroinstall/injector/fetch.py @@ -5,7 +5,7 @@ Downloads feeds, keys, packages and icons. # Copyright (C) 2009, Thomas Leonard # See the README file for details, or visit http://0install.net. -from zeroinstall import _ +from zeroinstall import _, NeedDownload import os from logging import info, debug, warn @@ -413,14 +413,10 @@ class Fetcher(object): info(_('No PNG icons found in %s'), interface) return - try: - dl = self.handler.monitored_downloads[source] - if dl and force: - dl.abort() - raise KeyError - except KeyError: - dl = download.Download(source, hint = interface, modification_time = modification_time) - self.handler.monitor_download(dl) + if self.handler.dry_run: + raise NeedDownload(source) + dl = download.Download(source, hint = interface, modification_time = modification_time) + self.handler.monitor_download(dl) @tasks.async def download_and_add_icon(): diff --git a/zeroinstall/injector/handler.py b/zeroinstall/injector/handler.py index 81b7c4f..1e55e3d 100644 --- a/zeroinstall/injector/handler.py +++ b/zeroinstall/injector/handler.py @@ -28,8 +28,8 @@ class Handler(object): """ A Handler is used to interact with the user (e.g. to confirm keys, display download progress, etc). - @ivar monitored_downloads: dict of downloads in progress - @type monitored_downloads: {URL: L{download.Download}} + @ivar monitored_downloads: set of downloads in progress + @type monitored_downloads: {L{download.Download}} @ivar n_completed_downloads: number of downloads which have finished for GUIs, etc (can be reset as desired). @type n_completed_downloads: int @ivar total_bytes_downloaded: informational counter for GUIs, etc (can be reset as desired). Updated when download finishes. @@ -41,7 +41,7 @@ class Handler(object): __slots__ = ['monitored_downloads', 'dry_run', 'total_bytes_downloaded', 'n_completed_downloads'] def __init__(self, mainloop = None, dry_run = False): - self.monitored_downloads = {} + self.monitored_downloads = set() self.dry_run = dry_run self.n_completed_downloads = 0 self.total_bytes_downloaded = 0 @@ -50,7 +50,7 @@ class Handler(object): """Called when a new L{download} is started. This is mainly used by the GUI to display the progress bar.""" dl.start() - self.monitored_downloads[dl.url] = dl + self.monitored_downloads.add(dl) self.downloads_changed() @tasks.async @@ -60,7 +60,7 @@ class Handler(object): try: self.n_completed_downloads += 1 self.total_bytes_downloaded += dl.get_bytes_downloaded_so_far() - del self.monitored_downloads[dl.url] + self.monitored_downloads.remove(dl) self.downloads_changed() except Exception as ex: self.report_error(ex) @@ -83,28 +83,17 @@ class Handler(object): tasks.wait_for_blocker(blocker) def get_download(self, url, force = False, hint = None, factory = None): - """Return the Download object currently downloading 'url'. - If no download for this URL has been started, start one now (and - start monitoring it). - If the download failed and force is False, return it anyway. - If force is True, abort any current or failed download and start - a new one. + """Create a Download object to download 'url'. @rtype: L{download.Download} """ if self.dry_run: raise NeedDownload(url) - try: - dl = self.monitored_downloads[url] - if dl and force: - dl.abort() - raise KeyError - except KeyError: - if factory is None: - dl = download.Download(url, hint) - else: - dl = factory(url, hint) - self.monitor_download(dl) + if factory is None: + dl = download.Download(url, hint) + else: + dl = factory(url, hint) + self.monitor_download(dl) return dl @tasks.async @@ -237,8 +226,8 @@ class ConsoleHandler(Handler): self.last_msg_len = None def show_progress(self): - urls = self.monitored_downloads.keys() - if not urls: return True + if not self.monitored_downloads: return True + urls = [(dl.url, dl) for dl in self.monitored_downloads] if self.disable_progress: return True @@ -247,8 +236,7 @@ class ConsoleHandler(Handler): url_width = item_width - 7 msg = "" - for url in sorted(urls): - dl = self.monitored_downloads[url] + for url, dl in sorted(urls): so_far = dl.get_bytes_downloaded_so_far() leaf = url.rsplit('/', 1)[-1] if len(leaf) >= url_width: -- 2.11.4.GIT