support user-defined format spec for message template

This commit is contained in:
Mix 2022-01-01 21:52:54 +08:00
parent 620938c26c
commit 983e5aefdb

View File

@ -4,6 +4,9 @@ from string import Formatter
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any, Any,
Callable,
Dict,
Optional,
Set, Set,
List, List,
Type, Type,
@ -23,6 +26,9 @@ if TYPE_CHECKING:
TM = TypeVar("TM", bound="Message") TM = TypeVar("TM", bound="Message")
TF = TypeVar("TF", str, "Message") TF = TypeVar("TF", str, "Message")
FormatSpecFunc = Callable[[Any], str]
FormatSpecFunc_T = TypeVar("FormatSpecFunc_T", bound=FormatSpecFunc)
class MessageTemplate(Formatter, Generic[TF]): class MessageTemplate(Formatter, Generic[TF]):
"""消息模板格式化实现类""" """消息模板格式化实现类"""
@ -50,8 +56,18 @@ class MessageTemplate(Formatter, Generic[TF]):
* ``template: Union[str, Message]``: 模板 * ``template: Union[str, Message]``: 模板
* ``factory: Union[str, Message]``: 消息构造类型默认为 `str` * ``factory: Union[str, Message]``: 消息构造类型默认为 `str`
""" """
self.template: Union[str, TF] = template self.template: TF = template
self.factory: Type[TF] = factory self.factory: Type[TF] = factory
self.format_specs: Dict[str, FormatSpecFunc] = {}
def add_format_spec(
self, spec: FormatSpecFunc_T, name: Optional[str] = None
) -> FormatSpecFunc_T:
name = name or spec.__name__
if name in self.format_specs:
raise ValueError(f"Format spec {name} already exists!")
self.format_specs[name] = spec
return spec
def format(self, *args: Any, **kwargs: Any) -> TF: def format(self, *args: Any, **kwargs: Any) -> TF:
""" """
@ -69,7 +85,7 @@ class MessageTemplate(Formatter, Generic[TF]):
else: else:
raise TypeError("template must be a string or instance of Message!") raise TypeError("template must be a string or instance of Message!")
return msg return msg # type:ignore
def vformat( def vformat(
self, format_string: str, args: Sequence[Any], kwargs: Mapping[str, Any] self, format_string: str, args: Sequence[Any], kwargs: Mapping[str, Any]
@ -165,12 +181,14 @@ class MessageTemplate(Formatter, Generic[TF]):
( (
super().format_field(value, format_spec) super().format_field(value, format_spec)
if ( if (
(method is None) method is None
or ( or not isinstance(method_type, (classmethod, staticmethod))
not isinstance(method_type, (classmethod, staticmethod)) )
) # Only Call staticmethod or classmethod else (
self.format_specs[format_spec](value)
if format_spec in self.format_specs
else method(value)
) )
else method(value)
) )
if format_spec if format_spec
else value else value