objectutils.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2012, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 from rosbridge_library.internal import ros_loader
00035 
00036 # Keep track of atomic types and special types
00037 atomics = ['bool', 'byte','int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'float32', 'float64', 'string']
00038 specials = ['time', 'duration']
00039 
00040 
00041 def get_typedef(type):
00042     """ A typedef is a dict containing the following fields:
00043          - string type
00044          - string[] fieldnames
00045          - string[] fieldtypes
00046          - int[] fieldarraylen
00047          - string[] examples
00048     get_typedef will return a typedef dict for the specified message type """
00049     if type in atomics:
00050         # Atomics don't get a typedef
00051         return None
00052 
00053     if type in specials:
00054         # Specials get their type def mocked up
00055         return _get_special_typedef(type)
00056 
00057     # Fetch an instance and return its typedef
00058     instance = ros_loader.get_message_instance(type)
00059     return _get_typedef(instance)
00060 
00061 def get_service_request_typedef(servicetype):
00062     """ Returns a typedef dict for the service request class for the specified service type """
00063     # Get an instance of the service request class and return its typedef
00064     instance = ros_loader.get_service_request_instance(servicetype)
00065     return _get_typedef(instance)
00066 
00067 def get_service_response_typedef(servicetype):
00068     """ Returns a typedef dict for the service response class for the specified service type """
00069     # Get an instance of the service response class and return its typedef
00070     instance = ros_loader.get_service_response_instance(servicetype)
00071     return _get_typedef(instance)
00072 
00073 def get_typedef_recursive(type):
00074     """ Returns a list of typedef dicts for this type and all contained type fields """
00075     # Just go straight into the recursive method
00076     return _get_typedefs_recursive(type, [])
00077 
00078 def get_service_request_typedef_recursive(servicetype):
00079     """ Returns a list of typedef dicts for this type and all contained type fields """
00080     # Get an instance of the service request class and get its typedef
00081     instance = ros_loader.get_service_request_instance(servicetype)
00082     typedef = _get_typedef(instance)
00083 
00084     # Return the list of sub-typedefs
00085     return _get_subtypedefs_recursive(typedef, [])
00086 
00087 def get_service_response_typedef_recursive(servicetype):
00088     """ Returns a list of typedef dicts for this type and all contained type fields """
00089     # Get an instance of the service response class and get its typedef
00090     instance = ros_loader.get_service_response_instance(servicetype)
00091     typedef = _get_typedef(instance)
00092 
00093     # Return the list of sub-typedefs
00094     return _get_subtypedefs_recursive(typedef, [])
00095 
00096 def _get_typedef(instance):
00097     """ Gets a typedef dict for the specified instance """
00098     if instance is None or not hasattr(instance, "__slots__") or not hasattr(instance, "_slot_types"):
00099         return None
00100 
00101     fieldnames = []
00102     fieldtypes = []
00103     fieldarraylen = []
00104     examples = []
00105     for i in xrange(len(instance.__slots__)):
00106         # Pull out the name
00107         name = instance.__slots__[i]
00108         fieldnames.append(name)
00109 
00110         # Pull out the type and determine whether it's an array
00111         field_type = instance._slot_types[i]
00112         arraylen = -1
00113         if field_type[-1:]==']':
00114             if field_type[-2:-1]=='[':
00115                 arraylen = 0
00116                 field_type = field_type[:-2]
00117             else:
00118                 split = field_type.find('[')
00119                 arraylen = int(field_type[split+1:-1])
00120                 field_type = field_type[:split]
00121         fieldarraylen.append(arraylen)
00122 
00123         # Get the fully qualified type
00124         field_instance = getattr(instance, name)
00125         fieldtypes.append(_type_name(field_type, field_instance))
00126 
00127         # Set the example as appropriate
00128         example = field_instance
00129         if arraylen>=0:
00130             example = []
00131         elif field_type not in atomics:
00132             example = {}
00133         examples.append(str(example))
00134 
00135     typedef = {
00136        "type": _type_name_from_instance(instance),
00137        "fieldnames": fieldnames,
00138        "fieldtypes": fieldtypes,
00139        "fieldarraylen": fieldarraylen,
00140        "examples": examples
00141     }
00142 
00143     return typedef
00144 
00145 def _get_special_typedef(type):
00146     example = None
00147     if type=="time" or type=="duration":
00148         example = {
00149             "type": type,
00150             "fieldnames": ["secs", "nsecs"],
00151             "fieldtypes": ["int32", "int32"],
00152             "fieldarraylen": [-1, -1],
00153             "examples": [ "0", "0" ]
00154         }
00155     return example
00156 
00157 def _get_typedefs_recursive(type, typesseen):
00158     """ returns the type def for this type as well as the type defs for any fields within the type """
00159     if type in typesseen:
00160         # Don't put a type if it's already been seen
00161         return []
00162 
00163     # Note that we have now seen this type
00164     typesseen.append(type)
00165 
00166     # Get the typedef for this type and make sure it's not None
00167     typedef = get_typedef(type)
00168 
00169     return _get_subtypedefs_recursive(typedef, typesseen)
00170 
00171 def _get_subtypedefs_recursive(typedef, typesseen):
00172     if typedef is None:
00173         return []
00174 
00175     # Create the list of subtypes and get the typedefs for fields
00176     typedefs = [ typedef ]
00177     for fieldtype in typedef["fieldtypes"]:
00178         typedefs = typedefs + _get_typedefs_recursive(fieldtype, typesseen)
00179 
00180     return typedefs
00181 
00182 def _type_name(type, instance):
00183     """ given a short type, and an object instance of that type,
00184     determines and returns the fully qualified type """
00185     # The fully qualified type of atomic and special types is just their original name
00186     if type in atomics or type in specials:
00187         return type
00188 
00189     # If the instance is a list, then we can get no more information from the instance.
00190     # However, luckily, the 'type' field for list types is usually already inflated to the full type.
00191     if isinstance(instance, list):
00192         return type
00193 
00194     # Otherwise, the type will come from the module and class name of the instance
00195     return _type_name_from_instance(instance)
00196 
00197 def _type_name_from_instance(instance):
00198     mod = instance.__module__
00199     type = mod[0:mod.find('.')]+"/"+instance.__class__.__name__
00200     return type
00201 


rosapi
Author(s): Jonathan Mace
autogenerated on Thu Jun 6 2019 21:51:49