mirror of
https://gitee.com/milvus-io/milvus.git
synced 2026-01-07 11:21:52 +08:00
refactor router plugins
This commit is contained in:
parent
cd0a112f55
commit
4ef3e416fa
@ -1,47 +1,17 @@
|
||||
import os
|
||||
import logging
|
||||
from functools import partial
|
||||
from utils.pluginextension import MiPluginBase as PluginBase
|
||||
from utils.plugins import BaseMixin
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
get_path = partial(os.path.join, here)
|
||||
|
||||
PLUGIN_PACKAGE_NAME = 'mishards.router.plugins'
|
||||
plugin_base = PluginBase(package=PLUGIN_PACKAGE_NAME,
|
||||
searchpath=[get_path('./plugins')])
|
||||
|
||||
|
||||
class RouterFactory(object):
|
||||
class RouterFactory(BaseMixin):
|
||||
PLUGIN_TYPE = 'Router'
|
||||
|
||||
def __init__(self, searchpath=None):
|
||||
self.plugin_package_name = PLUGIN_PACKAGE_NAME
|
||||
self.class_map = {}
|
||||
searchpath = searchpath if searchpath else []
|
||||
searchpath = [searchpath] if isinstance(searchpath, str) else searchpath
|
||||
self.source = plugin_base.make_plugin_source(searchpath=searchpath,
|
||||
identifier=self.__class__.__name__)
|
||||
super().__init__(searchpath=searchpath, package_name=PLUGIN_PACKAGE_NAME)
|
||||
|
||||
for plugin_name in self.source.list_plugins():
|
||||
plugin = self.source.load_plugin(plugin_name)
|
||||
plugin.setup(self)
|
||||
|
||||
def on_plugin_setup(self, plugin_class):
|
||||
name = getattr(plugin_class, 'name', plugin_class.__name__)
|
||||
self.class_map[name.lower()] = plugin_class
|
||||
|
||||
def plugin(self, name):
|
||||
return self.class_map.get(name, None)
|
||||
|
||||
def create(self, class_name, class_config=None, **kwargs):
|
||||
if not class_name:
|
||||
raise RuntimeError('Please specify router class_name first!')
|
||||
|
||||
this_class = self.plugin(class_name.lower())
|
||||
if not this_class:
|
||||
raise RuntimeError('{} Plugin \'{}\' Not Installed!'.format(self.PLUGIN_TYPE, class_name))
|
||||
|
||||
router = this_class.create(class_config, **kwargs)
|
||||
def _create(self, plugin_class, **kwargs):
|
||||
router = plugin_class.create(**kwargs)
|
||||
return router
|
||||
|
||||
@ -51,7 +51,7 @@ class Factory(RouterMixin):
|
||||
return routing
|
||||
|
||||
@classmethod
|
||||
def create(cls, config, **kwargs):
|
||||
def create(cls, **kwargs):
|
||||
conn_mgr = kwargs.pop('conn_mgr', None)
|
||||
if not conn_mgr:
|
||||
raise RuntimeError('Cannot find \'conn_mgr\' to initialize \'{}\''.format(self.name))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user