diff --git a/deluge/ui/console/eventlog.py b/deluge/ui/console/eventlog.py index 8dc9bddc3..c1ee6abe4 100644 --- a/deluge/ui/console/eventlog.py +++ b/deluge/ui/console/eventlog.py @@ -1,6 +1,7 @@ import time import deluge.component as component +from deluge.decorators import maybe_coroutine from deluge.ui.client import client from deluge.ui.console.utils import colors @@ -16,60 +17,44 @@ class EventLog(component.Component): self.prefix = '{!event!}* [%H:%M:%S] ' self.date_change_format = 'On {!yellow!}%a, %d %b %Y{!input!} %Z:' - client.register_event_handler('TorrentAddedEvent', self.on_torrent_added_event) - client.register_event_handler( - 'PreTorrentRemovedEvent', self.on_torrent_removed_event - ) - client.register_event_handler( - 'TorrentStateChangedEvent', self.on_torrent_state_changed_event - ) - client.register_event_handler( - 'TorrentFinishedEvent', self.on_torrent_finished_event - ) - client.register_event_handler( - 'NewVersionAvailableEvent', self.on_new_version_available_event - ) - client.register_event_handler( - 'SessionPausedEvent', self.on_session_paused_event - ) - client.register_event_handler( - 'SessionResumedEvent', self.on_session_resumed_event - ) - client.register_event_handler( - 'ConfigValueChangedEvent', self.on_config_value_changed_event - ) - client.register_event_handler( - 'PluginEnabledEvent', self.on_plugin_enabled_event - ) - client.register_event_handler( - 'PluginDisabledEvent', self.on_plugin_disabled_event - ) + event_callbacks = { + 'TorrentAddedEvent': self.on_torrent_added, + 'PreTorrentRemovedEvent': self.on_torrent_removed, + 'TorrentStateChangedEvent': self.on_torrent_state_changed, + 'TorrentFinishedEvent': self.on_torrent_finished, + 'NewVersionAvailableEvent': self.on_new_version_available, + 'SessionPausedEvent': self.on_session_paused, + 'SessionResumedEvent': self.on_session_resumed, + 'ConfigValueChangedEvent': self.on_config_value_changed, + 'PluginEnabledEvent': self.on_plugin_enabled, + 'PluginDisabledEvent': self.on_plugin_disabled, + } + + for event, callback in event_callbacks.items(): + client.register_event_handler(event, callback) self.previous_time = time.localtime(0) - def on_torrent_added_event(self, torrent_id, from_state): + @maybe_coroutine + async def on_torrent_added(self, torrent_id, from_state): if from_state: return - def on_torrent_status(status): - self.write( - '{!green!}Torrent Added: {!info!}%s ({!cyan!}%s{!info!})' - % (status['name'], torrent_id) - ) - # Write out what state the added torrent took - self.on_torrent_state_changed_event(torrent_id, status['state']) - - client.core.get_torrent_status(torrent_id, ['name', 'state']).addCallback( - on_torrent_status + status = await client.core.get_torrent_status(torrent_id, ['name', 'state']) + self.write( + '{!green!}Torrent Added: {!info!}%s ({!cyan!}%s{!info!})' + % (status['name'], torrent_id) ) + # Write out what state the added torrent took + self.on_torrent_state_changed(torrent_id, status['state']) - def on_torrent_removed_event(self, torrent_id): + def on_torrent_removed(self, torrent_id): self.write( '{!red!}Torrent Removed: {!info!}%s ({!cyan!}%s{!info!})' % (self.console.get_torrent_name(torrent_id), torrent_id) ) - def on_torrent_state_changed_event(self, torrent_id, state): + def on_torrent_state_changed(self, torrent_id, state): # It's probably a new torrent, ignore it if not state: return @@ -85,7 +70,7 @@ class EventLog(component.Component): self.write(f'{state}: {{!info!}}{t_name} ({{!cyan!}}{torrent_id}{{!info!}})') - def on_torrent_finished_event(self, torrent_id): + def on_torrent_finished(self, torrent_id): if component.get('TorrentList').config['ring_bell']: import curses.beep @@ -95,16 +80,16 @@ class EventLog(component.Component): % (self.console.get_torrent_name(torrent_id), torrent_id) ) - def on_new_version_available_event(self, version): + def on_new_version_available(self, version): self.write('{!input!}New Deluge version available: {!info!}%s' % (version)) - def on_session_paused_event(self): + def on_session_paused(self): self.write('{!input!}Session Paused') - def on_session_resumed_event(self): + def on_session_resumed(self): self.write('{!green!}Session Resumed') - def on_config_value_changed_event(self, key, value): + def on_config_value_changed(self, key, value): color = '{!white,black,bold!}' try: color = colors.type_color[type(value)] @@ -133,8 +118,8 @@ class EventLog(component.Component): self.console.write_event(p + s) self.previous_time = current_time - def on_plugin_enabled_event(self, name): + def on_plugin_enabled(self, name): self.write('PluginEnabled: {!info!}%s' % name) - def on_plugin_disabled_event(self, name): + def on_plugin_disabled(self, name): self.write('PluginDisabled: {!info!}%s' % name)