Nav2 Navigation Stack - jazzy  jazzy
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 from typing import List
16 from typing import Text
17 
18 import launch
19 import yaml
20 
21 
22 class HasNodeParams(launch.Substitution):
23  """
24  Substitution that checks if a param file contains parameters for a node.
25 
26  Used in launch system
27  """
28 
29  def __init__(
30  self, source_file: launch.SomeSubstitutionsType, node_name: Text
31  ) -> None:
32  super().__init__()
33  """
34  Construct the substitution
35 
36  :param: source_file the parameter YAML file
37  :param: node_name the name of the node to check
38  """
39 
40  from launch.utilities import (
41  normalize_to_list_of_substitutions,
42  ) # import here to avoid loop
43 
44  self.__source_file__source_file = normalize_to_list_of_substitutions(source_file)
45  self.__node_name__node_name = node_name
46 
47  @property
48  def name(self) -> List[launch.Substitution]:
49  """Getter for name."""
50  return self.__source_file__source_file
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  data = yaml.safe_load(open(yaml_filename, 'r'))
59 
60  if self.__node_name__node_name in data.keys():
61  return 'True'
62  return 'False'