From e390641d2be442b398c21aeb9bb0b5e5f031a3ad Mon Sep 17 00:00:00 2001 From: "Tomoya.Fujita" Date: Wed, 28 Oct 2020 22:07:06 +0900 Subject: [PATCH 1/3] add "--param-type" option to ros2param list. Signed-off-by: Tomoya.Fujita --- ros2param/ros2param/api/__init__.py | 16 ++++++++++++++++ ros2param/ros2param/verb/describe.py | 18 +----------------- ros2param/ros2param/verb/list.py | 14 +++++++++++++- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/ros2param/ros2param/api/__init__.py b/ros2param/ros2param/api/__init__.py index cd8a4b29a..47ea94611 100644 --- a/ros2param/ros2param/api/__init__.py +++ b/ros2param/ros2param/api/__init__.py @@ -185,6 +185,22 @@ def call_list_parameters(*, node, node_name, prefix=None): return response.result.names +def get_parameter_type_string(parameter_type): + mapping = { + ParameterType.PARAMETER_BOOL: 'boolean', + ParameterType.PARAMETER_INTEGER: 'integer', + ParameterType.PARAMETER_DOUBLE: 'double', + ParameterType.PARAMETER_STRING: 'string', + ParameterType.PARAMETER_BYTE_ARRAY: 'byte array', + ParameterType.PARAMETER_BOOL_ARRAY: 'boolean array', + ParameterType.PARAMETER_INTEGER_ARRAY: 'integer array', + ParameterType.PARAMETER_DOUBLE_ARRAY: 'double array', + ParameterType.PARAMETER_STRING_ARRAY: 'string array', + ParameterType.PARAMETER_NOT_SET: 'not set', + } + return mapping[parameter_type] + + class ParameterNameCompleter: """Callable returning a list of parameter names.""" diff --git a/ros2param/ros2param/verb/describe.py b/ros2param/ros2param/verb/describe.py index 1cc591dd8..02ef49d17 100644 --- a/ros2param/ros2param/verb/describe.py +++ b/ros2param/ros2param/verb/describe.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from rcl_interfaces.msg import ParameterType from ros2cli.node.direct import DirectNode from ros2cli.node.strategy import add_arguments from ros2cli.node.strategy import NodeStrategy @@ -20,6 +19,7 @@ from ros2node.api import get_node_names from ros2node.api import NodeNameCompleter from ros2param.api import call_describe_parameters +from ros2param.api import get_parameter_type_string from ros2param.api import ParameterNameCompleter from ros2param.verb import VerbExtension @@ -77,19 +77,3 @@ def _print_descriptor(self, descriptor): if descriptor.additional_constraints: print(' Additional constraints:', descriptor.additional_constraints) - - -def get_parameter_type_string(parameter_type): - mapping = { - ParameterType.PARAMETER_BOOL: 'boolean', - ParameterType.PARAMETER_INTEGER: 'integer', - ParameterType.PARAMETER_DOUBLE: 'double', - ParameterType.PARAMETER_STRING: 'string', - ParameterType.PARAMETER_BYTE_ARRAY: 'byte array', - ParameterType.PARAMETER_BOOL_ARRAY: 'boolean array', - ParameterType.PARAMETER_INTEGER_ARRAY: 'integer array', - ParameterType.PARAMETER_DOUBLE_ARRAY: 'double array', - ParameterType.PARAMETER_STRING_ARRAY: 'string array', - ParameterType.PARAMETER_NOT_SET: 'not set', - } - return mapping[parameter_type] diff --git a/ros2param/ros2param/verb/list.py b/ros2param/ros2param/verb/list.py index 77135cdbc..f4fd7d430 100644 --- a/ros2param/ros2param/verb/list.py +++ b/ros2param/ros2param/verb/list.py @@ -22,6 +22,8 @@ from ros2node.api import get_absolute_node_name from ros2node.api import get_node_names from ros2node.api import NodeNameCompleter +from ros2param.api import call_describe_parameters +from ros2param.api import get_parameter_type_string from ros2param.verb import VerbExtension from ros2service.api import get_service_names @@ -41,6 +43,9 @@ def add_arguments(self, parser, cli_name): # noqa: D102 parser.add_argument( '--param-prefixes', nargs='+', default=[], help='Only list parameters with the provided prefixes') + parser.add_argument( + '--param-type', action='store_true', + help='Print parameter types with parameter names') def main(self, *, args): # noqa: D102 with NodeStrategy(args) as node: @@ -97,7 +102,14 @@ def main(self, *, args): # noqa: D102 print(f'{node_name.full_name}:') response = future.result() for name in sorted(response.result.names): - print(f' {name}') + if args.param_type is True: + resp = call_describe_parameters( + node=node, node_name=node_name.full_name, + parameter_names={name}) + param_type_str = get_parameter_type_string(resp.descriptors[0].type) + print(f' {name} [{param_type_str}]') + else: + print(f' {name}') else: e = future.exception() print( From c39ebce0c49697e3e26529a181aac711cf457cab Mon Sep 17 00:00:00 2001 From: "Tomoya.Fujita" Date: Thu, 29 Oct 2020 15:58:02 +0900 Subject: [PATCH 2/3] Get parameter descriptors at once for a node. Signed-off-by: Tomoya.Fujita --- ros2param/ros2param/verb/list.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ros2param/ros2param/verb/list.py b/ros2param/ros2param/verb/list.py index f4fd7d430..8215090c1 100644 --- a/ros2param/ros2param/verb/list.py +++ b/ros2param/ros2param/verb/list.py @@ -101,13 +101,18 @@ def main(self, *, args): # noqa: D102 if not args.node_name: print(f'{node_name.full_name}:') response = future.result() + # get descriptors for the node if needs to print parameter type + if args.param_type is True: + resp = call_describe_parameters( + node=node, node_name=node_name.full_name, + parameter_names=sorted(response.result.names)) for name in sorted(response.result.names): if args.param_type is True: - resp = call_describe_parameters( - node=node, node_name=node_name.full_name, - parameter_names={name}) - param_type_str = get_parameter_type_string(resp.descriptors[0].type) - print(f' {name} [{param_type_str}]') + param_type_str = None + for descriptor in resp.descriptors: + if descriptor.name == name: + param_type_str = get_parameter_type_string(descriptor.type) + print(f' {name} (type: {param_type_str})') else: print(f' {name}') else: From 174dd2fe9c53b1c428ab52593692cf8ffc04d92d Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Thu, 29 Oct 2020 14:50:19 +0000 Subject: [PATCH 3/3] Make type lookup faster. Use a map so we don't have to continually do a search in the inner loop. Signed-off-by: Chris Lalancette --- ros2param/ros2param/verb/list.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ros2param/ros2param/verb/list.py b/ros2param/ros2param/verb/list.py index 8215090c1..8614b8581 100644 --- a/ros2param/ros2param/verb/list.py +++ b/ros2param/ros2param/verb/list.py @@ -101,17 +101,20 @@ def main(self, *, args): # noqa: D102 if not args.node_name: print(f'{node_name.full_name}:') response = future.result() + sorted_names = sorted(response.result.names) # get descriptors for the node if needs to print parameter type + name_to_type_map = {} if args.param_type is True: resp = call_describe_parameters( node=node, node_name=node_name.full_name, - parameter_names=sorted(response.result.names)) - for name in sorted(response.result.names): + parameter_names=sorted_names) + for descriptor in resp.descriptors: + name_to_type_map[descriptor.name] = get_parameter_type_string( + descriptor.type) + + for name in sorted_names: if args.param_type is True: - param_type_str = None - for descriptor in resp.descriptors: - if descriptor.name == name: - param_type_str = get_parameter_type_string(descriptor.type) + param_type_str = name_to_type_map[name] print(f' {name} (type: {param_type_str})') else: print(f' {name}')