Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 from string import find
00035
00036 from rosbridge_library.internal import ros_loader
00037
00038
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
00053 return None
00054
00055 if type in specials:
00056
00057 return _get_special_typedef(type)
00058
00059
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
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
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
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
00083 instance = ros_loader.get_service_request_instance(servicetype)
00084 typedef = _get_typedef(instance)
00085
00086
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
00092 instance = ros_loader.get_service_response_instance(servicetype)
00093 typedef = _get_typedef(instance)
00094
00095
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
00109 name = instance.__slots__[i]
00110 fieldnames.append(name)
00111
00112
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
00126 field_instance = getattr(instance, name)
00127 fieldtypes.append(_type_name(field_type, field_instance))
00128
00129
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": ["sec", "nsec"],
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
00163 return []
00164
00165
00166 typesseen.append(type)
00167
00168
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
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
00188 if type in atomics or type in specials:
00189 return type
00190
00191
00192
00193 if isinstance(instance, list):
00194 return type
00195
00196
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