Skip to content

Commit

Permalink
兼容 Pydantic V2
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Feb 26, 2024
1 parent 62fe056 commit c849843
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 82 deletions.
1 change: 1 addition & 0 deletions nonebot_plugin_memes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ async def finish(msg: str) -> NoReturn:
try:
parse_result = meme.parse_args(raw_texts)
except ArgParserExit:
logger.warning(traceback.format_exc())
await finish("参数解析错误")
texts = parse_result["texts"]
parse_result.pop("texts")
Expand Down
8 changes: 4 additions & 4 deletions nonebot_plugin_memes/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import List

from nonebot import get_driver
from pydantic import BaseModel, Extra
from nonebot import get_plugin_config
from pydantic import BaseModel


class Config(BaseModel, extra=Extra.ignore):
class Config(BaseModel):
memes_command_start: List[str] = []
memes_command_force_whitespace: bool = True
memes_disabled_list: List[str] = []
Expand All @@ -14,4 +14,4 @@ class Config(BaseModel, extra=Extra.ignore):
memes_use_default_when_no_text: bool = False


memes_config = Config.parse_obj(get_driver().config.dict())
memes_config = get_plugin_config(Config)
8 changes: 6 additions & 2 deletions nonebot_plugin_memes/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import yaml
from meme_generator.manager import get_memes
from meme_generator.meme import Meme
from nonebot.compat import model_dump, type_validate_python
from nonebot.log import logger
from nonebot_plugin_localstore import get_config_file
from pydantic import BaseModel
Expand Down Expand Up @@ -134,7 +135,8 @@ def __load(self):
logger.warning("表情列表解析失败,将重新生成")
try:
meme_list = {
name: MemeConfig.parse_obj(config) for name, config in raw_list.items()
name: type_validate_python(MemeConfig, config)
for name, config in raw_list.items()
}
except Exception:
meme_list = {}
Expand All @@ -144,7 +146,9 @@ def __load(self):

def __dump(self):
self.__path.parent.mkdir(parents=True, exist_ok=True)
meme_list = {name: config.dict() for name, config in self.__meme_list.items()}
meme_list = {
name: model_dump(config) for name, config in self.__meme_list.items()
}
with self.__path.open("w", encoding="utf-8") as f:
yaml.dump(meme_list, f, allow_unicode=True)

Expand Down
Loading

0 comments on commit c849843

Please sign in to comment.