get_filter_tree

This commit is contained in:
Martijn Voncken 2008-08-18 18:05:48 +00:00
parent 1b03c0eff4
commit eee10dab76
3 changed files with 37 additions and 6 deletions

View File

@ -526,6 +526,13 @@ class Core(
# Emit the torrent_status signal to the clients # Emit the torrent_status signal to the clients
return status_dict 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): def export_get_session_state(self):
"""Returns a list of torrent_ids in the session.""" """Returns a list of torrent_ids in the session."""
# Get the torrent list from the TorrentManager # Get the torrent list from the TorrentManager

View File

@ -40,9 +40,8 @@ def filter_keyword(torrent_ids, values):
keywords = [v.lower() for v in values] #cleanup. keywords = [v.lower() for v in values] #cleanup.
all_torrents = component.get("TorrentManager").torrents all_torrents = component.get("TorrentManager").torrents
#filter: #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(): if keyword in all_torrents[torrent_id].filename.lower():
yield torrent_id yield torrent_id
@ -61,6 +60,7 @@ class FilterManager(component.Component):
self.torrents = core.torrents self.torrents = core.torrents
self.registered_filters = {} self.registered_filters = {}
self.register_filter("keyword", filter_keyword) self.register_filter("keyword", filter_keyword)
self.filter_tree_fields = ["state","tracker_host"]
def filter_torrent_ids(self, filter_dict): def filter_torrent_ids(self, filter_dict):
""" """
@ -82,7 +82,8 @@ class FilterManager(component.Component):
#Registered filters: #Registered filters:
for field, values in filter_dict.items(): for field, values in filter_dict.items():
if field in self.registered_filters: 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] del filter_dict[field]
if not filter_dict: #return if there's nothing more to filter if not filter_dict: #return if there's nothing more to filter
@ -93,20 +94,37 @@ class FilterManager(component.Component):
if filter_dict: if filter_dict:
status_func = self.core.export_get_torrent_status #premature optimalisation.. status_func = self.core.export_get_torrent_status #premature optimalisation..
for torrent_id in list(torrent_ids): 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(): for field, values in filter_dict.iteritems():
if (not status[field] in values) and torrent_id in torrent_ids: if (not status[field] in values) and torrent_id in torrent_ids:
torrent_ids.remove(torrent_id) torrent_ids.remove(torrent_id)
return torrent_ids 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 self.registered_filters[id] = filter_func
def deregister_filter(self, id): def deregister_filter(self, id):
del self.registered_filters[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

View File

@ -43,5 +43,11 @@ print sclient.get_torrents_status({"keyword":["az"]}, KEYS)
print "#registered filters, overriude on 1 value(not yet)" print "#registered filters, overriude on 1 value(not yet)"
print sclient.get_torrents_status({"state":["Active"]}, KEYS) 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 "#must have an error here:"
print sclient.get_torrents_status({"invalid-filter":[]}, KEYS) print sclient.get_torrents_status({"invalid-filter":[]}, KEYS)