mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-02-08 03:46:18 +08:00
♻️ refactor template format_field
to improve readability
This commit is contained in:
parent
983e5aefdb
commit
43938a004e
@ -171,27 +171,16 @@ class MessageTemplate(Formatter, Generic[TF]):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def format_field(self, value: Any, format_spec: str) -> Any:
|
def format_field(self, value: Any, format_spec: str) -> Any:
|
||||||
if issubclass(self.factory, str):
|
formatter: Optional[FormatSpecFunc] = self.format_specs.get(format_spec)
|
||||||
return super().format_field(value, format_spec)
|
if (formatter is None) and (not issubclass(self.factory, str)):
|
||||||
|
segment_class: Type["MessageSegment"] = self.factory.get_segment_class()
|
||||||
segment_class: Type[MessageSegment] = self.factory.get_segment_class()
|
|
||||||
method = getattr(segment_class, format_spec, None)
|
method = getattr(segment_class, format_spec, None)
|
||||||
method_type = inspect.getattr_static(segment_class, format_spec, None)
|
if inspect.ismethod(method):
|
||||||
|
formatter = getattr(segment_class, format_spec)
|
||||||
return (
|
return (
|
||||||
(
|
|
||||||
super().format_field(value, format_spec)
|
super().format_field(value, format_spec)
|
||||||
if (
|
if formatter is None
|
||||||
method is None
|
else formatter(value)
|
||||||
or not isinstance(method_type, (classmethod, staticmethod))
|
|
||||||
)
|
|
||||||
else (
|
|
||||||
self.format_specs[format_spec](value)
|
|
||||||
if format_spec in self.format_specs
|
|
||||||
else method(value)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if format_spec
|
|
||||||
else value
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def _add(self, a: Any, b: Any) -> Any:
|
def _add(self, a: Any, b: Any) -> Any:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user