From 385a0c4eab08a7ff933962bf8ebf284525a5f034 Mon Sep 17 00:00:00 2001 From: rf_tar_railt <3165388245@qq.com> Date: Thu, 11 Jul 2024 00:02:41 +0800 Subject: [PATCH] :ambulance: version 0.6.1 fix plugin finder --- arclet/entari/plugin/module.py | 57 ++++++++++++++++++---------------- example_plugin.py | 2 +- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/arclet/entari/plugin/module.py b/arclet/entari/plugin/module.py index d279e1d..592c227 100644 --- a/arclet/entari/plugin/module.py +++ b/arclet/entari/plugin/module.py @@ -9,24 +9,6 @@ from .model import Plugin, PluginMetadata, _current_plugin, _plugins -class PluginFinder(MetaPathFinder): - def find_spec( - self, - fullname: str, - path: Optional[Sequence[str]], - target: Optional[ModuleType] = None, - ): - module_spec = PathFinder.find_spec(fullname, path, target) - if not module_spec: - return - module_origin = module_spec.origin - if not module_origin: - return - - module_spec.loader = PluginLoader(fullname, module_origin) - return module_spec - - class PluginLoader(SourceFileLoader): def __init__(self, fullname: str, path: str) -> None: self.loaded = False @@ -51,9 +33,9 @@ def exec_module(self, module: ModuleType) -> None: try: super().exec_module(module) - # except Exception: - # # _revert_plugin(plugin) - # raise + except Exception: + plugin.dispose() + raise finally: # leave plugin context _current_plugin.reset(_plugin_token) @@ -64,9 +46,6 @@ def exec_module(self, module: ModuleType) -> None: return -_finder = PluginFinder() - - def find_spec(name, package=None): fullname = resolve_name(name, package) if name.startswith(".") else name parent_name = fullname.rpartition(".")[0] @@ -81,7 +60,14 @@ def find_spec(name, package=None): ) from e else: parent_path = None - return _finder.find_spec(fullname, parent_path) + module_spec = PathFinder.find_spec(fullname, parent_path, None) + if not module_spec: + return + module_origin = module_spec.origin + if not module_origin: + return + module_spec.loader = PluginLoader(fullname, module_origin) + return module_spec def import_plugin(name, package=None): @@ -94,4 +80,23 @@ def import_plugin(name, package=None): return -sys.meta_path.insert(0, _finder) +class _PluginFinder(MetaPathFinder): + def find_spec( + self, + fullname: str, + path: Optional[Sequence[str]], + target: Optional[ModuleType] = None, + ): + module_spec = PathFinder.find_spec(fullname, path, target) + if not module_spec: + return + module_origin = module_spec.origin + if not module_origin: + return + if module_spec.name in _plugins: + module_spec.loader = PluginLoader(fullname, module_origin) + return module_spec + return + + +sys.meta_path.insert(0, _PluginFinder()) diff --git a/example_plugin.py b/example_plugin.py index 4a08f46..b5c0e7b 100644 --- a/example_plugin.py +++ b/example_plugin.py @@ -12,7 +12,7 @@ ) from arclet.entari.command import Match -__plugin_metadata__ = PluginMetadata() +__plugin_metadata__ = PluginMetadata(__file__) disp_message = MessageCreatedEvent.dispatch()