moveit2
The MoveIt Motion Planning Framework for ROS 2.
xacro.py
Go to the documentation of this file.
1 from pathlib import Path
2 from typing import Iterable, Text, Optional
3 
4 from launch.frontend import expose_substitution
5 from launch.launch_context import LaunchContext
6 from launch.some_substitutions_type import SomeSubstitutionsType
7 from launch.substitution import Substitution
8 from launch.utilities import normalize_to_list_of_substitutions
9 from launch_param_builder import load_xacro
10 
11 
12 @expose_substitution("xacro")
13 class Xacro(Substitution):
14  """Substitution that can access load xacro file with mappings involving any subsititutable."""
15 
16  def __init__(
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__file_path = normalize_to_list_of_substitutions(file_path)
26  if mappings is None:
27  self.__mappings__mappings = {}
28  else:
29  self.__mappings__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__file_path
46 
47  @property
48  def mappings(self) -> dict[SomeSubstitutionsType, SomeSubstitutionsType]:
49  """Getter for mappings."""
50  return self.__mappings__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.mappingsmappings.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__file_path)
66  expanded_mappings = {}
67  for (key, value) in self.__mappings__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
def 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