Skip to content
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

Created public get_topic_list() function for use in other scripts. #1154

Merged
merged 3 commits into from
Apr 20, 2018

Conversation

EliteMasterEric
Copy link
Contributor

@EliteMasterEric EliteMasterEric commented Aug 24, 2017

A third pull request since #1146 required changes and #1151 was based on kinetic-devel. I would have reopened #1146 but I broke it or something.

Resolves issue #946.

The only function in rostopic for listing the available topics is built exclusively for the command line interface; this adds a public function for other applications that want a list of topics.

Usage:

import rostopic
publishers, subscribers = rostopic.get_topic_list()
for topic, type, nodes in publishers:
    print("Publisher: %s, [%s] (%d nodes)" % (topic, type, len(nodes)))
for topic, type, nodes in subscribers:
    print("Subscriber: %s, [%s] (%d nodes)" % (topic, type, len(nodes)))

Copy link
Member

@dirk-thomas dirk-thomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

publishers, subscribers = rostopic.get_topic_list()
for topic, type, node_count in publishers:

The usage seems to be wrong. As far as I can see the code returns a list of publishers as the third element - not a count?

topic_ns = rosgraph.names.make_global_ns(topic)
subs = (x for x in subs if x[0] == topic or x[0].startswith(topic_ns))
pubs = (x for x in pubs if x[0] == topic or x[0].startswith(topic_ns))
pubs, subs = get_topic_list()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the filtering by the optional topic not needed here anymore?

# test through public function
topics = ['/chatter', '/foo/chatter', '/bar/chatter', '/rosout', '/rosout_agg']

with fakestdout() as b:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary?

topics.sort()
print('\n'.join(["%s%s"%(indent, t) for t in topics]))

def get_topic_list():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to pass in the master since each caller already has a master variable.

if verbose:
topic_types = _master_get_topic_types(master)

if not subscribers_only:
print("\n%sPublished topics:"%indent)
for t, l in pubs:
for t, y, l in pubs:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a different variable name than y.

Same below.

print('')
else:
if publishers_only:
topics = [t for t,_ in pubs]
topics = [t for t,_,_ in pubs]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space after each comma (PEP 8).

Same below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is still pending.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is still pending.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same several times below.

@EliteMasterEric
Copy link
Contributor Author

@dirk-thomas Commits have been made resolving the issues you pushed, and the outdated usage example in the description has been fixed.

Copy link
Member

@dirk-thomas dirk-thomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While new / modified lines should follow PEP8 unchanged lines shouldn't be changed since that makes backporting changes to other branch much more difficult. Please revert any unrelated / unnecessary (whitespace) change in your patch.

topic_ns = rosgraph.names.make_global_ns(topic)
subs = (x for x in subs if x[0] == topic or x[0].startswith(topic_ns))
pubs, subs = get_topic_list(master=master)
if topic is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this condition changed from if topic to if topic is not None?

pubs = (x for x in pubs if x[0] == topic or x[0].startswith(topic_ns))

subs = (x for x in subs if x[0] == topic or x[0].startswith(topic_ns))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not change the order of lines for no reason.

@EliteMasterEric
Copy link
Contributor Author

@dirk-thomas Sorry for the delay, I edited some of the commented lines. Please review these changes and provide feedback.

@EliteMasterEric
Copy link
Contributor Author

@mikepurvis Did you just push a commit with my changes in it rather than merging my pull request with the base branch?

@mikepurvis
Copy link
Member

mikepurvis commented Nov 3, 2017

@mastereric Yes, lunar-edge is a branch that Clearpath uses for our internal development builds, which are tested across a variety of use-cases (robots, simulation, development). Patching changes on to the edge branch is a way for us to build confidence in them before merging to devel.

(Others are welcome to build from edge as well; we're the only current consumer that I'm directly aware of.)

mikepurvis added a commit that referenced this pull request Nov 7, 2017
@mikepurvis
Copy link
Member

I've backed this PR out of our testing. Please see the regression below:

$ roscore &
$ rostopic info /rosout
Traceback (most recent call last):
  File "/opt/clearpath/2.8devel/sdk/bin/rostopic", line 35, in <module>
    rostopic.rostopicmain()
  File "/opt/clearpath/2.8devel/sdk/lib/python2.7/dist-packages/rostopic/__init__.py", line 2111, in rostopicmain
    _rostopic_cmd_info(argv)
  File "/opt/clearpath/2.8devel/sdk/lib/python2.7/dist-packages/rostopic/__init__.py", line 2068, in _rostopic_cmd_info
    exitval = _rostopic_info(topic) or 0
  File "/opt/clearpath/2.8devel/sdk/lib/python2.7/dist-packages/rostopic/__init__.py", line 1302, in _rostopic_info
    print(get_info_text(topic))
  File "/opt/clearpath/2.8devel/sdk/lib/python2.7/dist-packages/rostopic/__init__.py", line 1289, in get_info_text
    for p in itertools.chain(*[l for x, l in subs]):
ValueError: too many values to unpack

@EliteMasterEric
Copy link
Contributor Author

@mikepurvis Fixed the error you're getting, a test looks fine on my machine

@EliteMasterEric
Copy link
Contributor Author

EliteMasterEric commented Feb 26, 2018

Hey @mikepurvis,
The changes in this pull request were merged into lunar-edge back in November. If you require no further changes for them and they appear to function without issues, can you please merge them into the main branch?
Note that another pull request (ros-visualization/rqt#124) is relying on these changes to be implemented properly.

topics.sort()
print('\n'.join(["%s%s"%(indent, t) for t in topics]))

def get_topic_list(master=rosgraph.Master('/rostopic')):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually ran into a small issue with this just today— it's a bit of a corner case, but when ROS_MASTER_URI isn't set, this bombs out on import, for example:

$ unset ROS_MASTER_URI
administrator@vm-mpurvis-xenial-ws:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rostopic
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "workspace/lib/python2.7/dist-packages/rostopic/__init__.py", line 1152, in <module>
    def get_topic_list(master=rosgraph.Master('/rostopic')):
  File "workspace/lib/python2.7/dist-packages/rosgraph/masterapi.py", line 100, in __init__
    self._reinit(master_uri)
  File "workspace/lib/python2.7/dist-packages/rosgraph/masterapi.py", line 113, in _reinit
    raise ValueError("ROS master URI is not set")
ValueError: ROS master URI is not set
>>> 

I believe this will be corrected if you instead do:

def get_topic_list(master=None):
    if not master:
        master = rosgraph.Master('/rostopic')

Other than this, I'd be happy to merge this to mainline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. Please also address the style concern from @dirk-thomas and then this is good to go in.

@EliteMasterEric
Copy link
Contributor Author

@mikepurvis I am apparently blind and didn't notice the leftover spacing fix had yet to be done. The fix has been pushed for review.

@dirk-thomas
Copy link
Member

While new / modified lines should follow PEP8 unchanged lines shouldn't be changed since that makes backporting changes to other branch much more difficult. Please revert any unrelated / unnecessary (whitespace) change in your patch.

This comment still applies.

@EliteMasterEric
Copy link
Contributor Author

@dirk-thomas Sorry for making that mistake again, after my current PRs are done I'll look into making separate PEP8 pull requests for this and other repositories.

@dirk-thomas
Copy link
Member

My previous comment still applies. See the diff should not touch any line just for style adjustments. If the line isn't being changed fo a functional reason in this patch please avoid the change to keep this patch minimal.

after my current PRs are done I'll look into making separate PEP8 pull requests for this and other repositories.

For newly added code we aim to follow common style guides. But don't want to reformat existing code because this repository has currently three (soon four) actively maintained branches and a code reformatting patch would severely hinder the ability to backport changes to other branch.

@EliteMasterEric
Copy link
Contributor Author

@dirk-thomas I have double-checked with the diff and ensured the changes are as requested.

If you have any other issues please cite specific line numbers.

@EliteMasterEric
Copy link
Contributor Author

@dirk-thomas It has been one week since I pushed these changes. Can you please review and approve them? I would like for them to be approved before the Melodic release.

@mikepurvis
Copy link
Member

Please fix the remaining PEP8 issues (spacing around commas).

@mikepurvis
Copy link
Member

Looks great, thanks for this contribution— going to merge into lunar-devel for now, and then it will be part of melodic-devel when we create that branch.

@mikepurvis
Copy link
Member

mikepurvis commented Apr 20, 2018

The failing test on Melodic/debian is unrelated to this change (see #1358)

print(indent+" * %s [%s] %s subscribers"%(t, topic_type(t, topic_types), len(l)))
for t, ttype, tlist in subs:
if len(tlist) > 1:
print(indent+" * %s [%s] %s subscribers"%(t, ttype, len(llist)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llist doesn't exist in this context. See #1407 for a follow up patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants