🐛 fix missing matcher when load external plugin with dot

This commit is contained in:
yanyongyu 2021-04-03 17:32:01 +08:00
parent 0fd82d4f54
commit c402b7599f

View File

@ -7,9 +7,10 @@
import re import re
import json import json
from types import ModuleType from types import ModuleType
from functools import reduce
from dataclasses import dataclass from dataclasses import dataclass
from collections import defaultdict from collections import defaultdict
from contextvars import Context, ContextVar, copy_context from contextvars import Context, copy_context
from typing import Any, Set, List, Dict, Type, Tuple, Union, Optional, TYPE_CHECKING from typing import Any, Set, List, Dict, Type, Tuple, Union, Optional, TYPE_CHECKING
import tomlkit import tomlkit
@ -49,11 +50,14 @@ class Plugin(object):
- **类型**: ``ModuleType`` - **类型**: ``ModuleType``
- **说明**: 插件模块对象 - **说明**: 插件模块对象
""" """
export: Export
""" @property
- **类型**: ``Export`` def export(self) -> Export:
- **说明**: 插件内定义的导出内容 """
""" - **类型**: ``Export``
- **说明**: 插件内定义的导出内容
"""
return getattr(self.module, "__export__", Export())
@property @property
def matcher(self) -> Set[Type[Matcher]]: def matcher(self) -> Set[Type[Matcher]]:
@ -61,13 +65,15 @@ class Plugin(object):
- **类型**: ``Set[Type[Matcher]]`` - **类型**: ``Set[Type[Matcher]]``
- **说明**: 插件内定义的 ``Matcher`` - **说明**: 插件内定义的 ``Matcher``
""" """
return _plugin_matchers[self.name] return reduce(
lambda x, y: x | _plugin_matchers[y],
filter(lambda x: x.startswith(self.name), _plugin_matchers.keys()),
set())
def _store_matcher(matcher: Type[Matcher]): def _store_matcher(matcher: Type[Matcher]):
if matcher.module: if matcher.module:
plugin_name = matcher.module.split(".", maxsplit=1)[0] _plugin_matchers[matcher.module].add(matcher)
_plugin_matchers[plugin_name].add(matcher)
def on(type: str = "", def on(type: str = "",
@ -928,8 +934,7 @@ def _load_plugin(manager: PluginManager, plugin_name: str) -> Optional[Plugin]:
try: try:
module = manager.load_plugin(plugin_name) module = manager.load_plugin(plugin_name)
plugin = Plugin(plugin_name, module, plugin = Plugin(plugin_name, module)
getattr(module, "__export__", Export()))
plugins[plugin_name] = plugin plugins[plugin_name] = plugin
logger.opt( logger.opt(
colors=True).info(f'Succeeded to import "<y>{plugin_name}</y>"') colors=True).info(f'Succeeded to import "<y>{plugin_name}</y>"')