summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--openstack/common/scheduler/base_filter.py22
-rw-r--r--openstack/common/scheduler/base_handler.py46
-rw-r--r--openstack/common/scheduler/base_weight.py23
-rw-r--r--tests/unit/scheduler/test_base_filter.py4
4 files changed, 53 insertions, 42 deletions
diff --git a/openstack/common/scheduler/base_filter.py b/openstack/common/scheduler/base_filter.py
index 52c18af..5f2fc9c 100644
--- a/openstack/common/scheduler/base_filter.py
+++ b/openstack/common/scheduler/base_filter.py
@@ -17,9 +17,7 @@
Filter support
"""
-import inspect
-
-from stevedore import extension
+from openstack.common.scheduler import base_handler
class BaseFilter(object):
@@ -42,27 +40,11 @@ class BaseFilter(object):
yield obj
-class BaseFilterHandler(object):
+class BaseFilterHandler(base_handler.BaseHandler):
""" Base class to handle loading filter classes.
This class should be subclassed where one needs to use filters.
"""
- def __init__(self, filter_class_type, filter_namespace):
- self.namespace = filter_namespace
- self.filter_class_type = filter_class_type
- self.filter_manager = extension.ExtensionManager(filter_namespace)
-
- def _is_correct_class(self, obj):
- """Return whether an object is a class of the correct type and
- is not prefixed with an underscore.
- """
- return (inspect.isclass(obj) and
- not obj.__name__.startswith('_') and
- issubclass(obj, self.filter_class_type))
-
- def get_all_classes(self):
- return [x.plugin for x in self.filter_manager
- if self._is_correct_class(x.plugin)]
def get_filtered_objects(self, filter_classes, objs,
filter_properties):
diff --git a/openstack/common/scheduler/base_handler.py b/openstack/common/scheduler/base_handler.py
new file mode 100644
index 0000000..147d90d
--- /dev/null
+++ b/openstack/common/scheduler/base_handler.py
@@ -0,0 +1,46 @@
+# Copyright (c) 2011-2013 OpenStack Foundation.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+"""
+A common base for handling extension classes.
+
+Used by BaseFilterHandler and BaseWeightHandler
+"""
+
+import inspect
+
+from stevedore import extension
+
+
+class BaseHandler(object):
+ """ Base class to handle loading filter and weight classes.
+ """
+ def __init__(self, modifier_class_type, modifier_namespace):
+ self.namespace = modifier_namespace
+ self.modifier_class_type = modifier_class_type
+ self.extension_manager = extension.ExtensionManager(modifier_namespace)
+
+ def _is_correct_class(self, cls):
+ """Return whether an object is a class of the correct type and
+ is not prefixed with an underscore.
+ """
+ return (inspect.isclass(cls) and
+ not cls.__name__.startswith('_') and
+ issubclass(cls, self.modifier_class_type))
+
+ def get_all_classes(self):
+ # We use a set, as some classes may have an entrypoint of their own,
+ # and also be returned by a function such as 'all_filters' for example
+ return [ext.plugin for ext in self.extension_manager if
+ self._is_correct_class(ext.plugin)]
diff --git a/openstack/common/scheduler/base_weight.py b/openstack/common/scheduler/base_weight.py
index 82f1d25..362685e 100644
--- a/openstack/common/scheduler/base_weight.py
+++ b/openstack/common/scheduler/base_weight.py
@@ -17,9 +17,7 @@
Pluggable Weighing support
"""
-import inspect
-
-from stevedore import extension
+from openstack.common.scheduler import base_handler
class WeighedObject(object):
@@ -56,26 +54,9 @@ class BaseWeigher(object):
self._weigh_object(obj.obj, weight_properties))
-class BaseWeightHandler(object):
+class BaseWeightHandler(base_handler.BaseHandler):
object_class = WeighedObject
- def __init__(self, weighed_object_type, weight_namespace):
- self.namespace = weight_namespace
- self.weighed_object_type = weighed_object_type
- self.weight_manager = extension.ExtensionManager(weight_namespace)
-
- def _is_correct_class(self, obj):
- """Return whether an object is a class of the correct type and
- is not prefixed with an underscore.
- """
- return (inspect.isclass(obj) and
- not obj.__name__.startswith('_') and
- issubclass(obj, self.weighed_object_type))
-
- def get_all_classes(self):
- return [x.plugin for x in self.weight_manager
- if self._is_correct_class(x.plugin)]
-
def get_weighed_objects(self, weigher_classes, obj_list,
weighing_properties):
"""Return a sorted (highest score first) list of WeighedObjects."""
diff --git a/tests/unit/scheduler/test_base_filter.py b/tests/unit/scheduler/test_base_filter.py
index 680e0de..65809ea 100644
--- a/tests/unit/scheduler/test_base_filter.py
+++ b/tests/unit/scheduler/test_base_filter.py
@@ -60,6 +60,7 @@ class FakeFilter1(BaseFakeFilter):
* AND
* It has a fake entry point defined (is returned by fake ExtensionManager)
"""
+ pass
class FakeFilter2(BaseFakeFilter):
@@ -69,6 +70,7 @@ class FakeFilter2(BaseFakeFilter):
* BUT
* It has no entry point
"""
+ pass
class FakeFilter3(base_filter.BaseFilter):
@@ -112,7 +114,7 @@ class TestBaseFilterHandler(utils.BaseTestCase):
def setUp(self):
super(TestBaseFilterHandler, self).setUp()
- self.stubs.Set(base_filter.extension, 'ExtensionManager',
+ self.stubs.Set(base_filter.base_handler.extension, 'ExtensionManager',
FakeExtensionManager)
self.handler = base_filter.BaseFilterHandler(BaseFakeFilter,
'fake_filters')