objectutils.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 from string import find
00003 
00004 from rosbridge_library.internal import ros_loader
00005 
00006 # Keep track of atomic types and special types
00007 atomics = ['bool', 'byte','int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'float32', 'float64', 'string']
00008 specials = ['time', 'duration']
00009     
00010 
00011 def get_typedef(type):
00012     """ A typedef is a dict containing the following fields:
00013          - string type
00014          - string[] fieldnames
00015          - string[] fieldtypes
00016          - int[] fieldarraylen
00017          - string[] examples 
00018     get_typedef will return a typedef dict for the specified message type """
00019     if type in atomics:
00020         # Atomics don't get a typedef
00021         return None 
00022     
00023     if type in specials:
00024         # Specials get their type def mocked up
00025         return _get_special_typedef(type)
00026     
00027     # Fetch an instance and return its typedef
00028     instance = ros_loader.get_message_instance(type)    
00029     return _get_typedef(instance)
00030 
00031 def get_service_request_typedef(servicetype):
00032     """ Returns a typedef dict for the service request class for the specified service type """
00033     # Get an instance of the service request class and return its typedef
00034     instance = ros_loader.get_service_request_instance(servicetype)
00035     return _get_typedef(instance)
00036 
00037 def get_service_response_typedef(servicetype):
00038     """ Returns a typedef dict for the service response class for the specified service type """
00039     # Get an instance of the service response class and return its typedef 
00040     instance = ros_loader.get_service_response_instance(servicetype)
00041     return _get_typedef(instance)
00042 
00043 def get_typedef_recursive(type):
00044     """ Returns a list of typedef dicts for this type and all contained type fields """
00045     # Just go straight into the recursive method
00046     return _get_typedefs_recursive(type, [])
00047 
00048 def get_service_request_typedef_recursive(servicetype):
00049     """ Returns a list of typedef dicts for this type and all contained type fields """
00050     # Get an instance of the service request class and get its typedef
00051     instance = ros_loader.get_service_request_instance(servicetype)
00052     typedef = _get_typedef(instance)
00053     
00054     # Return the list of sub-typedefs
00055     return _get_subtypedefs_recursive(typedef, [])
00056 
00057 def get_service_response_typedef_recursive(servicetype):
00058     """ Returns a list of typedef dicts for this type and all contained type fields """
00059     # Get an instance of the service response class and get its typedef
00060     instance = ros_loader.get_service_response_instance(servicetype)
00061     typedef = _get_typedef(instance)
00062     
00063     # Return the list of sub-typedefs
00064     return _get_subtypedefs_recursive(typedef, [])
00065 
00066 def _get_typedef(instance):
00067     """ Gets a typedef dict for the specified instance """
00068     if instance is None or not hasattr(instance, "__slots__") or not hasattr(instance, "_slot_types"):
00069         return None
00070     
00071     fieldnames = []
00072     fieldtypes = []
00073     fieldarraylen = []
00074     examples = []
00075     for i in xrange(len(instance.__slots__)):
00076         # Pull out the name
00077         name = instance.__slots__[i]        
00078         fieldnames.append(name)
00079         
00080         # Pull out the type and determine whether it's an array
00081         field_type = instance._slot_types[i]
00082         arraylen = -1
00083         if field_type[-1:]==']':
00084             if field_type[-2:-1]=='[':
00085                 arraylen = 0
00086                 field_type = field_type[:-2]
00087             else:
00088                 split = find(field_type, '[')
00089                 arraylen = int(field_type[split+1:-1])
00090                 field_type = field_type[:split]
00091         fieldarraylen.append(arraylen)
00092         
00093         # Get the fully qualified type
00094         field_instance = getattr(instance, name)
00095         fieldtypes.append(_type_name(field_type, field_instance))
00096         
00097         # Set the example as appropriate
00098         example = field_instance
00099         if arraylen>=0:
00100             example = []
00101         elif field_type not in atomics:
00102             example = {}
00103         examples.append(str(example))
00104     
00105     typedef = {
00106        "type": _type_name_from_instance(instance),
00107        "fieldnames": fieldnames,
00108        "fieldtypes": fieldtypes,
00109        "fieldarraylen": fieldarraylen,
00110        "examples": examples
00111     }
00112     
00113     return typedef
00114     
00115 def _get_special_typedef(type):
00116     example = None
00117     if type=="time" or type=="duration":
00118         example = {
00119             "type": type,
00120             "fieldnames": ["sec", "nsec"],
00121             "fieldtypes": ["int32", "int32"],
00122             "fieldarraylen": [-1, -1],
00123             "examples": [ "0", "0" ]
00124         }
00125     return example
00126 
00127 def _get_typedefs_recursive(type, typesseen):
00128     """ returns the type def for this type as well as the type defs for any fields within the type """
00129     if type in typesseen:
00130         # Don't put a type if it's already been seen
00131         return []
00132     
00133     # Note that we have now seen this type
00134     typesseen.append(type)
00135     
00136     # Get the typedef for this type and make sure it's not None
00137     typedef = get_typedef(type)
00138     
00139     return _get_subtypedefs_recursive(typedef, typesseen)
00140     
00141 def _get_subtypedefs_recursive(typedef, typesseen):
00142     if typedef is None:
00143         return []
00144     
00145     # Create the list of subtypes and get the typedefs for fields
00146     typedefs = [ typedef ]
00147     for fieldtype in typedef["fieldtypes"]:
00148         typedefs = typedefs + _get_typedefs_recursive(fieldtype, typesseen)
00149         
00150     return typedefs
00151 
00152 def _type_name(type, instance):
00153     """ given a short type, and an object instance of that type, 
00154     determines and returns the fully qualified type """    
00155     # The fully qualified type of atomic and special types is just their original name
00156     if type in atomics or type in specials:
00157         return type
00158             
00159     # If the instance is a list, then we can get no more information from the instance.
00160     # However, luckily, the 'type' field for list types is usually already inflated to the full type.
00161     if isinstance(instance, list):
00162         return type
00163     
00164     # Otherwise, the type will come from the module and class name of the instance                
00165     return _type_name_from_instance(instance)
00166     
00167 def _type_name_from_instance(instance):
00168     mod = instance.__module__
00169     type = mod[0:find(mod, '.')]+"/"+instance.__class__.__name__
00170     return type   
00171     


rosapi
Author(s): Jon
autogenerated on Thu Jan 2 2014 11:53:39