From f3cdfa835103e4b0aa9c9e7c22f0e3d16f41230e Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Tue, 11 Dec 2007 01:35:36 +0000 Subject: [PATCH] Fix Queue plugin loading when state file is wrong. --- deluge/core/pluginmanager.py | 5 ++++- deluge/plugins/queue/queue/core.py | 6 ++++-- deluge/plugins/queue/queue/torrentqueue.py | 23 ++++++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/deluge/core/pluginmanager.py b/deluge/core/pluginmanager.py index b57877754..067f63056 100644 --- a/deluge/core/pluginmanager.py +++ b/deluge/core/pluginmanager.py @@ -112,4 +112,7 @@ class PluginManager(deluge.pluginmanagerbase.PluginManagerBase, log.debug("run_post_torrent_remove") for function in self.hooks["post_torrent_remove"]: function(torrent_id) - + + def get_torrent_list(self): + """Returns a list of torrent_id's in the current session.""" + return component.get("TorrentManager").get_torrent_list() diff --git a/deluge/plugins/queue/queue/core.py b/deluge/plugins/queue/queue/core.py index 12f354b02..992ee7454 100644 --- a/deluge/plugins/queue/queue/core.py +++ b/deluge/plugins/queue/queue/core.py @@ -37,8 +37,10 @@ from deluge.plugins.corepluginbase import CorePluginBase class Core(CorePluginBase): def enable(self): - # Instantiate the TorrentQueue object - self.queue = TorrentQueue() + # Get a list of torrent_ids in the session + # We give this to the TorrentQueue to compare with the saved state + # and create the queuing order. + self.queue = TorrentQueue(self.plugin.get_torrent_list()) # Register core hooks self.plugin.register_hook("post_torrent_add", self._post_torrent_add) diff --git a/deluge/plugins/queue/queue/torrentqueue.py b/deluge/plugins/queue/queue/torrentqueue.py index 2677240fe..a07df8a91 100644 --- a/deluge/plugins/queue/queue/torrentqueue.py +++ b/deluge/plugins/queue/queue/torrentqueue.py @@ -37,10 +37,19 @@ import deluge.common from deluge.log import LOG as log class TorrentQueue: - def __init__(self): - self.queue = [] + def __init__(self, torrent_list): # Try to load the queue state from file - self.load_state() + self.queue = self.load_state() + # First remove any torrent_ids in self.queue that are not in the current + # session list. + for torrent_id in self.queue: + if torrent_id not in torrent_list: + self.queue.remove(torrent_id) + + # Next we append any torrents in the session list to self.queue + for torrent_id in torrent_list: + if torrent_id not in self.queue: + self.queue.append(torrent_id) def __getitem__(self, torrent_id): """Return the queue position of the torrent_id""" @@ -57,9 +66,11 @@ class TorrentQueue: "rb") state = pickle.load(state_file) state_file.close() - self.queue = state - except IOError: - log.warning("Unable to load queue state file.") + return state + except IOError, e: + log.warning("Unable to load queue state file: %s", e) + + return [] def save_state(self): """Save the queue state"""