From ae22a52f2f4c7b3d41b0b81c0fbc19a5b38da822 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Mon, 27 Feb 2023 13:25:05 +0000 Subject: [PATCH] [Console] Refactor callbacks and cleanup main --- deluge/ui/console/main.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index af89dbf44..eb7cf6ee4 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -365,27 +365,23 @@ deluge-console.exe "add -p c:\\mytorrents c:\\new.torrent" self.started_deferred.callback(True) # Register event handlers to keep the torrent list up-to-date - client.register_event_handler('TorrentAddedEvent', self.on_torrent_added_event) - client.register_event_handler( - 'TorrentRemovedEvent', self.on_torrent_removed_event - ) + client.register_event_handler('TorrentAddedEvent', self.on_torrent_added) + client.register_event_handler('TorrentRemovedEvent', self.on_torrent_removed) - def on_torrent_added_event(self, event, from_state=False): - def on_torrent_status(status): - self.torrents.append((event, status['name'])) + @defer.inlineCallbacks + def on_torrent_added(self, event, from_state=False): + status = yield client.core.get_torrent_status(event, ['name']) + self.torrents.append((event, status['name'])) - client.core.get_torrent_status(event, ['name']).addCallback(on_torrent_status) - - def on_torrent_removed_event(self, event): + def on_torrent_removed(self, event): for index, (tid, name) in enumerate(self.torrents): if event == tid: del self.torrents[index] def match_torrents(self, strings): - torrent_ids = [] - for s in strings: - torrent_ids.extend(self.match_torrent(s)) - return list(set(torrent_ids)) + return list( + {torrent for string in strings for torrent in self.match_torrent(string)} + ) def match_torrent(self, string): """