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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions tools/rostopic/src/rostopic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,42 +1119,59 @@ def _rostopic_list_bag(bag_file, topic=None):
break

def _sub_rostopic_list(master, pubs, subs, publishers_only, subscribers_only, verbose, indent=''):
def topic_type(t, topic_types):
matches = [t_type for t_name, t_type in topic_types if t_name == t]
if matches:
return matches[0]
return 'unknown type'

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.

if len(l) > 1:
print(indent+" * %s [%s] %s publishers"%(t, topic_type(t, topic_types), len(l)))
print(indent+" * %s [%s] %s publishers"%(t, y, len(l)))
else:
print(indent+" * %s [%s] 1 publisher"%(t, topic_type(t, topic_types)))
print(indent+" * %s [%s] 1 publisher"%(t, y))

if not publishers_only:
print(indent)
print(indent+"Subscribed topics:")
for t,l in subs:
for t, y, l in subs:
if len(l) > 1:
print(indent+" * %s [%s] %s subscribers"%(t, topic_type(t, topic_types), len(l)))
print(indent+" * %s [%s] %s subscribers"%(t, y, len(l)))
else:
print(indent+" * %s [%s] 1 subscriber"%(t, topic_type(t, topic_types)))
print(indent+" * %s [%s] 1 subscriber"%(t, y))
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.

elif subscribers_only:
topics = [t for t,_ in subs]
topics = [t for t,_,_ in subs]
else:
topics = list(set([t for t,_ in pubs] + [t for t,_ in subs]))
topics = list(set([t for t,_,_ in pubs] + [t for t,_,_ in subs]))
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.

def topic_type(t, topic_types):
matches = [t_type for t_name, t_type in topic_types if t_name == t]
if matches:
return matches[0]
return 'unknown type'

# Return an array of tuples; (<topic>, <type>, <node_count>)
master = rosgraph.Master('/rostopic')
state = master.getSystemState()
topic_types = _master_get_topic_types(master)

pubs, subs, _ = state
pubs_out = []
for topic, nodes in pubs:
pubs_out.append((topic, topic_type(topic, topic_types), nodes))
subs_out = []
for topic, nodes in subs:
subs_out.append((topic, topic_type(topic, topic_types), nodes))

# List of published topics, list of subscribed topics.
return (pubs_out, subs_out)

# #3145
def _rostopic_list_group_by_host(master, pubs, subs):
"""
Expand All @@ -1163,7 +1180,7 @@ def _rostopic_list_group_by_host(master, pubs, subs):
"""
def build_map(master, state, uricache):
tmap = {}
for topic, tnodes in state:
for topic, ttype, tnodes in state:
for p in tnodes:
if not p in uricache:
uricache[p] = master.lookupNode(p)
Expand Down Expand Up @@ -1202,14 +1219,7 @@ def _rostopic_list(topic, verbose=False,

master = rosgraph.Master('/rostopic')
try:
state = master.getSystemState()

pubs, subs, _ = state
if topic:
# filter based on topic
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?


except socket.error:
raise ROSTopicIOException("Unable to communicate with master!")
Expand Down Expand Up @@ -1249,9 +1259,7 @@ def topic_type(t, topic_types):

master = rosgraph.Master('/rostopic')
try:
state = master.getSystemState()

pubs, subs, _ = state
pubs, subs = get_topic_list()
# filter based on topic
subs = [x for x in subs if x[0] == topic]
pubs = [x for x in pubs if x[0] == topic]
Expand Down
9 changes: 8 additions & 1 deletion tools/rostopic/test/test_rostopic.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,21 @@ def test_cmd_list(self):
# test main entry
rostopic.rostopicmain([cmd, 'list'])

# test directly
# test through running command
topics = ['/chatter', '/foo/chatter', '/bar/chatter', '/rosout', '/rosout_agg']

with fakestdout() as b:
rostopic.rostopicmain([cmd, 'list'])
v = [x.strip() for x in b.getvalue().split('\n') if x.strip()]
self.failIf(set(topics)-set(v))

# 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?

v = rostopic.get_topic_list()
self.failIf(set(topics)-set(v))

# publishers-only
topics = ['/chatter', '/foo/chatter', '/bar/chatter', '/rosout', '/rosout_agg']
with fakestdout() as b:
Expand Down