module documentation

Start a Machine from one TOML file, with no code of the deployer's.

[machine]
name = "Robot"

[machine.identification]
serial_number = "example-0001"
manufacturer = "Example Manufacturer"
product_instance_uri = "urn:example:opensmi-ros2:robot"

[ros2]
domain_id = 42

[[component]]
name = "Platform"

[[component.monitoring]]
name = "Odom"
path = "/odom"
message_type = "nav_msgs/msg/Odometry"

[[component]]
name = "Arm"
parent = "Platform"

The function load_machine reads the file and returns a Machine ready for opensmi's Server.add_machine.

The whole file is checked before anything is built. Every mistake is collected with the path to its place (component[1].monitoring[2].message_type) and raised together as one ~opensmi.ros2.server.errors.FileDeclarationError. The checks reuse the Binding declarations' own validation and the loader of opensmi.ros2.server.config, so a new rule on a Binding applies to the file at once. The Runtime comes from runtime_factory and is made only once the file has passed.

An optional [server] config = "server.toml" is resolved against the file's directory, must exist, and is carried as ConfiguredMachine.server_config for the caller that builds the Server, as opensmi.ros2.cli does. Without it, opensmi's defaults apply.

A Component names its parent with parent (default: the Machine), and a parent comes before its children in the file. A Binding file can be included with bindings_toml.

[[component.projection]] and [[machine.projection]] hold one Projection entry each: recipe (one of RECIPES), type, and the recipe's own parameters. The keys of a recipe are derived from its dataclass, minus fields with no TOML spelling (callables) and minus the functions recipe, which is only callables. Entries register through ~opensmi.ros2.server.projection.ProjectionRegistry.extended in the same layers as in Python (Component, then Machine, then the shipped catalogue), so a second entry for one type is an error unless it says override = true. Every recipe takes override and type_hash. scalar also takes variant_type (a name such as Float, or its number) and datatype (a namespace 0 DataType number), blob takes field and datatype, enum takes fields (a table per field name of number to label) and drop takes fields (a list of field paths):

[[component.projection]]
recipe = "drop"
type = "nav_msgs/msg/Odometry"
fields = ["pose/covariance", "twist/covariance"]

[[component.projection]]
recipe = "enum"
type = "sensor_msgs/msg/BatteryState"

[component.projection.fields.power_supply_status]
0 = "Unknown"
1 = "Charging"

[[component.projection]]
recipe = "scalar"
type = "std_msgs/msg/Float64"
variant_type = "Float"

A file may name the deployer's own classes by import path (package.module.Name or package.module:Name, the colon reaching a nested name): machine_class and component_class, startup_skill_type, driver_type inside a lifecycle table, and the callables of a projection table (recipe = "functions"). Without a key the stock class applies. Importing runs that module's code, so a machine file is trusted configuration. Nothing is imported until the whole file has passed the structural checks. Import errors and classes of the wrong kind are reported at their key.

[machine]
name = "Robot"
machine_class = "my_robot.cell:Cell"
startup_skill_type = "my_robot.cell:CellStartup"

[[component]]
name = "Platform"
component_class = "my_robot.cell:Platform"

[[component.lifecycle]]
name = "Navigation"
path = "/lifecycle_manager"
driver_type = "my_robot.cell:NavigationDriver"

[[component.projection]]
recipe = "functions"
type = "my_msgs/msg/Pose"
shaped_by = "my_robot.curation:pose_shape"
decoded_by = "my_robot.curation:pose_decode"
encode = "my_robot.curation:pose_encode"

machine_class must subclass ConfiguredMachine and component_class ConfiguredComponent; a class with its own __init__ passes unknown keywords on to the base. startup_skill_type must subclass the stock StartupSkill (the lifecycle one on a Component with lifecycle Bindings) and driver_type LifecycleDriver. The functions recipe takes one key per callable (shaped_by, decoded_by and the optional encode); the encode of a blob is named the same way.

Anything beyond Bindings, Projections, identification and these classes stays Python, such as the Skill, Method or goal classes and the message functions of a Binding, and logic between declarations (adding Components in a loop), which belongs in a machine_class.

From Python:

async with Server() as server:
    await server.add_machine(load_machine("machine.toml"))
    await server.start(blocking=True)
