Skip to content

Commit

Permalink
add "--param-type" option to ros2param list. (#572)
Browse files Browse the repository at this point in the history
* add "--param-type" option to ros2param list.

Signed-off-by: Tomoya.Fujita <[email protected]>
Signed-off-by: Chris Lalancette <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
  • Loading branch information
fujitatomoya and clalancette authored Oct 29, 2020
1 parent 2b6930e commit 0006274
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
16 changes: 16 additions & 0 deletions ros2param/ros2param/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
18 changes: 1 addition & 17 deletions ros2param/ros2param/verb/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
# 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
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.api import ParameterNameCompleter
from ros2param.verb import VerbExtension

Expand Down Expand Up @@ -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]
24 changes: 22 additions & 2 deletions ros2param/ros2param/verb/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -96,8 +101,23 @@ def main(self, *, args): # noqa: D102
if not args.node_name:
print(f'{node_name.full_name}:')
response = future.result()
for name in sorted(response.result.names):
print(f' {name}')
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_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 = name_to_type_map[name]
print(f' {name} (type: {param_type_str})')
else:
print(f' {name}')
else:
e = future.exception()
print(
Expand Down

0 comments on commit 0006274

Please sign in to comment.