generated from pufereq/python-template
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d5a1e95da | ||
|
4bba344e8a
|
|||
|
29bf802356
|
|||
|
|
a805ccf38e | ||
|
1e9077c3a8
|
|||
|
89f95004c1
|
|||
|
795410047e
|
|||
|
|
48a848bb60 | ||
|
0c0595b5f6
|
27
CHANGELOG.md
27
CHANGELOG.md
@@ -2,6 +2,33 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [0.9.0] - 2026-03-05
|
||||
|
||||
### Features
|
||||
|
||||
- [`4bba344`](https://gitea.pufereq.pl/judas/judas_protocol/commit/4bba344e8abcd64947c06b371ae84ca8f0591a9c) **message.py**: add `Message.Telemetry` factory class
|
||||
- [`29bf802`](https://gitea.pufereq.pl/judas/judas_protocol/commit/29bf80235631f069be519e3e3ac0772c9a9d44db) **types.py**: add `TELEMETRY` category
|
||||
|
||||
## [0.8.0] - 2026-03-03
|
||||
|
||||
### Features
|
||||
|
||||
- [`7954100`](https://gitea.pufereq.pl/judas/judas_protocol/commit/795410047ef20ebb39503c09a5359e53475f2274) **types.py**: add `ActionType` type
|
||||
|
||||
### Miscellaneous Tasks
|
||||
|
||||
- [`89f9500`](https://gitea.pufereq.pl/judas/judas_protocol/commit/89f95004c1877263fbd1047fc09fe9196d8494f5) **__init__.py**: add `ActionType` to `__all__`
|
||||
|
||||
### Refactor
|
||||
|
||||
- [`1e9077c`](https://gitea.pufereq.pl/judas/judas_protocol/commit/1e9077c3a837d00c793ee50c5cbd754d5f0ec58a) **message.py**: use `ActionType` instead of `Enum`
|
||||
|
||||
## [0.7.0] - 2026-03-01
|
||||
|
||||
### Features
|
||||
|
||||
- [`0c0595b`](https://gitea.pufereq.pl/judas/judas_protocol/commit/0c0595b5f67526f1caa4f3650c04ae36180045c3) **message.py**: move CONTROL-specific methods into `Message.Control` inner class
|
||||
|
||||
## [0.6.0] - 2026-02-28
|
||||
|
||||
### Refactor
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
||||
|
||||
[project]
|
||||
name = "judas_protocol"
|
||||
version = "0.6.0"
|
||||
version = "0.9.0"
|
||||
description = "The judas protocol"
|
||||
readme = "README.md"
|
||||
authors = []
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
__version__: str = "0.1.0"
|
||||
|
||||
from .message import Message
|
||||
from .types import Category, ControlAction
|
||||
from .types import ActionType, Category, ControlAction
|
||||
|
||||
__all__ = ["__version__", "Message", "Category", "ControlAction"]
|
||||
__all__ = ["__version__", "Message", "ActionType", "Category", "ControlAction"]
|
||||
|
||||
@@ -3,10 +3,14 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from enum import Enum
|
||||
from typing import Any, override
|
||||
|
||||
from judas_protocol.types import Category, ControlAction
|
||||
from judas_protocol.types import (
|
||||
ActionType,
|
||||
Category,
|
||||
ControlAction,
|
||||
TelemetryAction,
|
||||
)
|
||||
|
||||
|
||||
class Message:
|
||||
@@ -16,7 +20,7 @@ class Message:
|
||||
self,
|
||||
id_: str | None,
|
||||
category: Category,
|
||||
action: Enum,
|
||||
action: ActionType,
|
||||
payload: dict[str, Any] | None = None,
|
||||
*,
|
||||
acknowledged: bool = False,
|
||||
@@ -27,14 +31,14 @@ class 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.
|
||||
action (ActionType): 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 = id_ or str(uuid.uuid4())
|
||||
self.category: Category = category
|
||||
self.action: Enum = action
|
||||
self.action: ActionType = action
|
||||
self.payload: dict[str, Any] = payload or {}
|
||||
|
||||
self.acknowledged: bool = acknowledged
|
||||
@@ -52,6 +56,123 @@ class Message:
|
||||
f", ack_required={self.ack_required})"
|
||||
)
|
||||
|
||||
class Control:
|
||||
"""CONTROL category message factory methods."""
|
||||
|
||||
@staticmethod
|
||||
def ack(target_id: str) -> Message:
|
||||
"""Create an ACK message.
|
||||
|
||||
Args:
|
||||
target_id (str): The ID of the message to acknowledge.
|
||||
|
||||
Returns:
|
||||
Message: The created ACK message.
|
||||
"""
|
||||
return Message(
|
||||
id_=None,
|
||||
category=Category.CONTROL,
|
||||
action=ControlAction.ACK,
|
||||
payload={"target_id": target_id},
|
||||
acknowledged=False,
|
||||
ack_required=False,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def hello(id_: str) -> Message:
|
||||
"""Create a HELLO message.
|
||||
|
||||
Args:
|
||||
id_ (str): The ID to include in the HELLO message.
|
||||
Returns:
|
||||
Message: The created HELLO message.
|
||||
"""
|
||||
return Message(
|
||||
id_=None,
|
||||
category=Category.CONTROL,
|
||||
action=ControlAction.HELLO,
|
||||
payload={"id": id_},
|
||||
acknowledged=False,
|
||||
ack_required=True,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def close() -> Message:
|
||||
"""Create a CLOSE message.
|
||||
Prompts the recipient to close the connection and not reconnect.
|
||||
|
||||
Returns:
|
||||
Message: The created CLOSE message.
|
||||
"""
|
||||
return Message(
|
||||
id_=None,
|
||||
category=Category.CONTROL,
|
||||
action=ControlAction.CLOSE,
|
||||
payload={},
|
||||
acknowledged=False,
|
||||
ack_required=True,
|
||||
)
|
||||
|
||||
class Telemetry:
|
||||
"""TELEMETRY category message factory methods."""
|
||||
|
||||
@staticmethod
|
||||
def initial(payload: dict[str, Any]) -> Message:
|
||||
"""Create an INITIAL telemetry message.
|
||||
|
||||
Args:
|
||||
payload (dict[str, Any]): The payload of the message.
|
||||
|
||||
Returns:
|
||||
Message: The created INITIAL telemetry message.
|
||||
"""
|
||||
return Message(
|
||||
id_=None,
|
||||
category=Category.TELEMETRY,
|
||||
action=TelemetryAction.INITIAL,
|
||||
payload=payload,
|
||||
acknowledged=False,
|
||||
ack_required=True,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def update(payload: dict[str, Any]) -> Message:
|
||||
"""Create an UPDATE telemetry message.
|
||||
|
||||
Args:
|
||||
payload (dict[str, Any]): The payload of the message.
|
||||
|
||||
Returns:
|
||||
Message: The created UPDATE telemetry message.
|
||||
"""
|
||||
return Message(
|
||||
id_=None,
|
||||
category=Category.TELEMETRY,
|
||||
action=TelemetryAction.UPDATE,
|
||||
payload=payload,
|
||||
acknowledged=False,
|
||||
ack_required=False,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def on_demand(payload: dict[str, Any]) -> Message:
|
||||
"""Create an ON_DEMAND telemetry message.
|
||||
|
||||
Args:
|
||||
payload (dict[str, Any]): The payload of the message.
|
||||
|
||||
Returns:
|
||||
Message: The created ON_DEMAND telemetry message.
|
||||
"""
|
||||
return Message(
|
||||
id_=None,
|
||||
category=Category.TELEMETRY,
|
||||
action=TelemetryAction.ON_DEMAND,
|
||||
payload=payload,
|
||||
acknowledged=False,
|
||||
ack_required=True,
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert the message to a dictionary.
|
||||
|
||||
@@ -144,57 +265,3 @@ class Message:
|
||||
Message: The created message.
|
||||
"""
|
||||
return cls.from_json(data.decode("utf-8"))
|
||||
|
||||
@classmethod
|
||||
def ack(cls, target_id: str) -> Message:
|
||||
"""Create an ACK message.
|
||||
|
||||
Args:
|
||||
target_id (str): The ID of the message to acknowledge.
|
||||
|
||||
Returns:
|
||||
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
|
||||
def hello(cls, id_: str) -> Message:
|
||||
"""Create a HELLO message.
|
||||
|
||||
Args:
|
||||
id_ (str): The ID to include in the HELLO message.
|
||||
Returns:
|
||||
Message: The created HELLO message.
|
||||
"""
|
||||
return cls(
|
||||
id_=None,
|
||||
category=Category.CONTROL,
|
||||
action=ControlAction.HELLO,
|
||||
payload={"id": id_},
|
||||
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,
|
||||
)
|
||||
|
||||
@@ -5,12 +5,21 @@ from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
|
||||
type ActionType = ControlAction | TelemetryAction
|
||||
|
||||
|
||||
class Category(str, Enum):
|
||||
CONTROL = "control"
|
||||
TELEMETRY = "telemetry"
|
||||
|
||||
|
||||
class ControlAction(str, Enum):
|
||||
HELLO = "hello"
|
||||
ACK = "ack"
|
||||
CLOSE = "close"
|
||||
|
||||
|
||||
class TelemetryAction(str, Enum):
|
||||
INITIAL = "initial"
|
||||
UPDATE = "update"
|
||||
ON_DEMAND = "on_demand"
|
||||
|
||||
Reference in New Issue
Block a user