pprint json

This commit is contained in:
histausse 2021-10-06 00:16:45 +02:00
parent d9ef6690fc
commit 06afbc187d
Signed by: histausse
GPG key ID: 67486F107F62E9E9
2 changed files with 21 additions and 7 deletions

View file

@ -9,9 +9,10 @@ from typing import (
)
from matrix_bot.client import Client
from matrix_bot.invite_policy import WhiteList
from .format import Message
async def __send_messsages(
message_queue: asyncio.Queue[dict[str, Any]], # For now, type will change in the futur
message_queue: asyncio.Queue[Message],
bot: Client,
rooms: list[str]
)->NoReturn:
@ -20,9 +21,12 @@ async def __send_messsages(
"""
while True:
message = await message_queue.get()
message = str(message)
for room in rooms:
await bot.send_message(room, message)
await bot.send_formated_message(
room,
message.formated_body,
unformated_message=message.body
)
message_queue.task_done()
async def send_messages(

View file

@ -3,22 +3,32 @@ Format the alert message.
"""
import asyncio
import dataclasses
import json
from typing import (
Any,
NoReturn
NoReturn,
Optional
)
@dataclasses.dataclass
class Message:
body: str
formated_body: Optional[str]
def format_alert(
alert: dict[str, Any]
)->str:
)->Message:
"""
Format an alert in json format to a nice string.
"""
return str(alert)
body = json.dumps(alert, indent=4)
formated_body = f"<pre><code>{body}</code></pre>\n"
return Message(body, formated_body)
async def format_alerts(
alert_queue: asyncio.Queue[dict[str,Any]],
message_queue: asyncio.Queue[str]
message_queue: asyncio.Queue[Message]
)->NoReturn:
"""
Read alerts from alert_queue, format them, and put them in message_queue.