Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
replace_string.py
1 # Copyright (c) 2019 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from typing import Dict
16 from typing import List
17 from typing import Text
18 from typing import Optional
19 import tempfile
20 import launch
21 
22 class ReplaceString(launch.Substitution):
23  """
24  Substitution that replaces strings on a given file.
25 
26  Used in launch system
27  """
28 
29  def __init__(self,
30  source_file: launch.SomeSubstitutionsType,
31  replacements: Dict,
32  condition: Optional[launch.Condition] = None) -> None:
33  super().__init__()
34 
35  from launch.utilities import normalize_to_list_of_substitutions # import here to avoid loop
36  self.__source_file__source_file = normalize_to_list_of_substitutions(source_file)
37  self.__replacements__replacements = {}
38  for key in replacements:
39  self.__replacements__replacements[key] = normalize_to_list_of_substitutions(replacements[key])
40  self.__condition__condition = condition
41 
42  @property
43  def name(self) -> List[launch.Substitution]:
44  """Getter for name."""
45  return self.__source_file__source_file
46 
47  @property
48  def condition(self) -> Optional[launch.Condition]:
49  """Getter for condition."""
50  return self.__condition__condition
51 
52  def describe(self) -> Text:
53  """Return a description of this substitution as a string."""
54  return ''
55 
56  def perform(self, context: launch.LaunchContext) -> Text:
57  yaml_filename = launch.utilities.perform_substitutions(context, self.namename)
58  if self.__condition__condition is None or self.__condition__condition.evaluate(context):
59  output_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
60  replacements = self.resolve_replacementsresolve_replacements(context)
61  try:
62  input_file = open(yaml_filename, 'r')
63  self.replacereplace(input_file, output_file, replacements)
64  except Exception as err: # noqa: B902
65  print('ReplaceString substitution error: ', err)
66  finally:
67  input_file.close()
68  output_file.close()
69  return output_file.name
70  else:
71  return yaml_filename
72 
73  def resolve_replacements(self, context):
74  resolved_replacements = {}
75  for key in self.__replacements__replacements:
76  resolved_replacements[key] = launch.utilities.perform_substitutions(context, self.__replacements__replacements[key])
77  return resolved_replacements
78 
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):
83  if key in line:
84  line = line.replace(key, value)
85  else:
86  raise TypeError('A provided replacement pair is not a string. Both key and value should be strings.')
87  output_file.write(line)
def replace(self, input_file, output_file, replacements)
Optional[launch.Condition] condition(self)
List[launch.Substitution] name(self)