Skip to content

Commit

Permalink
Add substitution when loading yaml files (#1354)
Browse files Browse the repository at this point in the history
* Add substitution when loading yaml files

like in #1051

* Add tests
  • Loading branch information
nano-meter authored and dirk-thomas committed Apr 5, 2018
1 parent 2c8a3f6 commit 2838185
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion tools/roslaunch/src/roslaunch/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def add_param(self, ros_config, param_name, param_value, verbose=True):
else:
ros_config.add_param(Param(param_name, param_value), verbose=verbose)

def load_rosparam(self, context, ros_config, cmd, param, file_, text, verbose=True):
def load_rosparam(self, context, ros_config, cmd, param, file_, text, verbose=True, subst_function=None):
"""
Load rosparam setting
Expand Down Expand Up @@ -401,6 +401,8 @@ def load_rosparam(self, context, ros_config, cmd, param, file_, text, verbose=Tr
with open(file_, 'r') as f:
text = f.read()

if subst_function is not None:
text = subst_function(text)
# parse YAML text
# - lazy import: we have to import rosparam in oder to to configure the YAML constructors
global rosparam
Expand Down
5 changes: 3 additions & 2 deletions tools/roslaunch/src/roslaunch/xmlloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ def _rosparam_tag(self, tag, context, ros_config, verbose=True):
# load is the default command
cmd = cmd or 'load'
value = _get_text(tag)
subst_function = None
if subst_value:
value = self.resolve_args(value, context)
self.load_rosparam(context, ros_config, cmd, param, file, value, verbose=verbose)
subst_function = lambda x: self.resolve_args(x, context)
self.load_rosparam(context, ros_config, cmd, param, file, value, verbose=verbose, subst_function=subst_function)

except ValueError as e:
raise loader.LoadException("error loading <rosparam> tag: \n\t"+str(e)+"\nXML is %s"%tag.toxml())
Expand Down
1 change: 1 addition & 0 deletions tools/roslaunch/test/params_subst.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
string1: $(anon foo)
8 changes: 8 additions & 0 deletions tools/roslaunch/test/unit/test_xmlloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ def test_rosparam_valid(self):
self.assertEquals('bar', p.value)
p = [p for p in mock.params if p.key == '/node_rosparam/robots/childparam'][0]
self.assertEquals('a child namespace parameter', p.value)

# test substitution in yaml files
p = [p for p in mock.params if p.key == '/rosparam_subst/string1'][0]
self.assertTrue('$(anon foo)' not in p.value)

exes = [e for e in mock.executables if e.command == 'rosparam']
self.assertEquals(len(exes), 2, "expected 2 rosparam exes, got %s"%len(exes))
Expand Down Expand Up @@ -274,6 +278,10 @@ def test_rosparam_valid(self):
p = [p for p in mock.params if p.key == '/inline_dict2/key4'][0]
self.assertEquals('value4', p.value)

# test substitution in inline yaml
p = [p for p in mock.params if p.key == '/inline_subst'][0]
self.assertTrue('$(anon foo)' not in p.value)

# verify that later tags override
# - key2 is overriden
self.assertEquals(1, len([p for p in mock.params if p.key == '/override/key1']))
Expand Down
6 changes: 6 additions & 0 deletions tools/roslaunch/test/xml/test-rosparam-valid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
<rosparam file="$(find roslaunch)/test/params.yaml" command="load" />
</node>

<group ns="rosparam_subst">
<rosparam file="$(find roslaunch)/test/params_subst.yaml" command="load" subst_value="true" />
</group>

<rosparam param="inline_str">value1</rosparam>
<rosparam param="inline_list">[1, 2, 3, 4]</rosparam>
<rosparam param="inline_dict">{key1: value1, key2: value2}</rosparam>
<rosparam param="inline_dict2">
key3: value3
key4: value4
</rosparam>

<rosparam param="inline_subst" subst_value="true">$(anon foo)</rosparam>

<rosparam param="override">{key1: value1, key2: value2}</rosparam>
<rosparam param="override">{key1: override1}</rosparam>
Expand Down

0 comments on commit 2838185

Please sign in to comment.