Nav2 Navigation Stack - jazzy  jazzy
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 import tempfile
16 from typing import Dict, List, Optional, Text
17 
18 import launch
19 
20 
21 class ReplaceString(launch.Substitution):
22  """
23  Substitution that replaces strings on a given file.
24 
25  Used in launch system
26  """
27 
28  def __init__(
29  self,
30  source_file: launch.SomeSubstitutionsType,
31  replacements: Dict,
32  condition: Optional[launch.Condition] = None,
33  ) -> None:
34  super().__init__()
35 
36  from launch.utilities import (
37  normalize_to_list_of_substitutions,
38  ) # import here to avoid loop
39 
40  self.__source_file__source_file = normalize_to_list_of_substitutions(source_file)
41  self.__replacements__replacements = {}
42  for key in replacements:
43  self.__replacements__replacements[key] = normalize_to_list_of_substitutions(
44  replacements[key]
45  )
46  self.__condition__condition = condition
47 
48  @property
49  def name(self) -> List[launch.Substitution]:
50  """Getter for name."""
51  return self.__source_file__source_file
52 
53  @property
54  def condition(self) -> Optional[launch.Condition]:
55  """Getter for condition."""
56  return self.__condition__condition
57 
58  def describe(self) -> Text:
59  """Return a description of this substitution as a string."""
60  return ''
61 
62  def perform(self, context: launch.LaunchContext) -> Text:
63  yaml_filename = launch.utilities.perform_substitutions(context, self.namename)
64  if self.__condition__condition is None or self.__condition__condition.evaluate(context):
65  output_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
66  replacements = self.resolve_replacementsresolve_replacements(context)
67  try:
68  input_file = open(yaml_filename, 'r')
69  self.replacereplace(input_file, output_file, replacements)
70  except Exception as err: # noqa: B902
71  print('ReplaceString substitution error: ', err)
72  finally:
73  input_file.close()
74  output_file.close()
75  return output_file.name
76  else:
77  return yaml_filename
78 
79  def resolve_replacements(self, context):
80  resolved_replacements = {}
81  for key in self.__replacements__replacements:
82  resolved_replacements[key] = launch.utilities.perform_substitutions(
83  context, self.__replacements__replacements[key]
84  )
85  return resolved_replacements
86 
87  def replace(self, input_file, output_file, replacements):
88  for line in input_file:
89  for key, value in replacements.items():
90  if isinstance(key, str) and isinstance(value, str):
91  if key in line:
92  line = line.replace(key, value)
93  else:
94  raise TypeError(
95  'A provided replacement pair is not a string. Both key and value should be'
96  'strings.'
97  )
98  output_file.write(line)
def replace(self, input_file, output_file, replacements)
Optional[launch.Condition] condition(self)
List[launch.Substitution] name(self)