Featue: load_plugin 支持 pathlib.Path (#1194)

Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
This commit is contained in:
Lan
2022-08-31 10:07:14 +08:00
committed by GitHub
parent 4e76518a58
commit 1cfdee2645
4 changed files with 45 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import json
import asyncio
import inspect
import dataclasses
from pathlib import Path
from functools import wraps, partial
from contextlib import asynccontextmanager
from typing_extensions import ParamSpec, get_args, get_origin
@ -165,6 +166,14 @@ def get_name(obj: Any) -> str:
return obj.__class__.__name__
def path_to_module_name(path: Path) -> str:
rel_path = path.resolve().relative_to(Path(".").resolve())
if rel_path.stem == "__init__":
return ".".join(rel_path.parts[:-1])
else:
return ".".join(rel_path.parts[:-1] + (rel_path.stem,))
class DataclassEncoder(json.JSONEncoder):
"""在JSON序列化 {re}`nonebot.adapters._message.Message` (List[Dataclass]) 时使用的 `JSONEncoder`"""