stompman 2.0.1__tar.gz → 2.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,3 +3,5 @@
3
3
  .venv
4
4
  dist
5
5
  uv.lock
6
+ .vscode
7
+ __pycache__
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: stompman
3
- Version: 2.0.1
3
+ Version: 2.2.0
4
4
  Summary: Python STOMP client with pleasant API
5
5
  Project-URL: repository, https://github.com/community-of-python/stompman
6
6
  Author-email: Lev Vereshchagin <mail@vrslev.com>
@@ -160,6 +160,7 @@ Also, I want to pointed out that:
160
160
 
161
161
  - Protocol parsing is inspired by [aiostomp](https://github.com/pedrokiefer/aiostomp/blob/3449dcb53f43e5956ccc7662bb5b7d76bc6ef36b/aiostomp/protocol.py) (meaning: consumed by me and refactored from).
162
162
  - stompman is tested and used with [ActiveMQ Artemis](https://activemq.apache.org/components/artemis/) and [ActiveMQ Classic](https://activemq.apache.org/components/classic/).
163
+ - Caveat: a message sent by a Stomp client is converted into a JMS `TextMessage`/`BytesMessage` based on the `content-length` header (see the docs [here](https://activemq.apache.org/components/classic/documentation/stomp)). In order to send a `TextMessage`, `Client.send` needs to be invoked with `add_content_length` header set to `False`
163
164
  - Specification says that headers in CONNECT and CONNECTED frames shouldn't be escaped for backwards compatibility. stompman escapes headers in CONNECT frame (outcoming), but does not unescape headers in CONNECTED (outcoming).
164
165
 
165
166
  ### FastStream STOMP broker
@@ -47,7 +47,7 @@ class Client:
47
47
  connection_class: type[AbstractConnection] = Connection
48
48
 
49
49
  _connection_manager: ConnectionManager = field(init=False)
50
- _active_subscriptions: ActiveSubscriptions = field(default_factory=dict, init=False)
50
+ _active_subscriptions: ActiveSubscriptions = field(default_factory=ActiveSubscriptions, init=False)
51
51
  _active_transactions: set[Transaction] = field(default_factory=set, init=False)
52
52
  _exit_stack: AsyncExitStack = field(default_factory=AsyncExitStack, init=False)
53
53
  _listen_task: asyncio.Task[None] = field(init=False)
@@ -86,8 +86,8 @@ class Client:
86
86
  self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None
87
87
  ) -> None:
88
88
  try:
89
- if self._active_subscriptions and not exc_value:
90
- await asyncio.Future()
89
+ if not exc_value:
90
+ await self._active_subscriptions.wait_until_empty()
91
91
  finally:
92
92
  self._listen_task.cancel()
93
93
  await asyncio.wait([self._listen_task])
@@ -98,7 +98,7 @@ class Client:
98
98
  async for frame in self._connection_manager.read_frames_reconnecting():
99
99
  match frame:
100
100
  case MessageFrame():
101
- if subscription := self._active_subscriptions.get(frame.headers["subscription"]):
101
+ if subscription := self._active_subscriptions.get_by_id(frame.headers["subscription"]):
102
102
  task_group.create_task(
103
103
  subscription._run_handler(frame=frame) # noqa: SLF001
104
104
  if isinstance(subscription, AutoAckSubscription)
@@ -115,11 +115,22 @@ class Client:
115
115
  pass
116
116
 
117
117
  async def send(
118
- self, body: bytes, destination: str, *, content_type: str | None = None, headers: dict[str, str] | None = None
118
+ self,
119
+ body: bytes,
120
+ destination: str,
121
+ *,
122
+ content_type: str | None = None,
123
+ add_content_length: bool = True,
124
+ headers: dict[str, str] | None = None,
119
125
  ) -> None:
120
126
  await self._connection_manager.write_frame_reconnecting(
121
127
  SendFrame.build(
122
- body=body, destination=destination, transaction=None, content_type=content_type, headers=headers
128
+ body=body,
129
+ destination=destination,
130
+ transaction=None,
131
+ content_type=content_type,
132
+ add_content_length=add_content_length,
133
+ headers=headers,
123
134
  )
124
135
  )
125
136
 
@@ -149,11 +149,13 @@ class SendFrame:
149
149
  destination: str,
150
150
  transaction: str | None,
151
151
  content_type: str | None,
152
+ add_content_length: bool,
152
153
  headers: dict[str, str] | None,
153
154
  ) -> Self:
154
155
  all_headers: SendHeaders = headers or {} # type: ignore[assignment]
155
156
  all_headers["destination"] = destination
156
- all_headers["content-length"] = str(len(body))
157
+ if add_content_length:
158
+ all_headers["content-length"] = str(len(body))
157
159
  if content_type is not None:
158
160
  all_headers["content-type"] = content_type
159
161
  if transaction is not None:
@@ -71,7 +71,6 @@ COMMANDS_TO_FRAMES: Final[dict[bytes, type[AnyClientFrame | AnyServerFrame]]] =
71
71
  }
72
72
  FRAMES_TO_COMMANDS: Final = {value: key for key, value in COMMANDS_TO_FRAMES.items()}
73
73
  FRAMES_WITH_BODY: Final = (SendFrame, MessageFrame, ErrorFrame)
74
- COMMANDS_BYTES_LISTS: Final = [list(iter_bytes(command)) for command in COMMANDS_TO_FRAMES]
75
74
 
76
75
 
77
76
  def dump_header(key: str, value: str) -> bytes:
@@ -1,3 +1,4 @@
1
+ import asyncio
1
2
  from collections.abc import Awaitable, Callable, Coroutine
2
3
  from dataclasses import dataclass, field
3
4
  from typing import Any
@@ -14,7 +15,35 @@ from stompman.frames import (
14
15
  UnsubscribeFrame,
15
16
  )
16
17
 
17
- ActiveSubscriptions = dict[str, "AutoAckSubscription | ManualAckSubscription"]
18
+
19
+ @dataclass(kw_only=True, slots=True, frozen=True)
20
+ class ActiveSubscriptions:
21
+ subscriptions: dict[str, "AutoAckSubscription | ManualAckSubscription"] = field(default_factory=dict, init=False)
22
+ event: asyncio.Event = field(default_factory=asyncio.Event, init=False)
23
+
24
+ def __post_init__(self) -> None:
25
+ self.event.set()
26
+
27
+ def get_by_id(self, subscription_id: str) -> "AutoAckSubscription | ManualAckSubscription | None":
28
+ return self.subscriptions.get(subscription_id)
29
+
30
+ def get_all(self) -> list["AutoAckSubscription | ManualAckSubscription"]:
31
+ return list(self.subscriptions.values())
32
+
33
+ def delete_by_id(self, subscription_id: str) -> None:
34
+ del self.subscriptions[subscription_id]
35
+ if not self.subscriptions:
36
+ self.event.set()
37
+
38
+ def add(self, subscription: "AutoAckSubscription | ManualAckSubscription") -> None:
39
+ self.subscriptions[subscription.id] = subscription
40
+ self.event.clear()
41
+
42
+ def contains_by_id(self, subscription_id: str) -> bool:
43
+ return subscription_id in self.subscriptions
44
+
45
+ async def wait_until_empty(self) -> bool:
46
+ return await self.event.wait()
18
47
 
19
48
 
20
49
  @dataclass(kw_only=True, slots=True)
@@ -32,20 +61,20 @@ class BaseSubscription:
32
61
  subscription_id=self.id, destination=self.destination, ack=self.ack, headers=self.headers
33
62
  )
34
63
  )
