Skip to content

Commit ba8f175

Browse files
committed
Python: Moved PubSubMsg class to a shared module
Signed-off-by: barshaul <[email protected]>
1 parent a878b63 commit ba8f175

File tree

6 files changed

+64
-64
lines changed

6 files changed

+64
-64
lines changed

python/python/glide/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
22

3-
from glide.commands.async_commands.core import CoreCommands
43
from glide.commands.batch import (
54
Batch,
65
ClusterBatch,
@@ -38,6 +37,7 @@
3837
InfoSection,
3938
InsertPosition,
4039
OnlyIfEqual,
40+
PubSubMsg,
4141
UpdateOptions,
4242
)
4343
from glide.commands.server_modules import ft, glide_json, json_batch
@@ -170,8 +170,6 @@
170170

171171
from .glide import ClusterScanCursor, Script
172172

173-
PubSubMsg = CoreCommands.PubSubMsg
174-
175173
__all__ = [
176174
# Client
177175
"GlideClient",

python/python/glide/commands/async_commands/core.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2-
from dataclasses import dataclass
32
from typing import Dict, List, Mapping, Optional, Protocol, Set, Tuple, Union, cast
43

54
from glide.commands.bitmap import (
@@ -18,6 +17,7 @@
1817
ExpirySet,
1918
InsertPosition,
2019
OnlyIfEqual,
20+
PubSubMsg,
2121
UpdateOptions,
2222
_build_sort_args,
2323
)
@@ -6647,21 +6647,6 @@ async def watch(self, keys: List[TEncodable]) -> TOK:
66476647
await self._execute_command(RequestType.Watch, keys),
66486648
)
66496649

6650-
@dataclass
6651-
class PubSubMsg:
6652-
"""
6653-
Describes the incoming pubsub message
6654-
6655-
Attributes:
6656-
message (TEncodable): Incoming message.
6657-
channel (TEncodable): Name of an channel that triggered the message.
6658-
pattern (Optional[TEncodable]): Pattern that triggered the message.
6659-
"""
6660-
6661-
message: TEncodable
6662-
channel: TEncodable
6663-
pattern: Optional[TEncodable]
6664-
66656650
async def get_pubsub_message(self) -> PubSubMsg:
66666651
"""
66676652
Returns the next pubsub message.

python/python/glide/commands/core_options.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
from glide.constants import TEncodable
99

1010

11+
@dataclass
12+
class PubSubMsg:
13+
"""
14+
Describes the incoming pubsub message
15+
16+
Attributes:
17+
message (TEncodable): Incoming message.
18+
channel (TEncodable): Name of an channel that triggered the message.
19+
pattern (Optional[TEncodable]): Pattern that triggered the message.
20+
"""
21+
22+
message: TEncodable
23+
channel: TEncodable
24+
pattern: Optional[TEncodable]
25+
26+
1127
class ConditionalChange(Enum):
1228
"""
1329
A condition to the `SET`, `ZADD` and `GEOADD` commands.

python/python/glide/config.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from enum import Enum, IntEnum
77
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
88

9-
from glide.commands.async_commands.core import CoreCommands
9+
from glide.commands.core_options import PubSubMsg
1010
from glide.exceptions import ConfigurationError
1111
from glide.protobuf.connection_request_pb2 import ConnectionRequest
1212
from glide.protobuf.connection_request_pb2 import ProtocolVersion as SentProtocolVersion
@@ -308,7 +308,7 @@ def _is_pubsub_configured(self) -> bool:
308308

309309
def _get_pubsub_callback_and_context(
310310
self,
311-
) -> Tuple[Optional[Callable[[CoreCommands.PubSubMsg, Any], None]], Any]:
311+
) -> Tuple[Optional[Callable[[PubSubMsg, Any], None]], Any]:
312312
return None, None
313313

314314

@@ -387,7 +387,7 @@ class PubSubSubscriptions:
387387
Attributes:
388388
channels_and_patterns (Dict[GlideClientConfiguration.PubSubChannelModes, Set[str]]):
389389
Channels and patterns by modes.
390-
callback (Optional[Callable[[CoreCommands.PubSubMsg, Any], None]]):
390+
callback (Optional[Callable[[PubSubMsg, Any], None]]):
391391
Optional callback to accept the incomming messages.
392392
context (Any):
393393
Arbitrary context to pass to the callback.
@@ -396,7 +396,7 @@ class PubSubSubscriptions:
396396
channels_and_patterns: Dict[
397397
GlideClientConfiguration.PubSubChannelModes, Set[str]
398398
]
399-
callback: Optional[Callable[[CoreCommands.PubSubMsg, Any], None]]
399+
callback: Optional[Callable[[PubSubMsg, Any], None]]
400400
context: Any
401401

