mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-18 09:25:46 +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
|
||||
from string import Formatter
|
||||
from typing import (TYPE_CHECKING, Any, Set, List, Type, Tuple, Union, Generic,
|
||||
@ -114,8 +115,14 @@ class MessageTemplate(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
|
||||
segment_class: Type[MessageSegment] = self.factory.get_segment_class()
|
||||
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:
|
||||
try:
|
||||
|
Loading…
Reference in New Issue
Block a user