From 211c571508824f17ba0221c7c953c4eea6931c00 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sat, 13 Mar 2021 18:21:56 +0800 Subject: [PATCH] :bug: fix reimport by require --- nonebot/plugin/manager.py | 22 +++++++- poetry.lock | 112 ++++++++++++++++++++++++++++++++++---- pyproject.toml | 3 +- 3 files changed, 123 insertions(+), 14 deletions(-) diff --git a/nonebot/plugin/manager.py b/nonebot/plugin/manager.py index 5965d02b..40426b7f 100644 --- a/nonebot/plugin/manager.py +++ b/nonebot/plugin/manager.py @@ -6,7 +6,7 @@ from hashlib import md5 from types import ModuleType from collections import Counter from importlib.abc import MetaPathFinder -from importlib.machinery import PathFinder +from importlib.machinery import PathFinder, SourceFileLoader from typing import Set, List, Optional, Iterable _internal_space = ModuleType(__name__ + "._internal") @@ -142,6 +142,7 @@ class PluginManager: if "." in name: raise ValueError("Plugin name cannot contain '.'") + with self: return importlib.import_module(f"{self.namespace}.{name}") @@ -173,9 +174,28 @@ class PluginFinder(MetaPathFinder): list(manager.search_path), target) if spec: + spec.loader = PluginLoader(newname, spec.origin) return spec index -= 1 return None +class PluginLoader(SourceFileLoader): + + def __init__(self, fullname: str, path) -> None: + self.loaded = False + super().__init__(fullname, path) + + def create_module(self, spec) -> Optional[ModuleType]: + if self.name in sys.modules: + self.loaded = True + return sys.modules[self.name] + return super().create_module(spec) + + def exec_module(self, module: ModuleType) -> None: + if self.loaded: + return + return super().exec_module(module) + + sys.meta_path.insert(0, PluginFinder()) diff --git a/poetry.lock b/poetry.lock index 73598b33..d33bbbc2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -3,7 +3,7 @@ name = "aiofiles" version = "0.6.0" description = "File support for asyncio." category = "main" -optional = true +optional = false python-versions = "*" [package.source] @@ -389,7 +389,7 @@ reference = "aliyun" [[package]] name = "nonebot-adapter-cqhttp" -version = "2.0.0-alpha.11" +version = "2.0.0a11.post2" description = "OneBot(CQHTTP) adapter for nonebot2" category = "dev" optional = false @@ -406,7 +406,7 @@ url = "packages/nonebot-adapter-cqhttp" [[package]] name = "nonebot-adapter-ding" -version = "2.0.0-alpha.11" +version = "2.0.0a11.post2" description = "Ding adapter for nonebot2" category = "dev" optional = false @@ -423,7 +423,7 @@ url = "packages/nonebot-adapter-ding" [[package]] name = "nonebot-adapter-mirai" -version = "2.0.0-alpha.11" +version = "2.0.0a11.post2" description = "Mirai Api HTTP adapter for nonebot2" category = "dev" optional = false @@ -439,6 +439,24 @@ websockets = "^8.1" type = "directory" url = "packages/nonebot-adapter-mirai" +[[package]] +name = "nonebot-plugin-test" +version = "0.2.0" +description = "Test frontend for nonebot v2+" +category = "dev" +optional = false +python-versions = ">=3.7,<4.0" + +[package.dependencies] +aiofiles = ">=0.6.0,<0.7.0" +nonebot2 = ">=2.0.0-alpha.9,<3.0.0" +python-socketio = ">=4.6.1,<5.0.0" + +[package.source] +type = "legacy" +url = "https://mirrors.aliyun.com/pypi/simple" +reference = "aliyun" + [[package]] name = "packaging" version = "20.9" @@ -507,7 +525,7 @@ reference = "aliyun" [[package]] name = "pygments" -version = "2.8.0" +version = "2.8.1" description = "Pygments is a syntax highlighting package written in Python." category = "dev" optional = false @@ -560,6 +578,47 @@ type = "legacy" url = "https://mirrors.aliyun.com/pypi/simple" reference = "aliyun" +[[package]] +name = "python-engineio" +version = "3.14.2" +description = "Engine.IO server" +category = "dev" +optional = false +python-versions = "*" + +[package.dependencies] +six = ">=1.9.0" + +[package.extras] +asyncio_client = ["aiohttp (>=3.4)"] +client = ["requests (>=2.21.0)", "websocket-client (>=0.54.0)"] + +[package.source] +type = "legacy" +url = "https://mirrors.aliyun.com/pypi/simple" +reference = "aliyun" + +[[package]] +name = "python-socketio" +version = "4.6.1" +description = "Socket.IO server" +category = "dev" +optional = false +python-versions = "*" + +[package.dependencies] +python-engineio = ">=3.13.0,<4" +six = ">=1.9.0" + +[package.extras] +asyncio_client = ["aiohttp (>=3.4)", "websockets (>=7.0)"] +client = ["requests (>=2.21.0)", "websocket-client (>=0.54.0)"] + +[package.source] +type = "legacy" +url = "https://mirrors.aliyun.com/pypi/simple" +reference = "aliyun" + [[package]] name = "pytz" version = "2021.1" @@ -655,6 +714,19 @@ type = "legacy" url = "https://mirrors.aliyun.com/pypi/simple" reference = "aliyun" +[[package]] +name = "six" +version = "1.15.0" +description = "Python 2 and 3 compatibility utilities" +category = "dev" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" + +[package.source] +type = "legacy" +url = "https://mirrors.aliyun.com/pypi/simple" +reference = "aliyun" + [[package]] name = "sniffio" version = "1.2.0" @@ -683,7 +755,7 @@ reference = "aliyun" [[package]] name = "sphinx" -version = "3.5.1" +version = "3.5.2" description = "Python documentation generator" category = "dev" optional = false @@ -735,7 +807,7 @@ yapf = "*" [package.source] type = "git" -url = "https://github.com/nonebot/sphinx-markdown-builder.git" +url = "git@github.com:nonebot/sphinx-markdown-builder.git" reference = "master" resolved_reference = "fdbc39e1b50aabf8dbcf129895fbbd02cbf54554" @@ -1083,7 +1155,7 @@ quart = ["Quart"] [metadata] lock-version = "1.1" python-versions = "^3.7.3" -content-hash = "f4e49d25ac5c37b3c7527935b52acf5d0d6d0261a645f4247120f9e28c5ca282" +content-hash = "51c469a7330ef3dc06805b771670f2c1526ebfc6e50d7c22571ae3ffc7b0f352" [metadata.files] aiofiles = [ @@ -1248,6 +1320,10 @@ markupsafe = [ nonebot-adapter-cqhttp = [] nonebot-adapter-ding = [] nonebot-adapter-mirai = [] +nonebot-plugin-test = [ + {file = "nonebot-plugin-test-0.2.0.tar.gz", hash = "sha256:c9ee997c5c96160de4af02d10a7c6301b3fc4e942df7e70906df0534606ea23b"}, + {file = "nonebot_plugin_test-0.2.0-py3-none-any.whl", hash = "sha256:75cd18cc282815a03250bb86c7d2a8d6a66a5064ac335bedc9a3e268a1e7dd13"}, +] packaging = [ {file = "packaging-20.9-py2.py3-none-any.whl", hash = "sha256:67714da7f7bc052e064859c05c595155bd1ee9f69f76557e21f051443c20947a"}, {file = "packaging-20.9.tar.gz", hash = "sha256:5b327ac1320dc863dca72f4514ecc086f31186744b84a230374cc1fd776feae5"}, @@ -1285,8 +1361,8 @@ pydash = [ {file = "pydash-4.9.3.tar.gz", hash = "sha256:d709e57b537b1aaf118f188da3ec6242a665090ecd7839b66f857ee3dc2bb006"}, ] pygments = [ - {file = "Pygments-2.8.0-py3-none-any.whl", hash = "sha256:b21b072d0ccdf29297a82a2363359d99623597b8a265b8081760e4d0f7153c88"}, - {file = "Pygments-2.8.0.tar.gz", hash = "sha256:37a13ba168a02ac54cc5891a42b1caec333e59b66addb7fa633ea8a6d73445c0"}, + {file = "Pygments-2.8.1-py3-none-any.whl", hash = "sha256:534ef71d539ae97d4c3a4cf7d6f110f214b0e687e92f9cb9d2a3b0d3101289c8"}, + {file = "Pygments-2.8.1.tar.gz", hash = "sha256:2656e1a6edcdabf4275f9a3640db59fd5de107d88e8663c5d4e9a0fa62f77f94"}, ] pygtrie = [ {file = "pygtrie-2.4.2.tar.gz", hash = "sha256:43205559d28863358dbbf25045029f58e2ab357317a59b11f11ade278ac64692"}, @@ -1299,6 +1375,14 @@ python-dotenv = [ {file = "python-dotenv-0.15.0.tar.gz", hash = "sha256:587825ed60b1711daea4832cf37524dfd404325b7db5e25ebe88c495c9f807a0"}, {file = "python_dotenv-0.15.0-py2.py3-none-any.whl", hash = "sha256:0c8d1b80d1a1e91717ea7d526178e3882732420b03f08afea0406db6402e220e"}, ] +python-engineio = [ + {file = "python-engineio-3.14.2.tar.gz", hash = "sha256:eab4553f2804c1ce97054c8b22cf0d5a9ab23128075248b97e1a5b2f29553085"}, + {file = "python_engineio-3.14.2-py2.py3-none-any.whl", hash = "sha256:5a9e6086d192463b04a1428ff1f85b6ba631bbb19d453b144ffc04f530542b84"}, +] +python-socketio = [ + {file = "python-socketio-4.6.1.tar.gz", hash = "sha256:cd1f5aa492c1eb2be77838e837a495f117e17f686029ebc03d62c09e33f4fa10"}, + {file = "python_socketio-4.6.1-py2.py3-none-any.whl", hash = "sha256:5a21da53fdbdc6bb6c8071f40e13d100e0b279ad997681c2492478e06f370523"}, +] pytz = [ {file = "pytz-2021.1-py2.py3-none-any.whl", hash = "sha256:eb10ce3e7736052ed3623d49975ce333bcd712c7bb19a58b9e2089d4057d0798"}, {file = "pytz-2021.1.tar.gz", hash = "sha256:83a4a90894bf38e243cf052c8b58f381bfe9a7a483f6a9cab140bc7f702ac4da"}, @@ -1338,6 +1422,10 @@ rfc3986 = [ {file = "rfc3986-1.4.0-py2.py3-none-any.whl", hash = "sha256:af9147e9aceda37c91a05f4deb128d4b4b49d6b199775fd2d2927768abdc8f50"}, {file = "rfc3986-1.4.0.tar.gz", hash = "sha256:112398da31a3344dc25dbf477d8df6cb34f9278a94fee2625d89e4514be8bb9d"}, ] +six = [ + {file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"}, + {file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"}, +] sniffio = [ {file = "sniffio-1.2.0-py3-none-any.whl", hash = "sha256:471b71698eac1c2112a40ce2752bb2f4a4814c22a54a3eed3676bc0f5ca9f663"}, {file = "sniffio-1.2.0.tar.gz", hash = "sha256:c4666eecec1d3f50960c6bdf61ab7bc350648da6c126e3cf6898d8cd4ddcd3de"}, @@ -1347,8 +1435,8 @@ snowballstemmer = [ {file = "snowballstemmer-2.1.0.tar.gz", hash = "sha256:e997baa4f2e9139951b6f4c631bad912dfd3c792467e2f03d7239464af90e914"}, ] sphinx = [ - {file = "Sphinx-3.5.1-py3-none-any.whl", hash = "sha256:e90161222e4d80ce5fc811ace7c6787a226b4f5951545f7f42acf97277bfc35c"}, - {file = "Sphinx-3.5.1.tar.gz", hash = "sha256:11d521e787d9372c289472513d807277caafb1684b33eb4f08f7574c405893a9"}, + {file = "Sphinx-3.5.2-py3-none-any.whl", hash = "sha256:ef64a814576f46ec7de06adf11b433a0d6049be007fefe7fd0d183d28b581fac"}, + {file = "Sphinx-3.5.2.tar.gz", hash = "sha256:672cfcc24b6b69235c97c750cb190a44ecd72696b4452acaf75c2d9cc78ca5ff"}, ] sphinx-markdown-builder = [] sphinxcontrib-applehelp = [ diff --git a/pyproject.toml b/pyproject.toml index 7e64c234..283b7927 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,10 +35,11 @@ uvicorn = { version = "^0.13.0", extras = ["standard"] } [tool.poetry.dev-dependencies] yapf = "^0.30.0" sphinx = "^3.4.1" +nonebot-plugin-test = "^0.2.0" nonebot-adapter-cqhttp = { path = "./packages/nonebot-adapter-cqhttp", develop = true } nonebot-adapter-ding = { path = "./packages/nonebot-adapter-ding", develop = true } nonebot-adapter-mirai = { path = "./packages/nonebot-adapter-mirai", develop = true } -sphinx-markdown-builder = { git = "https://github.com/nonebot/sphinx-markdown-builder.git" } +sphinx-markdown-builder = { git = "git@github.com:nonebot/sphinx-markdown-builder.git" } [tool.poetry.extras] quart = ["quart"]