Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions elementary/messages/block_builders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import List, Optional, Tuple, Union
from typing import List, Optional, Sequence, Tuple, Union

from .blocks import (
CodeBlock,
Expand All @@ -16,7 +16,7 @@
)

SimpleInlineBlock = Union[str, Icon]
SimpleLineBlock = Union[str, Icon, List[SimpleInlineBlock]]
SimpleLineBlock = Union[str, Icon, Sequence[SimpleInlineBlock]]


def _build_inline_block(
Expand All @@ -28,9 +28,9 @@ def _build_inline_block(
def _build_inlines(
content: SimpleLineBlock, style: Optional[TextStyle] = None
) -> List[InlineBlock]:
if isinstance(content, list):
return [_build_inline_block(line, style) for line in content]
return [_build_inline_block(content)]
if isinstance(content, (str, Icon)):
return [_build_inline_block(content, style)]
return [_build_inline_block(line, style) for line in content]


def BulletListBlock(
Expand Down Expand Up @@ -73,7 +73,7 @@ def LinkLineBlock(*, text: str, url: str) -> LineBlock:

def SummaryLineBlock(
*,
summary: List[Tuple[SimpleLineBlock, SimpleLineBlock]],
summary: Sequence[Tuple[SimpleLineBlock, SimpleLineBlock]],
include_empty_values: bool = False,
) -> LineBlock:
text_blocks: List[InlineBlock] = []
Expand All @@ -89,7 +89,7 @@ def SummaryLineBlock(

def FactsBlock(
*,
facts: List[
facts: Sequence[
Tuple[
SimpleLineBlock,
SimpleLineBlock,
Expand Down
18 changes: 15 additions & 3 deletions elementary/messages/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Optional, Union

from pydantic import BaseModel
from typing_extensions import Literal


class Icon(Enum):
Expand All @@ -25,6 +26,7 @@ class TextStyle(Enum):


class BaseBlock(BaseModel):
type: str
pass


Expand All @@ -33,35 +35,41 @@ class BaseInlineTextBlock(BaseBlock):


class TextBlock(BaseInlineTextBlock):
type: Literal["text"] = "text"
text: str
style: Optional[TextStyle] = None


class LinkBlock(BaseInlineTextBlock):
type: Literal["link"] = "link"
text: str
url: str


class IconBlock(BaseInlineTextBlock):
type: Literal["icon"] = "icon"
icon: Icon


InlineBlock = Union[TextBlock, LinkBlock, IconBlock]


class HeaderBlock(BaseBlock):
type: Literal["header"] = "header"
text: str


class CodeBlock(BaseBlock):
type: Literal["code"] = "code"
text: str


class DividerBlock(BaseBlock):
pass
type: Literal["divider"] = "divider"


class LineBlock(BaseBlock):
type: Literal["line"] = "line"
inlines: List[InlineBlock]
sep: str = " "

Expand All @@ -71,19 +79,22 @@ class BaseLinesBlock(BaseBlock):


class LinesBlock(BaseLinesBlock):
pass
type: Literal["lines"] = "lines"


class FactBlock(BaseBlock):
type: Literal["fact"] = "fact"
title: LineBlock
value: LineBlock


class FactListBlock(BaseBlock):
type: Literal["fact_list"] = "fact_list"
facts: List[FactBlock]


class ExpandableBlock(BaseBlock):
type: Literal["expandable"] = "expandable"
title: str
body: List["InExpandableBlock"]
expanded: bool = False
Expand All @@ -98,4 +109,5 @@ class ExpandableBlock(BaseBlock):
"ExpandableBlock",
]

ExpandableBlock.model_rebuild()
# Update forward references for recursive types
ExpandableBlock.update_forward_refs()
2 changes: 1 addition & 1 deletion elementary/messages/message_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class MessageBody(BaseModel):
color: Optional[Color] = None


MessageBody.model_rebuild()
MessageBody.update_forward_refs()
Loading