Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
has_node_params.py
1 # Copyright (c) 2021 PAL Robotics S.L.
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 launch
16 import yaml
17 
18 
19 class HasNodeParams(launch.Substitution): # type: ignore[misc]
20  """
21  Substitution that checks if a param file contains parameters for a node.
22 
23  Used in launch system
24  """
25 
26  def __init__(
27  self, source_file: launch.SomeSubstitutionsType, node_name: str
28  ) -> None:
29  super().__init__()
30  """
31  Construct the substitution
32 
33  :param: source_file the parameter YAML file
34  :param: node_name the name of the node to check
35  """
36 
37  # import here to avoid loop
38  from launch.utilities import normalize_to_list_of_substitutions
39 
40  self.__source_file: list[launch.Substitution] = \
41  normalize_to_list_of_substitutions(source_file)
42  self.__node_name__node_name = node_name
43 
44  @property
45  def name(self) -> list[launch.Substitution]:
46  """Getter for name."""
47  return self.__source_file
48 
49  def describe(self) -> str:
50  """Return a description of this substitution as a string."""
51  return ''
52 
53  def perform(self, context: launch.LaunchContext) -> str:
54  yaml_filename = launch.utilities.perform_substitutions(context, self.namename)
55  data = yaml.safe_load(open(yaml_filename))
56 
57  if self.__node_name__node_name in data.keys():
58  return 'True'
59  return 'False'