generated from pufereq/python-template
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c25ee1ebdf | ||
|
ffc4ed1897
|
|||
|
bc1bf46388
|
|||
|
9b5b104061
|
|||
|
|
cccda80784 | ||
|
fd070b1763
|
|||
|
df55e0c5cb
|
|||
|
762d09ea9b
|
|||
|
b90653b3fd
|
20
CHANGELOG.md
20
CHANGELOG.md
@@ -2,6 +2,26 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [0.3.0] - 2025-09-23
|
||||
|
||||
### Features
|
||||
|
||||
- [`ffc4ed1`](https://gitea.pufereq.pl/judas/judas_protocol/commit/ffc4ed18974e89c6f1f5554d9e8832065432310a) **message.py**: add `__str__` and `__repr__` methods
|
||||
- [`bc1bf46`](https://gitea.pufereq.pl/judas/judas_protocol/commit/bc1bf46388eb904738893a2f86b5050b4ce2489e) **message.py**: raise ValueError if invalid json in `Message.from_bytes()`
|
||||
|
||||
### Styling
|
||||
|
||||
- [`9b5b104`](https://gitea.pufereq.pl/judas/judas_protocol/commit/9b5b104061859b2dd4ab93258414edd8ce34b4a7) **message.py**: organize imports
|
||||
|
||||
## [0.2.0] - 2025-08-29
|
||||
|
||||
### Features
|
||||
|
||||
- [`fd070b1`](https://gitea.pufereq.pl/judas/judas_protocol/commit/fd070b176347a0f7b81f937b189d8f50736f3514) **message.py**: add `b"\n"` at end of data from `to_bytes()`
|
||||
- [`df55e0c`](https://gitea.pufereq.pl/judas/judas_protocol/commit/df55e0c5cba00c6d08b98cf09f265c130bc11f73) **message.py**: add `Message.from_bytes()`
|
||||
- [`762d09e`](https://gitea.pufereq.pl/judas/judas_protocol/commit/762d09ea9b3e28efdfdd2ac0674bb874154b41a9) **message.py**: add `Message.to_bytes()`
|
||||
- [`b90653b`](https://gitea.pufereq.pl/judas/judas_protocol/commit/b90653b3fd90957a0056dcd803da20a78a589314) **__init__.py**: add public api
|
||||
|
||||
## [0.1.0] - 2025-08-28
|
||||
|
||||
### Features
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
||||
|
||||
[project]
|
||||
name = "judas_protocol"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
description = "The judas protocol"
|
||||
readme = "README.md"
|
||||
authors = []
|
||||
|
||||
@@ -1 +1,6 @@
|
||||
__version__: str = "0.1.0"
|
||||
|
||||
from .message import Message
|
||||
from .types import Category, ControlAction
|
||||
|
||||
__all__ = ["__version__", "Message", "Category", "ControlAction"]
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
import json
|
||||
from typing import Any
|
||||
from enum import Enum
|
||||
from typing import Any, override
|
||||
|
||||
from judas_protocol.types import Category, ControlAction
|
||||
|
||||
@@ -28,6 +28,14 @@ class Message:
|
||||
self.action: Enum = action
|
||||
self.payload: dict[str, Any] = payload or {}
|
||||
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return f"Message [{self.category.name}] {self.action.name} - {self.payload}"
|
||||
|
||||
@override
|
||||
def __repr__(self) -> str:
|
||||
return f"Message(category={self.category}, action={self.action}, payload={self.payload})"
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert the message to a dictionary.
|
||||
|
||||
@@ -48,6 +56,14 @@ class Message:
|
||||
"""
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
def to_bytes(self) -> bytes:
|
||||
"""Convert the message to bytes.
|
||||
|
||||
Returns:
|
||||
bytes: The message as bytes.
|
||||
"""
|
||||
return self.to_json().encode("utf-8") + b"\n"
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict[str, Any]) -> Message:
|
||||
"""Create a Message from a dictionary.
|
||||
@@ -79,7 +95,23 @@ class Message:
|
||||
Returns:
|
||||
Message: The created message.
|
||||
"""
|
||||
return cls.from_dict(json.loads(data))
|
||||
try:
|
||||
json_data = json.loads(data)
|
||||
except json.JSONDecodeError as e:
|
||||
raise ValueError("Invalid JSON data") from e
|
||||
|
||||
return cls.from_dict(json_data)
|
||||
|
||||
@classmethod
|
||||
def from_bytes(cls, data: bytes) -> Message:
|
||||
"""Create a Message from bytes.
|
||||
|
||||
Args:
|
||||
data (bytes): The bytes to create the message from.
|
||||
Returns:
|
||||
Message: The created message.
|
||||
"""
|
||||
return cls.from_json(data.decode("utf-8"))
|
||||
|
||||
@classmethod
|
||||
def ack(cls) -> Message:
|
||||
|
||||
Reference in New Issue
Block a user