module documentation

Configuration: a generic dataclass loader, and the ROS2 settings an application injects.

ROS2 settings are their own TOML file and dataclass, not a [ros2] table in the opensmi server configuration, because that tree is closed and refuses keys it does not know. The application loads ROS2Config itself and hands it to the Runtime and the Machine mixin.

A setting lives here and not on a declaration when it is true of the Machine (one ROS2 Domain ID) and not of one object on it. TransformConfig is the case: the transform topics and the TF poll period look like Binding keywords, but one Domain ID has one frame network and one poll. Such a value declared per object is refused and named here instead.

The two settings dataclasses raise ~opensmi.ros2.server.errors.DeclarationError on contradictory values. The loader raises ValueError and TypeError only.

The loader is one function over any dataclass and also reads the Binding TOML of opensmi.ros2.server.bindings. It behaves like opensmi.core.config.load_dataclass, including ValueError on an unknown key, with two differences:

  • It converts a TOML array into a tuple when the field asks for one, since the declarations are frozen and a list would make them unhashable.
  • A TOML file can name no Python class. A field whose type is not a string, number, boolean, time or a container of those (enums and nested dataclasses are built from them) is refused a value, so an editable file cannot run code. Custom implementation classes and callables are Python-side keywords only. The machine file (opensmi.ros2.server.loader) reaches a few by importing a named path and handing the object in through supplied.
Class ROS2Config The ROS2 settings of one Machine, which is the unit of one Domain ID.
Class TransformConfig Where this Machine reads TF from, and how often the frame network is sampled.
Exception FieldError A TOML value does not fit the field it was written under.
Function declarable_keys Return the fields of a dataclass that a TOML table can set, in declaration order.
Function from_dict Build a dataclass instance from a TOML table.
Function load_toml Load a TOML file into a dictionary.
Function python_keys Return the fields of a dataclass that a TOML table cannot set: the complement of declarable_keys.
Constant TF_INTERVAL How many seconds pass between polls of the frame network, unless a deployment says otherwise.
Constant TF_QUIET_AFTER How many seconds a frame may go unpublished before it is quiet, by default.
Class _Unresolved The annotation of a field that cannot be evaluated at run time, so no TOML value fits it.
Function _as_container Return a TOML array or table as the read-only container a frozen declaration annotates.
Function _as_enum Return the enum member a TOML value names, or say which ones exist.
Function _as_tuple Return a TOML array as the tuple a frozen declaration holds.
Function _convert Convert one TOML value into the type its field declares.
Function _convert_key Convert a table key, which TOML always spells as a string, into the type the field wants.
Function _expressible Return whether a TOML document can produce a value of one field's declared type.
Function _file_members Return the members of a union a file can write: not None, and not type beside a name.
Function _hints Return the type hints of a dataclass, field by field where they cannot be had at once.
Function _require_expressible Refuse a key whose field is not something a TOML document can produce a value for.
Function _require_scalar Return a value after checking it is the TOML scalar its field declares.
Constant _TOML_SCALARS Every Python type a TOML document can produce a value of.
Type Variable _T Undocumented
def declarable_keys(data_cls: type) -> tuple[str, ...]:

Return the fields of a dataclass that a TOML table can set, in declaration order.

The complement stays Python-side: a callable or a class has no TOML spelling.

def from_dict(data_cls: type[_T], data: Mapping[str, Any], *, supplied: Mapping[str, Any] | None = None) -> _T:

Build a dataclass instance from a TOML table.

Parameters
data_cls:type[_T]Undocumented
data:Mapping[str, Any]Undocumented
supplied:Mapping[str, Any] | NoneValues for fields a TOML table cannot produce a value of (a class or a callable), which the caller has obtained some other way. They are passed to the constructor as they are. A name in both data and here is the caller's mistake.
Returns
_TUndocumented
Raises
TypeErrorWhen the target is not a dataclass, or a table was expected and something else arrived.
FieldErrorWhen a value does not fit its field: the wrong TOML type, an enum member that does not exist, a nested table that is not one. A ValueError and a TypeError, naming the field.
ValueErrorWhen the table carries a key the dataclass does not declare, or a key whose field a TOML file cannot produce a value for (a string would otherwise sit where a callable belongs).
def load_toml(path: str | Path) -> dict[str, Any]:

Load a TOML file into a dictionary.

def python_keys(data_cls: type) -> tuple[str, ...]:

Return the fields of a dataclass that a TOML table cannot set: the complement of declarable_keys.

A caller that lets a file name one by import path passes the imported object to from_dict as supplied.

TF_INTERVAL: float =

How many seconds pass between polls of the frame network, unless a deployment says otherwise.

Value
1.0
TF_QUIET_AFTER: float =

How many seconds a frame may go unpublished before it is quiet, by default.

This has to clear the slowest publisher on the robot: a diagnostic frame republished every two seconds is healthy. A deployment that knows its publishers can tighten it.

Value
5.0
def _as_container(value: Any, origin: Any, arguments: tuple[Any, ...]) -> Any:

Return a TOML array or table as the read-only container a frozen declaration annotates.

An array becomes a tuple and a table a dict, with its keys converted to the key type.

def _as_enum(value: Any, target: type[Enum]) -> Enum:

Return the enum member a TOML value names, or say which ones exist.

def _as_tuple(value: Any, target: Any) -> tuple[Any, ...]:

Return a TOML array as the tuple a frozen declaration holds.

tuple[str, ...] converts element by element, a fixed-length tuple position by position.

def _convert(value: Any, target: Any) -> Any:

Convert one TOML value into the type its field declares.

def _convert_key(key: Any, target: Any) -> Any:

Convert a table key, which TOML always spells as a string, into the type the field wants.

def _expressible(target: Any) -> bool:

Return whether a TOML document can produce a value of one field's declared type.

object takes any TOML value as it was read, for a field that checks its own shape later.

def _file_members(union: Any) -> list[Any]:

Return the members of a union a file can write: not None, and not type beside a name.

A class alternative (str | type) is the Python spelling of what the file writes as a name.

def _hints(data_cls: type) -> dict[str, Any]:

Return the type hints of a dataclass, field by field where they cannot be had at once.

A field annotated with a type-checker-only alias maps to _Unresolved, a Python-side keyword.

def _require_expressible(data_cls: type, name: str, target: Any):

Refuse a key whose field is not something a TOML document can produce a value for.

Raises
ValueErrorWhen the field's type is not a TOML scalar, an enum, a dataclass, or a container of those (a callable or class would arrive as a string).
def _require_scalar(value: Any, target: Any) -> Any:

Return a value after checking it is the TOML scalar its field declares.

Only bool, int, float and str are checked. An int may stand where a float is wanted, and a bool is never a number.

_TOML_SCALARS =

Every Python type a TOML document can produce a value of.

Value
(str, int, float, bool, dt.datetime, dt.date, dt.time)
_T =

Undocumented

Value
TypeVar('_T')