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 rosbridge_library.internal import ros_loader
00035
00036
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
00051 return None
00052
00053 if type in specials:
00054
00055 return _get_special_typedef(type)
00056
00057
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
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
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
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
00081 instance = ros_loader.get_service_request_instance(servicetype)
00082 typedef = _get_typedef(instance)
00083
00084
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
00090 instance = ros_loader.get_service_response_instance(servicetype)
00091 typedef = _get_typedef(instance)
00092
00093
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
00107 name = instance.__slots__[i]
00108 fieldnames.append(name)
00109
00110
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
00124 field_instance = getattr(instance, name)
00125 fieldtypes.append(_type_name(field_type, field_instance))
00126
00127
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
00161 return []
00162
00163
00164 typesseen.append(type)
00165
00166
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
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
00186 if type in atomics or type in specials:
00187 return type
00188
00189
00190
00191 if isinstance(instance, list):
00192 return type
00193
00194
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