Skip to content

Commit

Permalink
🚑 version 0.6.1
Browse files Browse the repository at this point in the history
fix plugin finder
  • Loading branch information
RF-Tar-Railt committed Jul 10, 2024
1 parent 27605b3 commit 385a0c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
57 changes: 31 additions & 26 deletions arclet/entari/plugin/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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]
Expand All @@ -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):
Expand All @@ -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())
2 changes: 1 addition & 1 deletion example_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from arclet.entari.command import Match

__plugin_metadata__ = PluginMetadata()
__plugin_metadata__ = PluginMetadata(__file__)

disp_message = MessageCreatedEvent.dispatch()

Expand Down

0 comments on commit 385a0c4

Please sign in to comment.