change get_torrents_status() api for filtering in core

This commit is contained in:
Martijn Voncken 2008-08-16 14:48:43 +00:00
commit 14db4bc999
6 changed files with 107 additions and 97 deletions

View file

@ -507,32 +507,45 @@ class Core(
status.update(self.plugins.get_status(torrent_id, leftover_fields)) status.update(self.plugins.get_status(torrent_id, leftover_fields))
return status return status
def export_get_dev_torrents_status(self, filter_dict, keys ): def filter_torrent_ids(self, filter_dict):
"""
internal :
returns a list of torrent_id's matching filter_dict.
"""
if not filter_dict:
return self.torrents.get_torrent_list()
if "id"in filter_dict: #optimized filter for id:
torrent_ids = filter_dict["id"]
del filter_dict["id"]
else:
torrent_ids = self.torrents.get_torrent_list()
#todo:
#register/deregister special filters like "text search" and "active"
#
#leftover filter arguments:
#default filter on status fields.
if filter_dict:
for torrent_id in list(torrent_ids):
status = self.export_get_torrent_status(torrent_id, filter_dict.keys()) #status={id:{key:value}}
for field, value_list in filter_dict.iteritems():
if (not status[field] in value_list) and torrent_id in torrent_ids:
torrent_ids.remove(torrent_id)
return torrent_ids
def export_get_torrents_status(self, filter_dict, keys ):
""" """
returns all torrents , optionally filtered by filter_dict. returns all torrents , optionally filtered by filter_dict.
""" """
if filter_dict: torrent_ids = self.filter_torrent_ids(filter_dict)
raise NotImplementedError("not yet")
torrent_ids = self.torrents.get_torrent_list()
return self.export_get_torrents_status(torrent_ids, keys)
def export_get_torrents_status(self, torrent_ids, keys):
status_dict = {}.fromkeys(torrent_ids) status_dict = {}.fromkeys(torrent_ids)
# Get the torrent status for each torrent_id # Get the torrent status for each torrent_id
for torrent_id in torrent_ids: for torrent_id in torrent_ids:
try: status_dict[torrent_id] = self.export_get_torrent_status(torrent_id, keys)
status = self.torrents[torrent_id].get_status(keys)
except KeyError:
return None
# Get the leftover fields and ask the plugin manager to fill them
leftover_fields = list(set(keys) - set(status.keys()))
if len(leftover_fields) > 0:
status.update(
self.plugins.get_status(torrent_id, leftover_fields))
status_dict[torrent_id] = status
# Emit the torrent_status signal to the clients # Emit the torrent_status signal to the clients
return status_dict return status_dict

View file

@ -33,7 +33,7 @@ print len(sclient.label_get_filtered_ids({'state':'Paused','tracker':'tracker.ae
print "#test status-fields:" print "#test status-fields:"
ids = sclient.get_session_state() ids = sclient.get_session_state()
torrents = sclient.get_torrents_status(ids,['name', 'tracker_host', 'label']) torrents = sclient.get_torrents_status({"id":ids},['name', 'tracker_host', 'label'])
for id,torrent in torrents.iteritems(): for id,torrent in torrents.iteritems():
print id, torrent print id, torrent

View file

@ -2,6 +2,7 @@
# moving and refactoring torrent-filtering from labels-plugin to core. # moving and refactoring torrent-filtering from labels-plugin to core.
# #
KEYS = ["name","state", "label"]
#init: #init:
from deluge.ui.client import sclient from deluge.ui.client import sclient
sclient.set_core_uri() sclient.set_core_uri()
@ -17,31 +18,27 @@ print torrent_id
print sorted(sclient.get_torrent_status(torrent_id,[]).keys()) print sorted(sclient.get_torrent_status(torrent_id,[]).keys())
print sorted(sclient.get_status_keys()) print sorted(sclient.get_status_keys())
#default, no filter argument. print "#default, no filter argument."
print sclient.get_dev_torrents_status(None, ["name","state"]) print sclient.get_torrents_status(None, KEYS)
print "HI! , after this the errors start"
#torrent_id filters and list-arguments:
print sclient.get_dev_torrents_status({"id":torrent_id}, ["name","state"])
print sclient.get_dev_torrents_status({"id":[torrent_id, torrent_id2]}, ["name","state"])
#filters on default state fields
print sclient.get_dev_torrents_status({"state":"Paused"}, ["name","state"])
print sclient.get_dev_torrents_status({"state":["Paused","Downloading"]}, ["name","state"])
print sclient.get_dev_torrents_status({"tracker_host":"aelitis.com"}, ["name","state"])
#plugin status fields:
print sclient.get_dev_torrents_status({"label":"test"}, ["name","state"])
print sclient.get_dev_torrents_status({"label":["test","tpb"]}, ["name","state"])
#special filters:
print sclient.get_dev_torrents_status({"keyword":"az"}, ["name","state"])
print "#torrent_id filter:"
print sclient.get_torrents_status({"id":[torrent_id, torrent_id2]}, KEYS)
print "#filters on default status fields:"
#print sclient.get_torrents_status({"state":"Paused"}, KEYS)
print sclient.get_torrents_status({"state":["Paused","Downloading"]}, KEYS)
print sclient.get_torrents_status({"tracker_host":["aelitis.com"]}, KEYS)
print "#status fields from plugins:"
#print sclient.get_torrents_status({"label":"test"}, KEYS)
print sclient.get_torrents_status({"label":["test","tpb"]}, KEYS)
print "#special filters (ERRORS START HERE!):"
print sclient.get_torrents_status({"keyword":"az"}, KEYS)
print sclient.get_torrents_status({"state":"Active"}, KEYS)

View file

@ -2,19 +2,19 @@
# torrentview.py # torrentview.py
# #
# Copyright (C) 2007, 2008 Andrew Resch ('andar') <andrewresch@gmail.com> # Copyright (C) 2007, 2008 Andrew Resch ('andar') <andrewresch@gmail.com>
# #
# Deluge is free software. # Deluge is free software.
# #
# You may redistribute it and/or modify it under the terms of the # You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software # GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) # Foundation; either version 3 of the License, or (at your option)
# any later version. # any later version.
# #
# deluge is distributed in the hope that it will be useful, # deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details. # See the GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with deluge. If not, write to: # along with deluge. If not, write to:
# The Free Software Foundation, Inc., # The Free Software Foundation, Inc.,
@ -60,7 +60,7 @@ icon_queued = gtk.gdk.pixbuf_new_from_file(
deluge.common.get_pixmap("queued16.png")) deluge.common.get_pixmap("queued16.png"))
icon_checking = gtk.gdk.pixbuf_new_from_file( icon_checking = gtk.gdk.pixbuf_new_from_file(
deluge.common.get_pixmap("checking16.png")) deluge.common.get_pixmap("checking16.png"))
# Holds the info for which status icon to display based on state # Holds the info for which status icon to display based on state
ICON_STATE = { ICON_STATE = {
"Allocating": icon_checking, "Allocating": icon_checking,
@ -71,7 +71,7 @@ ICON_STATE = {
"Error": icon_alert, "Error": icon_alert,
"Queued": icon_queued "Queued": icon_queued
} }
def cell_data_statusicon(column, cell, model, row, data): def cell_data_statusicon(column, cell, model, row, data):
"""Display text with an icon""" """Display text with an icon"""
try: try:
@ -80,7 +80,7 @@ def cell_data_statusicon(column, cell, model, row, data):
cell.set_property("pixbuf", icon) cell.set_property("pixbuf", icon)
except KeyError: except KeyError:
pass pass
def cell_data_progress(column, cell, model, row, data): def cell_data_progress(column, cell, model, row, data):
"""Display progress bar with text""" """Display progress bar with text"""
(value, state_str) = model.get(row, *data) (value, state_str) = model.get(row, *data)
@ -89,7 +89,7 @@ def cell_data_progress(column, cell, model, row, data):
textstr = "%s" % state_str textstr = "%s" % state_str
if state_str != "Seeding" and value < 100: if state_str != "Seeding" and value < 100:
textstr = textstr + " %.2f%%" % value textstr = textstr + " %.2f%%" % value
if cell.get_property("text") != textstr: if cell.get_property("text") != textstr:
cell.set_property("text", textstr) cell.set_property("text", textstr)
@ -99,18 +99,18 @@ def cell_data_queue(column, cell, model, row, data):
cell.set_property("text", "") cell.set_property("text", "")
else: else:
cell.set_property("text", value + 1) cell.set_property("text", value + 1)
class TorrentView(listview.ListView, component.Component): class TorrentView(listview.ListView, component.Component):
"""TorrentView handles the listing of torrents.""" """TorrentView handles the listing of torrents."""
def __init__(self): def __init__(self):
component.Component.__init__(self, "TorrentView", interval=2000) component.Component.__init__(self, "TorrentView", interval=2000)
self.window = component.get("MainWindow") self.window = component.get("MainWindow")
# Call the ListView constructor # Call the ListView constructor
listview.ListView.__init__(self, listview.ListView.__init__(self,
self.window.main_glade.get_widget("torrent_view"), self.window.main_glade.get_widget("torrent_view"),
"torrentview.state") "torrentview.state")
log.debug("TorrentView Init..") log.debug("TorrentView Init..")
# This is where status updates are put # This is where status updates are put
self.status = {} self.status = {}
@ -118,30 +118,30 @@ class TorrentView(listview.ListView, component.Component):
# accordingly. # accordingly.
self.register_checklist_menu( self.register_checklist_menu(
self.window.main_glade.get_widget("menu_columns")) self.window.main_glade.get_widget("menu_columns"))
# Add the columns to the listview # Add the columns to the listview
self.add_text_column("torrent_id", hidden=True) self.add_text_column("torrent_id", hidden=True)
self.add_bool_column("dirty", hidden=True) self.add_bool_column("dirty", hidden=True)
self.add_func_column("#", cell_data_queue, [int], status_field=["queue"]) self.add_func_column("#", cell_data_queue, [int], status_field=["queue"])
self.add_texticon_column(_("Name"), status_field=["state", "name"], self.add_texticon_column(_("Name"), status_field=["state", "name"],
function=cell_data_statusicon) function=cell_data_statusicon)
self.add_func_column(_("Size"), self.add_func_column(_("Size"),
listview.cell_data_size, listview.cell_data_size,
[gobject.TYPE_UINT64], [gobject.TYPE_UINT64],
status_field=["total_wanted"]) status_field=["total_wanted"])
self.add_progress_column(_("Progress"), self.add_progress_column(_("Progress"),
status_field=["progress", "state"], status_field=["progress", "state"],
col_types=[float, str], col_types=[float, str],
function=cell_data_progress) function=cell_data_progress)
self.add_func_column(_("Seeders"), self.add_func_column(_("Seeders"),
listview.cell_data_peer, listview.cell_data_peer,
[int, int], [int, int],
status_field=["num_seeds", status_field=["num_seeds",
"total_seeds"]) "total_seeds"])
self.add_func_column(_("Peers"), self.add_func_column(_("Peers"),
listview.cell_data_peer, listview.cell_data_peer,
[int, int], [int, int],
status_field=["num_peers", status_field=["num_peers",
"total_peers"]) "total_peers"])
self.add_func_column(_("Down Speed"), self.add_func_column(_("Down Speed"),
listview.cell_data_speed, listview.cell_data_speed,
@ -164,7 +164,7 @@ class TorrentView(listview.ListView, component.Component):
[float], [float],
status_field=["distributed_copies"]) status_field=["distributed_copies"])
self.add_text_column(_("Tracker"), status_field=["tracker_host"]) self.add_text_column(_("Tracker"), status_field=["tracker_host"])
# Set filter to None for now # Set filter to None for now
self.filter = (None, None) self.filter = (None, None)
@ -175,11 +175,11 @@ class TorrentView(listview.ListView, component.Component):
self.on_button_press_event) self.on_button_press_event)
# Connect to the 'changed' event of TreeViewSelection to get selection # Connect to the 'changed' event of TreeViewSelection to get selection
# changes. # changes.
self.treeview.get_selection().connect("changed", self.treeview.get_selection().connect("changed",
self.on_selection_changed) self.on_selection_changed)
self.treeview.connect("drag-drop", self.on_drag_drop) self.treeview.connect("drag-drop", self.on_drag_drop)
def start(self): def start(self):
"""Start the torrentview""" """Start the torrentview"""
# We need to get the core session state to know which torrents are in # We need to get the core session state to know which torrents are in
@ -189,10 +189,10 @@ class TorrentView(listview.ListView, component.Component):
def _on_session_state(self, state): def _on_session_state(self, state):
for torrent_id in state: for torrent_id in state:
self.add_row(torrent_id, update=False) self.add_row(torrent_id, update=False)
self.update_filter() self.update_filter()
self.update() self.update()
def stop(self): def stop(self):
"""Stops the torrentview""" """Stops the torrentview"""
# We need to clear the liststore # We need to clear the liststore
@ -201,27 +201,27 @@ class TorrentView(listview.ListView, component.Component):
def shutdown(self): def shutdown(self):
"""Called when GtkUi is exiting""" """Called when GtkUi is exiting"""
self.save_state("torrentview.state") self.save_state("torrentview.state")
def set_filter(self, field, condition): def set_filter(self, field, condition):
"""Sets filters for the torrentview..""" """Sets filters for the torrentview.."""
if self.filter != (None, None): if self.filter != (None, None):
self.filter = (None, None) self.filter = (None, None)
self.update_filter() self.update_filter()
self.filter = (field, condition) self.filter = (field, condition)
self.update_filter() self.update_filter()
self.update() self.update()
def send_status_request(self, columns=None): def send_status_request(self, columns=None):
# Store the 'status_fields' we need to send to core # Store the 'status_fields' we need to send to core
status_keys = [] status_keys = []
# Store the actual columns we will be updating # Store the actual columns we will be updating
self.columns_to_update = [] self.columns_to_update = []
if columns is None: if columns is None:
# We need to iterate through all columns # We need to iterate through all columns
columns = self.columns.keys() columns = self.columns.keys()
# Iterate through supplied list of columns to update # Iterate through supplied list of columns to update
for column in columns: for column in columns:
# Make sure column is visible and has 'status_field' set. # Make sure column is visible and has 'status_field' set.
@ -232,17 +232,17 @@ class TorrentView(listview.ListView, component.Component):
for field in self.columns[column].status_field: for field in self.columns[column].status_field:
status_keys.append(field) status_keys.append(field)
self.columns_to_update.append(column) self.columns_to_update.append(column)
# Remove duplicate keys # Remove duplicate keys
self.columns_to_update = list(set(self.columns_to_update)) self.columns_to_update = list(set(self.columns_to_update))
# If there is nothing in status_keys then we must not continue # If there is nothing in status_keys then we must not continue
if status_keys is []: if status_keys is []:
return return
# Remove duplicates from status_key list # Remove duplicates from status_key list
status_keys = list(set(status_keys)) status_keys = list(set(status_keys))
# Create list of torrent_ids in need of status updates # Create list of torrent_ids in need of status updates
torrent_ids = [] torrent_ids = []
for row in self.liststore: for row in self.liststore:
@ -258,8 +258,8 @@ class TorrentView(listview.ListView, component.Component):
# Request the statuses for all these torrent_ids, this is async so we # Request the statuses for all these torrent_ids, this is async so we
# will deal with the return in a signal callback. # will deal with the return in a signal callback.
client.get_torrents_status( client.get_torrents_status(
self._on_get_torrents_status, torrent_ids, status_keys) self._on_get_torrents_status, {"id":torrent_ids}, status_keys)
def update_filter(self): def update_filter(self):
# Update the filter view # Update the filter view
for row in self.liststore: for row in self.liststore:
@ -281,11 +281,11 @@ class TorrentView(listview.ListView, component.Component):
row[filter_column] = True row[filter_column] = True
else: else:
row[filter_column] = False row[filter_column] = False
def update(self): def update(self):
# Send a status request # Send a status request
self.send_status_request() self.send_status_request()
def update_view(self, columns=None): def update_view(self, columns=None):
"""Update the view. If columns is not None, it will attempt to only """Update the view. If columns is not None, it will attempt to only
update those columns selected. update those columns selected.
@ -299,7 +299,7 @@ class TorrentView(listview.ListView, component.Component):
for column in self.columns_to_update: for column in self.columns_to_update:
column_index = self.get_column_index(column) column_index = self.get_column_index(column)
if type(column_index) is not list: if type(column_index) is not list:
# We only have a single list store column we need to # We only have a single list store column we need to
# update # update
try: try:
# Only update if different # Only update if different
@ -308,7 +308,7 @@ class TorrentView(listview.ListView, component.Component):
row[column_index] = status[torrent_id][ row[column_index] = status[torrent_id][
self.columns[column].status_field[0]] self.columns[column].status_field[0]]
except (TypeError, KeyError), e: except (TypeError, KeyError), e:
log.warning("Unable to update column %s: %s", log.warning("Unable to update column %s: %s",
column, e) column, e)
else: else:
# We have more than 1 liststore column to update # We have more than 1 liststore column to update
@ -320,7 +320,7 @@ class TorrentView(listview.ListView, component.Component):
status[torrent_id][ status[torrent_id][
self.columns[column].status_field[ self.columns[column].status_field[
column_index.index(index)]]: column_index.index(index)]]:
row[index] = \ row[index] = \
status[torrent_id][ status[torrent_id][
self.columns[column].status_field[ self.columns[column].status_field[
@ -331,7 +331,7 @@ class TorrentView(listview.ListView, component.Component):
# Update the toolbar buttons just in case some state has changed # Update the toolbar buttons just in case some state has changed
component.get("ToolBar").update_buttons() component.get("ToolBar").update_buttons()
component.get("MenuBar").update_menu() component.get("MenuBar").update_menu()
def _on_get_torrents_status(self, status): def _on_get_torrents_status(self, status):
"""Callback function for get_torrents_status(). 'status' should be a """Callback function for get_torrents_status(). 'status' should be a
dictionary of {torrent_id: {key, value}}.""" dictionary of {torrent_id: {key, value}}."""
@ -339,10 +339,10 @@ class TorrentView(listview.ListView, component.Component):
self.status = status self.status = status
else: else:
self.status = {} self.status = {}
if self.status != {}: if self.status != {}:
self.update_view() self.update_view()
def add_row(self, torrent_id, update=True): def add_row(self, torrent_id, update=True):
"""Adds a new torrent row to the treeview""" """Adds a new torrent row to the treeview"""
# Insert a new row to the liststore # Insert a new row to the liststore
@ -350,12 +350,12 @@ class TorrentView(listview.ListView, component.Component):
# Store the torrent id # Store the torrent id
self.liststore.set_value( self.liststore.set_value(
row, row,
self.columns["torrent_id"].column_indices[0], self.columns["torrent_id"].column_indices[0],
torrent_id) torrent_id)
if update: if update:
self.update() self.update()
self.update_filter() self.update_filter()
def remove_row(self, torrent_id): def remove_row(self, torrent_id):
"""Removes a row with torrent_id""" """Removes a row with torrent_id"""
for row in self.liststore: for row in self.liststore:
@ -372,7 +372,7 @@ class TorrentView(listview.ListView, component.Component):
log.debug("marking %s dirty", torrent_id) log.debug("marking %s dirty", torrent_id)
row[self.columns["dirty"].column_indices[0]] = True row[self.columns["dirty"].column_indices[0]] = True
if torrent_id: break if torrent_id: break
def get_selected_torrent(self): def get_selected_torrent(self):
"""Returns a torrent_id or None. If multiple torrents are selected, """Returns a torrent_id or None. If multiple torrents are selected,
it will return the torrent_id of the first one.""" it will return the torrent_id of the first one."""
@ -381,7 +381,7 @@ class TorrentView(listview.ListView, component.Component):
return selected[0] return selected[0]
else: else:
return selected return selected
def get_selected_torrents(self): def get_selected_torrents(self):
"""Returns a list of selected torrents or None""" """Returns a list of selected torrents or None"""
torrent_ids = [] torrent_ids = []
@ -397,7 +397,7 @@ class TorrentView(listview.ListView, component.Component):
except Exception, e: except Exception, e:
log.debug("Unable to get iter from path: %s", e) log.debug("Unable to get iter from path: %s", e)
continue continue
child_row = self.treeview.get_model().convert_iter_to_child_iter(None, row) child_row = self.treeview.get_model().convert_iter_to_child_iter(None, row)
child_row = self.treeview.get_model().get_model().convert_iter_to_child_iter(child_row) child_row = self.treeview.get_model().get_model().convert_iter_to_child_iter(child_row)
if self.liststore.iter_is_valid(child_row): if self.liststore.iter_is_valid(child_row):
@ -409,22 +409,22 @@ class TorrentView(listview.ListView, component.Component):
torrent_ids.append(value) torrent_ids.append(value)
if len(torrent_ids) == 0: if len(torrent_ids) == 0:
return [] return []
return torrent_ids return torrent_ids
except ValueError, TypeError: except ValueError, TypeError:
return [] return []
def get_torrent_status(self, torrent_id): def get_torrent_status(self, torrent_id):
"""Returns data stored in self.status, it may not be complete""" """Returns data stored in self.status, it may not be complete"""
try: try:
return self.status[torrent_id] return self.status[torrent_id]
except: except:
return {} return {}
def get_visible_torrents(self): def get_visible_torrents(self):
return self.status.keys() return self.status.keys()
### Callbacks ### ### Callbacks ###
def on_button_press_event(self, widget, event): def on_button_press_event(self, widget, event):
"""This is a callback for showing the right-click context menu.""" """This is a callback for showing the right-click context menu."""
log.debug("on_button_press_event") log.debug("on_button_press_event")
@ -445,7 +445,7 @@ class TorrentView(listview.ListView, component.Component):
torrentmenu = component.get("MenuBar").torrentmenu torrentmenu = component.get("MenuBar").torrentmenu
torrentmenu.popup(None, None, None, event.button, event.time) torrentmenu.popup(None, None, None, event.button, event.time)
return True return True
def on_selection_changed(self, treeselection): def on_selection_changed(self, treeselection):
"""This callback is know when the selection has changed.""" """This callback is know when the selection has changed."""
log.debug("on_selection_changed") log.debug("on_selection_changed")
@ -455,4 +455,4 @@ class TorrentView(listview.ListView, component.Component):
def on_drag_drop(self, widget, drag_context, x, y, timestamp): def on_drag_drop(self, widget, drag_context, x, y, timestamp):
widget.stop_emission("drag-drop") widget.stop_emission("drag-drop")

View file

@ -203,7 +203,7 @@ class json_rpc:
filters = {"state":[["All",-1]],"tracker":[],"label":[]} filters = {"state":[["All",-1]],"tracker":[],"label":[]}
return { return {
"torrents":sclient.get_torrents_status(torrent_ids, keys), "torrents":sclient.get_torrents_status({"id":torrent_ids}, keys),
"filters":filters, "filters":filters,
"stats":self.get_stats(), "stats":self.get_stats(),
"cache_id":-1 "cache_id":-1

View file

@ -166,7 +166,7 @@ def get_enhanced_torrent_list(torrent_ids):
""" """
returns a list of storified-torrent-dicts. returns a list of storified-torrent-dicts.
""" """
torrent_dict = sclient.get_torrents_status(torrent_ids, TORRENT_KEYS) torrent_dict = sclient.get_torrents_status({"id":torrent_ids}, TORRENT_KEYS)
return [enhance_torrent_status(id, status) return [enhance_torrent_status(id, status)
for id, status in torrent_dict.iteritems()] for id, status in torrent_dict.iteritems()]