1
# Copyright (c) 2011-2012 OpenStack, LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
22
from stevedore import extension
25
class BaseFilter(object):
26
"""Base class for all filter classes."""
27
def _filter_one(self, obj, filter_properties):
28
"""Return True if it passes the filter, False otherwise.
29
Override this in a subclass.
33
def filter_all(self, filter_obj_list, filter_properties):
34
"""Yield objects that pass the filter.
36
Can be overriden in a subclass, if you need to base filtering
37
decisions on all objects. Otherwise, one can just override
38
_filter_one() to filter a single object.
40
for obj in filter_obj_list:
41
if self._filter_one(obj, filter_properties):
45
class BaseFilterHandler(object):
46
""" Base class to handle loading filter classes.
48
This class should be subclassed where one needs to use filters.
50
def __init__(self, filter_class_type, filter_namespace):
51
self.namespace = filter_namespace
52
self.filter_class_type = filter_class_type
53
self.filter_manager = extension.ExtensionManager(filter_namespace)
55
def _is_correct_class(self, obj):
56
"""Return whether an object is a class of the correct type and
57
is not prefixed with an underscore.
59
return (inspect.isclass(obj) and
60
not obj.__name__.startswith('_') and
61
issubclass(obj, self.filter_class_type))
63
def get_all_classes(self):
64
return [x.plugin for x in self.filter_manager
65
if self._is_correct_class(x.plugin)]
67
def get_filtered_objects(self, filter_classes, objs,
69
for filter_cls in filter_classes:
70
objs = filter_cls().filter_all(objs, filter_properties)