Skip to content

Commit

Permalink
Add verbose info for topic list (#351)
Browse files Browse the repository at this point in the history
* Add help argument for verbose in topic list.

Signed-off-by: evshary <[email protected]>

* Able to show verbose info of topic list.

Signed-off-by: evshary <[email protected]>

* Refactor the code and use function instead.

Signed-off-by: evshary <[email protected]>

* Use underscore to replace unused variables

Co-Authored-By: Claire Wang <[email protected]>
Signed-off-by: evshary <[email protected]>

* Use the same indentation for consistency

Co-Authored-By: Claire Wang <[email protected]>
Signed-off-by: evshary <[email protected]>

* Use NodeStrategy instead of DirectNode for showing topic list.

We also need to register functions for NodeStrategy: count_publishers and
count_subscribers.

Signed-off-by: evshary <[email protected]>

* Remove blank line.

Signed-off-by: ChenYing Kuo <[email protected]>

* Add test for topic list verbose.

Signed-off-by: ChenYing Kuo <[email protected]>

* Restruct the code.

Signed-off-by: ChenYing Kuo <[email protected]>

* Restruct the code.

Signed-off-by: ChenYing Kuo <[email protected]>

Co-authored-by: Claire Wang <[email protected]>
  • Loading branch information
evshary and Claire Wang authored Apr 1, 2021
1 parent 1b60ee3 commit d03ee6c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
39 changes: 33 additions & 6 deletions ros2topic/ros2topic/verb/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,46 @@ def add_arguments(self, parser, cli_name):
parser.add_argument(
'--include-hidden-topics', action='store_true',
help='Consider hidden topics as well')
parser.add_argument(
'-v', '--verbose', action='store_true',
help='List full details about each topic')

@staticmethod
def show_topic_info(topic_info, is_publisher):
message = ('Published' if is_publisher else 'Subscribed') + ' topics:\n'
for (topic_name, topic_types, pub_count, sub_count) in topic_info:
count = pub_count if is_publisher else sub_count
if count:
topic_types_formatted = ', '.join(topic_types)
count_str = str(count) + ' ' + ('publisher' if is_publisher else 'subscriber') \
+ ('s' if count > 1 else '')
message += f' * {topic_name} [{topic_types_formatted}] {count_str}\n'
return message

def main(self, *, args):
topic_info = []
with NodeStrategy(args) as node:
topic_names_and_types = get_topic_names_and_types(
node=node,
include_hidden_topics=args.include_hidden_topics)
for (topic_name, topic_types) in topic_names_and_types:
if args.verbose:
pub_count = node.count_publishers(topic_name)
sub_count = node.count_subscribers(topic_name)
topic_info.append((topic_name, topic_types, pub_count, sub_count))
else:
topic_info.append((topic_name, topic_types, 0, 0))

if args.count_topics:
print(len(topic_names_and_types))
elif topic_names_and_types:
for (topic_name, topic_types) in topic_names_and_types:
msg = f'{topic_name}'
topic_types_formatted = ', '.join(topic_types)
if args.show_types:
msg += f' [{topic_types_formatted}]'
print(msg)
if args.verbose:
print(self.show_topic_info(topic_info, is_publisher=True))
print(self.show_topic_info(topic_info, is_publisher=False))
else:
for (topic_name, topic_types, _, _) in topic_info:
msg = '{topic_name}'
topic_types_formatted = ', '.join(topic_types)
if args.show_types:
msg += ' [{topic_types_formatted}]'
print(msg.format_map(locals()))
25 changes: 25 additions & 0 deletions ros2topic/test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,31 @@ def test_list_with_types(self):
strict=True
)

@launch_testing.markers.retry_on_failure(times=5, delay=1)
def test_list_with_verbose(self):
with self.launch_topic_command(arguments=['list', '-v']) as topic_command:
assert topic_command.wait_for_shutdown(timeout=10)
assert topic_command.exit_code == launch_testing.asserts.EXIT_OK
assert launch_testing.tools.expect_output(
expected_lines=[
'Published topics:',
' * /arrays [test_msgs/msg/Arrays] 1 publisher',
' * /bounded_sequences [test_msgs/msg/BoundedSequences] 1 publisher',
' * /chatter [std_msgs/msg/String] 1 publisher',
' * /cmd_vel [geometry_msgs/msg/TwistStamped] 1 publisher',
' * /defaults [test_msgs/msg/Defaults] 1 publisher',
' * /parameter_events [rcl_interfaces/msg/ParameterEvent] 9 publishers',
' * /rosout [rcl_interfaces/msg/Log] 9 publishers',
' * /unbounded_sequences [test_msgs/msg/UnboundedSequences] 1 publisher',
'',
'Subscribed topics:',
' * /chit_chatter [std_msgs/msg/String] 1 subscriber',
'',
],
text=topic_command.output,
strict=True
)

@launch_testing.markers.retry_on_failure(times=5, delay=1)
def test_list_count(self):
with self.launch_topic_command(arguments=['list', '-c']) as topic_command:
Expand Down

0 comments on commit d03ee6c

Please sign in to comment.