/
ros_types.py
841 lines (700 loc) · 24.2 KB
/
ros_types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# -*- coding: utf-8 -*-
#Copyright (c) 2018 André Santos
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
###############################################################################
# Imports
###############################################################################
from builtins import range # Python 2 and 3: forward-compatible
from collections import namedtuple
import importlib
###############################################################################
# Type Tokens
###############################################################################
class TypeToken(object):
__slots__ = ()
@property
def type_name(self):
raise NotImplementedError()
@property
def is_builtin(self):
return False
@property
def is_primitive(self):
return False
@property
def is_number(self):
return False
@property
def is_int(self):
return False
@property
def is_float(self):
return False
@property
def is_bool(self):
return False
@property
def is_string(self):
return False
@property
def is_time(self):
return False
@property
def is_duration(self):
return False
@property
def is_message(self):
return False
@property
def is_header(self):
return False
@property
def is_array(self):
return False
def __eq__(self, other):
if not isinstance(other, TypeToken):
return False
return self.type_name == other.type_name
def __hash__(self):
return hash(self.type_name)
def __str__(self):
return self.type_name
def __repr__(self):
return "{}()".format(type(self).__name__)
class MessageType(TypeToken):
__slots__ = ("_type", "fields", "constants")
def __init__(self, type_name, fields, constants=None):
# type_name :: string
# fields :: dict(string, TypeToken)
# constants :: dict(string, RosLiteral)
self._type = type_name
self.fields = fields
self.constants = constants if constants is not None else {}
@property
def type_name(self):
return self._type
@property
def is_message(self):
return True
@property
def is_builtin(self):
return self.is_header
@property
def is_header(self):
return self._type == HEADER.type_name
@property
def package(self):
return self._type.split("/")[0]
@property
def message(self):
return self._type.split("/")[-1]
def leaf_fields(self, name="msg"):
primitives = {}
arrays = {}
stack = [(name, self)]
while stack:
name, type_token = stack.pop()
if type_token.is_message:
for field_name, field_type in type_token.fields.items():
n = "{}.{}".format(name, field_name)
stack.append((n, field_type))
elif type_token.is_array:
arrays[name] = type_token
else:
assert type_token.is_primitive
primitives[name] = type_token
return primitives, arrays
def __repr__(self):
return "{}({}, {}, constants={})".format(type(self).__name__,
repr(self.type_name), repr(self.fields), repr(self.constants))
class ArrayType(TypeToken):
__slots__ = ("type_token", "length")
def __init__(self, type_token, length=None):
# type_token :: TypeToken
# length :: int >= 0
self.type_token = type_token
self.length = length
@property
def type_name(self):
return self.type_token.type_name
@property
def is_builtin(self):
return self.type_token.is_builtin
@property
def is_primitive(self):
return self.type_token.is_primitive
@property
def is_number(self):
return self.type_token.is_number
@property
def is_int(self):
return self.type_token.is_int
@property
def is_float(self):
return self.type_token.is_float
@property
def is_bool(self):
return self.type_token.is_bool
@property
def is_string(self):
return self.type_token.is_string
@property
def is_time(self):
return self.type_token.is_time
@property
def is_duration(self):
return self.type_token.is_duration
@property
def is_header(self):
return self.type_token.is_header
@property
def is_message(self):
return False
@property
def is_array(self):
return True
@property
def is_fixed_length(self):
return self.length is not None
def contains_index(self, index):
return (index >= 0 and (self.length is None or index < self.length))
def __eq__(self, other):
if not isinstance(other, ArrayType):
return False
return (self.type_token == other.type_token
and self.length == other.length)
def __hash__(self):
return 31 * hash(self.type_token) + hash(self.length)
def __str__(self):
return "{}[{}]".format(self.type_token,
"" if self.length is None else self.length)
def __repr__(self):
return "{}({}, length={})".format(type(self).__name__,
repr(self.type_token), repr(self.length))
###############################################################################
# Builtin Type Tokens
###############################################################################
_TOKEN_PROPERTIES = ("type_name", "is_builtin", "is_primitive", "is_number",
"is_int", "is_float", "is_bool", "is_string", "is_time",
"is_duration", "is_header", "is_message", "is_array")
BuiltinPlainTypeToken = namedtuple("BuiltinPlainTypeToken", _TOKEN_PROPERTIES)
BuiltinPlainTypeToken.__bases__ = (TypeToken,) + BuiltinPlainTypeToken.__bases__
_NUM_PROPERTIES = _TOKEN_PROPERTIES + ("min_value", "max_value")
BuiltinNumTypeToken = namedtuple("BuiltinNumTypeToken", _NUM_PROPERTIES)
BuiltinNumTypeToken.__bases__ = (TypeToken,) + BuiltinNumTypeToken.__bases__
_MSG_PROPERTIES = _TOKEN_PROPERTIES + ("fields", "constants")
BuiltinMsgTypeToken = namedtuple("BuiltinMsgTypeToken", _MSG_PROPERTIES)
BuiltinMsgTypeToken.__bases__ = (TypeToken,) + BuiltinMsgTypeToken.__bases__
BOOL = BuiltinNumTypeToken(
"bool", # type_name
True, # is_builtin
True, # is_primitive
False, # is_number
False, # is_int
False, # is_float
True, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
0, # min_value
1 # max_value
)
UINT8 = BuiltinNumTypeToken(
"uint8", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
0, # min_value
(2 ** 8) - 1 # max_value
)
CHAR = UINT8
UINT16 = BuiltinNumTypeToken(
"uint16", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
0, # min_value
(2 ** 16) - 1 # max_value
)
UINT32 = BuiltinNumTypeToken(
"uint32", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
0, # min_value
(2 ** 32) - 1 # max_value
)
UINT64 = BuiltinNumTypeToken(
"uint64", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
0, # min_value
(2 ** 64) - 1 # max_value
)
INT8 = BuiltinNumTypeToken(
"int8", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
-(2 ** 7), # min_value
(2 ** 7) - 1 # max_value
)
BYTE = INT8
INT16 = BuiltinNumTypeToken(
"int16", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
-(2 ** 15), # min_value
(2 ** 15) - 1 # max_value
)
INT32 = BuiltinNumTypeToken(
"int32", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
-(2 ** 31), # min_value
(2 ** 31) - 1 # max_value
)
INT64 = BuiltinNumTypeToken(
"int64", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
True, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
-(2 ** 63), # min_value
(2 ** 63) - 1 # max_value
)
FLOAT32 = BuiltinNumTypeToken(
"float32", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
False, # is_int
True, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
-3.3999999521443642e+38, # min_value
3.3999999521443642e+38 # max_value
)
FLOAT64 = BuiltinNumTypeToken(
"float64", # type_name
True, # is_builtin
True, # is_primitive
True, # is_number
False, # is_int
True, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False, # is_array
-1.7E+308, # min_value
1.7E+308 # max_value
)
STRING = BuiltinPlainTypeToken(
"string", # type_name
True, # is_builtin
True, # is_primitive
False, # is_number
False, # is_int
False, # is_float
False, # is_bool
True, # is_string
False, # is_time
False, # is_duration
False, # is_header
False, # is_message
False # is_array
)
TIME = BuiltinMsgTypeToken(
"time", # type_name
True, # is_builtin
False, # is_primitive
False, # is_number
False, # is_int
False, # is_float
False, # is_bool
False, # is_string
True, # is_time
False, # is_duration
False, # is_header
True, # is_message
False, # is_array
{ # fields
"secs": UINT32,
"nsecs": UINT32
},
{} # constants
)
DURATION = BuiltinMsgTypeToken(
"duration", # type_name
True, # is_builtin
False, # is_primitive
False, # is_number
False, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
True, # is_duration
False, # is_header
True, # is_message
False, # is_array
{ # fields
"secs": INT32,
"nsecs": INT32
},
{} # constants
)
HEADER = BuiltinMsgTypeToken(
"std_msgs/Header", # type_name
True, # is_builtin
False, # is_primitive
False, # is_number
False, # is_int
False, # is_float
False, # is_bool
False, # is_string
False, # is_time
False, # is_duration
True, # is_header
True, # is_message
False, # is_array
{ # fields
"seq": UINT32,
"stamp": TIME,
"frame_id": STRING
},
{} # constants
)
###############################################################################
# Type Constants
###############################################################################
ROS_BUILTIN_TYPES = (BOOL, UINT8, INT8, UINT16, INT16, UINT32, INT32, UINT64,
INT64, FLOAT32, FLOAT64, STRING, CHAR, BYTE, TIME,
DURATION, HEADER)
ROS_PRIMITIVE_TYPES = (BOOL, UINT8, INT8, UINT16, INT16, UINT32, INT32, UINT64,
INT64, FLOAT32, FLOAT64, STRING, CHAR, BYTE)
ROS_NUMBER_TYPES = (UINT8, INT8, UINT16, INT16, UINT32, INT32, UINT64, INT64,
FLOAT32, FLOAT64, CHAR, BYTE)
ROS_INT_TYPES = (UINT8, INT8, UINT16, INT16, UINT32, INT32, UINT64, INT64,
CHAR, BYTE)
ROS_FLOAT_TYPES = (FLOAT32, FLOAT64)
ROS_STRING_TYPES = (STRING,)
ROS_BOOLEAN_TYPES = (BOOL,)
UINT8_COMPATIBLE = (UINT8, CHAR)
UINT16_COMPATIBLE = (UINT8, CHAR, UINT16)
UINT32_COMPATIBLE = (UINT8, CHAR, UINT16, UINT32)
UINT64_COMPATIBLE = (UINT8, CHAR, UINT16, UINT32, UINT64)
INT8_COMPATIBLE = (INT8, BYTE)
INT16_COMPATIBLE = (UINT8, CHAR, INT8, BYTE, INT16)
INT32_COMPATIBLE = (UINT8, CHAR, INT8, BYTE, UINT16, INT16, INT32)
INT64_COMPATIBLE = (UINT8, CHAR, INT8, BYTE, UINT16,
INT16, UINT32, INT32, INT64)
FLOAT32_COMPATIBLE = (UINT8, CHAR, INT8, BYTE, UINT16, INT16, FLOAT32)
FLOAT64_COMPATIBLE = (UINT8, INT8, UINT16, INT16, UINT32,
INT32, CHAR, BYTE, FLOAT32, FLOAT64)
###############################################################################
# Value Wrappers
###############################################################################
class RosValue(object):
__slots__ = ()
def __init__(self):
raise NotImplementedError()
@property
def ros_type(self):
raise NotImplementedError()
@property
def type_name(self):
raise NotImplementedError()
@property
def is_dynamic(self):
raise NotImplementedError()
class RosLiteral(RosValue):
__slots__ = ("value",)
@property
def is_dynamic(self):
return False
def __eq__(self, other):
if not isinstance(other, RosLiteral):
return False
return self.value == other.value
def __hash__(self):
return hash(self.value)
def __str__(self):
return str(self.value)
def __repr__(self):
return "{}({})".format(type(self).__name__, repr(self.value))
class RosBool(RosLiteral):
__slots__ = RosLiteral.__slots__
def __init__(self, value):
if isinstance(value, int):
if value != 0 and value != 1:
raise ValueError("invalid bool conversion: " + repr(value))
if not isinstance(value, bool):
raise TypeError("expected a bool value: " + repr(value))
self.value = value
@property
def ros_type(self):
return BOOL
@property
def type_name(self):
return BOOL.type_name
class RosInt(RosLiteral):
__slots__ = RosLiteral.__slots__ + ("_type",)
def __init__(self, value, type_token):
if not type_token in ROS_INT_TYPES:
raise ValueError("expected an int type: " + repr(type_token))
if (value is True or value is False
or not isinstance(value, (int, long))):
raise TypeError("expected an int value: " + repr(value))
if value < type_token.min_value or value > type_token.max_value:
raise ValueError("value out of range: " + repr(value))
self.value = value
self._type = type_token
@property
def ros_type(self):
return self._type
@property
def type_name(self):
return self._type.type_name
def __repr__(self):
return "{}({}, {})".format(type(self).__name__, repr(self.value),
repr(self._type))
class RosFloat(RosLiteral):
__slots__ = RosLiteral.__slots__ + ("_type",)
def __init__(self, value, type_token):
if not type_token in ROS_FLOAT_TYPES:
raise ValueError("expected a float type: " + repr(type_token))
if (value is True or value is False
or not isinstance(value, (float, int, long))):
raise TypeError("expected a float value: " + repr(value))
if value < type_token.min_value or value > type_token.max_value:
raise ValueError("value out of range: " + repr(value))
self.value = value
self._type = type_token
@property
def ros_type(self):
return self._type
@property
def type_name(self):
return self._type.type_name
def __repr__(self):
return "{}({}, {})".format(type(self).__name__, repr(self.value),
repr(self._type))
class RosString(RosLiteral):
__slots__ = RosLiteral.__slots__
def __init__(self, value):
if not isinstance(value, basestring):
raise TypeError("expected a string value: " + repr(value))
self.value = value
@property
def ros_type(self):
return STRING
@property
def type_name(self):
return STRING.type_name
###############################################################################
# Type Checking
###############################################################################
def compatible_types(expected_type, type_token):
assert isinstance(expected_type, TypeToken)
assert isinstance(type_token, TypeToken)
if expected_type is UINT8 or expected_type is CHAR:
return type_token in UINT8_COMPATIBLE
if expected_type is UINT16:
return type_token in UINT16_COMPATIBLE
if expected_type is UINT32:
return type_token in UINT32_COMPATIBLE
if expected_type is UINT64:
return type_token in UINT64_COMPATIBLE
if expected_type is INT8 or expected_type is BYTE:
return type_token in INT8_COMPATIBLE
if expected_type is INT16:
return type_token in INT16_COMPATIBLE
if expected_type is INT32:
return type_token in INT32_COMPATIBLE
if expected_type is INT64:
return type_token in INT64_COMPATIBLE
if expected_type is FLOAT32:
return type_token in FLOAT32_COMPATIBLE
if expected_type is FLOAT64:
return type_token in FLOAT64_COMPATIBLE
return expected_type == type_token
def possible_types(value):
if isinstance(value, basestring):
return STRING
if isinstance(value, bool):
return BOOL
if isinstance(value, float):
return tuple(ros_type for ros_type in ROS_FLOAT_TYPES
if value >= ros_type.min_value and value <= ros_type.max_value)
assert isinstance(value, (int, long))
return tuple(ros_type for ros_type in ROS_NUMBER_TYPES
if value >= ros_type.min_value and value <= ros_type.max_value)
###############################################################################
# Type System
###############################################################################
_cache = {t.type_name: t for t in ROS_BUILTIN_TYPES}
_cache["Header"] = HEADER
def clear_cache():
global _cache
_cache = {t.type_name: t for t in ROS_BUILTIN_TYPES}
_cache["Header"] = HEADER
def get_type(type_name):
global _cache
try:
return _cache[type_name]
except KeyError as ke:
if "/" not in type_name:
raise ValueError("Invalid message type name: {}".format(type_name))
try:
msg_class = _load_msg_class(type_name)
fields = _get_class_fields(msg_class)
constants = _get_class_constants(msg_class)
type_token = MessageType(type_name, fields, constants=constants)
_cache[type_name] = type_token
return type_token
except ImportError as ie:
raise KeyError("Cannot import message {}".format(type_name))
def _load_msg_class(type_name):
pkg, msg = type_name.split("/")
module = importlib.import_module(pkg + ".msg")
msg_class = getattr(module, msg)
return msg_class
def _get_class_fields(msg_class):
from genmsg.msgs import parse_type
fields = {}
for i in range(len(msg_class.__slots__)):
field_name = msg_class.__slots__[i]
field_type = msg_class._slot_types[i]
base_type, is_array, length = parse_type(field_type)
type_token = get_type(base_type)
if is_array:
type_token = ArrayType(type_token, length=length)
fields[field_name] = type_token
return fields
def _get_class_constants(msg_class):
from genmsg.base import InvalidMsgSpec
from genmsg.msg_loader import _load_constant_line, _strip_comments
constants = {}
lines = msg_class._full_text.splitlines()
for line in lines:
clean_line = _strip_comments(line)
if not clean_line or not "=" in clean_line:
continue # ignore empty/field lines
try:
constant = _load_constant_line(line)
constants[constant.name] = _make_literal(
constant.val, get_type(constant.type))
except InvalidMsgSpec as e:
pass
return constants
def _make_literal(value, type_token):
assert type_token.is_primitive
if type_token.is_int:
return RosInt(value, type_token)
if type_token.is_float:
return RosFloat(value, type_token)
if type_token.is_bool:
return RosBool(value)
assert type_token.is_string
return RosString(value)