From 73e58c20c8a312af5da72b898b60418a4c22c87d Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sun, 19 Apr 2009 19:57:06 +0000 Subject: [PATCH] Add event logging --- deluge/ui/console/colors.py | 3 +- deluge/ui/console/eventlog.py | 95 +++++++++++++++++++++++++++++++++++ deluge/ui/console/main.py | 18 ++++++- 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 deluge/ui/console/eventlog.py diff --git a/deluge/ui/console/colors.py b/deluge/ui/console/colors.py index 242b9fa3c..304864313 100644 --- a/deluge/ui/console/colors.py +++ b/deluge/ui/console/colors.py @@ -46,7 +46,8 @@ schemes = { "status": ("yellow", "blue", "bold"), "info": ("white", "black", "bold"), "error": ("red", "black", "bold"), - "success": ("green", "black", "bold") + "success": ("green", "black", "bold"), + "event": ("magenta", "black", "bold") } diff --git a/deluge/ui/console/eventlog.py b/deluge/ui/console/eventlog.py new file mode 100644 index 000000000..daafd9d7b --- /dev/null +++ b/deluge/ui/console/eventlog.py @@ -0,0 +1,95 @@ +# +# eventlog.py +# +# Copyright (C) 2009 Andrew Resch +# +# Deluge is free software. +# +# You may redistribute it and/or modify it under the terms of the +# GNU General Public License, as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) +# any later version. +# +# deluge is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with deluge. If not, write to: +# The Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor +# Boston, MA 02110-1301, USA. +# + + +import deluge.component as component +import deluge.common +from deluge.ui.client import client + +from deluge.log import LOG as log + +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") + + 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) + + def on_torrent_added_event(self, torrent_id): + def on_torrent_status(status): + self.console.write("{{event}}* TorrentAdded: {{info}}%s (%s)" % (status["name"], torrent_id)) + client.core.get_torrent_status(torrent_id, ["name"]).addCallback(on_torrent_status) + + def on_torrent_removed_event(self, torrent_id): + self.console.write("{{event}}* TorrentRemovedEvent: {{info}}%s (%s)" % + (self.console.get_torrent_name(torrent_id), torrent_id)) + + def on_torrent_state_changed_event(self, torrent_id, state): + log.debug("on_torrent_state_changed_event!") + # Modify the state string color + state_color = { + "Seeding": "{{blue,black,bold}}", + "Downloading": "{{green,black,bold}}", + "Paused": "{{white,black}}", + "Checking": "{{green,black}}", + "Queued": "{{yellow,black}}", + "Error": "{{red,black,bold}}" + } + if state in state_color: + state = state_color[state] + state + + self.console.write("{{event}}* TorrentStateChanged: %s {{info}}%s (%s)" % + (state, self.console.get_torrent_name(torrent_id), torrent_id)) + + def on_torrent_paused_event(self, torrent_id): + self.console.write("{{event}}* TorrentPaused: {{info}}%s (%s)" % + (self.console.get_torrent_name(torrent_id), torrent_id)) + + def on_torrent_finished_event(self, torrent_id): + self.console.write("{{event}}* TorrentFinished: {{info}}%s (%s)" % + (self.console.get_torrent_name(torrent_id), torrent_id)) + + def on_new_version_available_event(self, version): + self.console.write("{{event}}* NewVersionAvailable: {{info}}%s" % + (version)) + + def on_session_paused_event(self): + self.console.write("{{event}}* SessionPaused") + + def on_session_resumed_event(self): + self.console.write("{{event}}* SessionResumed") + + def on_config_value_changed_event(self, key, value): + self.console.write("{{event}}* ConfigValueChanged: %s: %s" % + (key, value)) diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index 39861b419..5517246c6 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -26,12 +26,12 @@ import os, sys import optparse from deluge.ui.console import UI_PATH -#from deluge.ui.console.colors import Template, make_style, templates, default_style as style import deluge.component as component from deluge.ui.client import client import deluge.common from deluge.ui.coreconfig import CoreConfig from deluge.ui.console.statusbars import StatusBars +from deluge.ui.console.eventlog import EventLog from twisted.internet import defer, reactor import shlex @@ -163,6 +163,7 @@ class ConsoleUI(component.Component): colors.init_colors() self.screen = screen.Screen(stdscr, self.do_command, self.tab_completer) self.statusbars = StatusBars() + self.eventlog = EventLog() self.screen.topbar = "{{status}}Deluge " + deluge.common.get_version() + " Console" self.screen.bottombar = "{{status}}" @@ -314,6 +315,21 @@ class ConsoleUI(component.Component): self.write(cmd) return (line, cursor) + def get_torrent_name(self, torrent_id): + """ + Gets a torrent name from the torrents list. + + :param torrent_id: str, the torrent_id + + :returns: the name of the torrent or None + """ + + for tid, name in self.torrents: + if torrent_id == tid: + return name + + return None + def on_torrent_added_event(self, torrent_id): def on_torrent_status(status): self.torrents.append(torrent_id, status["name"])