Skip to content

Commit

Permalink
🍻 use weakref.ref in ProxyModule
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Sep 30, 2024
1 parent 589fb58 commit afa634f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
22 changes: 13 additions & 9 deletions arclet/entari/plugin/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
from types import ModuleType
from typing import TYPE_CHECKING, Any, Callable, TypeVar
from weakref import finalize
from weakref import finalize, ref

from arclet.letoderea import BaseAuxiliary, Provider, Publisher, StepOut, system_ctx
from arclet.letoderea.builtin.breakpoint import R
Expand Down Expand Up @@ -230,17 +230,21 @@ def keeping(id_: str, obj: T, dispose: Callable[[T], None] | None = None) -> T:

class _ProxyModule(ModuleType):

def __get_module(self):
if self.__plugin_id not in service.plugins:
def __get_module(self) -> ModuleType:
mod = self.__origin()
if not mod:
raise NameError(f"Plugin {self.__plugin_id!r} is not loaded")
if self.__sub_id:
return service.plugins[self.__plugin_id].submodules[self.__sub_id]
return service.plugins[self.__plugin_id].module
return mod

def __init__(self, plugin_id: str, sub_id: str | None = None) -> None:
self.__plugin_id = plugin_id
self.__sub_id = sub_id

if self.__plugin_id not in service.plugins:
raise NameError(f"Plugin {self.__plugin_id!r} is not loaded")
if self.__sub_id:
self.__origin = ref(service.plugins[self.__plugin_id].submodules[self.__sub_id])
else:
self.__origin = ref(service.plugins[self.__plugin_id].module)
super().__init__(self.__get_module().__name__)
self.__doc__ = self.__get_module().__doc__
self.__file__ = self.__get_module().__file__
Expand All @@ -257,14 +261,13 @@ def __repr__(self):

@property
def __dict__(self) -> dict[str, Any]:
if self.__plugin_id not in service.plugins:
raise NameError(f"Plugin {self.__plugin_id!r} is not loaded")
return self.__get_module().__dict__

def __getattr__(self, name: str):
if name in (
"_ProxyModule__plugin_id",
"_ProxyModule__sub_id",
"_ProxyModule__origin",
"__name__",
"__doc__",
"__file__",
Expand All @@ -288,6 +291,7 @@ def __setattr__(self, name: str, value):
if name in (
"_ProxyModule__plugin_id",
"_ProxyModule__sub_id",
"_ProxyModule__origin",
"__name__",
"__doc__",
"__file__",
Expand Down
13 changes: 11 additions & 2 deletions arclet/entari/plugin/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def _check_mod(name, package=None):
if not module:
raise ModuleNotFoundError(f"module {name!r} not found")
if hasattr(module, "__plugin__"):
return module.__plugin__.subproxy(f"{package}{name}") if package else module.__plugin__.proxy()
if not package:
if name != module.__plugin__.id:
service._referents[name].add(module.__plugin__.id)
return module.__plugin__.proxy()
return module.__plugin__.subproxy(f"{package}{name}")
return module


Expand All @@ -43,10 +47,15 @@ def _unpack_import_from(__fullname: str, mod: str, aliases: list[str]):

def _check_import(name: str, plugin_name: str):
if name in service.plugins:
return service.plugins[name].proxy()
plug = service.plugins[name]
if plugin_name != plug.id:
service._referents[plug.id].add(plugin_name)
return plug.proxy()
if name in _SUBMODULE_WAITLIST.get(plugin_name, ()):
mod = import_plugin(name)
if mod:
if plugin_name != mod.__plugin__.id:
service._referents[mod.__plugin__.id].add(plugin_name)
return mod.__plugin__.subproxy(name)
return __import__(name)

Expand Down
2 changes: 1 addition & 1 deletion example_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def append(data: str, session: Session):
async def show(session: Session):
await session.send_message(f"Data: {kept_data}")

TEST = 6
TEST = 4

print([*Plugin.current().dispatchers.keys()])
print(Plugin.current().submodules)
Expand Down

0 comments on commit afa634f

Please sign in to comment.