Debian Codegraph @

neutron_lib/objects/registry.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from neutron_lib.utils import runtime
14
15
16 NEUTRON_OBJECT_NAMESPACE = 'neutron.objects'
17 _REGISTRY = runtime.NamespacedPlugins(NEUTRON_OBJECT_NAMESPACE)
18
19
20 def load_class(object_class_name):
21 """Return the versioned object for the given class name.
22
23 :param object_class_name: The class name of the versioned object to
24 get.
25 :returns: A reference to the class for the said object_class_name.
26 """
27 return _REGISTRY.get_plugin_class(object_class_name)
28
29
30 def new_instance(object_class_name, *inst_args, **inst_kwargs):
31 """Create a new instance of a versioned object.
32
33 :param object_class_name: The name of the versioned object's class to
34 instantiate.
35 :param inst_args: Any args pass onto the constructor of the versioned
36 object when creating it.
37 :param inst_kwargs: Any kwargs to pass onto the constructor of the object
38 when creating it.
39 :returns: A new instance of the versioned object.
40 """
41 return _REGISTRY.new_plugin_instance(
42 object_class_name, *inst_args, **inst_kwargs)
43
44
45 def contains(object_class_name):
46 """Determine if a given versioned object is loaded.
47
48 :param object_class_name: The class name of the versioned object to check
49 for.
50 :returns: True if the versioned object is loaded, and False otherwise.
51 """
52 return object_class_name in _REGISTRY.loaded_plugin_names