Fix Queue plugin loading when state file is wrong.

This commit is contained in:
Andrew Resch 2007-12-11 01:35:36 +00:00
parent 78b78ce8b3
commit f3cdfa8351
3 changed files with 25 additions and 9 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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"""