remotivelabs.topology.namespaces.scripted

  1from __future__ import annotations
  2
  3from collections.abc import Awaitable, Callable, Sequence
  4from typing import AsyncIterator
  5
  6from remotivelabs.broker import (
  7    BrokerClient,
  8    Frame,
  9    FrameInfo,
 10    FrameSubscription,
 11    NamespaceInfo,
 12    NamespaceName,
 13    Signal,
 14    SignalInfo,
 15    SignalName,
 16)
 17from remotivelabs.broker.filters import AllFramesFilter, FrameFilter, ReceiverFilter, SenderFilter, SignalFilter
 18
 19from remotivelabs.topology.namespaces.input_handlers import FrameHandler, InputHandler
 20from remotivelabs.topology.namespaces.namespace import Namespace
 21
 22
 23class ScriptedNamespace(
 24    Namespace[SenderFilter | ReceiverFilter | FrameFilter | AllFramesFilter | SignalFilter, Callable[[Frame], Awaitable[None]]]
 25):
 26    """
 27    ScriptedNamespace provides access to frames that have been transformed by scripts in the broker.
 28    """
 29
 30    def __init__(
 31        self,
 32        name: NamespaceName,
 33        broker_client: BrokerClient,
 34        decode_named_values: bool = False,
 35    ):
 36        """
 37        Initialize the scripted namespace client.
 38
 39        Args:
 40            name: The namespace name to operate in.
 41            broker_client: The client used to communicate with the broker.
 42            decode_named_values: True will decode named values to str.
 43
 44        Note:
 45            Use together with a `BehavioralModel` or start the instance using a context manager:
 46            ```python
 47            async with ScriptedNamespace(...) as broker_client:
 48                ...
 49            ```
 50        """
 51        super().__init__(name=name, broker_client=broker_client)
 52        self._decode_named_values = decode_named_values
 53
 54    async def _on_open(self, info: NamespaceInfo) -> None:
 55        if info.type != "scripted":
 56            raise ValueError(f"Namespace '{self.name}' is not of type 'scripted'")
 57
 58    async def list_frame_infos(self) -> list[FrameInfo]:
 59        """Return all available frame infos in the namespace."""
 60        return await self._broker_client.list_frame_infos(self.name)
 61
 62    async def get_frame_info(self, name: str) -> FrameInfo | None:
 63        """Return information about a specific frame by name."""
 64        return await self._broker_client.get_frame_info(name, namespace=self.name)
 65
 66    async def list_signal_infos(self) -> list[SignalInfo]:
 67        """Return all available signal infos in the namespace."""
 68        return await self._broker_client.list_signal_infos(self.name)
 69
 70    async def get_signal_info(self, name: SignalName) -> SignalInfo | None:
 71        """Return information about a specific signal by name."""
 72        return await self._broker_client.get_signal_info(name, namespace=self.name)
 73
 74    async def subscribe(self, *name: SignalName, on_change: bool = False) -> AsyncIterator[list[Signal]]:
 75        """
 76        Subscribe to a list of signals.
 77
 78        Args:
 79            *signals:
 80                One or more signals to subscribe to.
 81            on_change: Whether to receive updates only on change.
 82
 83        Note: does not support decoding enums
 84
 85        Returns:
 86            An asynchronous iterator of lists of Signal objects.
 87        """
 88        return await self._broker_client.subscribe((self.name, list(name)), on_change=on_change, initial_empty=True)
 89
 90    async def subscribe_frames(
 91        self,
 92        *frames: FrameSubscription,
 93        on_change: bool = False,
 94        initial_empty: bool = True,
 95        decode_named_values: bool | None = None,
 96    ) -> AsyncIterator[Frame]:
 97        """
 98        Subscribe to a Frames.
 99
100        Args:
101            *frames: One or more frames to subscribe to.
102            on_change: Whether to receive updates only on change.
103            initial_empty: If `True`, the first broker message (an empty sync marker) is
104                consumed internally before the iterator is returned, ensuring the broker is
105                ready before the first real value arrives. Useful when you need to guarantee
106                the subscription is live before publishing something.
107            decode_named_values: True will decode named values to str. Defaults to the value
108                set in the constructor.
109
110        Returns:
111            An asynchronous iterator with Frames.
112        """
113        decode_values = self._decode_named_values if decode_named_values is None else decode_named_values
114        return await self._broker_client.subscribe_frames(
115            (self.name, list(frames)), on_change=on_change, initial_empty=initial_empty, decode_named_values=decode_values
116        )
117
118    def create_input_handler(
119        self,
120        filters: Sequence[SenderFilter | ReceiverFilter | FrameFilter | AllFramesFilter | SignalFilter],
121        callback: Callable[[Frame], Awaitable[None]],
122    ) -> tuple[NamespaceName, InputHandler]:
123        """
124        Create an input handler for this namespace to be used with a `BehavioralModel`.
125
126        Args:
127            filters: A sequence of filter objects to select relevant frames or signals.
128            callback: An async callback invoked with each matching `Frame`.
129
130        Returns:
131            A `(namespace_name, handler)` pair ready to pass to `BehavioralModel`.
132        """
133        return (self.name, FrameHandler(filters=filters, cb=callback, decode_named_values=self._decode_named_values))
 24class ScriptedNamespace(
 25    Namespace[SenderFilter | ReceiverFilter | FrameFilter | AllFramesFilter | SignalFilter, Callable[[Frame], Awaitable[None]]]
 26):
 27    """
 28    ScriptedNamespace provides access to frames that have been transformed by scripts in the broker.
 29    """
 30
 31    def __init__(
 32        self,
 33        name: NamespaceName,
 34        broker_client: BrokerClient,
 35        decode_named_values: bool = False,
 36    ):
 37        """
 38        Initialize the scripted namespace client.
 39
 40        Args:
 41            name: The namespace name to operate in.
 42            broker_client: The client used to communicate with the broker.
 43            decode_named_values: True will decode named values to str.
 44
 45        Note:
 46            Use together with a `BehavioralModel` or start the instance using a context manager:
 47            ```python
 48            async with ScriptedNamespace(...) as broker_client:
 49                ...
 50            ```
 51        """
 52        super().__init__(name=name, broker_client=broker_client)
 53        self._decode_named_values = decode_named_values
 54
 55    async def _on_open(self, info: NamespaceInfo) -> None:
 56        if info.type != "scripted":
 57            raise ValueError(f"Namespace '{self.name}' is not of type 'scripted'")
 58
 59    async def list_frame_infos(self) -> list[FrameInfo]:
 60        """Return all available frame infos in the namespace."""
 61        return await self._broker_client.list_frame_infos(self.name)
 62
 63    async def get_frame_info(self, name: str) -> FrameInfo | None:
 64        """Return information about a specific frame by name."""
 65        return await self._broker_client.get_frame_info(name, namespace=self.name)
 66
 67    async def list_signal_infos(self) -> list[SignalInfo]:
 68        """Return all available signal infos in the namespace."""
 69        return await self._broker_client.list_signal_infos(self.name)
 70
 71    async def get_signal_info(self, name: SignalName) -> SignalInfo | None:
 72        """Return information about a specific signal by name."""
 73        return await self._broker_client.get_signal_info(name, namespace=self.name)
 74
 75    async def subscribe(self, *name: SignalName, on_change: bool = False) -> AsyncIterator[list[Signal]]:
 76        """
 77        Subscribe to a list of signals.
 78
 79        Args:
 80            *signals:
 81                One or more signals to subscribe to.
 82            on_change: Whether to receive updates only on change.
 83
 84        Note: does not support decoding enums
 85
 86        Returns:
 87            An asynchronous iterator of lists of Signal objects.
 88        """
 89        return await self._broker_client.subscribe((self.name, list(name)), on_change=on_change, initial_empty=True)
 90
 91    async def subscribe_frames(
 92        self,
 93        *frames: FrameSubscription,
 94        on_change: bool = False,
 95        initial_empty: bool = True,
 96        decode_named_values: bool | None = None,
 97    ) -> AsyncIterator[Frame]:
 98        """
 99        Subscribe to a Frames.
100
101        Args:
102            *frames: One or more frames to subscribe to.
103            on_change: Whether to receive updates only on change.
104            initial_empty: If `True`, the first broker message (an empty sync marker) is
105                consumed internally before the iterator is returned, ensuring the broker is
106                ready before the first real value arrives. Useful when you need to guarantee
107                the subscription is live before publishing something.
108            decode_named_values: True will decode named values to str. Defaults to the value
109                set in the constructor.
110
111        Returns:
112            An asynchronous iterator with Frames.
113        """
114        decode_values = self._decode_named_values if decode_named_values is None else decode_named_values
115        return await self._broker_client.subscribe_frames(
116            (self.name, list(frames)), on_change=on_change, initial_empty=initial_empty, decode_named_values=decode_values
117        )
118
119    def create_input_handler(
120        self,
121        filters: Sequence[SenderFilter | ReceiverFilter | FrameFilter | AllFramesFilter | SignalFilter],
122        callback: Callable[[Frame], Awaitable[None]],
123    ) -> tuple[NamespaceName, InputHandler]:
124        """
125        Create an input handler for this namespace to be used with a `BehavioralModel`.
126
127        Args:
128            filters: A sequence of filter objects to select relevant frames or signals.
129            callback: An async callback invoked with each matching `Frame`.
130
131        Returns:
132            A `(namespace_name, handler)` pair ready to pass to `BehavioralModel`.
133        """
134        return (self.name, FrameHandler(filters=filters, cb=callback, decode_named_values=self._decode_named_values))

