mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 05:37:26 +08:00
♻️ rename message formatter into template
This commit is contained in:
parent
59f567004f
commit
44d13acc81
@ -22,5 +22,5 @@ except Exception:
|
|||||||
|
|
||||||
from ._bot import Bot
|
from ._bot import Bot
|
||||||
from ._event import Event
|
from ._event import Event
|
||||||
from ._formatter import MessageFormatter
|
from ._template import MessageTemplate
|
||||||
from ._message import Message, MessageSegment
|
from ._message import Message, MessageSegment
|
||||||
|
@ -4,7 +4,7 @@ from dataclasses import dataclass, field, asdict
|
|||||||
from typing import (Any, List, Dict, Type, Union, TypeVar, Mapping, Generic,
|
from typing import (Any, List, Dict, Type, Union, TypeVar, Mapping, Generic,
|
||||||
Iterable)
|
Iterable)
|
||||||
|
|
||||||
from ._formatter import MessageFormatter
|
from ._template import MessageTemplate
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
TMS = TypeVar("TMS", covariant=True)
|
TMS = TypeVar("TMS", covariant=True)
|
||||||
@ -103,7 +103,7 @@ class Message(List[TMS], abc.ABC):
|
|||||||
self.extend(self._construct(message))
|
self.extend(self._construct(message))
|
||||||
|
|
||||||
@classmethod
|
@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]``: 消息格式化器
|
- ``MessageFormatter[TM]``: 消息格式化器
|
||||||
"""
|
"""
|
||||||
return MessageFormatter(cls, format_string)
|
return MessageTemplate(cls, format_string)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
|
@ -10,7 +10,7 @@ if TYPE_CHECKING:
|
|||||||
TM = TypeVar("TM", bound="Message")
|
TM = TypeVar("TM", bound="Message")
|
||||||
|
|
||||||
|
|
||||||
class MessageFormatter(Formatter, Generic[TM]):
|
class MessageTemplate(Formatter, Generic[TM]):
|
||||||
"""消息模板格式化实现类"""
|
"""消息模板格式化实现类"""
|
||||||
|
|
||||||
def __init__(self, factory: Type[TM], template: str) -> None:
|
def __init__(self, factory: Type[TM], template: str) -> None:
|
||||||
@ -105,4 +105,5 @@ class MessageFormatter(Formatter, Generic[TM]):
|
|||||||
[""])), auto_arg_index
|
[""])), auto_arg_index
|
||||||
|
|
||||||
def format_field(self, value: Any, format_spec: str) -> Any:
|
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
|
@ -16,7 +16,7 @@ from typing import (Any, Type, List, Dict, Union, Mapping, Iterable, Callable,
|
|||||||
from nonebot.rule import Rule
|
from nonebot.rule import Rule
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
from nonebot.handler import Handler
|
from nonebot.handler import Handler
|
||||||
from nonebot.adapters import MessageFormatter
|
from nonebot.adapters import MessageTemplate
|
||||||
from nonebot.permission import Permission, USER
|
from nonebot.permission import Permission, USER
|
||||||
from nonebot.typing import (T_State, T_StateFactory, T_Handler, T_ArgsParser,
|
from nonebot.typing import (T_State, T_StateFactory, T_Handler, T_ArgsParser,
|
||||||
T_TypeUpdater, T_PermissionUpdater)
|
T_TypeUpdater, T_PermissionUpdater)
|
||||||
@ -403,7 +403,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
cls,
|
cls,
|
||||||
key: str,
|
key: str,
|
||||||
prompt: Optional[Union[str, "Message", "MessageSegment",
|
prompt: Optional[Union[str, "Message", "MessageSegment",
|
||||||
MessageFormatter]] = None,
|
MessageTemplate]] = None,
|
||||||
args_parser: Optional[T_ArgsParser] = None
|
args_parser: Optional[T_ArgsParser] = None
|
||||||
) -> Callable[[T_Handler], T_Handler]:
|
) -> Callable[[T_Handler], T_Handler]:
|
||||||
"""
|
"""
|
||||||
@ -422,7 +422,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
state["_current_key"] = key
|
state["_current_key"] = key
|
||||||
if key not in state:
|
if key not in state:
|
||||||
if prompt:
|
if prompt:
|
||||||
if isinstance(prompt, MessageFormatter):
|
if isinstance(prompt, MessageTemplate):
|
||||||
_prompt = prompt.format(**state)
|
_prompt = prompt.format(**state)
|
||||||
else:
|
else:
|
||||||
_prompt = prompt
|
_prompt = prompt
|
||||||
|
Loading…
Reference in New Issue
Block a user