-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Mahad-10/add-pub-sub-parser
python: Add PubSub parsers
- Loading branch information
Showing
7 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from typing import Any | ||
|
||
from wampproto.messages import Message | ||
from wampproto.messages.event import IEventFields, Event | ||
|
||
from wampprotobuf.gen import event_pb2 | ||
from wampprotobuf.parsers import helpers | ||
|
||
|
||
class EventFields(IEventFields): | ||
def __init__(self, msg: event_pb2.Event): | ||
super().__init__() | ||
self._msg = msg | ||
self._args = None | ||
self._kwargs = None | ||
self._unpacked = False | ||
|
||
@property | ||
def subscription_id(self) -> int: | ||
return self._msg.subscription_id | ||
|
||
@property | ||
def publication_id(self) -> int: | ||
return self._msg.publication_id | ||
|
||
def unpack(self): | ||
try: | ||
self._unpacked = True | ||
args, kwargs = helpers.from_cbor_payload(self._msg.payload) | ||
self._args = args | ||
self._kwargs = kwargs | ||
except Exception as e: | ||
print(f"error parsing CBOR payload: {e}") | ||
|
||
@property | ||
def args(self) -> list[Any] | None: | ||
if not self._unpacked: | ||
self.unpack() | ||
|
||
return self._args | ||
|
||
@property | ||
def kwargs(self) -> dict[str, Any] | None: | ||
if not self._unpacked: | ||
self.unpack() | ||
|
||
return self._kwargs | ||
|
||
@property | ||
def details(self) -> dict[str, Any]: | ||
return {} | ||
|
||
def payload_is_binary(self) -> bool: | ||
return True | ||
|
||
@property | ||
def payload(self) -> bytes | None: | ||
return self._msg.payload | ||
|
||
@property | ||
def payload_serializer(self) -> int: | ||
return self._msg.payload_serializer | ||
|
||
|
||
def from_protobuf(payload: bytes) -> Message: | ||
result = event_pb2.Event() | ||
result.ParseFromString(payload) | ||
|
||
return Event(EventFields(result)) | ||
|
||
|
||
def to_protobuf(event: Event) -> bytes: | ||
result = event_pb2.Event() | ||
result.subscription_id = event.subscription_id | ||
result.publication_id = event.publication_id | ||
|
||
if event.payload_is_binary(): | ||
result.payload = event.payload | ||
result.payload_serializer = event.payload_serializer | ||
else: | ||
try: | ||
payload = helpers.to_cbor_payload(event.args, event.kwargs) | ||
except Exception as e: | ||
raise Exception(f"error in serialization to cbor: {e}") | ||
|
||
serializer = 1 | ||
|
||
result.payload = payload | ||
result.payload_serializer = serializer | ||
|
||
return result.SerializeToString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from typing import Any | ||
|
||
from wampproto.messages import Message | ||
from wampproto.messages.publish import IPublishFields, Publish | ||
|
||
from wampprotobuf.gen import publish_pb2 | ||
from wampprotobuf.parsers import helpers | ||
|
||
|
||
class PublishFields(IPublishFields): | ||
def __init__(self, msg: publish_pb2.Publish): | ||
super().__init__() | ||
self._msg = msg | ||
self._args = None | ||
self._kwargs = None | ||
self._unpacked = False | ||
|
||
@property | ||
def request_id(self) -> int: | ||
return self._msg.request_id | ||
|
||
@property | ||
def uri(self) -> str: | ||
return self._msg.topic | ||
|
||
def unpack(self): | ||
try: | ||
self._unpacked = True | ||
args, kwargs = helpers.from_cbor_payload(self._msg.payload) | ||
self._args = args | ||
self._kwargs = kwargs | ||
except Exception as e: | ||
print(f"error parsing CBOR payload: {e}") | ||
|
||
@property | ||
def args(self) -> list[Any] | None: | ||
if not self._unpacked: | ||
self.unpack() | ||
|
||
return self._args | ||
|
||
@property | ||
def kwargs(self) -> dict[str, Any] | None: | ||
if not self._unpacked: | ||
self.unpack() | ||
|
||
return self._kwargs | ||
|
||
@property | ||
def options(self) -> dict[str, Any]: | ||
return {} | ||
|
||
def payload_is_binary(self) -> bool: | ||
return True | ||
|
||
@property | ||
def payload(self) -> bytes | None: | ||
return self._msg.payload | ||
|
||
@property | ||
def payload_serializer(self) -> int: | ||
return self._msg.payload_serializer | ||
|
||
|
||
def from_protobuf(payload: bytes) -> Message: | ||
result = publish_pb2.Publish() | ||
result.ParseFromString(payload) | ||
|
||
return Publish(PublishFields(result)) | ||
|
||
|
||
def to_protobuf(publish: Publish) -> bytes: | ||
result = publish_pb2.Publish() | ||
result.request_id = publish.request_id | ||
result.topic = publish.uri | ||
|
||
if publish.payload_is_binary(): | ||
result.payload = publish.payload | ||
result.payload_serializer = publish.payload_serializer | ||
else: | ||
try: | ||
payload = helpers.to_cbor_payload(publish.args, publish.kwargs) | ||
except Exception as e: | ||
raise Exception(f"error in serialization to cbor: {e}") | ||
|
||
serializer = 1 | ||
|
||
result.payload = payload | ||
result.payload_serializer = serializer | ||
|
||
return result.SerializeToString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from wampproto.messages import Message | ||
from wampproto.messages.published import IPublishedFields, Published | ||
|
||
from wampprotobuf.gen import published_pb2 | ||
|
||
|
||
class PublishedFields(IPublishedFields): | ||
def __init__(self, msg: published_pb2.Published): | ||
self._msg = msg | ||
|
||
@property | ||
def request_id(self) -> int: | ||
return self._msg.request_id | ||
|
||
@property | ||
def publication_id(self) -> int: | ||
return self._msg.publication_id | ||
|
||
|
||
def from_protobuf(payload: bytes) -> Message: | ||
result = published_pb2.Published() | ||
result.ParseFromString(payload) | ||
|
||
return Published(PublishedFields(result)) | ||
|
||
|
||
def to_protobuf(published: Published) -> bytes: | ||
result = published_pb2.Published() | ||
result.request_id = published.request_id | ||
result.publication_id = published.publication_id | ||
|
||
return result.SerializeToString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Any | ||
|
||
from wampproto.messages import Message | ||
from wampproto.messages.subscribe import ISubscribeFields, Subscribe | ||
|
||
from wampprotobuf.gen import subscribe_pb2 | ||
|
||
|
||
class SubscribeFields(ISubscribeFields): | ||
def __init__(self, msg: subscribe_pb2.Subscribe): | ||
self._msg = msg | ||
|
||
@property | ||
def request_id(self) -> int: | ||
return self._msg.request_id | ||
|
||
@property | ||
def topic(self) -> str: | ||
return self._msg.topic | ||
|
||
@property | ||
def options(self) -> dict[str, Any]: | ||
return {} | ||
|
||
|
||
def from_protobuf(payload: bytes) -> Message: | ||
result = subscribe_pb2.Subscribe() | ||
result.ParseFromString(payload) | ||
|
||
return Subscribe(SubscribeFields(result)) | ||
|
||
|
||
def to_protobuf(subscribe: Subscribe) -> bytes: | ||
result = subscribe_pb2.Subscribe() | ||
result.request_id = subscribe.request_id | ||
result.topic = subscribe.topic | ||
|
||
return result.SerializeToString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from wampproto.messages import Message | ||
from wampproto.messages.subscribed import ISubscribedFields, Subscribed | ||
|
||
from wampprotobuf.gen import subscribed_pb2 | ||
|
||
|
||
class SubscribedFields(ISubscribedFields): | ||
def __init__(self, msg: subscribed_pb2.Subscribed): | ||
self._msg = msg | ||
|
||
@property | ||
def request_id(self) -> int: | ||
return self._msg.request_id | ||
|
||
@property | ||
def subscription_id(self) -> int: | ||
return self._msg.subscription_id | ||
|
||
|
||
def from_protobuf(payload: bytes) -> Message: | ||
result = subscribed_pb2.Subscribed() | ||
result.ParseFromString(payload) | ||
|
||
return Subscribed(SubscribedFields(result)) | ||
|
||
|
||
def to_protobuf(subscribed: Subscribed) -> bytes: | ||
result = subscribed_pb2.Subscribed() | ||
result.request_id = subscribed.request_id | ||
result.subscription_id = subscribed.subscription_id | ||
|
||
return result.SerializeToString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from wampproto.messages import Message | ||
from wampproto.messages.unsubscribe import IUnSubscribeFields, UnSubscribe | ||
|
||
from wampprotobuf.gen import unsubscribe_pb2 | ||
|
||
|
||
class UnSubscribeFields(IUnSubscribeFields): | ||
def __init__(self, msg: unsubscribe_pb2.UnSubscribe): | ||
self._msg = msg | ||
|
||
@property | ||
def request_id(self) -> int: | ||
return self._msg.request_id | ||
|
||
@property | ||
def subscription_id(self) -> int: | ||
return self._msg.subscription_id | ||
|
||
|
||
def from_protobuf(payload: bytes) -> Message: | ||
result = unsubscribe_pb2.UnSubscribe() | ||
result.ParseFromString(payload) | ||
|
||
return UnSubscribe(UnSubscribeFields(result)) | ||
|
||
|
||
def to_protobuf(unsubscribe: UnSubscribe) -> bytes: | ||
result = unsubscribe_pb2.UnSubscribe() | ||
result.request_id = unsubscribe.request_id | ||
result.subscription_id = unsubscribe.subscription_id | ||
|
||
return result.SerializeToString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from wampproto.messages import Message | ||
from wampproto.messages.unsubscribed import IUnSubscribedFields, UnSubscribed | ||
|
||
from wampprotobuf.gen import unsubscribed_pb2 | ||
|
||
|
||
class UnSubscribedFields(IUnSubscribedFields): | ||
def __init__(self, msg: unsubscribed_pb2.UnSubscribed): | ||
self._msg = msg | ||
|
||
@property | ||
def request_id(self) -> int: | ||
return self._msg.request_id | ||
|
||
|
||
def from_protobuf(payload: bytes) -> Message: | ||
result = unsubscribed_pb2.UnSubscribed() | ||
result.ParseFromString(payload) | ||
|
||
return UnSubscribed(UnSubscribedFields(result)) | ||
|
||
|
||
def to_protobuf(unsubscribed: UnSubscribed) -> bytes: | ||
result = unsubscribed_pb2.UnSubscribed() | ||
result.request_id = unsubscribed.request_id | ||
|
||
return result.SerializeToString() |