From 5ebbff177f3517e5065549a3219318dce7148640 Mon Sep 17 00:00:00 2001 From: Martijn Voncken Date: Sat, 11 Oct 2008 08:04:02 +0000 Subject: [PATCH] filter: hide_zero_hits;hide_cat : core+test --- deluge/core/core.py | 8 +++--- deluge/core/filtermanager.py | 48 ++++++++++++++++++++++++++++++------ deluge/tests/test_filters.py | 27 ++++++++++++++++++-- 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/deluge/core/core.py b/deluge/core/core.py index 4964980a2..3d7f1e807 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -394,7 +394,7 @@ class Core( def fetch_torrent_url_thread(self, callback, url, options): # Get the actual filename of the torrent from the url provided. filename = url.split("/")[-1] - + # Get the .torrent file from the url torrent_file = deluge.common.fetch_url(url) if torrent_file is None: @@ -494,12 +494,12 @@ class Core( # Emit the torrent_status signal to the clients return status_dict - def export_get_filter_tree(self): + def export_get_filter_tree(self , hide_zero_hits=False, hide_cat=None): """ returns {field: [(value,count)] } for use in sidebar(s) """ - return self.filtermanager.get_filter_tree() + return self.filtermanager.get_filter_tree(hide_zero_hits, hide_cat) def export_get_session_state(self): """Returns a list of torrent_ids in the session.""" @@ -706,7 +706,7 @@ class Core( """Renames files in 'torrent_id'. The 'filenames' parameter should be a list of (index, filename) pairs.""" self.torrents[torrent_id].rename_files(filenames) - + ## Queueing functions ## def export_queue_top(self, torrent_ids): log.debug("Attempting to queue %s to top", torrent_ids) diff --git a/deluge/core/filtermanager.py b/deluge/core/filtermanager.py index 89c71df1e..529492719 100644 --- a/deluge/core/filtermanager.py +++ b/deluge/core/filtermanager.py @@ -34,6 +34,7 @@ import deluge.component as component from deluge.log import LOG as log +STATE_SORT = ["All", "Downloading", "Seeding", "Active", "Paused", "Queued"] #special purpose filters: def filter_keyword(torrent_ids, values): @@ -45,7 +46,6 @@ def filter_keyword(torrent_ids, values): if keyword in all_torrents[torrent_id].filename.lower(): yield torrent_id - class FilterManager(component.Component): """FilterManager @@ -117,15 +117,19 @@ class FilterManager(component.Component): return torrent_ids - def get_filter_tree(self): + def get_filter_tree(self, hide_zero_hits=False, hide_cat=None): """ returns {field: [(value,count)] } for use in sidebar. """ torrent_ids = self.torrents.get_torrent_list() status_func = self.core.export_get_torrent_status #premature optimalisation.. - tree_keys = self.tree_fields.keys() - items = dict( (field, init_func()) for field, init_func in self.tree_fields.iteritems()) + tree_keys = list(self.tree_fields.keys()) + if hide_cat: + for cat in hide_cat: + tree_keys.remove(cat) + + items = dict( (field, self.tree_fields[field]()) for field in tree_keys) #count status fields. for torrent_id in list(torrent_ids): @@ -134,10 +138,18 @@ class FilterManager(component.Component): value = status[field] items[field][value] = items[field].get(value, 0) + 1 - for field in tree_keys: - items[field] = sorted(items[field].iteritems()) + if "state" in tree_keys and hide_zero_hits: + self._hide_state_items(items["state"]) - return items + #return a dict of tuples: + sorted_items = {} + for field in tree_keys: + sorted_items[field] = sorted(items[field].iteritems()) + + if "state" in tree_keys: + sorted_items["state"].sort(self._sort_state_items) + + return sorted_items def _init_state_tree(self): return {"All":len(self.torrents.get_torrent_list()), @@ -171,3 +183,25 @@ class FilterManager(component.Component): else: torrent_ids.remove(torrent_id) return torrent_ids + + def _hide_state_items(self, state_items): + "for hide_zero hits" + for (value, count) in state_items.items(): + if value != "All" and count == 0: + del state_items[value] + + def _sort_state_items(self, x, y): + "" + if x[0] in STATE_SORT: + ix = STATE_SORT.index(x[0]) + else: + ix = 99 + if y[0] in STATE_SORT: + iy = STATE_SORT.index(y[0]) + else: + iy = 99 + + return ix - iy + + + diff --git a/deluge/tests/test_filters.py b/deluge/tests/test_filters.py index 7e0f18a4a..c8007f72b 100644 --- a/deluge/tests/test_filters.py +++ b/deluge/tests/test_filters.py @@ -43,11 +43,34 @@ print sclient.get_torrents_status({"keyword":["az"]}, KEYS) print "#registered filters, overriude on 1 value(not yet)" print sclient.get_torrents_status({"state":["Active"]}, KEYS) -print "#tree:" +print "#tree: Default (Active must be listed after Seeding)" for field, items in sclient.get_filter_tree().iteritems(): print "*",field for value, count in items: print "-",value,count +print "#tree: Hide_zero" +for field, items in sclient.get_filter_tree(True).iteritems(): + print "*",field + for value, count in items: + print "-",value,count + +print "#tree: Hide tracker" +for field, items in sclient.get_filter_tree(False, ["tracker_host"]).iteritems(): + print "*",field + for value, count in items: + print "-",value,count + +print "#tree: Hide all" +if sclient.get_filter_tree(False, ["tracker_host","label","state"]): + raise Exception("result should be {}") +print "hide-all :ok" + + print "#must have an error here:" -print sclient.get_torrents_status({"invalid-filter":[]}, KEYS) +try: + print sclient.get_torrents_status({"invalid-filter":[]}, KEYS) + print "WTF!" +except Exception, e: + print "ok, an exception was raised:", e ,e.message +