ScriptedNamespace provides access to frames that have been transformed by scripts in the broker.

ScriptedNamespace( name: str, broker_client: remotivelabs.broker.client.BrokerClient, decode_named_values: bool = False)
31    def __init__(
32        self,
33        name: NamespaceName,
34        broker_client: BrokerClient,
35        decode_named_values: bool = False,
36    ):
37        """
38        Initialize the scripted namespace client.
39
40        Args:
41            name: The namespace name to operate in.
42            broker_client: The client used to communicate with the broker.
43            decode_named_values: True will decode named values to str.
44
45        Note:
46            Use together with a `BehavioralModel` or start the instance using a context manager:
47            ```python
48            async with ScriptedNamespace(...) as broker_client:
49                ...
50            ```
51        """
52        super().__init__(name=name, broker_client=broker_client)
53        self._decode_named_values = decode_named_values

Initialize the scripted namespace client.

Arguments:
  • name: The namespace name to operate in.
  • broker_client: The client used to communicate with the broker.
  • decode_named_values: True will decode named values to str.
Note:

Use together with a BehavioralModel or start the instance using a context manager:

async with ScriptedNamespace(...) as broker_client:
    ...
async def list_frame_infos(self) -> list[remotivelabs.broker.frame.FrameInfo]:
59    async def list_frame_infos(self) -> list[FrameInfo]:
60        """Return all available frame infos in the namespace."""
61        return await self._broker_client.list_frame_infos(self.name)