Class ConfiguredComponent The stock Component a machine file declares: Bindings, identification and children.
Class ConfiguredMachine The stock Machine a machine file declares: identification, TF Bindings and Components.
Class Identification The identification block of a Machine or Component, as a file gives it.
Function load_machine Read a machine file and return the Machine it declares, ready for Server.add_machine.
Constant RECIPES The recipes a projection table can name. A test fails when a recipe of opensmi.ros2.server.recipes is missing from it.
Class _MachineTable What the [machine] table declares.
Class _Model A file that has passed every check.
Class _Problems The mistakes found so far, in the order the file reaches them, and what waits for an import.
Class _Ref A class or callable the file names by import path, resolved only after the structure passed.
Class _Spec One Component as the file declares it, before anything is built.
Class _Swap A frozen declaration built with stand-ins, rebuilt with the imported objects.
Function _arrange Place each Component under its parent, or report why it cannot be.
Function _build Construct a Component with its children, which are constructed first.
Function _callable Return what is wrong with an imported object that has to be called, if anything.
Function _check_keys Report each key the table has that the model does not know, at the key.
Function _component Read one [[component]] table. Where it sits in the tree is decided afterwards.
Function _declare Build one Binding from its table through the declaration's own checks.
Function _declare_all Build the Bindings the kind tables of one component or machine declare, in file order.
Function _identification Read an identification table. opensmi refuses to start an item that leaves an entry empty.
Function _import Import the object an import path names.
Function _include Read the Binding file a component includes, relative to the machine file.
Function _join Undocumented
Function _layered Read the projection tables of a Machine or Component and register them over a registry.
Function _machine Read the [machine] table.
Function _misplaced Say why a Component's parent is not a place it can go.
Function _projection Build one Projection from its table, through the recipe's own dataclass.
Function _real_runtime Build the real Runtime. Imported here, as rclpy is needed to import it.
Function _recipe_keys Return the keys a recipe's table takes; a callable field is spelled as an import path.
Function _recipe_table_fits Report each key the recipe does not take and each it needs and lacks, and say whether none.
Function _reference Read a key that names a class or callable by import path, checking only its shape.
Function _required_keys Return the keys a recipe's table must carry: its fields that have no default.
Function _resolve Import everything the file names, report what fails, and rebuild the stand-in declarations.
Function _ros2 Read the [ros2] table into the settings the Runtime and the Machine share.
Function _server Read the [server] table: the opensmi server configuration the file points at, which must exist.
Function _stand_in Stand for a callable a file names until it is imported.
Function _string Return a non-empty string value, or report a wrong one at its path (an absent key is silent).
Function _subclass_of Return the check that an imported object is a class derived from base.
Function _swapped Replace each stand-in declaration by its real one.
Function _table Return the value if it is a table, else report it at the path.
Function _unique Check no two Bindings share a name, with the rule a Component applies at init.
Function _validate Read and check the whole file, and raise every mistake in it at once.
Async Function _write_identification Write what the file gave into the item's identification block.
Constant _COMPONENT_IDENTIFICATION A Component's identification block has no product URI; only a Machine's does.
Constant _COMPONENT_KEYS Undocumented
Constant _LOCALE The locale of the manufacturer's text; a file carries one string.
Constant _MACHINE_IDENTIFICATION Undocumented
Constant _MACHINE_KEYS Undocumented
Constant _PATH An import path: dotted names, with an optional : before the attribute (which may be dotted).
Constant _TOP_KEYS Undocumented
Constant _TYPE_KEY The key a table spells a recipe's type_name with.
Type Variable _T Undocumented
def load_machine(path: str | Path, *, runtime_factory: Callable[[ROS2Config], RuntimeInterface] | None = None) -> ConfiguredMachine:

Read a machine file and return the Machine it declares, ready for Server.add_machine.

Parameters
path:str | PathThe TOML file. A relative bindings_toml or [server] config in it is relative to this file.
runtime_factory:Callable[[ROS2Config], RuntimeInterface] | NoneHow to make the Runtime from the file's [ros2] settings, called once and only after the file has passed. None builds the real Runtime, which needs rclpy; a test passes a function returning its fake.
Returns
ConfiguredMachineUndocumented
Raises
FileDeclarationErrorWhen the file has any mistake, with all of them and the path to each. Nothing has been constructed by then.
OSErrorWhen the file cannot be read.
RECIPES: Mapping[str, type[Projection]] =

The recipes a projection table can name. A test fails when a recipe of opensmi.ros2.server.recipes is missing from it.

Value
{'default': Default,
 'scalar': Scalar,
 'enum': Enum,
 'blob': Blob,
 'drop': Drop,
 'functions': Functions}
def _arrange(specs: list[_Spec | None], problems: _Problems) -> list[_Spec]:

Place each Component under its parent, or report why it cannot be.

def _build(spec: _Spec, swaps: Mapping[int, Any]) -> ConfiguredComponent:

Construct a Component with its children, which are constructed first.

def _callable(value: Any) -> str | None:

Return what is wrong with an imported object that has to be called, if anything.

def _check_keys(table: Mapping[str, Any], allowed: Iterable[str], path: str, problems: _Problems):

Report each key the table has that the model does not know, at the key.

def _component(index: int, raw: Any, base: Path, machine: ProjectionRegistry, problems: _Problems) -> _Spec | None:

Read one [[component]] table. Where it sits in the tree is decided afterwards.

def _declare(kind: type[Any], entry: Any, path: str, problems: _Problems) -> Binding | None:

Build one Binding from its table through the declaration's own checks.

def _declare_all(tables: Mapping[str, Any], kinds: Iterable[str], path: str, problems: _Problems) -> list[Binding]:

Build the Bindings the kind tables of one component or machine declare, in file order.

