From f4f4accd345e8211b1b753681f9717640b84d7f6 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Mon, 27 Feb 2023 13:29:12 +0000 Subject: [PATCH] [Console] Move eventlog class to separate file --- deluge/ui/console/eventlog.py | 140 ++++++++++++++++++++++++++++++++++ deluge/ui/console/main.py | 137 +-------------------------------- 2 files changed, 141 insertions(+), 136 deletions(-) create mode 100644 deluge/ui/console/eventlog.py diff --git a/deluge/ui/console/eventlog.py b/deluge/ui/console/eventlog.py new file mode 100644 index 000000000..8dc9bddc3 --- /dev/null +++ b/deluge/ui/console/eventlog.py @@ -0,0 +1,140 @@ +import time + +import deluge.component as component +from deluge.ui.client import client +from deluge.ui.console.utils import colors + + +class EventLog(component.Component): + """ + Prints out certain events as they are received from the core. + """ + + def __init__(self): + component.Component.__init__(self, 'EventLog') + self.console = component.get('ConsoleUI') + 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 + ) + + self.previous_time = time.localtime(0) + + def on_torrent_added_event(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 + ) + + def on_torrent_removed_event(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): + # It's probably a new torrent, ignore it + if not state: + return + # Modify the state string color + if state in colors.state_color: + state = colors.state_color[state] + state + + t_name = self.console.get_torrent_name(torrent_id) + + # Again, it's most likely a new torrent + if not t_name: + return + + self.write(f'{state}: {{!info!}}{t_name} ({{!cyan!}}{torrent_id}{{!info!}})') + + def on_torrent_finished_event(self, torrent_id): + if component.get('TorrentList').config['ring_bell']: + import curses.beep + + curses.beep() + self.write( + '{!info!}Torrent Finished: %s ({!cyan!}%s{!info!})' + % (self.console.get_torrent_name(torrent_id), torrent_id) + ) + + def on_new_version_available_event(self, version): + self.write('{!input!}New Deluge version available: {!info!}%s' % (version)) + + def on_session_paused_event(self): + self.write('{!input!}Session Paused') + + def on_session_resumed_event(self): + self.write('{!green!}Session Resumed') + + def on_config_value_changed_event(self, key, value): + color = '{!white,black,bold!}' + try: + color = colors.type_color[type(value)] + except KeyError: + pass + + self.write(f'ConfigValueChanged: {{!input!}}{key}: {color}{value}') + + def write(self, s): + current_time = time.localtime() + + date_different = False + for field in ['tm_mday', 'tm_mon', 'tm_year']: + c = getattr(current_time, field) + p = getattr(self.previous_time, field) + if c != p: + date_different = True + + if date_different: + string = time.strftime(self.date_change_format) + self.console.write_event(' ') + self.console.write_event(string) + + p = time.strftime(self.prefix) + + self.console.write_event(p + s) + self.previous_time = current_time + + def on_plugin_enabled_event(self, name): + self.write('PluginEnabled: {!info!}%s' % name) + + def on_plugin_disabled_event(self, name): + self.write('PluginDisabled: {!info!}%s' % name) diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index eb7cf6ee4..ae6f5aef9 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -11,7 +11,6 @@ import locale import logging import os import sys -import time from twisted.internet import defer, error, reactor @@ -20,6 +19,7 @@ import deluge.component as component from deluge.configmanager import ConfigManager from deluge.decorators import maybe_coroutine, overrides from deluge.ui.client import client +from deluge.ui.console.eventlog import EventLog from deluge.ui.console.modes.addtorrents import AddTorrents from deluge.ui.console.modes.basemode import TermResizeHandler from deluge.ui.console.modes.cmdline import CmdLine @@ -585,138 +585,3 @@ deluge-console.exe "add -p c:\\mytorrents c:\\new.torrent" ]['order'] return config - - -class EventLog(component.Component): - """ - Prints out certain events as they are received from the core. - """ - - def __init__(self): - component.Component.__init__(self, 'EventLog') - self.console = component.get('ConsoleUI') - 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 - ) - - self.previous_time = time.localtime(0) - - def on_torrent_added_event(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 - ) - - def on_torrent_removed_event(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): - # It's probably a new torrent, ignore it - if not state: - return - # Modify the state string color - if state in colors.state_color: - state = colors.state_color[state] + state - - t_name = self.console.get_torrent_name(torrent_id) - - # Again, it's most likely a new torrent - if not t_name: - return - - self.write(f'{state}: {{!info!}}{t_name} ({{!cyan!}}{torrent_id}{{!info!}})') - - def on_torrent_finished_event(self, torrent_id): - if component.get('TorrentList').config['ring_bell']: - import curses.beep - - curses.beep() - self.write( - '{!info!}Torrent Finished: %s ({!cyan!}%s{!info!})' - % (self.console.get_torrent_name(torrent_id), torrent_id) - ) - - def on_new_version_available_event(self, version): - self.write('{!input!}New Deluge version available: {!info!}%s' % (version)) - - def on_session_paused_event(self): - self.write('{!input!}Session Paused') - - def on_session_resumed_event(self): - self.write('{!green!}Session Resumed') - - def on_config_value_changed_event(self, key, value): - color = '{!white,black,bold!}' - try: - color = colors.type_color[type(value)] - except KeyError: - pass - - self.write(f'ConfigValueChanged: {{!input!}}{key}: {color}{value}') - - def write(self, s): - current_time = time.localtime() - - date_different = False - for field in ['tm_mday', 'tm_mon', 'tm_year']: - c = getattr(current_time, field) - p = getattr(self.previous_time, field) - if c != p: - date_different = True - - if date_different: - string = time.strftime(self.date_change_format) - self.console.write_event(' ') - self.console.write_event(string) - - p = time.strftime(self.prefix) - - self.console.write_event(p + s) - self.previous_time = current_time - - def on_plugin_enabled_event(self, name): - self.write('PluginEnabled: {!info!}%s' % name) - - def on_plugin_disabled_event(self, name): - self.write('PluginDisabled: {!info!}%s' % name)