00001 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt 00002 # All rights reserved. 00003 # 00004 # Redistribution and use in source and binary forms, with or without 00005 # modification, are permitted provided that the following conditions 00006 # are met: 00007 # 00008 # * Redistributions of source code must retain the above copyright 00009 # notice, this list of conditions and the following disclaimer. 00010 # * Redistributions in binary form must reproduce the above 00011 # copyright notice, this list of conditions and the following 00012 # disclaimer in the documentation and/or other materials provided 00013 # with the distribution. 00014 # * Neither the name of the TU Darmstadt nor the names of its 00015 # contributors may be used to endorse or promote products derived 00016 # from this software without specific prior written permission. 00017 # 00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00021 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00022 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00024 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00026 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00027 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00028 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 # POSSIBILITY OF SUCH DAMAGE. 00030 00031 import roslib 00032 import rospy 00033 00034 00035 class TopicDict(object): 00036 00037 def __init__(self): 00038 self.update_topics() 00039 00040 def get_topics(self): 00041 return self.topic_dict 00042 00043 def update_topics(self): 00044 self.topic_dict = {} 00045 topic_list = rospy.get_published_topics() 00046 for topic_name, topic_type in topic_list: 00047 message = roslib.message.get_message_class(topic_type)() 00048 self.topic_dict.update(self._recursive_create_field_dict(topic_name, message)) 00049 00050 def _recursive_create_field_dict(self, topic_name, field): 00051 field_dict = {} 00052 field_dict[topic_name] = { 00053 'type': type(field), 00054 'children': {}, 00055 } 00056 00057 if hasattr(field, '__slots__'): 00058 for slot_name in field.__slots__: 00059 field_dict[topic_name]['children'].update(self._recursive_create_field_dict(slot_name, getattr(field, slot_name))) 00060 00061 return field_dict 00062 00063 00064 if __name__ == '__main__': 00065 import pprint 00066 pprint.pprint(TopicDict().get_topics())