From 824067e238ac4e622ec817c71db6f2878a1a9abd Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Tue, 29 Sep 2015 19:23:20 +0100 Subject: [PATCH] [Core] Emit TorrentStateChangedEvent in update_state --- deluge/core/torrent.py | 18 +++++++++--------- deluge/core/torrentmanager.py | 24 +++--------------------- deluge/tests/test_torrent.py | 2 ++ 3 files changed, 14 insertions(+), 30 deletions(-) diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index 1c557c357..33193c4dc 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -598,35 +598,35 @@ class Torrent(object): def update_state(self): """Updates the state, based on libtorrent's torrent state""" status = self.handle.status() - session_is_paused = component.get("Core").session.is_paused() - + session_paused = component.get("Core").session.is_paused() + old_state = self.state if status.error or self.error_statusmsg: self.state = "Error" # This will be reverted upon resuming. self.handle.auto_managed(False) if not status.paused: self.handle.pause() - if status.error: self.set_error_statusmsg(decode_string(status.error)) log.debug("Error state from lt: %s", self.error_statusmsg) else: - # As this is not a libtorrent Error we should emit a state changed event - component.get("EventManager").emit(TorrentStateChangedEvent(self.torrent_id, "Error")) log.debug("Error state forced by Deluge, error_statusmsg: %s", self.error_statusmsg) self.set_status_message(self.error_statusmsg) elif self.moving_storage: self.state = "Moving" - elif not session_is_paused and status.paused and status.auto_managed: + elif not session_paused and status.paused and status.auto_managed: self.state = "Queued" - elif session_is_paused or status.paused: + elif session_paused or status.paused: self.state = "Paused" else: self.state = LT_TORRENT_STATE_MAP.get(str(status.state), str(status.state)) + if self.state != old_state: + component.get("EventManager").emit(TorrentStateChangedEvent(self.torrent_id, self.state)) + if log.isEnabledFor(logging.DEBUG): - log.debug("State from lt was: %s | Session is paused: %s", status.state, session_is_paused) - log.debug("Torrent state set to '%s' (%s)", self.state, self.torrent_id) + log.debug("State from lt was: %s | Session is paused: %s\nTorrent state set from '%s' to '%s' (%s)", + status.state, session_paused, old_state, self.state, self.torrent_id) def set_status_message(self, message): """Sets the torrent status message. diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index f276529ea..63fce1ab1 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -28,8 +28,7 @@ from deluge.core.authmanager import AUTH_LEVEL_ADMIN from deluge.core.torrent import Torrent, TorrentOptions, sanitize_filepath from deluge.error import InvalidTorrentError from deluge.event import (PreTorrentRemovedEvent, SessionStartedEvent, TorrentAddedEvent, TorrentFileCompletedEvent, - TorrentFileRenamedEvent, TorrentFinishedEvent, TorrentRemovedEvent, TorrentResumedEvent, - TorrentStateChangedEvent) + TorrentFileRenamedEvent, TorrentFinishedEvent, TorrentRemovedEvent, TorrentResumedEvent) log = logging.getLogger(__name__) @@ -888,12 +887,7 @@ class TorrentManager(component.Component): torrent = self.torrents[torrent_id] except (RuntimeError, KeyError): return - # Set the torrent state - old_state = torrent.state torrent.update_state() - if torrent.state != old_state: - component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state)) - # Write the fastresume file if we are not waiting on a bulk write if torrent_id not in self.waiting_on_resume_data: self.save_resume_data((torrent_id,)) @@ -907,14 +901,12 @@ class TorrentManager(component.Component): except (RuntimeError, KeyError): return - # Check to see if we're forcing a recheck and set it back to paused - # if necessary + # Check to see if we're forcing a recheck and set it back to paused if necessary. if torrent.forcing_recheck: torrent.forcing_recheck = False if torrent.forcing_recheck_paused: torrent.handle.pause() - # Set the torrent state torrent.update_state() def on_alert_tracker_reply(self, alert): @@ -1016,11 +1008,7 @@ class TorrentManager(component.Component): torrent = self.torrents[torrent_id] except (RuntimeError, KeyError): return - old_state = torrent.state torrent.update_state() - if torrent.state != old_state: - # We need to emit a TorrentStateChangedEvent too - component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state)) component.get("EventManager").emit(TorrentResumedEvent(torrent_id)) def on_alert_state_changed(self, alert): @@ -1035,18 +1023,12 @@ class TorrentManager(component.Component): except (RuntimeError, KeyError): return - old_state = torrent.state torrent.update_state() - # Torrent may need to download data after checking. - if torrent.state in ('Checking', 'Checking Resume Data', 'Downloading'): + if torrent.state in ('Checking', 'Downloading'): torrent.is_finished = False self.queued_torrents.add(torrent_id) - # Only emit a state changed event if the state has actually changed - if torrent.state != old_state: - component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state)) - def on_alert_save_resume_data(self, alert): """Alert handler for libtorrent save_resume_data_alert""" if log.isEnabledFor(logging.DEBUG): diff --git a/deluge/tests/test_torrent.py b/deluge/tests/test_torrent.py index 53e0356a3..90774713e 100644 --- a/deluge/tests/test_torrent.py +++ b/deluge/tests/test_torrent.py @@ -25,6 +25,8 @@ def get(key): return core elif key is "RPCServer": return rpcserver + elif key is "EventManager": + return core.eventmanager else: return None