generated from pufereq/python-template
Compare commits
5 Commits
0.4.1
...
332ce3ffa1
| Author | SHA1 | Date | |
|---|---|---|---|
|
332ce3ffa1
|
|||
|
|
5ef300ff93 | ||
|
1ab577ebd5
|
|||
|
|
6ce742667a | ||
|
37eec56afe
|
12
CHANGELOG.md
12
CHANGELOG.md
@@ -2,6 +2,18 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [0.4.3] - 2025-10-05
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- [`1ab577e`](https://gitea.pufereq.pl/judas/judas_protocol/commit/1ab577ebd583ed17f813d37745b5dc195855d9c3) **message.py**: provide `id_` in `ack()` and `hello()`
|
||||
|
||||
## [0.4.2] - 2025-10-05
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- [`37eec56`](https://gitea.pufereq.pl/judas/judas_protocol/commit/37eec56afeb2697955702aba04476ccdde7435e4) **message.py**: allow providing ID via argument
|
||||
|
||||
## [0.4.1] - 2025-10-05
|
||||
|
||||
### Miscellaneous Tasks
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
||||
|
||||
[project]
|
||||
name = "judas_protocol"
|
||||
version = "0.4.1"
|
||||
version = "0.4.3"
|
||||
description = "The judas protocol"
|
||||
readme = "README.md"
|
||||
authors = []
|
||||
|
||||
@@ -14,29 +14,43 @@ class Message:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
id_: str | None,
|
||||
category: Category,
|
||||
action: Enum,
|
||||
payload: dict[str, Any] | None = None,
|
||||
*,
|
||||
acknowledged: bool = False,
|
||||
ack_required: bool = False,
|
||||
) -> None:
|
||||
"""Initialize a Message.
|
||||
|
||||
Args:
|
||||
id_ (str | None): The ID of the message. If None, a new UUID will be generated.
|
||||
category (Category): The category of the message.
|
||||
action (Enum): The action of the message.
|
||||
payload (dict[str, Any] | None): The payload of the message.
|
||||
acknowledged (bool): Whether the message has been acknowledged. Defaults to False.
|
||||
ack_required (bool): Whether the message requires an acknowledgment. Defaults to False.
|
||||
"""
|
||||
self.id: str = str(uuid.uuid4())
|
||||
self.id: str = id_ or str(uuid.uuid4())
|
||||
self.category: Category = category
|
||||
self.action: Enum = action
|
||||
self.payload: dict[str, Any] = payload or {}
|
||||
|
||||
self.acknowledged: bool = acknowledged
|
||||
self.ack_required: bool = ack_required
|
||||
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return f"Message ({self.id}) [{self.category.name}] {self.action.name} - {self.payload}"
|
||||
return f"Message ({self.id}) ack'd: {self.acknowledged}, req: {self.ack_required} [{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})"
|
||||
return (
|
||||
f"Message(id_={self.id}, category={self.category}, action={self.action}"
|
||||
f", payload={self.payload}, acknowledged={self.acknowledged}"
|
||||
f", ack_required={self.ack_required})"
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert the message to a dictionary.
|
||||
@@ -49,6 +63,8 @@ class Message:
|
||||
"category": self.category,
|
||||
"action": self.action,
|
||||
"payload": self.payload,
|
||||
"acknowledged": self.acknowledged,
|
||||
"ack_required": self.ack_required,
|
||||
}
|
||||
|
||||
def to_json(self) -> str:
|
||||
@@ -77,8 +93,14 @@ class Message:
|
||||
Returns:
|
||||
Message: The created message.
|
||||
"""
|
||||
if "id" not in data:
|
||||
data["id"] = None
|
||||
|
||||
id_: str | None = data["id"]
|
||||
category = Category(data["category"])
|
||||
action_str = data["action"]
|
||||
acknowledged = data["acknowledged"]
|
||||
ack_required = data["ack_required"]
|
||||
|
||||
match category:
|
||||
case Category.CONTROL:
|
||||
@@ -87,7 +109,14 @@ class Message:
|
||||
raise ValueError(f"Unknown category: {category}")
|
||||
|
||||
payload = data.get("payload", {})
|
||||
return cls(category=category, action=action, payload=payload)
|
||||
return cls(
|
||||
id_=id_,
|
||||
category=category,
|
||||
action=action,
|
||||
payload=payload,
|
||||
acknowledged=acknowledged,
|
||||
ack_required=ack_required,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, data: str) -> Message:
|
||||
@@ -127,9 +156,12 @@ class Message:
|
||||
Message: The created ACK message.
|
||||
"""
|
||||
return cls(
|
||||
id_=None,
|
||||
category=Category.CONTROL,
|
||||
action=ControlAction.ACK,
|
||||
payload={"target_id": target_id},
|
||||
acknowledged=False,
|
||||
ack_required=False,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@@ -142,7 +174,10 @@ class Message:
|
||||
Message: The created HELLO message.
|
||||
"""
|
||||
return cls(
|
||||
id_=None,
|
||||
category=Category.CONTROL,
|
||||
action=ControlAction.HELLO,
|
||||
payload={"mac": mac},
|
||||
acknowledged=False,
|
||||
ack_required=True,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user