16 from typing
import Dict, List, Optional, Text
24 def __init__(self, dictionary, key):
31 def setValue(self, value):
37 Substitution that modifies the given YAML file.
44 source_file: launch.SomeSubstitutionsType,
46 root_key: Optional[launch.SomeSubstitutionsType] =
None,
47 key_rewrites: Optional[Dict] =
None,
52 Construct the substitution
54 :param: source_file the original YAML file to modify
55 :param: param_rewrites mappings to replace
56 :param: root_key if provided, the contents are placed under this key
57 :param: key_rewrites keys of mappings to replace
58 :param: convert_types whether to attempt converting the string to a number or boolean
61 from launch.utilities
import (
62 normalize_to_list_of_substitutions,
65 self.
__source_file__source_file = normalize_to_list_of_substitutions(source_file)
70 for key
in param_rewrites:
71 self.
__param_rewrites__param_rewrites[key] = normalize_to_list_of_substitutions(
74 if key_rewrites
is not None:
75 for key
in key_rewrites:
76 self.
__key_rewrites__key_rewrites[key] = normalize_to_list_of_substitutions(
79 if root_key
is not None:
80 self.
__root_key__root_key = normalize_to_list_of_substitutions(root_key)
83 def name(self) -> List[launch.Substitution]:
84 """Getter for name."""
88 """Return a description of this substitution as a string."""
91 def perform(self, context: launch.LaunchContext) -> Text:
92 yaml_filename = launch.utilities.perform_substitutions(context, self.
namename)
93 rewritten_yaml = tempfile.NamedTemporaryFile(mode=
'w', delete=
False)
94 param_rewrites, keys_rewrites = self.
resolve_rewritesresolve_rewrites(context)
95 data = yaml.safe_load(open(yaml_filename,
'r'))
97 self.
add_paramsadd_params(data, param_rewrites)
100 root_key = launch.utilities.perform_substitutions(context, self.
__root_key__root_key)
102 data = {root_key: data}
103 yaml.dump(data, rewritten_yaml)
104 rewritten_yaml.close()
105 return rewritten_yaml.name
107 def resolve_rewrites(self, context):
110 resolved_params[key] = launch.utilities.perform_substitutions(
115 resolved_keys[key] = launch.utilities.perform_substitutions(
118 return resolved_params, resolved_keys
120 def substitute_params(self, yaml, param_rewrites):
123 if key.key()
in param_rewrites:
124 raw_value = param_rewrites[key.key()]
125 key.setValue(self.
convertconvert(raw_value))
128 yaml_paths = self.
pathifypathify(yaml)
129 for path
in yaml_paths:
130 if path
in param_rewrites:
132 rewrite_val = self.
convertconvert(param_rewrites[path])
133 yaml_keys = path.split(
'.')
136 def add_params(self, yaml, param_rewrites):
138 yaml_paths = self.
pathifypathify(yaml)
139 for path
in param_rewrites:
140 if not path
in yaml_paths:
141 new_val = self.
convertconvert(param_rewrites[path])
142 yaml_keys = path.split(
'.')
143 if 'ros__parameters' in yaml_keys:
146 def updateYamlPathVals(self, yaml, yaml_key_list, rewrite_val):
147 for key
in yaml_key_list:
148 if key == yaml_key_list[-1]:
149 yaml[key] = rewrite_val
151 key = yaml_key_list.pop(0)
152 if isinstance(yaml, list):
154 yaml[int(key)], yaml_key_list, rewrite_val
158 yaml.get(key, {}), yaml_key_list, rewrite_val
162 def substitute_keys(self, yaml, key_rewrites):
163 if len(key_rewrites) != 0:
164 for key
in list(yaml.keys()):
166 if key
in key_rewrites:
167 new_key = key_rewrites[key]
168 yaml[new_key] = yaml[key]
170 if isinstance(val, dict):
173 def getYamlLeafKeys(self, yamlData):
175 for key
in yamlData.keys():
179 except AttributeError:
182 def pathify(self, d, p=None, paths=None, joinchar='.'):
185 self.
pathifypathify(d,
'', paths, joinchar=joinchar)
190 if isinstance(d, dict):
193 self.
pathifypathify(v, str(pn) + str(k), paths, joinchar=joinchar)
194 elif isinstance(d, list):
195 for idx, e
in enumerate(d):
196 self.
pathifypathify(e, pn + str(idx), paths, joinchar=joinchar)
200 def convert(self, text_value):
204 return float(text_value)
if '.' in text_value
else int(text_value)
209 if text_value.lower() ==
'true':
211 if text_value.lower() ==
'false':
def substitute_params(self, yaml, param_rewrites)
def add_params(self, yaml, param_rewrites)
def substitute_keys(self, yaml, key_rewrites)
def updateYamlPathVals(self, yaml, yaml_key_list, rewrite_val)
def pathify(self, d, p=None, paths=None, joinchar='.')
def convert(self, text_value)
List[launch.Substitution] name(self)
def getYamlLeafKeys(self, yamlData)
def resolve_rewrites(self, context)