16 from typing
import Optional
23 Substitution that replaces strings on a given file.
30 source_file: launch.SomeSubstitutionsType,
31 replacements: dict[str, launch.SomeSubstitutionsType],
32 condition: Optional[launch.Condition] =
None,
36 from launch.utilities
import normalize_to_list_of_substitutions
40 self.__source_file: list[launch.Substitution] = \
41 normalize_to_list_of_substitutions(source_file)
43 for key
in replacements:
44 self.
__replacements__replacements[key] = normalize_to_list_of_substitutions(
50 def name(self) -> list[launch.Substitution]:
51 """Getter for name."""
52 return self.__source_file
56 """Getter for condition."""
60 """Return a description of this substitution as a string."""
63 def perform(self, context: launch.LaunchContext) -> str:
64 yaml_filename: str = launch.utilities.perform_substitutions(
65 context, self.
namename
68 output_file = tempfile.NamedTemporaryFile(mode=
'w', delete=
False)
71 input_file = open(yaml_filename)
72 self.
replacereplace(input_file, output_file, replacements)
73 except Exception
as err:
74 print(
'ReplaceString substitution error: ', err)
78 return output_file.name
82 def resolve_replacements(self, context: launch.LaunchContext) -> dict[str, str]:
83 resolved_replacements = {}
85 resolved_replacements[key] = launch.utilities.perform_substitutions(
88 return resolved_replacements
90 def replace(self, input_file: launch.SomeSubstitutionsType,
91 output_file: launch.SomeSubstitutionsType, replacements: dict[str, str]) ->
None:
92 for line
in input_file:
93 for key, value
in replacements.items():
94 if isinstance(key, str)
and isinstance(value, str):
96 line = line.replace(key, value)
99 'A provided replacement pair is not a string. Both key and value should be'
102 output_file.write(line)
Optional[launch.Condition] condition(self)
dict[str, str] resolve_replacements(self, launch.LaunchContext context)
list[launch.Substitution] name(self)
None replace(self, launch.SomeSubstitutionsType input_file, launch.SomeSubstitutionsType output_file, dict[str, str] replacements)