remotivelabs.topology.namespaces.factory

Protocol-aware Namespace construction.

Resolves a broker namespace's protocol and builds the matching Namespace — CAN with a restbus, LIN with a frame cache, SOME/IP, scripted, or a plain generic namespace. Shared by remotivelabs.topology.ecu_mock and remotivelabs.topology.mapping, which both need to turn a namespace name into the right protocol-specific output.

 1"""Protocol-aware `Namespace` construction.
 2
 3Resolves a broker namespace's protocol and builds the matching `Namespace` — CAN with a
 4restbus, LIN with a frame cache, SOME/IP, scripted, or a plain generic namespace. Shared by
 5`remotivelabs.topology.ecu_mock` and `remotivelabs.topology.mapping`, which both need to turn a
 6namespace name into the right protocol-specific output.
 7"""
 8
 9from __future__ import annotations
10
11import logging
12from typing import Any
13
14from remotivelabs.broker import BrokerClient
15from remotivelabs.broker.filters import FilterLike
16
17from remotivelabs.topology.namespaces.can import CanNamespace
18from remotivelabs.topology.namespaces.generic import GenericNamespace, RestbusConfig
19from remotivelabs.topology.namespaces.lin import LinNamespace
20from remotivelabs.topology.namespaces.namespace import Namespace
21from remotivelabs.topology.namespaces.scripted import ScriptedNamespace
22from remotivelabs.topology.namespaces.some_ip import SomeIPNamespace
23
24_logger = logging.getLogger(__name__)
25
26
27async def make_namespace(
28    broker_client: BrokerClient,
29    name: str,
30    *,
31    restbus_configs: list[RestbusConfig] | None = None,
32    cache_config: str | list[FilterLike] | None = None,
33    client_id: int = 0,
34) -> Namespace[Any, Any] | None:
35    """
36    Build the protocol-specific `Namespace` for a broker namespace name.
37
38    Resolves the namespace's type via `broker_client.get_namespace(name)` and returns:
39
40    - **CAN** (`can`/`canfd`/`udp`) → `CanNamespace` configured with `restbus_configs`.
41    - **LIN** (`lin`/`udp-lin`) → `LinNamespace` configured with `cache_config`.
42    - **SOME/IP** (`someip`) → `SomeIPNamespace` with `client_id`.
43    - **scripted** → `ScriptedNamespace`.
44    - anything else → `GenericNamespace`.
45
46    `restbus_configs` and `cache_config` are passed only to the namespace type that uses them, so a
47    caller can supply both and let the resolved protocol pick. Returns `None` if the broker has no
48    such namespace.
49    """
50    ns_info = await broker_client.get_namespace(name)
51    if ns_info is None:
52        _logger.warning("Namespace %s not found in broker", name)
53        return None
54
55    _logger.debug("Namespace %s resolved to type %s", name, ns_info.type)
56    if ns_info.type in ("lin", "udp-lin"):
57        return LinNamespace(name=name, broker_client=broker_client, cache_config=cache_config)
58    if ns_info.type in ("can", "canfd", "udp"):
59        return CanNamespace(name=name, broker_client=broker_client, restbus_configs=restbus_configs)
60    if ns_info.type == "someip":
61        return SomeIPNamespace(name=name, broker_client=broker_client, client_id=client_id)
62    if ns_info.type == "scripted":
63        return ScriptedNamespace(name=name, broker_client=broker_client)
64    return GenericNamespace(name=name, broker_client=broker_client)
async def make_namespace( broker_client: remotivelabs.broker.client.BrokerClient, name: str, *, restbus_configs: list[remotivelabs.topology.namespaces.generic.RestbusConfig] | None = None, cache_config: str | list[typing.Union[remotivelabs.broker.filters.FrameFilterPredicate, remotivelabs.broker.filters.SignalFilterPredicate, remotivelabs.broker.filters.FrameSignalFilter]] | None = None, client_id: int = 0) -> Optional[remotivelabs.topology.namespaces.namespace.Namespace[Any, Any]]:
28async def make_namespace(
29    broker_client: BrokerClient,
30    name: str,
31    *,
32    restbus_configs: list[RestbusConfig] | None = None,
33    cache_config: str | list[FilterLike] | None = None,
34    client_id: int = 0,
35) -> Namespace[Any, Any] | None:
36    """
37    Build the protocol-specific `Namespace` for a broker namespace name.
38
39    Resolves the namespace's type via `broker_client.get_namespace(name)` and returns:
40
41    - **CAN** (`can`/`canfd`/`udp`) → `CanNamespace` configured with `restbus_configs`.
42    - **LIN** (`lin`/`udp-lin`) → `LinNamespace` configured with `cache_config`.
43    - **SOME/IP** (`someip`) → `SomeIPNamespace` with `client_id`.
44    - **scripted** → `ScriptedNamespace`.
45    - anything else → `GenericNamespace`.
46
47    `restbus_configs` and `cache_config` are passed only to the namespace type that uses them, so a
48    caller can supply both and let the resolved protocol pick. Returns `None` if the broker has no
49    such namespace.
50    """
51    ns_info = await broker_client.get_namespace(name)
52    if ns_info is None:
53        _logger.warning("Namespace %s not found in broker", name)
54        return None
55
56    _logger.debug("Namespace %s resolved to type %s", name, ns_info.type)
57    if ns_info.type in ("lin", "udp-lin"):
58        return LinNamespace(name=name, broker_client=broker_client, cache_config=cache_config)
59    if ns_info.type in ("can", "canfd", "udp"):
60        return CanNamespace(name=name, broker_client=broker_client, restbus_configs=restbus_configs)
61    if ns_info.type == "someip":
62        return SomeIPNamespace(name=name, broker_client=broker_client, client_id=client_id)
63    if ns_info.type == "scripted":
64        return ScriptedNamespace(name=name, broker_client=broker_client)
65    return GenericNamespace(name=name, broker_client=broker_client)

Build the protocol-specific Namespace for a broker namespace name.

Resolves the namespace's type via broker_client.get_namespace(name) and returns:

  • CAN (can/canfd/udp) → CanNamespace configured with restbus_configs.
  • LIN (lin/udp-lin) → LinNamespace configured with cache_config.
  • SOME/IP (someip) → SomeIPNamespace with client_id.
  • scripted → ScriptedNamespace.
  • anything else → GenericNamespace.

restbus_configs and cache_config are passed only to the namespace type that uses them, so a caller can supply both and let the resolved protocol pick. Returns None if the broker has no such namespace.