Coverage for watcher/common/loader/default.py: 96%
44 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
1# -*- encoding: utf-8 -*-
2# Copyright (c) 2016 b<>com
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
17from oslo_config import cfg
18from oslo_log import log
19from stevedore import driver as drivermanager
20from stevedore import extension as extensionmanager
22from watcher.common import exception
23from watcher.common.loader import base
24from watcher.common import utils
26LOG = log.getLogger(__name__)
29class DefaultLoader(base.BaseLoader):
31 def __init__(self, namespace, conf=cfg.CONF):
32 """Entry point loader for Watcher using Stevedore
34 :param namespace: namespace of the entry point(s) to load or list
35 :type namespace: str
36 :param conf: ConfigOpts instance, defaults to cfg.CONF
37 """
38 super(DefaultLoader, self).__init__()
39 self.namespace = namespace
40 self.conf = conf
42 def load(self, name, **kwargs):
43 try:
44 LOG.debug("Loading in namespace %s => %s ", self.namespace, name)
45 driver_manager = drivermanager.DriverManager(
46 namespace=self.namespace,
47 name=name,
48 invoke_on_load=False,
49 )
51 driver_cls = driver_manager.driver
52 config = self._load_plugin_config(name, driver_cls)
54 driver = driver_cls(config, **kwargs)
55 except Exception as exc:
56 LOG.exception(exc)
57 raise exception.LoadingError(name=name)
59 return driver
61 def _reload_config(self):
62 self.conf(default_config_files=self.conf.default_config_files)
64 def get_entry_name(self, name):
65 return ".".join([self.namespace, name])
67 def _load_plugin_config(self, name, driver_cls):
68 """Load the config of the plugin"""
69 config = utils.Struct()
70 config_opts = driver_cls.get_config_opts()
71 if not config_opts:
72 return config
74 group_name = self.get_entry_name(name)
75 self.conf.register_opts(config_opts, group=group_name)
77 # Finalise the opt import by re-checking the configuration
78 # against the provided config files
79 self._reload_config()
81 config_group = self.conf.get(group_name)
82 if not config_group: 82 ↛ 83line 82 didn't jump to line 83 because the condition on line 82 was never true
83 raise exception.LoadingError(name=name)
85 config.update({
86 name: value for name, value in config_group.items()
87 })
89 return config
91 def list_available(self):
92 extension_manager = extensionmanager.ExtensionManager(
93 namespace=self.namespace)
94 return {ext.name: ext.plugin for ext in extension_manager.extensions}