moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
xacro.py
Go to the documentation of this file.
1from pathlib import Path
2from typing import Iterable, Text, Optional
3
4from launch.frontend import expose_substitution
5from launch.launch_context import LaunchContext
6from launch.some_substitutions_type import SomeSubstitutionsType
7from launch.substitution import Substitution
8from launch.utilities import normalize_to_list_of_substitutions
9from launch_param_builder import load_xacro
10
11
12@expose_substitution("xacro")
13class Xacro(Substitution):
14 """Substitution that can access load xacro file with mappings involving any subsititutable."""
15
17 self,
18 file_path: SomeSubstitutionsType,
19 *,
20 mappings: Optional[dict[SomeSubstitutionsType, SomeSubstitutionsType]] = None,
21 ) -> None:
22 """Create a Xacro substitution."""
23 super().__init__()
24
25 self.__file_path = normalize_to_list_of_substitutions(file_path)
26 if mappings is None:
27 self.__mappings = {}
28 else:
29 self.__mappings = mappings
30
31 @classmethod
32 def parse(cls, data: Iterable[SomeSubstitutionsType]):
33 """Parse `XacroSubstitution` substitution."""
34 if len(data) != 1:
35 raise TypeError(
36 "xacro substitution expects only support one argument use 'command' subsititutoin for parsing args"
37 )
38 kwargs = {}
39 kwargs["file_path"] = data[0]
40 return cls, kwargs
41
42 @property
43 def file_path(self) -> list[Substitution]:
44 """Getter for file_path."""
45 return self.__file_path
46
47 @property
48 def mappings(self) -> dict[SomeSubstitutionsType, SomeSubstitutionsType]:
49 """Getter for mappings."""
50 return self.__mappings
51
52 def describe(self) -> Text:
53 """Return a description of this substitution as a string."""
54 mappings_formatted = ", ".join(
55 [f"{k.describe()}:={v.describe()}" for k, v in self.mappings.items()]
56 )
57 return f"Xacro(file_path = {self.file_path}, mappings = {mappings_formatted})"
58
59 def perform(self, context: LaunchContext) -> Text:
60 """
61 Perform the substitution by retrieving the mappings and context.
62 """
63 from launch.utilities import perform_substitutions
64
65 expanded_file_path = perform_substitutions(context, self.__file_path)
66 expanded_mappings = {}
67 for key, value in self.__mappings.items():
68 normalized_key = normalize_to_list_of_substitutions(key)
69 normalized_value = normalize_to_list_of_substitutions(value)
70 expanded_mappings[
71 perform_substitutions(context, normalized_key)
72 ] = perform_substitutions(context, normalized_value)
73
74 return load_xacro(Path(expanded_file_path), mappings=expanded_mappings)
dict[SomeSubstitutionsType, SomeSubstitutionsType] mappings(self)
Definition xacro.py:48
Text perform(self, LaunchContext context)
Definition xacro.py:59
list[Substitution] file_path(self)
Definition xacro.py:43
parse(cls, Iterable[SomeSubstitutionsType] data)
Definition xacro.py:32
None __init__(self, SomeSubstitutionsType file_path, *Optional[dict[SomeSubstitutionsType, SomeSubstitutionsType]] mappings=None)
Definition xacro.py:21