1 from pathlib
import Path
2 from typing
import Iterable, Text, Optional
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
12 @expose_substitution("xacro")
14 """Substitution that can access load xacro file with mappings involving any subsititutable."""
18 file_path: SomeSubstitutionsType,
20 mappings: Optional[dict[SomeSubstitutionsType, SomeSubstitutionsType]] =
None,
22 """Create a Xacro substitution."""
25 self.
__file_path__file_path = normalize_to_list_of_substitutions(file_path)
32 def parse(cls, data: Iterable[SomeSubstitutionsType]):
33 """Parse `XacroSubstitution` substitution."""
36 "xacro substitution expects only support one argument use 'command' subsititutoin for parsing args"
39 kwargs[
"file_path"] = data[0]
44 """Getter for file_path."""
48 def mappings(self) -> dict[SomeSubstitutionsType, SomeSubstitutionsType]:
49 """Getter for mappings."""
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()]
57 return f
"Xacro(file_path = {self.file_path}, mappings = {mappings_formatted})"
59 def perform(self, context: LaunchContext) -> Text:
61 Perform the substitution by retrieving the mappings and context.
63 from launch.utilities
import perform_substitutions
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)
71 perform_substitutions(context, normalized_key)
72 ] = perform_substitutions(context, normalized_value)
74 return load_xacro(Path(expanded_file_path), mappings=expanded_mappings)
dict[SomeSubstitutionsType, SomeSubstitutionsType] mappings(self)
Text perform(self, LaunchContext context)
list[Substitution] file_path(self)
def parse(cls, Iterable[SomeSubstitutionsType] data)
None __init__(self, SomeSubstitutionsType file_path, *Optional[dict[SomeSubstitutionsType, SomeSubstitutionsType]] mappings=None)