From f185f19cfb298978b51376871150d9693fb82b96 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Wed, 22 Aug 2007 08:24:40 +0000 Subject: [PATCH] Plugins can now register 'status_fields' with core for use with get_status(). Updates to TorrentQueue plugin. --- deluge/core/core.py | 6 +++++- deluge/core/pluginmanager.py | 17 +++++++++++++++++ deluge/plugins/queue/queue/core.py | 7 ++++++- deluge/plugins/queue/queue/gtkui.py | 5 ++++- deluge/ui/gtkui/torrentview.py | 9 +++++---- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/deluge/core/core.py b/deluge/core/core.py index bb84e7081..a039a2ca9 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -155,8 +155,12 @@ class Core(dbus.service.Object): nkeys = [] for key in keys: nkeys.append(str(key)) - # Pickle the status dictionary from the torrent and return it + # Pickle the status dictionary from the torrent status = self.torrents[torrent_id].get_status(nkeys) + # Get the leftover fields and ask the plugin manager to fill them + leftover_fields = list(set(nkeys) - set(status.keys())) + if len(leftover_fields) > 0: + status.update(self.plugins.get_status(torrent_id, leftover_fields)) status = pickle.dumps(status) return status diff --git a/deluge/core/pluginmanager.py b/deluge/core/pluginmanager.py index b40bc5522..27fcc48ed 100644 --- a/deluge/core/pluginmanager.py +++ b/deluge/core/pluginmanager.py @@ -47,6 +47,8 @@ class PluginManager: "post_torrent_remove": [] } + self.status_fields = {} + # This will load any .eggs in the plugins folder inside the main # deluge egg.. Need to scan the local plugin folder too. @@ -70,6 +72,21 @@ class PluginManager: def __getitem__(self, key): return self.plugins[key] + def register_status_field(self, field, function): + """Register a new status field. This can be used in the same way the + client requests other status information from core.""" + self.status_fields[field] = function + + def get_status(self, torrent_id, fields): + status = {} + for field in fields: + try: + status[field] = self.status_fields[field](torrent_id) + except KeyError: + log.warning("Status field %s is not registered with the\ + PluginManager.") + return status + def register_hook(self, hook, function): """Register a hook function with the plugin manager""" try: diff --git a/deluge/plugins/queue/queue/core.py b/deluge/plugins/queue/queue/core.py index 1fdbc0d8a..87a9f3168 100644 --- a/deluge/plugins/queue/queue/core.py +++ b/deluge/plugins/queue/queue/core.py @@ -69,6 +69,7 @@ class Core(dbus.service.Object): self.plugin.register_hook("post_torrent_add", self.post_torrent_add) self.plugin.register_hook("post_torrent_remove", self.post_torrent_remove) + self.plugin.register_status_field("queue", self.status_field_queue) log.info("Queue Core plugin initialized..") @@ -80,7 +81,11 @@ class Core(dbus.service.Object): def post_torrent_remove(self, torrent_id): if torrent_id is not None: self.queue.remove(torrent_id) - + + ## Status field function ## + def status_field_queue(self, torrent_id): + return self.queue[torrent_id]+1 + ## Queueing functions ## @dbus.service.method(dbus_interface="org.deluge_torrent.Deluge.Queue", in_signature="s", out_signature="") diff --git a/deluge/plugins/queue/queue/gtkui.py b/deluge/plugins/queue/queue/gtkui.py index 31cba2b50..ff1ea661c 100644 --- a/deluge/plugins/queue/queue/gtkui.py +++ b/deluge/plugins/queue/queue/gtkui.py @@ -69,7 +69,8 @@ class GtkUI: self.torrentview.add_text_column("#", col_type=int, position=0, - get_function=self.column_get_function) + get_function=self.column_get_function, + status_field=["queue"]) # Add a toolbar buttons self.plugin.get_toolbar().add_separator() self.plugin.get_toolbar().add_toolbutton(stock="gtk-go-up", @@ -103,6 +104,8 @@ class GtkUI: signal from the core plugin. """ log.debug("torrent_queue_changed signal received..") + # We only need to update the queue column + self.torrentview.update(["#"]) return def column_get_function(self, torrent_id): diff --git a/deluge/ui/gtkui/torrentview.py b/deluge/ui/gtkui/torrentview.py index 961efef5c..103757681 100644 --- a/deluge/ui/gtkui/torrentview.py +++ b/deluge/ui/gtkui/torrentview.py @@ -126,10 +126,10 @@ class TorrentView(listview.ListView): else: # Iterate through supplied list of columns to update for column in columns: - if self.columns[column.name].column.get_visible() is True \ - and self.columns[column.name].hidden is False \ - and self.columns[column.name].status_field is not None: - for field in self.columns[column.name].status_field: + if self.columns[column].column.get_visible() is True \ + and self.columns[column].hidden is False \ + and self.columns[column].status_field is not None: + for field in self.columns[column].status_field: status_keys.append(field) columns_to_update.append(column) @@ -169,6 +169,7 @@ class TorrentView(listview.ListView): def add_row(self, torrent_id): """Adds a new torrent row to the treeview""" + ## REWRITE TO USE STATUS FIELDS # Get the status and info dictionaries status_keys = ["name", "total_size", "progress", "state", "num_seeds", "num_peers", "download_payload_rate",