topic_helpers.py
Go to the documentation of this file.
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 roslib.msgs
00033 from rostopic import get_topic_type
00034 from python_qt_binding.QtCore import qDebug
00035 
00036 def get_type_class(type_name):
00037     if roslib.msgs.is_valid_constant_type(type_name):
00038         if type_name == 'string':
00039             return str
00040         elif type_name == 'bool':
00041             return bool
00042         else:
00043             return type(roslib.msgs._convert_val(type_name, 0))
00044     else:
00045         return roslib.message.get_message_class(type_name)
00046 
00047 def get_field_type(topic_name):
00048     """
00049     Get the Python type of a specific field in the given registered topic.
00050     If the field is an array, the type of the array's values are returned and the is_array flag is set to True.
00051     This is a static type check, so it works for unpublished topics and with empty arrays.
00052 
00053     :param topic_name: name of field of a registered topic, ``str``, i.e. '/rosout/file'
00054     :returns: field_type, is_array
00055     """
00056     # get topic_type and message_evaluator
00057     topic_type, real_topic_name, _ = get_topic_type(topic_name)
00058     if topic_type is None:
00059         #qDebug('topic_helpers.get_field_type(%s): get_topic_type failed' % (topic_name))
00060         return None, False
00061 
00062     message_class = roslib.message.get_message_class(topic_type)
00063     if message_class is None:
00064         qDebug('topic_helpers.get_field_type(%s): get_message_class(%s) failed' % (topic_name, topic_type))
00065         return None, False
00066 
00067     slot_path = topic_name[len(real_topic_name):]
00068     return get_slot_type(message_class, slot_path)
00069 
00070 
00071 def get_slot_type(message_class, slot_path):
00072     """
00073     Get the Python type of a specific slot in the given message class.
00074     If the field is an array, the type of the array's values are returned and the is_array flag is set to True.
00075     This is a static type check, so it works for unpublished topics and with empty arrays.
00076 
00077     :param message_class: message class type, ``type``, usually inherits from genpy.message.Message
00078     :param slot_path: path to the slot inside the message class, ``str``, i.e. 'header/seq'
00079     :returns: field_type, is_array
00080     """
00081     is_array = False
00082     fields = [f for f in slot_path.split('/') if f]
00083     for field_name in fields:
00084         try:
00085             field_name, _, field_index = roslib.msgs.parse_type(field_name)
00086         except roslib.msgs.MsgSpecException:
00087             return None, False
00088         if field_name not in getattr(message_class, '__slots__', []):
00089             #qDebug('topic_helpers.get_slot_type(%s, %s): field not found: %s' % (message_class, slot_path, field_name))
00090             return None, False
00091         slot_type = message_class._slot_types[message_class.__slots__.index(field_name)]
00092         slot_type, slot_is_array, _ = roslib.msgs.parse_type(slot_type)
00093         is_array = slot_is_array and field_index is None
00094 
00095         message_class = get_type_class(slot_type)
00096     return message_class, is_array
00097 
00098 
00099 def is_slot_numeric(topic_name):
00100     """
00101     Check is a slot in the given topic is numeric, or an array of numeric values.
00102     This is a static type check, so it works for unpublished topics and with empty arrays.
00103 
00104     :param topic_name: name of field of a registered topic, ``str``, i.e. '/rosout/file'
00105     :returns: is_numeric, is_array, description
00106     """
00107     field_type, is_array = get_field_type(topic_name)
00108     if field_type in (int, float):
00109         if is_array:
00110             message = 'topic "%s" is numeric array: %s[]' % (topic_name, field_type)
00111         else:
00112             message = 'topic "%s" is numeric: %s' % (topic_name, field_type)
00113         return True, is_array, message
00114 
00115     return False, is_array, 'topic "%s" is NOT numeric: %s' % (topic_name, field_type)


rqt_py_common
Author(s): Dorian Scholz, Isaac Saito
autogenerated on Wed Sep 16 2015 06:58:00