module documentation

The Encoder: one message dataclass becomes one live ROS2 message instance.

The mirror of opensmi.ros2.runtime._decode, so that no rclpy object crosses the seam. It runs on the event loop. Each conversion inverts one the Decoder made, and a wrong shape aborts the process instead of raising:

  • a nested message becomes its generated interface class again;
  • a tuple becomes a list, which the generated constructors accept for every container;
  • bytes stays bytes for a uint8 container and becomes a list of one-byte values for an octet one;
  • a byte scalar becomes a one-byte bytes again, because handing the int back dies on Assertion 'PyBytes_Check(field)' failed (pinned by a Jazzy test).

The Encoder does not validate. opensmi.ros2.runtime.guard does that beforehand, as a pure function that needs no ROS2 installation to test.

Function encode Convert one message dataclass into the rclpy object its ROS2 type is.
Function _as_one_byte Return a byte scalar as the one-byte bytes rclpy uses for IDL octet.
Function _bytes_for Return how a byte container is handed over: uint8 as a buffer, byte as a list of bytes.
Function _element_encoder Return the encoder for a scalar, or None when the value already needs no work.
Function _field_encoder Return the function that turns one decoded field value back into what rclpy wants.
Function _plan Return the interface class and the field-name and field-encoder pairs, in field order.
def encode(message: Message) -> Any:

Convert one message dataclass into the rclpy object its ROS2 type is.

Parameters
message:MessageThe payload, already checked by opensmi.ros2.runtime.guard.require_sendable.
Returns
AnyUndocumented
Raises
MissingInterfacePackageErrorWhen the type's interface package is not installed, which a declared type is refused for at init and an encoded one cannot reach.
def _as_one_byte(value: Any) -> bytes:

Return a byte scalar as the one-byte bytes rclpy uses for IDL octet.

def _bytes_for(field_type: FieldType) -> Callable[[Any], Any]:

Return how a byte container is handed over: uint8 as a buffer, byte as a list of bytes.

def _element_encoder(element: ElementType, nested: type[Message] | None) -> Callable[[Any], Any] | None:

Return the encoder for a scalar, or None when the value already needs no work.

def _field_encoder(field_type: FieldType, nested: type[Message] | None) -> Callable[[Any], Any]:

Return the function that turns one decoded field value back into what rclpy wants.

@cache
def _plan(message_type: type[Message]) -> tuple[Any, tuple[tuple[str, Callable[[Any], Any]], ...]]:

Return the interface class and the field-name and field-encoder pairs, in field order.