def _identification(raw: Any, path: str, problems: _Problems, *, machine: bool) -> Identification:

Read an identification table. opensmi refuses to start an item that leaves an entry empty.

def _import(text: str) -> Any:

Import the object an import path names.

Raises
LookupErrorWith what went wrong, when the module does not import or lacks the attribute.
def _include(table: Mapping[str, Any], path: str, base: Path, problems: _Problems) -> list[Binding]:

Read the Binding file a component includes, relative to the machine file.

def _join(path: str, key: str | int) -> str:

Undocumented

def _layered(raw: Mapping[str, Any], path: str, below: ProjectionRegistry, problems: _Problems) -> tuple[tuple[Projection, ...], ProjectionRegistry]:

Read the projection tables of a Machine or Component and register them over a registry.

Returns
tuple[tuple[Projection, ...], ProjectionRegistry]The entries that registered, and the registry they extend.
def _machine(raw: Any, problems: _Problems) -> _MachineTable:

Read the [machine] table.

def _misplaced(spec: _Spec, index_of: Mapping[str, int]) -> str:

Say why a Component's parent is not a place it can go.

def _projection(raw: Any, path: str, problems: _Problems) -> Projection | None:

Build one Projection from its table, through the recipe's own dataclass.

def _real_runtime(config: ROS2Config) -> RuntimeInterface:

Build the real Runtime. Imported here, as rclpy is needed to import it.

def _recipe_keys(recipe: type[Projection]) -> tuple[str, ...]:

Return the keys a recipe's table takes; a callable field is spelled as an import path.

def _recipe_table_fits(table: Mapping[str, Any], name: str, path: str, problems: _Problems) -> bool:

Report each key the recipe does not take and each it needs and lacks, and say whether none.

def _reference(table: Mapping[str, Any], key: str, path: str, problems: _Problems, complaint: Callable[[Any], str | None]) -> _Ref | None:

Read a key that names a class or callable by import path, checking only its shape.

def _required_keys(recipe: type[Projection]) -> tuple[str, ...]:

Return the keys a recipe's table must carry: its fields that have no default.

def _resolve(problems: _Problems) -> dict[int, Any]:

Import everything the file names, report what fails, and rebuild the stand-in declarations.

Returns
dict[int, Any]The real declaration for each stand-in, by the stand-in's id.
def _ros2(raw: Any, problems: _Problems) -> ROS2Config:

Read the [ros2] table into the settings the Runtime and the Machine share.

def _server(raw: Any, base: Path, problems: _Problems) -> Path | None:

Read the [server] table: the opensmi server configuration the file points at, which must exist.

def _stand_in(*_args: object, **_kwargs: object) -> Any:

Stand for a callable a file names until it is imported.

def _string(table: Mapping[str, Any], key: str, path: str, problems: _Problems) -> str | None:

Return a non-empty string value, or report a wrong one at its path (an absent key is silent).

def _subclass_of(base: type, why: str) -> Callable[[Any], str | None]:

Return the check that an imported object is a class derived from base.

def _swapped(items: Iterable[_T], swaps: Mapping[int, Any]) -> tuple[_T, ...]:

Replace each stand-in declaration by its real one.

def _table(value: Any, path: str, problems: _Problems) -> dict[str, Any] | None:

Return the value if it is a table, else report it at the path.

def _unique(declared: list[Binding], path: str, problems: _Problems) -> tuple[Binding, ...]:

Check no two Bindings share a name, with the rule a Component applies at init.

def _validate(source: Path) -> _Model:

Read and check the whole file, and raise every mistake in it at once.

async def _write_identification(item: Any, given: Identification):

Write what the file gave into the item's identification block.

_COMPONENT_IDENTIFICATION: tuple[str, ...] =

A Component's identification block has no product URI; only a Machine's does.

Value
('serial_number', 'manufacturer')
_COMPONENT_KEYS =

Undocumented

Value
frozenset(set(['name',
               'parent',
               'identification',
               'bindings_toml',
               'projection',
               'component_class',
               'startup_skill_type',
...
_LOCALE: str =

The locale of the manufacturer's text; a file carries one string.

Value
'en-US'
_MACHINE_IDENTIFICATION: tuple[str, ...] =

Undocumented

Value
('serial_number', 'manufacturer', 'product_instance_uri')
_MACHINE_KEYS =

Undocumented

Value
frozenset(set(['name',
               'identification',
               'tf',
               'projection',
               'machine_class',
               'startup_skill_type']))
_PATH =

An import path: dotted names, with an optional : before the attribute (which may be dotted).

Value
re.compile(r'[^\W\d]\w*(\.[^\W\d]\w*)*(:[^\W\d]\w*(\.[^\W\d]\w*)*)?')
_TOP_KEYS =

Undocumented

Value
frozenset(set(['machine', 'ros2', 'server', 'component']))
_TYPE_KEY: str =

The key a table spells a recipe's type_name with.

Value
'type'
_T =

Undocumented

Value
TypeVar('_T')