Skip to content

Commit

Permalink
✨ version 0.10.3
Browse files Browse the repository at this point in the history
append `title`, `poster` for Resource
  • Loading branch information
RF-Tar-Railt committed Mar 31, 2024
1 parent 70e8ea7 commit 75d53e4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
47 changes: 41 additions & 6 deletions nonebot/adapters/satori/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def image(
path: Optional[Union[str, Path]] = None,
raw: Optional[Union[bytes, BytesIO]] = None,
mime: Optional[str] = None,
name: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
Expand All @@ -94,6 +95,8 @@ def image(
data = {"src": f"data:{mime};base64,{b64encode(bd).decode()}"}
else:
raise ValueError("image need at least one of url, path and raw")
if name:
data["title"] = name
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
Expand All @@ -106,6 +109,8 @@ def audio(
path: Optional[Union[str, Path]] = None,
raw: Optional[Union[bytes, BytesIO]] = None,
mime: Optional[str] = None,
name: Optional[str] = None,
poster: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
Expand All @@ -119,6 +124,10 @@ def audio(
data = {"src": f"data:{mime};base64,{b64encode(bd).decode()}"}
else:
raise ValueError("audio need at least one of url, path and raw")
if name:
data["title"] = name
if poster:
data["poster"] = poster
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
Expand All @@ -131,6 +140,8 @@ def video(
path: Optional[Union[str, Path]] = None,
raw: Optional[Union[bytes, BytesIO]] = None,
mime: Optional[str] = None,
name: Optional[str] = None,
poster: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
Expand All @@ -144,6 +155,10 @@ def video(
data = {"src": f"data:{mime};base64,{b64encode(bd).decode()}"}
else:
raise ValueError("video need at least one of url, path and raw")
if name:
data["title"] = name
if poster:
data["poster"] = poster
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
Expand All @@ -156,6 +171,8 @@ def file(
path: Optional[Union[str, Path]] = None,
raw: Optional[Union[bytes, BytesIO]] = None,
mime: Optional[str] = None,
name: Optional[str] = None,
poster: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
Expand All @@ -169,6 +186,10 @@ def file(
data = {"src": f"data:{mime};base64,{b64encode(bd).decode()}"}
else:
raise ValueError("file need at least one of url, path and raw")
if name:
data["title"] = name
if poster:
data["poster"] = poster
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
Expand Down Expand Up @@ -206,12 +227,12 @@ def quote(
@staticmethod
def author(
user_id: str,
nickname: Optional[str] = None,
name: Optional[str] = None,
avatar: Optional[str] = None,
) -> "Author":
data = {"id": user_id}
if nickname:
data["nickname"] = nickname
if name:
data["name"] = name
if avatar:
data["avatar"] = avatar
return Author("author", data) # type: ignore
Expand Down Expand Up @@ -461,6 +482,7 @@ def is_text(self) -> bool:

class ImageData(TypedDict):
src: str
title: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]
width: NotRequired[int]
Expand All @@ -479,6 +501,9 @@ def __post_init__(self, extra: Optional[dict]):

class AudioData(TypedDict):
src: str
title: NotRequired[str]
duration: NotRequired[float]
poster: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]

Expand All @@ -495,6 +520,11 @@ def __post_init__(self, extra: Optional[dict]):

class VideoData(TypedDict):
src: str
title: NotRequired[str]
width: NotRequired[int]
height: NotRequired[int]
duration: NotRequired[float]
poster: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]

Expand All @@ -511,6 +541,8 @@ def __post_init__(self, extra: Optional[dict]):

class FileData(TypedDict):
src: str
title: NotRequired[str]
poster: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]

Expand Down Expand Up @@ -560,7 +592,7 @@ def __str__(self):

class AuthorData(TypedDict):
id: str
nickname: NotRequired[str]
name: NotRequired[str]
avatar: NotRequired[str]


Expand Down Expand Up @@ -658,6 +690,7 @@ def handle(element: Element, upper_styles: Optional[List[str]] = None):
yield Text(
"text",
{
**element.attrs, # type: ignore
"text": child.attrs["text"],
"styles": {(0, len(child.attrs["text"])): [*(upper_styles or []), style]},
},
Expand All @@ -683,12 +716,14 @@ def get_segment_class(cls) -> Type[MessageSegment]:

@override
def __add__(self, other: Union[str, MessageSegment, Iterable[MessageSegment]]) -> "Message":
result = super().__add__(MessageSegment.text(other) if isinstance(other, str) else other)
result = self.copy()
result += MessageSegment.text(other) if isinstance(other, str) else other
return result.__merge_text__()

@override
def __radd__(self, other: Union[str, MessageSegment, Iterable[MessageSegment]]) -> "Message":
result = super().__radd__(MessageSegment.text(other) if isinstance(other, str) else other)
result = self.__class__(MessageSegment.text(other) if isinstance(other, str) else other)
result = result + self
return result.__merge_text__()

@staticmethod
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "nonebot-adapter-satori"
version = "0.10.2"
version = "0.10.3"
description = "Satori Protocol Adapter for Nonebot2"
authors = [
{name = "RF-Tar-Railt",email = "rf_tar_railt@qq.com"},
Expand Down Expand Up @@ -42,6 +42,8 @@ includes = ["nonebot"]

[tool.pdm.scripts]
test = "pytest -W ignore -s"
format = { composite = ["isort ./","black ./","ruff check ./"] }


[tool.black]
line-length = 110
Expand All @@ -64,7 +66,7 @@ target-version = "py38"

[tool.ruff.lint]
select = ["E", "W", "F", "UP", "C", "T", "Q"]
ignore = ["E402", "F403", "F405", "C901", "UP037"]
ignore = ["E402", "F403", "F405", "C901", "UP037", "W291"]

[tool.pyright]
pythonPlatform = "All"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
from nonebot.adapters.satori.message import Message, MessageSegment


def test_message():
code = """\
<quote id="123456789" chronocat:seq="1">
<author id="123456789"/>
Hello, World!
</quote>
"""
assert Message.from_satori_element(parse(code))[0].data["chronocat:seq"] == "1"
assert Message.from_satori_element(parse("<b test:aaa>1234</b>"))[0].data["test:aaa"] is True
assert (
Message.from_satori_element(parse("<img src='url' test:bbb='foo' width='123'/>"))[0].data["test:bbb"]
== "foo"
)


@pytest.mark.asyncio
async def test_message_rich_expr():
raw = """\
Expand Down

0 comments on commit 75d53e4

Please sign in to comment.