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 string import find
00035 
00036 from rosbridge_library.internal import ros_loader
00037 
00038 # Keep track of atomic types and special types
00039 atomics = ['bool', 'byte','int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'float32', 'float64', 'string']
00040 specials = ['time', 'duration']
00041     
00042 
00043 def get_typedef(type):
00044     """ A typedef is a dict containing the following fields:
00045          - string type
00046          - string[] fieldnames
00047          - string[] fieldtypes
00048          - int[] fieldarraylen
00049          - string[] examples 
00050     get_typedef will return a typedef dict for the specified message type """
00051     if type in atomics:
00052         # Atomics don't get a typedef
00053         return None 
00054     
00055     if type in specials:
00056         # Specials get their type def mocked up
00057         return _get_special_typedef(type)
00058     
00059     # Fetch an instance and return its typedef
00060     instance = ros_loader.get_message_instance(type)    
00061     return _get_typedef(instance)
00062 
00063 def get_service_request_typedef(servicetype):
00064     """ Returns a typedef dict for the service request class for the specified service type """
00065     # Get an instance of the service request class and return its typedef
00066     instance = ros_loader.get_service_request_instance(servicetype)
00067     return _get_typedef(instance)
00068 
00069 def get_service_response_typedef(servicetype):
00070     """ Returns a typedef dict for the service response class for the specified service type """
00071     # Get an instance of the service response class and return its typedef 
00072     instance = ros_loader.get_service_response_instance(servicetype)
00073     return _get_typedef(instance)
00074 
00075 def get_typedef_recursive(type):
00076     """ Returns a list of typedef dicts for this type and all contained type fields """
00077     # Just go straight into the recursive method
00078     return _get_typedefs_recursive(type, [])
00079 
00080 def get_service_request_typedef_recursive(servicetype):
00081     """ Returns a list of typedef dicts for this type and all contained type fields """
00082     # Get an instance of the service request class and get its typedef
00083     instance = ros_loader.get_service_request_instance(servicetype)
00084     typedef = _get_typedef(instance)
00085     
00086     # Return the list of sub-typedefs
00087     return _get_subtypedefs_recursive(typedef, [])
00088 
00089 def get_service_response_typedef_recursive(servicetype):
00090     """ Returns a list of typedef dicts for this type and all contained type fields """
00091     # Get an instance of the service response class and get its typedef
00092     instance = ros_loader.get_service_response_instance(servicetype)
00093     typedef = _get_typedef(instance)
00094     
00095     # Return the list of sub-typedefs
00096     return _get_subtypedefs_recursive(typedef, [])
00097 
00098 def _get_typedef(instance):
00099     """ Gets a typedef dict for the specified instance """
00100     if instance is None or not hasattr(instance, "__slots__") or not hasattr(instance, "_slot_types"):
00101         return None
00102     
00103     fieldnames = []
00104     fieldtypes = []
00105     fieldarraylen = []
00106     examples = []
00107     for i in xrange(len(instance.__slots__)):
00108         # Pull out the name
00109         name = instance.__slots__[i]        
00110         fieldnames.append(name)
00111         
00112         # Pull out the type and determine whether it's an array
00113         field_type = instance._slot_types[i]
00114         arraylen = -1
00115         if field_type[-1:]==']':
00116             if field_type[-2:-1]=='[':
00117                 arraylen = 0
00118                 field_type = field_type[:-2]
00119             else:
00120                 split = find(field_type, '[')
00121                 arraylen = int(field_type[split+1:-1])
00122                 field_type = field_type[:split]
00123         fieldarraylen.append(arraylen)
00124         
00125         # Get the fully qualified type
00126         field_instance = getattr(instance, name)
00127         fieldtypes.append(_type_name(field_type, field_instance))
00128         
00129         # Set the example as appropriate
00130         example = field_instance
00131         if arraylen>=0:
00132             example = []
00133         elif field_type not in atomics:
00134             example = {}
00135         examples.append(str(example))
00136     
00137     typedef = {
00138        "type": _type_name_from_instance(instance),
00139        "fieldnames": fieldnames,
00140        "fieldtypes": fieldtypes,
00141        "fieldarraylen": fieldarraylen,
00142        "examples": examples
00143     }
00144     
00145     return typedef
00146     
00147 def _get_special_typedef(type):
00148     example = None
00149     if type=="time" or type=="duration":
00150         example = {
00151             "type": type,
00152             "fieldnames": ["secs", "nsecs"],
00153             "fieldtypes": ["int32", "int32"],
00154             "fieldarraylen": [-1, -1],
00155             "examples": [ "0", "0" ]
00156         }
00157     return example
00158 
00159 def _get_typedefs_recursive(type, typesseen):
00160     """ returns the type def for this type as well as the type defs for any fields within the type """
00161     if type in typesseen:
00162         # Don't put a type if it's already been seen
00163         return []
00164     
00165     # Note that we have now seen this type
00166     typesseen.append(type)
00167     
00168     # Get the typedef for this type and make sure it's not None
00169     typedef = get_typedef(type)
00170     
00171     return _get_subtypedefs_recursive(typedef, typesseen)
00172     
00173 def _get_subtypedefs_recursive(typedef, typesseen):
00174     if typedef is None:
00175         return []
00176     
00177     # Create the list of subtypes and get the typedefs for fields
00178     typedefs = [ typedef ]
00179     for fieldtype in typedef["fieldtypes"]:
00180         typedefs = typedefs + _get_typedefs_recursive(fieldtype, typesseen)
00181         
00182     return typedefs
00183 
00184 def _type_name(type, instance):
00185     """ given a short type, and an object instance of that type, 
00186     determines and returns the fully qualified type """    
00187     # The fully qualified type of atomic and special types is just their original name
00188     if type in atomics or type in specials:
00189         return type
00190             
00191     # If the instance is a list, then we can get no more information from the instance.
00192     # However, luckily, the 'type' field for list types is usually already inflated to the full type.
00193     if isinstance(instance, list):
00194         return type
00195     
00196     # Otherwise, the type will come from the module and class name of the instance                
00197     return _type_name_from_instance(instance)
00198     
00199 def _type_name_from_instance(instance):
00200     mod = instance.__module__
00201     type = mod[0:find(mod, '.')]+"/"+instance.__class__.__name__
00202     return type 
00203     


rosapi
Author(s): Jonathan Mace
autogenerated on Wed Sep 13 2017 03:18:17