From 44d13acc817058e7c89e5851b7c445cd997b14a3 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sat, 11 Sep 2021 19:48:46 +0800 Subject: [PATCH] :recycle: rename message formatter into template --- nonebot/adapters/__init__.py | 2 +- nonebot/adapters/_message.py | 6 +++--- nonebot/adapters/{_formatter.py => _template.py} | 5 +++-- nonebot/matcher.py | 6 +++--- 4 files changed, 10 insertions(+), 9 deletions(-) rename nonebot/adapters/{_formatter.py => _template.py} (95%) diff --git a/nonebot/adapters/__init__.py b/nonebot/adapters/__init__.py index 70cd37aa..6c1b6f45 100644 --- a/nonebot/adapters/__init__.py +++ b/nonebot/adapters/__init__.py @@ -22,5 +22,5 @@ except Exception: from ._bot import Bot from ._event import Event -from ._formatter import MessageFormatter +from ._template import MessageTemplate from ._message import Message, MessageSegment diff --git a/nonebot/adapters/_message.py b/nonebot/adapters/_message.py index 403e2f1b..cc7cd377 100644 --- a/nonebot/adapters/_message.py +++ b/nonebot/adapters/_message.py @@ -4,7 +4,7 @@ from dataclasses import dataclass, field, asdict from typing import (Any, List, Dict, Type, Union, TypeVar, Mapping, Generic, Iterable) -from ._formatter import MessageFormatter +from ._template import MessageTemplate T = TypeVar("T") TMS = TypeVar("TMS", covariant=True) @@ -103,7 +103,7 @@ class Message(List[TMS], abc.ABC): self.extend(self._construct(message)) @classmethod - def template(cls: Type[TM], format_string: str) -> MessageFormatter[TM]: + def template(cls: Type[TM], format_string: str) -> MessageTemplate[TM]: """ :说明: @@ -126,7 +126,7 @@ class Message(List[TMS], abc.ABC): - ``MessageFormatter[TM]``: 消息格式化器 """ - return MessageFormatter(cls, format_string) + return MessageTemplate(cls, format_string) @classmethod @abc.abstractmethod diff --git a/nonebot/adapters/_formatter.py b/nonebot/adapters/_template.py similarity index 95% rename from nonebot/adapters/_formatter.py rename to nonebot/adapters/_template.py index c0449fd6..3e60d1a1 100644 --- a/nonebot/adapters/_formatter.py +++ b/nonebot/adapters/_template.py @@ -10,7 +10,7 @@ if TYPE_CHECKING: TM = TypeVar("TM", bound="Message") -class MessageFormatter(Formatter, Generic[TM]): +class MessageTemplate(Formatter, Generic[TM]): """消息模板格式化实现类""" def __init__(self, factory: Type[TM], template: str) -> None: @@ -105,4 +105,5 @@ class MessageFormatter(Formatter, Generic[TM]): [""])), auto_arg_index def format_field(self, value: Any, format_spec: str) -> Any: - return super().format_field(value, format_spec) if format_spec else value + return super().format_field(value, + format_spec) if format_spec else value diff --git a/nonebot/matcher.py b/nonebot/matcher.py index 58f338f9..095d3320 100644 --- a/nonebot/matcher.py +++ b/nonebot/matcher.py @@ -16,7 +16,7 @@ from typing import (Any, Type, List, Dict, Union, Mapping, Iterable, Callable, from nonebot.rule import Rule from nonebot.log import logger from nonebot.handler import Handler -from nonebot.adapters import MessageFormatter +from nonebot.adapters import MessageTemplate from nonebot.permission import Permission, USER from nonebot.typing import (T_State, T_StateFactory, T_Handler, T_ArgsParser, T_TypeUpdater, T_PermissionUpdater) @@ -403,7 +403,7 @@ class Matcher(metaclass=MatcherMeta): cls, key: str, prompt: Optional[Union[str, "Message", "MessageSegment", - MessageFormatter]] = None, + MessageTemplate]] = None, args_parser: Optional[T_ArgsParser] = None ) -> Callable[[T_Handler], T_Handler]: """ @@ -422,7 +422,7 @@ class Matcher(metaclass=MatcherMeta): state["_current_key"] = key if key not in state: if prompt: - if isinstance(prompt, MessageFormatter): + if isinstance(prompt, MessageTemplate): _prompt = prompt.format(**state) else: _prompt = prompt