402402
def __init__(
@@ -468,7 +468,7 @@ def _is_pubsub_configured(self) -> bool:
468468

469469
def _get_pubsub_callback_and_context(
470470
self,
471-
) -> Tuple[Optional[Callable[[CoreCommands.PubSubMsg, Any], None]], Any]:
471+
) -> Tuple[Optional[Callable[[PubSubMsg, Any], None]], Any]:
472472
if self.pubsub_subscriptions:
473473
return self.pubsub_subscriptions.callback, self.pubsub_subscriptions.context
474474
return None, None
@@ -557,7 +557,7 @@ class PubSubSubscriptions:
557557
Attributes:
558558
channels_and_patterns (Dict[GlideClusterClientConfiguration.PubSubChannelModes, Set[str]]):
559559
Channels and patterns by modes.
560-
callback (Optional[Callable[[CoreCommands.PubSubMsg, Any], None]]):
560+
callback (Optional[Callable[[PubSubMsg, Any], None]]):
561561
Optional callback to accept the incoming messages.
562562
context (Any):
563563
Arbitrary context to pass to the callback.
@@ -566,7 +566,7 @@ class PubSubSubscriptions:
566566
channels_and_patterns: Dict[
567567
GlideClusterClientConfiguration.PubSubChannelModes, Set[str]
568568
]
569-
callback: Optional[Callable[[CoreCommands.PubSubMsg, Any], None]]
569+
callback: Optional[Callable[[PubSubMsg, Any], None]]
570570
context: Any
571571

572572
def __init__(
@@ -644,7 +644,7 @@ def _is_pubsub_configured(self) -> bool:
644644

645645
def _get_pubsub_callback_and_context(
646646
self,
647-
) -> Tuple[Optional[Callable[[CoreCommands.PubSubMsg, Any], None]], Any]:
647+
) -> Tuple[Optional[Callable[[PubSubMsg, Any], None]], Any]:
648648
if self.pubsub_subscriptions:
649649
return self.pubsub_subscriptions.callback, self.pubsub_subscriptions.context
650650
return None, None

python/python/glide/glide_client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from glide.commands.async_commands.core import CoreCommands
1010
from glide.commands.async_commands.standalone_commands import StandaloneCommands
1111
from glide.commands.command_args import ObjectType
12+
from glide.commands.core_options import PubSubMsg
1213
from glide.config import BaseClientConfiguration, ServerCredentials
1314
from glide.constants import DEFAULT_READ_BYTES_SIZE, OK, TEncodable, TRequest, TResult
1415
from glide.exceptions import (
@@ -400,7 +401,7 @@ async def _execute_script(
400401
set_protobuf_route(request, route)
401402
return await self._write_request_await_response(request)
402403

403-
async def get_pubsub_message(self) -> CoreCommands.PubSubMsg:
404+
async def get_pubsub_message(self) -> PubSubMsg:
404405
if self._is_closed:
405406
raise ClosingError(
406407
"Unable to execute requests; the client is closed. Please create a new client."
@@ -426,7 +427,7 @@ async def get_pubsub_message(self) -> CoreCommands.PubSubMsg:
426427
self._pubsub_lock.release()
427428
return await response_future
428429

429-
def try_get_pubsub_message(self) -> Optional[CoreCommands.PubSubMsg]:
430+
def try_get_pubsub_message(self) -> Optional[PubSubMsg]:
430431
if self._is_closed:
431432
raise ClosingError(
432433
"Unable to execute requests; the client is closed. Please create a new client."
@@ -443,7 +444,7 @@ def try_get_pubsub_message(self) -> Optional[CoreCommands.PubSubMsg]:
443444
)
444445

445446
# locking might not be required
446-
msg: Optional[CoreCommands.PubSubMsg] = None
447+
msg: Optional[PubSubMsg] = None
447448
try:
448449
self._pubsub_lock.acquire()
449450
self._complete_pubsub_futures_safe()
@@ -462,7 +463,7 @@ def _cancel_pubsub_futures_with_exception_safe(self, exception: ConnectionError)
462463

463464
def _notification_to_pubsub_message_safe(
464465
self, response: Response
465-
) -> Optional[CoreCommands.PubSubMsg]:
466+
) -> Optional[PubSubMsg]:
466467
pubsub_message = None
467468
push_notification = cast(
468469
Dict[str, Any], value_from_pointer(response.resp_pointer)
@@ -481,11 +482,11 @@ def _notification_to_pubsub_message_safe(
481482
):
482483
values: List = push_notification["values"]
483484
if message_kind == "PMessage":
484-
pubsub_message = BaseClient.PubSubMsg(
485+
pubsub_message = PubSubMsg(
485486
message=values[2], channel=values[1], pattern=values[0]
486487
)
487488
else:
488-
pubsub_message = BaseClient.PubSubMsg(
489+
pubsub_message = PubSubMsg(
489490
message=values[1], channel=values[0], pattern=None
490491
)
491492
elif (

0 commit comments

Comments
 (0)