From 99a9594e9f922e406367fa3bb521b15891a7a8f9 Mon Sep 17 00:00:00 2001 From: Andrew Mirsky Date: Sun, 15 Jun 2025 10:21:41 -0400 Subject: [PATCH] creating MQTTEvents enumeration instead of individual string names. consolidating into a single file to eliminate import errors --- amqtt/__init__.py | 23 ---------------------- amqtt/broker.py | 2 +- amqtt/events.py | 28 +++++++++++++++++++++++++++ amqtt/mqtt/protocol/broker_handler.py | 7 +++---- amqtt/mqtt/protocol/client_handler.py | 5 +++-- amqtt/mqtt/protocol/handler.py | 8 +++----- amqtt/plugins/manager.py | 2 +- tests/test_broker.py | 2 +- tests/test_paho.py | 2 +- 9 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 amqtt/events.py diff --git a/amqtt/__init__.py b/amqtt/__init__.py index 0ae983c..ef244ad 100644 --- a/amqtt/__init__.py +++ b/amqtt/__init__.py @@ -1,26 +1,3 @@ """INIT.""" __version__ = "0.11.0" - -from enum import StrEnum - - -class Events(StrEnum): - """Class for all events.""" - -class ClientEvents(Events): - """Events issued by the client.""" - - -class BrokerEvents(Events): - """Events issued by the broker.""" - - PRE_START = "broker_pre_start" - POST_START = "broker_post_start" - PRE_SHUTDOWN = "broker_pre_shutdown" - POST_SHUTDOWN = "broker_post_shutdown" - CLIENT_CONNECTED = "broker_client_connected" - CLIENT_DISCONNECTED = "broker_client_disconnected" - CLIENT_SUBSCRIBED = "broker_client_subscribed" - CLIENT_UNSUBSCRIBED = "broker_client_unsubscribed" - MESSAGE_RECEIVED = "broker_message_received" diff --git a/amqtt/broker.py b/amqtt/broker.py index b06c310..5045197 100644 --- a/amqtt/broker.py +++ b/amqtt/broker.py @@ -28,7 +28,7 @@ from amqtt.mqtt.protocol.broker_handler import BrokerProtocolHandler from amqtt.session import ApplicationMessage, OutgoingApplicationMessage, Session from amqtt.utils import format_client_message, gen_client_id, read_yaml_config -from . import BrokerEvents +from .events import BrokerEvents from .mqtt.disconnect import DisconnectPacket from .plugins.manager import BaseContext, PluginManager diff --git a/amqtt/events.py b/amqtt/events.py new file mode 100644 index 0000000..bd228c2 --- /dev/null +++ b/amqtt/events.py @@ -0,0 +1,28 @@ +from enum import StrEnum + + +class Events(StrEnum): + """Class for all events.""" + + +class ClientEvents(Events): + """Events issued by the client.""" + + +class MQTTEvents(Events): + PACKET_SENT = "mqtt_packet_sent" + PACKET_RECEIVED = "mqtt_packet_received" + + +class BrokerEvents(Events): + """Events issued by the broker.""" + + PRE_START = "broker_pre_start" + POST_START = "broker_post_start" + PRE_SHUTDOWN = "broker_pre_shutdown" + POST_SHUTDOWN = "broker_post_shutdown" + CLIENT_CONNECTED = "broker_client_connected" + CLIENT_DISCONNECTED = "broker_client_disconnected" + CLIENT_SUBSCRIBED = "broker_client_subscribed" + CLIENT_UNSUBSCRIBED = "broker_client_unsubscribed" + MESSAGE_RECEIVED = "broker_message_received" diff --git a/amqtt/mqtt/protocol/broker_handler.py b/amqtt/mqtt/protocol/broker_handler.py index c8cae86..8b21d97 100644 --- a/amqtt/mqtt/protocol/broker_handler.py +++ b/amqtt/mqtt/protocol/broker_handler.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING from amqtt.adapters import ReaderAdapter, WriterAdapter from amqtt.errors import MQTTError +from amqtt.events import MQTTEvents from amqtt.mqtt.connack import ( BAD_USERNAME_PASSWORD, CONNECTION_ACCEPTED, @@ -25,8 +26,6 @@ from amqtt.plugins.manager import PluginManager from amqtt.session import Session from amqtt.utils import format_client_message -from .handler import EVENT_MQTT_PACKET_RECEIVED, EVENT_MQTT_PACKET_SENT - _MQTT_PROTOCOL_LEVEL_SUPPORTED = 4 if TYPE_CHECKING: @@ -164,7 +163,7 @@ class BrokerProtocolHandler(ProtocolHandler["BrokerContext"]): ) -> tuple["BrokerProtocolHandler", Session]: """Initialize from a CONNECT packet and validates the connection.""" connect = await ConnectPacket.from_stream(reader) - await plugins_manager.fire_event(EVENT_MQTT_PACKET_RECEIVED, packet=connect) + await plugins_manager.fire_event(MQTTEvents.PACKET_RECEIVED, packet=connect) if connect.variable_header is None: msg = "CONNECT packet: variable header not initialized." @@ -219,7 +218,7 @@ class BrokerProtocolHandler(ProtocolHandler["BrokerContext"]): connack = ConnackPacket.build(0, IDENTIFIER_REJECTED) if connack is not None: - await plugins_manager.fire_event(EVENT_MQTT_PACKET_SENT, packet=connack) + await plugins_manager.fire_event(MQTTEvents.PACKET_SENT, packet=connack) await connack.to_stream(writer) await writer.close() raise MQTTError(error_msg) from None diff --git a/amqtt/mqtt/protocol/client_handler.py b/amqtt/mqtt/protocol/client_handler.py index 6815ab7..f2a127d 100644 --- a/amqtt/mqtt/protocol/client_handler.py +++ b/amqtt/mqtt/protocol/client_handler.py @@ -2,12 +2,13 @@ import asyncio from typing import TYPE_CHECKING, Any from amqtt.errors import AMQTTError, NoDataError +from amqtt.events import MQTTEvents from amqtt.mqtt.connack import ConnackPacket from amqtt.mqtt.connect import ConnectPacket, ConnectPayload, ConnectVariableHeader from amqtt.mqtt.disconnect import DisconnectPacket from amqtt.mqtt.pingreq import PingReqPacket from amqtt.mqtt.pingresp import PingRespPacket -from amqtt.mqtt.protocol.handler import EVENT_MQTT_PACKET_RECEIVED, ProtocolHandler +from amqtt.mqtt.protocol.handler import ProtocolHandler from amqtt.mqtt.suback import SubackPacket from amqtt.mqtt.subscribe import SubscribePacket from amqtt.mqtt.unsuback import UnsubackPacket @@ -93,7 +94,7 @@ class ClientProtocolHandler(ProtocolHandler["ClientContext"]): connack = await ConnackPacket.from_stream(self.reader) except NoDataError as e: raise ConnectionError from e - await self.plugins_manager.fire_event(EVENT_MQTT_PACKET_RECEIVED, packet=connack, session=self.session) + await self.plugins_manager.fire_event(MQTTEvents.PACKET_RECEIVED, packet=connack, session=self.session) return connack.return_code def handle_write_timeout(self) -> None: diff --git a/amqtt/mqtt/protocol/handler.py b/amqtt/mqtt/protocol/handler.py index 6197868..f9a7238 100644 --- a/amqtt/mqtt/protocol/handler.py +++ b/amqtt/mqtt/protocol/handler.py @@ -21,6 +21,7 @@ from typing import Generic, TypeVar, cast from amqtt.adapters import ReaderAdapter, WriterAdapter from amqtt.errors import AMQTTError, MQTTError, NoDataError, ProtocolHandlerError +from amqtt.events import MQTTEvents from amqtt.mqtt import packet_class from amqtt.mqtt.connack import ConnackPacket from amqtt.mqtt.connect import ConnectPacket @@ -59,9 +60,6 @@ from amqtt.mqtt.unsubscribe import UnsubscribePacket from amqtt.plugins.manager import BaseContext, PluginManager from amqtt.session import INCOMING, OUTGOING, ApplicationMessage, IncomingApplicationMessage, OutgoingApplicationMessage, Session -EVENT_MQTT_PACKET_SENT = "mqtt_packet_sent" -EVENT_MQTT_PACKET_RECEIVED = "mqtt_packet_received" - C = TypeVar("C", bound=BaseContext) class ProtocolHandler(Generic[C]): @@ -473,7 +471,7 @@ class ProtocolHandler(Generic[C]): cls = packet_class(fixed_header) packet = await cls.from_stream(self.reader, fixed_header=fixed_header) - await self.plugins_manager.fire_event(EVENT_MQTT_PACKET_RECEIVED, packet=packet, session=self.session) + await self.plugins_manager.fire_event(MQTTEvents.PACKET_RECEIVED, packet=packet, session=self.session) if packet.fixed_header is None or packet.fixed_header.packet_type not in ( CONNACK, SUBSCRIBE, @@ -570,7 +568,7 @@ class ProtocolHandler(Generic[C]): self._keepalive_task.cancel() if self.keepalive_timeout is not None: self._keepalive_task = self._loop.call_later(self.keepalive_timeout, self.handle_write_timeout) - await self.plugins_manager.fire_event(EVENT_MQTT_PACKET_SENT, packet=packet, session=self.session) + await self.plugins_manager.fire_event(MQTTEvents.PACKET_SENT, packet=packet, session=self.session) except (ConnectionResetError, BrokenPipeError): await self.handle_connection_closed() except asyncio.CancelledError as e: diff --git a/amqtt/plugins/manager.py b/amqtt/plugins/manager.py index 9bb4d36..0ff244f 100644 --- a/amqtt/plugins/manager.py +++ b/amqtt/plugins/manager.py @@ -8,8 +8,8 @@ from importlib.metadata import EntryPoint, EntryPoints, entry_points import logging from typing import TYPE_CHECKING, Any, Generic, NamedTuple, Optional, TypeVar -from amqtt import Events from amqtt.errors import PluginImportError, PluginInitError +from amqtt.events import Events from amqtt.session import Session _LOGGER = logging.getLogger(__name__) diff --git a/tests/test_broker.py b/tests/test_broker.py index 140e005..5e0a3a3 100644 --- a/tests/test_broker.py +++ b/tests/test_broker.py @@ -6,7 +6,7 @@ from unittest.mock import MagicMock, call, patch import psutil import pytest -from amqtt import BrokerEvents +from amqtt.events import BrokerEvents from amqtt.adapters import StreamReaderAdapter, StreamWriterAdapter from amqtt.broker import Broker from amqtt.client import MQTTClient diff --git a/tests/test_paho.py b/tests/test_paho.py index 09d78db..ea8ff87 100644 --- a/tests/test_paho.py +++ b/tests/test_paho.py @@ -6,7 +6,7 @@ from unittest.mock import MagicMock, call, patch import pytest from paho.mqtt import client as mqtt_client -from amqtt.broker import BrokerEvents +from amqtt.events import BrokerEvents from amqtt.client import MQTTClient from amqtt.mqtt.constants import QOS_1, QOS_2