15 from typing
import Dict
16 from typing
import List
17 from typing
import Text
18 from typing
import Optional
24 Substitution that replaces strings on a given file.
30 source_file: launch.SomeSubstitutionsType,
32 condition: Optional[launch.Condition] =
None) ->
None:
35 from launch.utilities
import normalize_to_list_of_substitutions
36 self.
__source_file__source_file = normalize_to_list_of_substitutions(source_file)
38 for key
in replacements:
39 self.
__replacements__replacements[key] = normalize_to_list_of_substitutions(replacements[key])
43 def name(self) -> List[launch.Substitution]:
44 """Getter for name."""
49 """Getter for condition."""
53 """Return a description of this substitution as a string."""
56 def perform(self, context: launch.LaunchContext) -> Text:
57 yaml_filename = launch.utilities.perform_substitutions(context, self.
namename)
59 output_file = tempfile.NamedTemporaryFile(mode=
'w', delete=
False)
62 input_file = open(yaml_filename,
'r')
63 self.
replacereplace(input_file, output_file, replacements)
64 except Exception
as err:
65 print(
'ReplaceString substitution error: ', err)
69 return output_file.name
73 def resolve_replacements(self, context):
74 resolved_replacements = {}
76 resolved_replacements[key] = launch.utilities.perform_substitutions(context, self.
__replacements__replacements[key])
77 return resolved_replacements
79 def replace(self, input_file, output_file, replacements):
80 for line
in input_file:
81 for key, value
in replacements.items():
82 if isinstance(key, str)
and isinstance(value, str):
84 line = line.replace(key, value)
86 raise TypeError(
'A provided replacement pair is not a string. Both key and value should be strings.')
87 output_file.write(line)
def resolve_replacements(self, context)
def replace(self, input_file, output_file, replacements)
Optional[launch.Condition] condition(self)
List[launch.Substitution] name(self)