Return all available frame infos in the namespace.

async def get_frame_info(self, name: str) -> remotivelabs.broker.frame.FrameInfo | None:
63    async def get_frame_info(self, name: str) -> FrameInfo | None:
64        """Return information about a specific frame by name."""
65        return await self._broker_client.get_frame_info(name, namespace=self.name)

Return information about a specific frame by name.

async def list_signal_infos(self) -> list[remotivelabs.broker.signal.SignalInfo]:
67    async def list_signal_infos(self) -> list[SignalInfo]:
68        """Return all available signal infos in the namespace."""
69        return await self._broker_client.list_signal_infos(self.name)

Return all available signal infos in the namespace.

async def get_signal_info(self, name: str) -> remotivelabs.broker.signal.SignalInfo | None:
71    async def get_signal_info(self, name: SignalName) -> SignalInfo | None:
72        """Return information about a specific signal by name."""
73        return await self._broker_client.get_signal_info(name, namespace=self.name)

Return information about a specific signal by name.

async def subscribe( self, *name: str, on_change: bool = False) -> AsyncIterator[list[remotivelabs.broker.signal.Signal]]:
75    async def subscribe(self, *name: SignalName, on_change: bool = False) -> AsyncIterator[list[Signal]]:
76        """
77        Subscribe to a list of signals.
78
79        Args:
80            *signals:
81                One or more signals to subscribe to.
82            on_change: Whether to receive updates only on change.
83
84        Note: does not support decoding enums
85
86        Returns:
87            An asynchronous iterator of lists of Signal objects.
88        """
89        return await self._broker_client.subscribe((self.name, list(name)), on_change=on_change, initial_empty=True)

