Index: deluge/error.py =================================================================== --- deluge/error.py (revision 2796) +++ deluge/error.py (working copy) @@ -31,6 +31,8 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. +import deluge.xmlrpclib + class DelugeError(Exception): def __init__(self, value): self.value = value @@ -40,3 +42,13 @@ class NoCoreError(DelugeError): pass +class InvalidTorrent(DelugeError): + pass + +def from_xmlrpc_fault(fault): + prefix = "deluge.error." + string = fault.faultString + string = string.replace(':', '(', 1) + ')' + if string[:len(prefix)] == prefix: + raise eval(string) + Index: deluge/core/torrentmanager.py =================================================================== --- deluge/core/torrentmanager.py (revision 2796) +++ deluge/core/torrentmanager.py (working copy) @@ -46,6 +46,7 @@ from deluge.configmanager import ConfigManager from deluge.core.torrent import Torrent from deluge.log import LOG as log +import deluge.error class TorrentState: def __init__(self, @@ -145,6 +146,9 @@ def __getitem__(self, torrent_id): """Return the Torrent with torrent_id""" + if torrent_id not in self.torrents: + log.debug("torrent_id '%s' doesn't exist.", torrent_id) + raise deluge.error.InvalidTorrent("Invalid torrent id '%s'" % torrent_id) return self.torrents[torrent_id] def get_torrent_list(self): @@ -294,7 +298,7 @@ option = 1 self.session.remove_torrent(self.torrents[torrent_id].handle, option) - except (RuntimeError, KeyError), e: + except (RuntimeError), e: log.warning("Error removing torrent: %s", e) return False @@ -314,7 +318,7 @@ # Remove the torrent from deluge's session try: del self.torrents[torrent_id] - except KeyError, ValueError: + except ValueError: return False # Save the session state @@ -359,7 +363,7 @@ # We start by removing it from the lt session try: self.session.remove_torrent(torrent.handle, 0) - except (RuntimeError, KeyError), e: + except (RuntimeError), e: log.warning("Error removing torrent: %s", e) return False @@ -438,6 +442,7 @@ """Save the state of the TorrentManager to the torrents.state file""" state = TorrentManagerState() # Create the state for each Torrent and append to the list + print self.torrents.values() for torrent in self.torrents.values(): torrent_state = TorrentState( torrent.filename, @@ -454,11 +459,12 @@ torrent.private, torrent.file_priorities ) + print torrent.get_save_info() state.torrents.append(torrent_state) # Pickle the TorrentManagerState object try: - log.debug("Saving torrent state file.") + log.debug("Saving torrent state file: %s", state.torrents) state_file = open(deluge.common.get_config_dir("torrents.state"), "wb") cPickle.dump(state, state_file) @@ -523,11 +529,8 @@ # Get the torrent_id torrent_id = str(alert.handle.info_hash()) # Set the tracker status for the torrent - try: - if alert.msg != "Got peers from DHT": - self.torrents[torrent_id].set_tracker_status(_("Announce OK")) - except KeyError: - log.debug("torrent_id doesn't exist.") + if alert.msg != "Got peers from DHT": + self.torrents[torrent_id].set_tracker_status(_("Announce OK")) # Check to see if we got any peer information from the tracker if alert.handle.status().num_complete == -1 or \ @@ -540,10 +543,7 @@ # Get the torrent_id torrent_id = str(alert.handle.info_hash()) # Set the tracker status for the torrent - try: - self.torrents[torrent_id].set_tracker_status(_("Announce Sent")) - except KeyError: - log.debug("torrent_id doesn't exist.") + self.torrents[torrent_id].set_tracker_status(_("Announce Sent")) def on_alert_tracker(self, alert): log.debug("on_alert_tracker") @@ -552,10 +552,7 @@ tracker_status = "%s: %s" % \ (_("Alert"), str(alert.msg()).strip('"')[8:]) # Set the tracker status for the torrent - try: - self.torrents[torrent_id].set_tracker_status(tracker_status) - except KeyError: - log.debug("torrent_id doesn't exist.") + self.torrents[torrent_id].set_tracker_status(tracker_status) def on_alert_tracker_warning(self, alert): log.debug("on_alert_tracker_warning") @@ -563,17 +560,11 @@ torrent_id = str(alert.handle.info_hash()) tracker_status = '%s: %s' % (_("Warning"), str(alert.msg())) # Set the tracker status for the torrent - try: - self.torrents[torrent_id].set_tracker_status(tracker_status) - except KeyError: - log.debug("torrent_id doesn't exist.") + self.torrents[torrent_id].set_tracker_status(tracker_status) def on_alert_storage_moved(self, alert): log.debug("on_alert_storage_moved") # Get the torrent_id torrent_id = str(alert.handle.info_hash()) - try: - self.torrents[torrent_id].set_save_path(alert.handle.save_path()) - except KeyError: - log.debug("torrent_id doesn't exist.") - + self.torrents[torrent_id].set_save_path(alert.handle.save_path()) + Index: deluge/ui/client.py =================================================================== --- deluge/ui/client.py (revision 2796) +++ deluge/ui/client.py (working copy) @@ -99,6 +99,8 @@ except socket.error, e: log.warning("Could not contact daemon: %s", e) self.set_core_uri(None) + except deluge.xmlrpclib.Fault, e: + deluge.error.from_xmlrpc_fault(e) finally: self._callbacks = [] Index: deluge/ui/null/deluge_shell.py =================================================================== --- deluge/ui/null/deluge_shell.py (revision 2796) +++ deluge/ui/null/deluge_shell.py (working copy) @@ -346,6 +346,10 @@ print "*** Operation failed. You are not connected to a deluge daemon." print " Perhaps you want to 'connect' first." print "" + except deluge.error.InvalidTorrent, e: + print "*** Operation failed. You tried to perform an operation on a non-existent torrent." + print " Use the 'info' command for the list of torrents." + print "" print "Thanks."