♻️ rename message formatter into template

This commit is contained in:
yanyongyu 2021-09-11 19:48:46 +08:00
parent 59f567004f
commit 44d13acc81
4 changed files with 10 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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