Skip to content

Commit

Permalink
Created public get_topic_list() function for use in other scripts. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepurvis committed Nov 3, 2017
1 parent 42866d9 commit f901429
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
64 changes: 37 additions & 27 deletions tools/rostopic/src/rostopic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,42 +1119,58 @@ 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:
if len(l) > 1:
print(indent+" * %s [%s] %s publishers"%(t, topic_type(t, topic_types), len(l)))
for t, ttype, tlist in pubs:
if len(tlist) > 1:
print(indent+" * %s [%s] %s publishers"%(t, ttype, len(tlist)))
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:
if len(l) > 1:
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)))
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]
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(master=rosgraph.Master('/rostopic')):
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>)
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 +1179,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,15 +1218,11 @@ def _rostopic_list(topic, verbose=False,

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

pubs, subs, _ = state
pubs, subs = get_topic_list(master=master)
if topic:
# filter based on topic
topic_ns = rosgraph.names.make_global_ns(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))

except socket.error:
raise ROSTopicIOException("Unable to communicate with master!")

Expand Down Expand Up @@ -1249,9 +1261,7 @@ def topic_type(t, topic_types):

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

pubs, subs, _ = state
pubs, subs = get_topic_list(master=master)
# 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
8 changes: 7 additions & 1 deletion tools/rostopic/test/test_rostopic.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,20 @@ 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']

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

0 comments on commit f901429

Please sign in to comment.