diff options
author | Matthew Sherborne <msherborne@gmail.com> | 2013-05-05 16:14:38 +1000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2013-05-15 23:25:28 +0000 |
commit | 5dcc43b118fd6ce65676861bee126b1de5e0a8d1 (patch) | |
tree | 6f807f27084f4bec8dfedbfc50edcabee4104d7e /openstack/common/scheduler | |
parent | 378701bf3b2c67b3b98e692076f78cb83c2a419e (diff) | |
download | oslo-5dcc43b118fd6ce65676861bee126b1de5e0a8d1.tar.gz oslo-5dcc43b118fd6ce65676861bee126b1de5e0a8d1.tar.xz oslo-5dcc43b118fd6ce65676861bee126b1de5e0a8d1.zip |
Break out common functionality for filters and weights
In the context of openstack.common.scheduler:
base_filter.BaseFilterManager and base_weight.BaseWeightManager had
duplicated code.
This patch breaks that code out into a shared base class:
base_handler.BaseHandler
Change-Id: Ie5232c6dd9876a75170beda71adde90fc6bb4153
Diffstat (limited to 'openstack/common/scheduler')
-rw-r--r-- | openstack/common/scheduler/base_filter.py | 22 | ||||
-rw-r--r-- | openstack/common/scheduler/base_handler.py | 46 | ||||
-rw-r--r-- | openstack/common/scheduler/base_weight.py | 23 |
3 files changed, 50 insertions, 41 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.""" |