Go to the documentation of this file.00001
00002 from string import find
00003
00004 from rosbridge_library.internal import ros_loader
00005
00006
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
00021 return None
00022
00023 if type in specials:
00024
00025 return _get_special_typedef(type)
00026
00027
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
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
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
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
00051 instance = ros_loader.get_service_request_instance(servicetype)
00052 typedef = _get_typedef(instance)
00053
00054
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
00060 instance = ros_loader.get_service_response_instance(servicetype)
00061 typedef = _get_typedef(instance)
00062
00063
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
00077 name = instance.__slots__[i]
00078 fieldnames.append(name)
00079
00080
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
00094 field_instance = getattr(instance, name)
00095 fieldtypes.append(_type_name(field_type, field_instance))
00096
00097
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
00131 return []
00132
00133
00134 typesseen.append(type)
00135
00136
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
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
00156 if type in atomics or type in specials:
00157 return type
00158
00159
00160
00161 if isinstance(instance, list):
00162 return type
00163
00164
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