objectutils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2012, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 from rosbridge_library.internal import ros_loader
35 
36 # Keep track of atomic types and special types
37 atomics = ['bool', 'byte','int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'float32', 'float64', 'string']
38 specials = ['time', 'duration']
39 
40 
41 def get_typedef(type):
42  """ A typedef is a dict containing the following fields:
43  - string type
44  - string[] fieldnames
45  - string[] fieldtypes
46  - int[] fieldarraylen
47  - string[] examples
48  get_typedef will return a typedef dict for the specified message type """
49  if type in atomics:
50  # Atomics don't get a typedef
51  return None
52 
53  if type in specials:
54  # Specials get their type def mocked up
55  return _get_special_typedef(type)
56 
57  # Fetch an instance and return its typedef
58  instance = ros_loader.get_message_instance(type)
59  return _get_typedef(instance)
60 
61 def get_service_request_typedef(servicetype):
62  """ Returns a typedef dict for the service request class for the specified service type """
63  # Get an instance of the service request class and return its typedef
64  instance = ros_loader.get_service_request_instance(servicetype)
65  return _get_typedef(instance)
66 
68  """ Returns a typedef dict for the service response class for the specified service type """
69  # Get an instance of the service response class and return its typedef
70  instance = ros_loader.get_service_response_instance(servicetype)
71  return _get_typedef(instance)
72 
74  """ Returns a list of typedef dicts for this type and all contained type fields """
75  # Just go straight into the recursive method
76  return _get_typedefs_recursive(type, [])
77 
79  """ Returns a list of typedef dicts for this type and all contained type fields """
80  # Get an instance of the service request class and get its typedef
81  instance = ros_loader.get_service_request_instance(servicetype)
82  typedef = _get_typedef(instance)
83 
84  # Return the list of sub-typedefs
85  return _get_subtypedefs_recursive(typedef, [])
86 
88  """ Returns a list of typedef dicts for this type and all contained type fields """
89  # Get an instance of the service response class and get its typedef
90  instance = ros_loader.get_service_response_instance(servicetype)
91  typedef = _get_typedef(instance)
92 
93  # Return the list of sub-typedefs
94  return _get_subtypedefs_recursive(typedef, [])
95 
96 def _get_typedef(instance):
97  """ Gets a typedef dict for the specified instance """
98  if instance is None or not hasattr(instance, "__slots__") or not hasattr(instance, "_slot_types"):
99  return None
100 
101  fieldnames = []
102  fieldtypes = []
103  fieldarraylen = []
104  examples = []
105  for i in range(len(instance.__slots__)):
106  # Pull out the name
107  name = instance.__slots__[i]
108  fieldnames.append(name)
109 
110  # Pull out the type and determine whether it's an array
111  field_type = instance._slot_types[i]
112  arraylen = -1
113  if field_type[-1:]==']':
114  if field_type[-2:-1]=='[':
115  arraylen = 0
116  field_type = field_type[:-2]
117  else:
118  split = field_type.find('[')
119  arraylen = int(field_type[split+1:-1])
120  field_type = field_type[:split]
121  fieldarraylen.append(arraylen)
122 
123  # Get the fully qualified type
124  field_instance = getattr(instance, name)
125  fieldtypes.append(_type_name(field_type, field_instance))
126 
127  # Set the example as appropriate
128  example = field_instance
129  if arraylen>=0:
130  example = []
131  elif field_type not in atomics:
132  example = {}
133  examples.append(str(example))
134 
135  typedef = {
136  "type": _type_name_from_instance(instance),
137  "fieldnames": fieldnames,
138  "fieldtypes": fieldtypes,
139  "fieldarraylen": fieldarraylen,
140  "examples": examples
141  }
142 
143  return typedef
144 
146  example = None
147  if type=="time" or type=="duration":
148  example = {
149  "type": type,
150  "fieldnames": ["secs", "nsecs"],
151  "fieldtypes": ["int32", "int32"],
152  "fieldarraylen": [-1, -1],
153  "examples": [ "0", "0" ]
154  }
155  return example
156 
157 def _get_typedefs_recursive(type, typesseen):
158  """ returns the type def for this type as well as the type defs for any fields within the type """
159  if type in typesseen:
160  # Don't put a type if it's already been seen
161  return []
162 
163  # Note that we have now seen this type
164  typesseen.append(type)
165 
166  # Get the typedef for this type and make sure it's not None
167  typedef = get_typedef(type)
168 
169  return _get_subtypedefs_recursive(typedef, typesseen)
170 
171 def _get_subtypedefs_recursive(typedef, typesseen):
172  if typedef is None:
173  return []
174 
175  # Create the list of subtypes and get the typedefs for fields
176  typedefs = [ typedef ]
177  for fieldtype in typedef["fieldtypes"]:
178  typedefs = typedefs + _get_typedefs_recursive(fieldtype, typesseen)
179 
180  return typedefs
181 
182 def _type_name(type, instance):
183  """ given a short type, and an object instance of that type,
184  determines and returns the fully qualified type """
185  # The fully qualified type of atomic and special types is just their original name
186  if type in atomics or type in specials:
187  return type
188 
189  # If the instance is a list, then we can get no more information from the instance.
190  # However, luckily, the 'type' field for list types is usually already inflated to the full type.
191  if isinstance(instance, list):
192  return type
193 
194  # Otherwise, the type will come from the module and class name of the instance
195  return _type_name_from_instance(instance)
196 
198  mod = instance.__module__
199  type = mod[0:mod.find('.')]+"/"+instance.__class__.__name__
200  return type
201 
def _type_name(type, instance)
Definition: objectutils.py:182
def _get_special_typedef(type)
Definition: objectutils.py:145
def _get_typedef(instance)
Definition: objectutils.py:96
def get_service_request_typedef_recursive(servicetype)
Definition: objectutils.py:78
def get_service_request_typedef(servicetype)
Definition: objectutils.py:61
def get_service_response_typedef(servicetype)
Definition: objectutils.py:67
def _type_name_from_instance(instance)
Definition: objectutils.py:197
def _get_subtypedefs_recursive(typedef, typesseen)
Definition: objectutils.py:171
def get_service_response_typedef_recursive(servicetype)
Definition: objectutils.py:87
def get_typedef(type)
Definition: objectutils.py:41
def _get_typedefs_recursive(type, typesseen)
Definition: objectutils.py:157
def get_typedef_recursive(type)
Definition: objectutils.py:73


rosapi
Author(s): Jonathan Mace
autogenerated on Fri May 10 2019 02:17:04