35
- self._active_subscriptions[self.id] = self # type: ignore[assignment]
64
+ self._active_subscriptions.add(self) # type: ignore[arg-type]
36
65
 
37
66
  async def unsubscribe(self) -> None:
38
- del self._active_subscriptions[self.id]
67
+ self._active_subscriptions.delete_by_id(self.id)
39
68
  await self._connection_manager.maybe_write_frame(UnsubscribeFrame(headers={"id": self.id}))
40
69
 
41
70
  async def _nack(self, frame: MessageFrame) -> None:
42
- if self.id in self._active_subscriptions and (ack_id := frame.headers.get("ack")):
71
+ if self._active_subscriptions.contains_by_id(self.id) and (ack_id := frame.headers.get("ack")):
43
72
  await self._connection_manager.maybe_write_frame(
44
73
  NackFrame(headers={"id": ack_id, "subscription": frame.headers["subscription"]})
45
74
  )
46
75
 
47
76
  async def _ack(self, frame: MessageFrame) -> None:
48
- if self.id in self._active_subscriptions and (ack_id := frame.headers["ack"]):
77
+ if self._active_subscriptions.contains_by_id(self.id) and (ack_id := frame.headers["ack"]):
49
78
  await self._connection_manager.maybe_write_frame(
50
79
  AckFrame(headers={"id": ack_id, "subscription": frame.headers["subscription"]})
51
80
  )
@@ -96,7 +125,7 @@ def _make_subscription_id() -> str:
96
125
  async def resubscribe_to_active_subscriptions(
97
126
  *, connection: AbstractConnection, active_subscriptions: ActiveSubscriptions
98
127
  ) -> None:
99
- for subscription in active_subscriptions.values():
128
+ for subscription in active_subscriptions.get_all():
100
129
  await connection.write_frame(
101
130
  SubscribeFrame.build(
102
131
  subscription_id=subscription.id,
@@ -108,5 +137,5 @@ async def resubscribe_to_active_subscriptions(
108
137
 
109
138
 
110
139
  async def unsubscribe_from_all_active_subscriptions(*, active_subscriptions: ActiveSubscriptions) -> None:
111
- for subscription in active_subscriptions.copy().values():
140
+ for subscription in active_subscriptions.get_all():
112
141
  await subscription.unsubscribe()
@@ -34,10 +34,21 @@ class Transaction:
34
34
  self._active_transactions.remove(self)
35
35
 
36
36
  async def send(
37
- self, body: bytes, destination: str, *, content_type: str | None = None, headers: dict[str, str] | None = None
37
+ self,
38
+ body: bytes,
39
+ destination: str,
40
+ *,
41
+ content_type: str | None = None,
42
+ add_content_length: bool = True,
43
+ headers: dict[str, str] | None = None,
38
44
  ) -> None:
39
45
  frame = SendFrame.build(
40
- body=body, destination=destination, transaction=self.id, content_type=content_type, headers=headers
46
+ body=body,
47
+ destination=destination,
48
+ transaction=self.id,
49
+ content_type=content_type,
50
+ add_content_length=add_content_length,
51
+ headers=headers,
41
52
  )
42
53
  self.sent_frames.append(frame)
43
54
  await self._connection_manager.write_frame_reconnecting(frame)
File without changes
File without changes
File without changes
File without changes
File without changes