From 2f8fd0a4a82ad45067dd77beb8dc9f86711db5dc Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Mon, 23 Dec 2024 19:28:19 -0600 Subject: [PATCH 1/5] Replace textsize() with textbbox() --- plugins/map_plugin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/map_plugin.py b/plugins/map_plugin.py index a542e86..472a9f1 100644 --- a/plugins/map_plugin.py +++ b/plugins/map_plugin.py @@ -35,7 +35,9 @@ def render_pillow(self, renderer: staticmaps.PillowRenderer) -> None: x, y = renderer.transformer().ll2pixel(self.latlng()) x = x + renderer.offset_x() - tw, th = renderer.draw().textsize(self._text) + # Updated to use textbbox instead of textsize + bbox = renderer.draw().textbbox((0, 0), self._text) + tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1] w = max(self._arrow, tw + 2 * self._margin) h = th + 2 * self._margin From 54036b1d1fb2aefdaea09795c248443b6ca932d2 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Tue, 24 Dec 2024 18:30:13 -0600 Subject: [PATCH 2/5] Trunk code quality fixes --- .trunk/trunk.yaml | 15 +++++++++++---- matrix_utils.py | 14 +++++++++++--- plugin_loader.py | 1 + plugins/map_plugin.py | 18 ++++++++++-------- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index ce09662..190d574 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -21,15 +21,22 @@ lint: - actionlint@1.7.4 - bandit@1.8.0 - black@24.10.0 - - checkov@3.2.334 + - checkov@3.2.344 - git-diff-check - isort@5.13.2 - markdownlint@0.43.0 - - osv-scanner@1.9.1 + - osv-scanner@1.9.2 - prettier@3.4.2 - - ruff@0.8.3 - - trufflehog@3.86.1 + - ruff@0.8.4 + - trufflehog@3.88.0 - yamllint@1.35.1 + # Remove map_plugin.py after staticmaps is updated and we're able to work on it again + # For more info: https://github.com/geoffwhittington/meshtastic-matrix-relay/issues/117 + ignore: + - linters: [ALL] + paths: + - sample_config.yaml + - plugins/map_plugin.py actions: disabled: - trunk-announce diff --git a/matrix_utils.py b/matrix_utils.py index e868845..325e8f2 100644 --- a/matrix_utils.py +++ b/matrix_utils.py @@ -46,6 +46,7 @@ matrix_client = None + def bot_command(command, event): """ Checks if the given command is directed at the bot, @@ -62,11 +63,17 @@ def bot_command(command, event): if full_message.startswith(bot_user_id) or text_content.startswith(bot_user_id): # Construct a regex pattern to match variations of bot mention and command pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$" - return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content)) - elif full_message.startswith(bot_user_name) or text_content.startswith(bot_user_name): + return bool(re.match(pattern, full_message)) or bool( + re.match(pattern, text_content) + ) + elif full_message.startswith(bot_user_name) or text_content.startswith( + bot_user_name + ): # Construct a regex pattern to match variations of bot mention and command pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$" - return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content)) + return bool(re.match(pattern, full_message)) or bool( + re.match(pattern, text_content) + ) else: return False # # Construct a regex pattern to match variations of bot mention and command @@ -75,6 +82,7 @@ def bot_command(command, event): # # Check if the message matches the pattern # return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content)) + async def connect_matrix(): """ Establish a connection to the Matrix homeserver. diff --git a/plugin_loader.py b/plugin_loader.py index eded848..f90e6c8 100644 --- a/plugin_loader.py +++ b/plugin_loader.py @@ -1,3 +1,4 @@ +# trunk-ignore-all(bandit) import hashlib import importlib.util import os diff --git a/plugins/map_plugin.py b/plugins/map_plugin.py index 472a9f1..adcbb0d 100644 --- a/plugins/map_plugin.py +++ b/plugins/map_plugin.py @@ -1,11 +1,13 @@ -import staticmaps -import s2sphere +import io import math import random -import io import re -from PIL import Image + +import s2sphere +import staticmaps from nio import AsyncClient, UploadResponse +from PIL import Image + from plugins.base_plugin import BasePlugin @@ -204,7 +206,7 @@ async def upload_image(client: AsyncClient, image: Image.Image) -> UploadRespons async def send_room_image( client: AsyncClient, room_id: str, upload_response: UploadResponse ): - response = await client.room_send( + await client.room_send( room_id=room_id, message_type="m.room.message", content={"msgtype": "m.image", "url": upload_response.content_uri, "body": ""}, @@ -222,7 +224,7 @@ class Plugin(BasePlugin): @property def description(self): return ( - f"Map of mesh radio nodes. Supports `zoom` and `size` options to customize" + "Map of mesh radio nodes. Supports `zoom` and `size` options to customize" ) async def handle_meshtastic_message( @@ -237,7 +239,7 @@ def get_mesh_commands(self): return [] async def handle_room_message(self, room, event, full_message): - # Pass the whole event to matches() for compatibility w/ updated base_plugin.py + # Pass the whole event to matches() for compatibility w/ updated base_plugin.py if not self.matches(event): return False @@ -277,7 +279,7 @@ async def handle_room_message(self, room, event, full_message): image_size = (1000, 1000) locations = [] - for node, info in meshtastic_client.nodes.items(): + for _node, info in meshtastic_client.nodes.items(): if "position" in info and "latitude" in info["position"]: locations.append( { From eea8ad3257464a6f0decc3e4266f7d9059d3102e Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Tue, 24 Dec 2024 19:19:08 -0600 Subject: [PATCH 3/5] Apply monkeypatch to map_plugin.py --- plugins/map_plugin.py | 9 +++++++++ requirements.txt | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/map_plugin.py b/plugins/map_plugin.py index adcbb0d..96d39bb 100644 --- a/plugins/map_plugin.py +++ b/plugins/map_plugin.py @@ -1,3 +1,12 @@ +import PIL.ImageDraw + +def textsize(self: PIL.ImageDraw.ImageDraw, *args, **kwargs): + x, y, w, h = self.textbbox((0, 0), *args, **kwargs) + return w, h + +# Monkeypatch fix for https://github.com/flopp/py-staticmaps/issues/39 +PIL.ImageDraw.ImageDraw.textsize = textsize + import io import math import random diff --git a/requirements.txt b/requirements.txt index bd6fddb..c085adc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,9 @@ meshtastic Pillow==11 -py-staticmaps==0.4.0 +git+https://github.com/flopp/py-staticmaps.git@e0266dc40163e87ce42a0ea5d8836a9a4bd92208 matrix-nio==0.25.2 matplotlib==3.9.0 requests==2.32.3 markdown==3.4.3 haversine==2.8.0 -schedule==1.2.0 \ No newline at end of file +schedule==1.2.0 From c554c4de1e6150e359670898a0a4929d70c93294 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Tue, 24 Dec 2024 19:19:39 -0600 Subject: [PATCH 4/5] Add image.png to body --- plugins/map_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/map_plugin.py b/plugins/map_plugin.py index 96d39bb..e43c089 100644 --- a/plugins/map_plugin.py +++ b/plugins/map_plugin.py @@ -218,7 +218,7 @@ async def send_room_image( await client.room_send( room_id=room_id, message_type="m.room.message", - content={"msgtype": "m.image", "url": upload_response.content_uri, "body": ""}, + content={"msgtype": "m.image", "url": upload_response.content_uri, "body": "image.png"}, ) From 4fd00a2fa023f0f58554d67aeffc823d9242a608 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Tue, 24 Dec 2024 19:24:37 -0600 Subject: [PATCH 5/5] Move monkeypatch down --- plugins/map_plugin.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/plugins/map_plugin.py b/plugins/map_plugin.py index e43c089..b08c602 100644 --- a/plugins/map_plugin.py +++ b/plugins/map_plugin.py @@ -1,17 +1,9 @@ -import PIL.ImageDraw - -def textsize(self: PIL.ImageDraw.ImageDraw, *args, **kwargs): - x, y, w, h = self.textbbox((0, 0), *args, **kwargs) - return w, h - -# Monkeypatch fix for https://github.com/flopp/py-staticmaps/issues/39 -PIL.ImageDraw.ImageDraw.textsize = textsize - import io import math import random import re +import PIL.ImageDraw import s2sphere import staticmaps from nio import AsyncClient, UploadResponse @@ -20,6 +12,15 @@ def textsize(self: PIL.ImageDraw.ImageDraw, *args, **kwargs): from plugins.base_plugin import BasePlugin +def textsize(self: PIL.ImageDraw.ImageDraw, *args, **kwargs): + x, y, w, h = self.textbbox((0, 0), *args, **kwargs) + return w, h + + +# Monkeypatch fix for https://github.com/flopp/py-staticmaps/issues/39 +PIL.ImageDraw.ImageDraw.textsize = textsize + + class TextLabel(staticmaps.Object): def __init__(self, latlng: s2sphere.LatLng, text: str, fontSize: int = 12) -> None: staticmaps.Object.__init__(self) @@ -218,7 +219,11 @@ async def send_room_image( await client.room_send( room_id=room_id, message_type="m.room.message", - content={"msgtype": "m.image", "url": upload_response.content_uri, "body": "image.png"}, + content={ + "msgtype": "m.image", + "url": upload_response.content_uri, + "body": "image.png", + }, )