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
40 changes: 39 additions & 1 deletion bec_lib/bec_lib/messaging_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def __init__(self, service: MessagingService, scope: str | list[str] | None = No
self._scope = scope
self._content = []

def add_text(self, text: str) -> Self:
def add_text(self, text: str, **kwargs) -> Self:
"""
Add text to the message object.

Args:
text (str): The text to add.
**kwargs: Additional keyword arguments for specific messaging services.

Returns:
MessageObject: The updated message object.
Expand Down Expand Up @@ -268,6 +269,43 @@ class SciLogMessageServiceObject(MessageServiceObject):
A class representing a message object for the SciLog messaging service.
"""

def add_text(
self,
text: str,
bold: bool = False,
italic: bool = False,
color: str | None = None,
**kwargs,
) -> Self:
"""
Add text to the SciLog message with optional inline HTML formatting.

When any formatting option is supplied the text is wrapped in a ``<p>``
paragraph so SciLog renders it correctly.

Args:
text: The text content.
bold: Wrap the text in ``<strong>``.
italic: Wrap the text in ``<em>``.
color: Highlight colour using SciLog's pen marks (e.g. ``"red"``,
``"yellow"``, ``"green"``, ``"blue"``, ``"pink"``).

Returns:
SciLogMessageServiceObject: The updated message object.

Examples:
>>> msg.add_text("Checks failed", bold=True, color="red")
"""
if bold or italic or color:
if bold:
text = f"<strong>{text}</strong>"
if italic:
text = f"<em>{text}</em>"
if color:
text = f'<mark class="pen-{color}">{text}</mark>'
text = f"<p>{text}</p>"
return super().add_text(text)

def add_tags(self, tags: str | list[str]) -> Self:
"""
Add tags to the SciLog message object.
Expand Down
34 changes: 34 additions & 0 deletions bec_lib/tests/test_messaging_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,37 @@ def test_scilog_message_add_duplicate_tags(scilog_message, connected_connector):
assert isinstance(tags_part, messages.MessagingServiceTagsContent)
# The final tags should include all unique tags without duplicates
assert sorted(tags_part.tags) == sorted(["bec", "default_tag", "additional_tag"])


def test_scilog_add_text_no_formatting(scilog_message):
scilog_message.add_text("plain")
assert scilog_message._content[0].content == "plain"


def test_scilog_add_text_bold(scilog_message):
scilog_message.add_text("hello", bold=True)
assert scilog_message._content[0].content == "<p><strong>hello</strong></p>"


def test_scilog_add_text_italic(scilog_message):
scilog_message.add_text("hello", italic=True)
assert scilog_message._content[0].content == "<p><em>hello</em></p>"


def test_scilog_add_text_color(scilog_message):
scilog_message.add_text("warn", color="yellow")
assert scilog_message._content[0].content == '<p><mark class="pen-yellow">warn</mark></p>'


def test_scilog_add_text_bold_and_color(scilog_message, connected_connector):
"""bold + color produces the expected nested HTML and round-trips through redis."""
scilog_message.add_text("Beamline checks failed", bold=True, color="red")
scilog_message.send()

out = connected_connector.xread(MessageEndpoints.message_service_queue(), from_start=True)
text_part = out[0]["data"].message[0]
assert isinstance(text_part, messages.MessagingServiceTextContent)
assert (
text_part.content
== '<p><mark class="pen-red"><strong>Beamline checks failed</strong></mark></p>'
)
Loading