Subscribe to a list of signals.

Arguments:
  • *signals: One or more signals to subscribe to.
  • on_change: Whether to receive updates only on change.

Note: does not support decoding enums

Returns:

An asynchronous iterator of lists of Signal objects.

async def subscribe_frames( self, *frames: remotivelabs.broker.frame.FrameSubscription, on_change: bool = False, initial_empty: bool = True, decode_named_values: bool | None = None) -> AsyncIterator[remotivelabs.broker.frame.Frame]:
 91    async def subscribe_frames(
 92        self,
 93        *frames: FrameSubscription,
 94        on_change: bool = False,
 95        initial_empty: bool = True,
 96        decode_named_values: bool | None = None,
 97    ) -> AsyncIterator[Frame]:
 98        """
 99        Subscribe to a Frames.
100
101        Args:
102            *frames: One or more frames to subscribe to.
103            on_change: Whether to receive updates only on change.
104            initial_empty: If `True`, the first broker message (an empty sync marker) is
105                consumed internally before the iterator is returned, ensuring the broker is
106                ready before the first real value arrives. Useful when you need to guarantee
107                the subscription is live before publishing something.
108            decode_named_values: True will decode named values to str. Defaults to the value
109                set in the constructor.
110
111        Returns:
112            An asynchronous iterator with Frames.
113        """
114        decode_values = self._decode_named_values if decode_named_values is None else decode_named_values
115        return await self._broker_client.subscribe_frames(
116            (self.name, list(frames)), on_change=on_change, initial_empty=initial_empty, decode_named_values=decode_values
117        )

Subscribe to a Frames.

Arguments:
  • *frames: One or more frames to subscribe to.
  • on_change: Whether to receive updates only on change.
  • initial_empty: If True, the first broker message (an empty sync marker) is consumed internally before the iterator is returned, ensuring the broker is ready before the first real value arrives. Useful when you need to guarantee the subscription is live before publishing something.
  • decode_named_values: True will decode named values to str. Defaults to the value set in the constructor.
Returns:

An asynchronous iterator with Frames.

def create_input_handler( self, filters: Sequence[remotivelabs.broker.filters.SenderFilter | remotivelabs.broker.filters.ReceiverFilter | remotivelabs.broker.filters.FrameFilter | remotivelabs.broker.filters.AllFramesFilter | remotivelabs.broker.filters.SignalFilter], callback: Callable[[remotivelabs.broker.frame.Frame], Awaitable[None]]) -> tuple[str, remotivelabs.topology.namespaces.input_handlers.InputHandler]:
119    def create_input_handler(
120        self,
121        filters: Sequence[SenderFilter | ReceiverFilter | FrameFilter | AllFramesFilter | SignalFilter],
122        callback: Callable[[Frame], Awaitable[None]],
123    ) -> tuple[NamespaceName, InputHandler]:
124        """
125        Create an input handler for this namespace to be used with a `BehavioralModel`.
126
127        Args:
128            filters: A sequence of filter objects to select relevant frames or signals.
129            callback: An async callback invoked with each matching `Frame`.
130
131        Returns:
132            A `(namespace_name, handler)` pair ready to pass to `BehavioralModel`.
133        """
134        return (self.name, FrameHandler(filters=filters, cb=callback, decode_named_values=self._decode_named_values))

Create an input handler for this namespace to be used with a BehavioralModel.

Arguments:
  • filters: A sequence of filter objects to select relevant frames or signals.
  • callback: An async callback invoked with each matching Frame.
Returns:

A (namespace_name, handler) pair ready to pass to BehavioralModel.