From eee10dab76860c4834e3ce1094d53bbc49fefdc1 Mon Sep 17 00:00:00 2001 From: Martijn Voncken Date: Mon, 18 Aug 2008 18:05:48 +0000 Subject: [PATCH] get_filter_tree --- deluge/core/core.py | 7 +++++++ deluge/core/filtermanager.py | 30 ++++++++++++++++++++++++------ deluge/tests/test_filters.py | 6 ++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/deluge/core/core.py b/deluge/core/core.py index 1ff7b725d..63841da46 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -526,6 +526,13 @@ class Core( # Emit the torrent_status signal to the clients return status_dict + def export_get_filter_tree(self): + """ + returns {field: [(value,count)] } + for use in sidebar(s) + """ + return self.filtermanager.get_filter_tree() + def export_get_session_state(self): """Returns a list of torrent_ids in the session.""" # Get the torrent list from the TorrentManager diff --git a/deluge/core/filtermanager.py b/deluge/core/filtermanager.py index 6b88c702a..b5a23272d 100644 --- a/deluge/core/filtermanager.py +++ b/deluge/core/filtermanager.py @@ -40,9 +40,8 @@ def filter_keyword(torrent_ids, values): keywords = [v.lower() for v in values] #cleanup. all_torrents = component.get("TorrentManager").torrents #filter: - for torrent_id in torrent_ids: - log.debug(all_torrents[torrent_id].filename.lower()) - for keyword in keywords: + for keyword in keywords: + for torrent_id in torrent_ids: if keyword in all_torrents[torrent_id].filename.lower(): yield torrent_id @@ -61,6 +60,7 @@ class FilterManager(component.Component): self.torrents = core.torrents self.registered_filters = {} self.register_filter("keyword", filter_keyword) + self.filter_tree_fields = ["state","tracker_host"] def filter_torrent_ids(self, filter_dict): """ @@ -82,7 +82,8 @@ class FilterManager(component.Component): #Registered filters: for field, values in filter_dict.items(): if field in self.registered_filters: - torrent_ids = set(self.registered_filters[field](torrent_ids, values)) # a set filters out doubles, + # a set filters out the doubles. + torrent_ids = list(set(self.registered_filters[field](torrent_ids, values))) del filter_dict[field] if not filter_dict: #return if there's nothing more to filter @@ -93,20 +94,37 @@ class FilterManager(component.Component): if filter_dict: status_func = self.core.export_get_torrent_status #premature optimalisation.. for torrent_id in list(torrent_ids): - status = status_func(torrent_id, filter_dict.keys()) #status={id:{key:value}} + status = status_func(torrent_id, filter_dict.keys()) #status={key:value} for field, values in filter_dict.iteritems(): if (not status[field] in values) and torrent_id in torrent_ids: torrent_ids.remove(torrent_id) return torrent_ids - def register_filter(self, id, filter_func): + def register_filter(self, id, filter_func, filter_value = None): self.registered_filters[id] = filter_func def deregister_filter(self, id): del self.registered_filters[id] + def get_filter_tree(self): + """ + returns {field: [(value,count)] } + for use in sidebar. + """ + torrent_ids = self.torrents.get_torrent_list() + items = dict( (field,{}) for field in self.filter_tree_fields) + status_func = self.core.export_get_torrent_status #premature optimalisation.. + items["state"]["All"] = len(torrent_ids) + for torrent_id in list(torrent_ids): + status = status_func(torrent_id, self.filter_tree_fields) #status={key:value} + for field in self.filter_tree_fields: + value = status[field] + items[field][value] = items[field].get(value, 0) + 1 + for field in self.filter_tree_fields: + items[field] = sorted(items[field].iteritems()) + return items diff --git a/deluge/tests/test_filters.py b/deluge/tests/test_filters.py index f9cc86ef4..7e0f18a4a 100644 --- a/deluge/tests/test_filters.py +++ b/deluge/tests/test_filters.py @@ -43,5 +43,11 @@ 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:" +for field, items in sclient.get_filter_tree().iteritems(): + print "*",field + for value, count in items: + print "-",value,count + print "#must have an error here:" print sclient.get_torrents_status({"invalid-filter":[]}, KEYS)