From b48c487acb577a69591beba55694af32fc720f34 Mon Sep 17 00:00:00 2001 From: Martijn Voncken Date: Sat, 16 Aug 2008 23:07:10 +0000 Subject: [PATCH] move filtering to filtermanager --- deluge/core/core.py | 35 +++------------ deluge/core/filtermanager.py | 84 ++++++++++++++++++++++++++++++++++++ deluge/tests/test_filters.py | 1 - 3 files changed, 89 insertions(+), 31 deletions(-) create mode 100644 deluge/core/filtermanager.py diff --git a/deluge/core/core.py b/deluge/core/core.py index d7ab062ad..7c2812ae8 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -54,6 +54,7 @@ from deluge.core.torrentmanager import TorrentManager from deluge.core.pluginmanager import PluginManager from deluge.core.alertmanager import AlertManager from deluge.core.signalmanager import SignalManager +from deluge.core.filtermanager import FilterManager from deluge.core.autoadd import AutoAdd from deluge.log import LOG as log @@ -323,6 +324,9 @@ class Core( # Start the TorrentManager self.torrents = TorrentManager(self.session, self.alerts) + # Start the FilterManager + self.filtermanager = FilterManager(self) + # Create the AutoAdd component self.autoadd = AutoAdd() @@ -507,40 +511,11 @@ class Core( status.update(self.plugins.get_status(torrent_id, leftover_fields)) return status - 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. """ - torrent_ids = self.filter_torrent_ids(filter_dict) + torrent_ids = self.filtermanager.filter_torrent_ids(filter_dict) status_dict = {}.fromkeys(torrent_ids) # Get the torrent status for each torrent_id diff --git a/deluge/core/filtermanager.py b/deluge/core/filtermanager.py new file mode 100644 index 000000000..8e7dc27dc --- /dev/null +++ b/deluge/core/filtermanager.py @@ -0,0 +1,84 @@ +# +# core.py +# +# Copyright (C) 2008 Martijn Voncken +# +# Deluge is free software. +# +# You may redistribute it and/or modify it under the terms of the +# GNU General Public License, as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) +# any later version. +# +# deluge is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with deluge. If not, write to: +# The Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor +# Boston, MA 02110-1301, USA. +# +# In addition, as a special exception, the copyright holders give +# permission to link the code of portions of this program with the OpenSSL +# library. +# You must obey the GNU General Public License in all respects for all of +# the code used other than OpenSSL. If you modify file(s) with this +# exception, you may extend this exception to your version of the file(s), +# but you are not obligated to do so. If you do not wish to do so, delete +# this exception statement from your version. If you delete this exception +# statement from all source files in the program, then also delete it here. + +import deluge.component as component +from deluge.log import LOG as log + +#special purpose filters: +def filter_keyword(torrent): + pass + +def filter_state_active(torrent): + pass + +class FilterManager(component.Component): + """FilterManager + + """ + def __init__(self, core): + component.Component.__init__(self, "FilterManager") + log.debug("FilterManager init..") + self.core = core + self.torrents = core.torrents + + 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: + 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}} + 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 + + diff --git a/deluge/tests/test_filters.py b/deluge/tests/test_filters.py index 36d89d332..a9bb44067 100644 --- a/deluge/tests/test_filters.py +++ b/deluge/tests/test_filters.py @@ -37,7 +37,6 @@ print "test&tpb:",len(sclient.get_torrents_status({"label":["test","tpb"]}, KEYS print "test:",len(sclient.get_torrents_status({"label":["test"]}, KEYS)) print "No Label:" , len(sclient.get_torrents_status({"label":[""]}, KEYS)) - print "#special filters (ERRORS START HERE!):" print sclient.get_torrents_status({"keyword":["az"]}, KEYS) print sclient.get_torrents_status({"state":["Active"]}, KEYS)