Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add substitution when loading yaml files #1354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the patch even a plain value (no file) was being substituted when subst_value was set. After this change substitution seems to only happen on the file content. Is that intentional? This seems to change the existing behavior which would be undesired.

Copy link
Contributor Author

@nano-meter nano-meter Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I am not changing existing behavior, the code that does the change is moved to
https://github.com/nano-meter/ros_comm/blob/a7ed7d8c003f52757b75121ee021b578b4b33a07/tools/roslaunch/src/roslaunch/loader.py#L405

This way both value and file content substitution is handled the same way.
I also created tests for file and value substitution, so if I introduced undesired changes, those probably would have caught it. If you see a problem, please tell me so I could fix it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying. I see now this is only relevant when cmd is load where it is handles in the function. The other commands don't mind about value so it is ok to not substitute it in these cases.


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