-
Notifications
You must be signed in to change notification settings - Fork 170
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 verbose info for topic list #351
Changes from 6 commits
7eedd6e
3014292
0b14c84
d32b728
5682647
b61a605
a4c438b
8f0e5a3
8d176cb
5713a51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,18 @@ | |
from ros2topic.verb import VerbExtension | ||
|
||
|
||
def show_topic_info(topic_info, isPub): | ||
evshary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print('\n' + ('Published' if isPub else 'Subscribed') + ' topics:') | ||
for (topic_name, topic_types, pub_cnt, sub_cnt) in topic_info: | ||
cnt = pub_cnt if isPub else sub_cnt | ||
if cnt: | ||
topic_types_formatted = ', '.join(topic_types) | ||
cnt_str = str(cnt) + ' ' + ('publisher' if isPub else 'subscriber') \ | ||
+ ('s' if cnt > 1 else '') | ||
msg = ' * {topic_name} [{topic_types_formatted}] {cnt_str}' | ||
print(msg.format_map(locals())) | ||
evshary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class ListVerb(VerbExtension): | ||
"""Output a list of available topics.""" | ||
|
||
|
@@ -34,19 +46,32 @@ 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') | ||
evshary marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps the command should change to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to add this feature in another PR. |
||
|
||
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: | ||
pub_cnt = node.count_publishers(topic_name) | ||
sub_cnt = node.count_subscribers(topic_name) | ||
fujitatomoya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
topic_info.append((topic_name, topic_types, pub_cnt, sub_cnt)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me, having a list of tuples with four elements isn't desirable. In There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why I compute them here is that I need the NodeStrategy. I'm not sure how to pass NodeStrategy to show_topic_info is a better way. def show_topic_info(args, topic_info, isPub):
with NodeStrategy(args) as node:
.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @audrow I add the if-condition to avoid running count_publishers and count_subscribers while there is no verbose. I'm not sure whether it's a better way, but I try to avoid pass args into another function just for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. |
||
|
||
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: | ||
show_topic_info(topic_info, isPub=True) | ||
show_topic_info(topic_info, isPub=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())) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nitpicks
isPub
isn't really Pythonic. Could you dois_publisher
instead? Although, maybe there's a better way to structure this as "subscription" isn't really the opposite of "publisher", as anything else would not be a publisher.cnt
, you can just writecount
.ListVerb
class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you.