mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 05:27:30 +08:00
✨ support use MessageSegment
method as template format spec
This commit is contained in:
parent
69f8821acb
commit
6b79106cd1
@ -1,3 +1,4 @@
|
|||||||
|
import inspect
|
||||||
import functools
|
import functools
|
||||||
from string import Formatter
|
from string import Formatter
|
||||||
from typing import (TYPE_CHECKING, Any, Set, List, Type, Tuple, Union, Generic,
|
from typing import (TYPE_CHECKING, Any, Set, List, Type, Tuple, Union, Generic,
|
||||||
@ -114,8 +115,14 @@ class MessageTemplate(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,
|
segment_class: Type[MessageSegment] = self.factory.get_segment_class()
|
||||||
format_spec) if format_spec else value
|
method = getattr(segment_class, format_spec, None)
|
||||||
|
method_type = inspect.getattr_static(segment_class, format_spec, None)
|
||||||
|
return (super().format_field(value, format_spec) if
|
||||||
|
((method is None) or
|
||||||
|
(not isinstance(method_type, (classmethod, staticmethod))
|
||||||
|
) # Only Call staticmethod or classmethod
|
||||||
|
) else method(value)) if format_spec else value
|
||||||
|
|
||||||
def _add(self, a: Any, b: Any) -> Any:
|
def _add(self, a: Any, b: Any) -> Any:
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user