Skip to content

Commit

Permalink
Fix _list_parameters_callback & test (#1161)
Browse files Browse the repository at this point in the history
Signed-off-by: leeminju531 <[email protected]>
  • Loading branch information
leeminju531 authored Sep 12, 2023
1 parent 0574ad9 commit 0a02395
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
45 changes: 6 additions & 39 deletions rclpy/rclpy/parameter_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

import weakref

from rcl_interfaces.msg import ListParametersResult
from rcl_interfaces.msg import SetParametersResult
from rcl_interfaces.srv import DescribeParameters, GetParameters, GetParameterTypes
from rcl_interfaces.srv import ListParameters, SetParameters, SetParametersAtomically
from rclpy.exceptions import ParameterNotDeclaredException, ParameterUninitializedException
from rclpy.parameter import Parameter, PARAMETER_SEPARATOR_STRING
from rclpy.parameter import Parameter
from rclpy.qos import qos_profile_parameters
from rclpy.validate_topic_name import TOPIC_SEPARATOR_STRING

Expand Down Expand Up @@ -98,45 +99,11 @@ def _get_parameter_types_callback(self, request, response):
return response

def _list_parameters_callback(self, request, response):
names_with_prefixes = []
node = self._get_node()
for name in node._parameters.keys():
if PARAMETER_SEPARATOR_STRING in name:
names_with_prefixes.append(name)
continue
elif request.prefixes:
for prefix in request.prefixes:
if name.startswith(prefix):
response.result.names.append(name)
continue
else:
response.result.names.append(name)
if 1 == request.depth:
return response

if not request.DEPTH_RECURSIVE == request.depth:
names_with_prefixes = filter(
lambda name:
name.count(PARAMETER_SEPARATOR_STRING) < request.depth, names_with_prefixes
)
for name in names_with_prefixes:
if request.prefixes:
for prefix in request.prefixes:
if name.startswith(prefix + PARAMETER_SEPARATOR_STRING):
response.result.names.append(name)
full_prefix = PARAMETER_SEPARATOR_STRING.join(
name.split(PARAMETER_SEPARATOR_STRING)[0:-1])
if full_prefix not in response.result.prefixes:
response.result.prefixes.append(full_prefix)
if prefix not in response.result.prefixes:
response.result.prefixes.append(prefix)
else:
prefix = PARAMETER_SEPARATOR_STRING.join(
name.split(PARAMETER_SEPARATOR_STRING)[0:-1])
if prefix not in response.result.prefixes:
response.result.prefixes.append(prefix)
response.result.names.append(name)

try:
response.result = node.list_parameters(request.prefixes, request.depth)
except (TypeError, ValueError):
response.result = ListParametersResult()
return response

def _set_parameters_callback(self, request, response):
Expand Down
8 changes: 8 additions & 0 deletions rclpy/test/test_parameter_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def test_list_parameters(self):
assert 'int_arr_param' in results.result.names
assert 'float.param..' in results.result.names

future = self.client.list_parameters(['float.param.'], 1)
self.executor.spin_until_future_complete(future)
results = future.result()
assert results is not None
assert 'int_arr_param' not in results.result.names
assert 'float.param..' in results.result.names
assert 'float.param.' in results.result.prefixes

def test_describe_parameters(self):
future = self.client.describe_parameters(['int_arr_param'])
self.executor.spin_until_future_complete(future)
Expand Down

0 comments on commit 0a02395

Please sign in to comment.