generated from pufereq/python-template
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c48b69ecee | ||
|
2ef4adaaa1
|
|||
|
aa0703fcde
|
|||
|
332ce3ffa1
|
|||
|
|
5ef300ff93 | ||
|
1ab577ebd5
|
|||
|
|
6ce742667a | ||
|
37eec56afe
|
|||
|
|
8c4b49f0d7 | ||
|
f307a7f457
|
|||
|
|
0e72c93d55 | ||
|
4cc3a5bb3a
|
32
CHANGELOG.md
32
CHANGELOG.md
@@ -2,6 +2,38 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [0.5.0] - 2025-11-19
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- [`2ef4ada`](https://gitea.pufereq.pl/judas/judas_protocol/commit/2ef4adaaa1e872c3c622ec6c37966dda1c7665d3) **message.py**: add `CLOSE` msg constructor
|
||||||
|
- [`aa0703f`](https://gitea.pufereq.pl/judas/judas_protocol/commit/aa0703fcde9d531fc5eaf8d9c93b160422692387) **types.py**: add `CLOSE` msg ControlAction
|
||||||
|
- [`332ce3f`](https://gitea.pufereq.pl/judas/judas_protocol/commit/332ce3ffa16ba43d6af1ba71bce1bc633e1661a9) **message.py**: add `acknowledged` and `ack_required` properties
|
||||||
|
|
||||||
|
## [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
|
||||||
|
|
||||||
|
- [`f307a7f`](https://gitea.pufereq.pl/judas/judas_protocol/commit/f307a7f457924ff1717ed4c8ce195ce022dd5680) **message.py**: add `target_id` argument to `Message.ack` classmethod
|
||||||
|
|
||||||
|
## [0.4.0] - 2025-10-05
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- [`4cc3a5b`](https://gitea.pufereq.pl/judas/judas_protocol/commit/4cc3a5bb3a0fce0c4d90958f26fa09a1d638a4b3) **message.py**: add message uuid
|
||||||
|
|
||||||
## [0.3.0] - 2025-09-23
|
## [0.3.0] - 2025-09-23
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "judas_protocol"
|
name = "judas_protocol"
|
||||||
version = "0.3.0"
|
version = "0.5.0"
|
||||||
description = "The judas protocol"
|
description = "The judas protocol"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
authors = []
|
authors = []
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, override
|
from typing import Any, override
|
||||||
|
|
||||||
@@ -13,28 +14,43 @@ class Message:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
id_: str | None,
|
||||||
category: Category,
|
category: Category,
|
||||||
action: Enum,
|
action: Enum,
|
||||||
payload: dict[str, Any] | None = None,
|
payload: dict[str, Any] | None = None,
|
||||||
|
*,
|
||||||
|
acknowledged: bool = False,
|
||||||
|
ack_required: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Message.
|
"""Initialize a Message.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
id_ (str | None): The ID of the message. If None, a new UUID will be generated.
|
||||||
category (Category): The category of the message.
|
category (Category): The category of the message.
|
||||||
action (Enum): The action of the message.
|
action (Enum): The action of the message.
|
||||||
payload (dict[str, Any] | None): The payload 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 = id_ or str(uuid.uuid4())
|
||||||
self.category: Category = category
|
self.category: Category = category
|
||||||
self.action: Enum = action
|
self.action: Enum = action
|
||||||
self.payload: dict[str, Any] = payload or {}
|
self.payload: dict[str, Any] = payload or {}
|
||||||
|
|
||||||
|
self.acknowledged: bool = acknowledged
|
||||||
|
self.ack_required: bool = ack_required
|
||||||
|
|
||||||
@override
|
@override
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"Message [{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
|
@override
|
||||||
def __repr__(self) -> str:
|
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]:
|
def to_dict(self) -> dict[str, Any]:
|
||||||
"""Convert the message to a dictionary.
|
"""Convert the message to a dictionary.
|
||||||
@@ -43,9 +59,12 @@ class Message:
|
|||||||
dict[str, Any]: The message as a dictionary.
|
dict[str, Any]: The message as a dictionary.
|
||||||
"""
|
"""
|
||||||
return {
|
return {
|
||||||
|
"id": self.id,
|
||||||
"category": self.category,
|
"category": self.category,
|
||||||
"action": self.action,
|
"action": self.action,
|
||||||
"payload": self.payload,
|
"payload": self.payload,
|
||||||
|
"acknowledged": self.acknowledged,
|
||||||
|
"ack_required": self.ack_required,
|
||||||
}
|
}
|
||||||
|
|
||||||
def to_json(self) -> str:
|
def to_json(self) -> str:
|
||||||
@@ -74,8 +93,14 @@ class Message:
|
|||||||
Returns:
|
Returns:
|
||||||
Message: The created message.
|
Message: The created message.
|
||||||
"""
|
"""
|
||||||
|
if "id" not in data:
|
||||||
|
data["id"] = None
|
||||||
|
|
||||||
|
id_: str | None = data["id"]
|
||||||
category = Category(data["category"])
|
category = Category(data["category"])
|
||||||
action_str = data["action"]
|
action_str = data["action"]
|
||||||
|
acknowledged = data["acknowledged"]
|
||||||
|
ack_required = data["ack_required"]
|
||||||
|
|
||||||
match category:
|
match category:
|
||||||
case Category.CONTROL:
|
case Category.CONTROL:
|
||||||
@@ -84,7 +109,14 @@ class Message:
|
|||||||
raise ValueError(f"Unknown category: {category}")
|
raise ValueError(f"Unknown category: {category}")
|
||||||
|
|
||||||
payload = data.get("payload", {})
|
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
|
@classmethod
|
||||||
def from_json(cls, data: str) -> Message:
|
def from_json(cls, data: str) -> Message:
|
||||||
@@ -114,14 +146,22 @@ class Message:
|
|||||||
return cls.from_json(data.decode("utf-8"))
|
return cls.from_json(data.decode("utf-8"))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def ack(cls) -> Message:
|
def ack(cls, target_id: str) -> Message:
|
||||||
"""Create an ACK message.
|
"""Create an ACK message.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
target_id (str): The ID of the message to acknowledge.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Message: The created ACK message.
|
Message: The created ACK message.
|
||||||
"""
|
"""
|
||||||
return cls(
|
return cls(
|
||||||
category=Category.CONTROL, action=ControlAction.ACK, payload={}
|
id_=None,
|
||||||
|
category=Category.CONTROL,
|
||||||
|
action=ControlAction.ACK,
|
||||||
|
payload={"target_id": target_id},
|
||||||
|
acknowledged=False,
|
||||||
|
ack_required=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -134,7 +174,27 @@ class Message:
|
|||||||
Message: The created HELLO message.
|
Message: The created HELLO message.
|
||||||
"""
|
"""
|
||||||
return cls(
|
return cls(
|
||||||
|
id_=None,
|
||||||
category=Category.CONTROL,
|
category=Category.CONTROL,
|
||||||
action=ControlAction.HELLO,
|
action=ControlAction.HELLO,
|
||||||
payload={"mac": mac},
|
payload={"mac": mac},
|
||||||
|
acknowledged=False,
|
||||||
|
ack_required=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def close(cls) -> Message:
|
||||||
|
"""Create a CLOSE message.
|
||||||
|
Prompts the recipient to close the connection and not reconnect.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Message: The created CLOSE message.
|
||||||
|
"""
|
||||||
|
return cls(
|
||||||
|
id_=None,
|
||||||
|
category=Category.CONTROL,
|
||||||
|
action=ControlAction.CLOSE,
|
||||||
|
payload={},
|
||||||
|
acknowledged=False,
|
||||||
|
ack_required=True,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,3 +13,4 @@ class Category(str, Enum):
|
|||||||
class ControlAction(str, Enum):
|
class ControlAction(str, Enum):
|
||||||
HELLO = "hello"
|
HELLO = "hello"
|
||||||
ACK = "ack"
|
ACK = "ack"
|
||||||
|
CLOSE = "close"
|
||||||
|
|||||||
Reference in New Issue
Block a user