factory.py 723 字节
Newer Older
P
peng.xu 已提交
1 2
import logging
from discovery import DiscoveryConfig
P
peng.xu 已提交
3
from utils.plugins import BaseMixin
P
peng.xu 已提交
4 5 6 7 8

logger = logging.getLogger(__name__)
PLUGIN_PACKAGE_NAME = 'discovery.plugins'


P
peng.xu 已提交
9
class DiscoveryFactory(BaseMixin):
P
peng.xu 已提交
10 11
    PLUGIN_TYPE = 'Discovery'
    def __init__(self, searchpath=None):
P
peng.xu 已提交
12
        super().__init__(searchpath=searchpath, package_name=PLUGIN_PACKAGE_NAME)
P
peng.xu 已提交
13

P
peng.xu 已提交
14
    def _create(self, plugin_class, **kwargs):
P
peng.xu 已提交
15 16 17 18 19 20 21
        conn_mgr = kwargs.pop('conn_mgr', None)
        if not conn_mgr:
            raise RuntimeError('Please pass conn_mgr to create discovery!')

        plugin_config = DiscoveryConfig.Create()
        plugin = plugin_class.create(plugin_config=plugin_config, conn_mgr=conn_mgr, **kwargs)
        return plugin