-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmcUtils.py
1453 lines (1241 loc) · 56.5 KB
/
SmcUtils.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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
created by Nikolay V. Ulyanov (ulianownv@mail.ru)
http://www.smcsystem.ru
"""
import base64
import sys
import time
import traceback
from __builtin__ import long, unicode
from typing import List, Optional, Callable
import SMCApi
from enum import Enum
def isNumber(value):
# type: (SMCApi.IValue) -> bool
return value is not None and (
SMCApi.ValueType.BYTE == value.getType() or SMCApi.ValueType.SHORT == value.getType() or SMCApi.ValueType.INTEGER == value.getType() or
SMCApi.ValueType.LONG == value.getType() or SMCApi.ValueType.FLOAT == value.getType() or SMCApi.ValueType.DOUBLE == value.getType() or
SMCApi.ValueType.BIG_INTEGER == value.getType() or SMCApi.ValueType.BIG_DECIMAL == value.getType())
def isNumberField(field):
# type: (SMCApi.ObjectField) -> bool
return field is not None and (
SMCApi.ObjectType.BYTE == field.getType() or SMCApi.ObjectType.SHORT == field.getType() or SMCApi.ObjectType.INTEGER == field.getType() or
SMCApi.ObjectType.LONG == field.getType() or SMCApi.ObjectType.FLOAT == field.getType() or SMCApi.ObjectType.DOUBLE == field.getType() or
SMCApi.ObjectType.BIG_INTEGER == field.getType() or SMCApi.ObjectType.BIG_DECIMAL == field.getType())
def isString(value):
# type: (SMCApi.IValue) -> bool
return value is not None and SMCApi.ValueType.STRING == value.getType()
def isStringField(field):
# type: (SMCApi.ObjectField) -> bool
return field is not None and SMCApi.ObjectType.STRING == field.getType()
def isBytes(value):
# type: (SMCApi.IValue) -> bool
return value is not None and SMCApi.ValueType.BYTES == value.getType()
def isBytesField(field):
# type: (SMCApi.ObjectField) -> bool
return field is not None and SMCApi.ObjectType.BYTES == field.getType()
def isBoolean(value):
# type: (SMCApi.IValue) -> bool
return value is not None and SMCApi.ValueType.BOOLEAN == value.getType()
def isBooleanField(field):
# type: (SMCApi.ObjectField) -> bool
return field is not None and SMCApi.ObjectType.BOOLEAN == field.getType()
def isObjectArray(value):
# type: (SMCApi.IValue) -> bool
return value is not None and SMCApi.ValueType.OBJECT_ARRAY == value.getType()
def isObjectArrayField(field):
# type: (SMCApi.ObjectField) -> bool
return field is not None and SMCApi.ObjectType.OBJECT_ARRAY == field.getType()
def isObjectElementField(field):
# type: (SMCApi.ObjectField) -> bool
return field is not None and SMCApi.ObjectType.OBJECT_ELEMENT == field.getType()
def getNumber(value):
# type: (SMCApi.IValue) -> Optional[int or long or float]
if isNumber(value):
return value.getValue()
else:
None
def getNumberField(field):
# type: (SMCApi.ObjectField) -> Optional[int or long or float]
if isNumberField(field):
return field.getValue()
else:
return None
def getString(value):
# type: (SMCApi.IValue) -> Optional[str]
if isString(value):
return value.getValue()
else:
None
def getStringField(field):
# type: (SMCApi.ObjectField) -> Optional[str]
if isStringField(field):
return field.getValue()
else:
return None
def getBytes(value):
# type: (SMCApi.IValue) -> Optional[bytes]
if isBytes(value):
return value.getValue()
else:
None
def getBytesField(field):
# type: (SMCApi.ObjectField) -> Optional[bytes]
if isBytesField(field):
return field.getValue()
else:
return None
def getBoolean(value):
# type: (SMCApi.IValue) -> Optional[bool]
if isBoolean(value):
return value.getValue()
else:
None
def getBooleanField(field):
# type: (SMCApi.ObjectField) -> Optional[bool]
if isBooleanField(field):
return field.getValue()
else:
None
def getObjectArray(value):
# type: (SMCApi.IValue) -> Optional[SMCApi.ObjectArray]
if isObjectArray(value):
return value.getValue()
else:
None
def getObjectArrayField(field):
# type: (SMCApi.ObjectField) -> Optional[SMCApi.ObjectArray]
if isObjectArrayField(field):
return field.getValue()
else:
return None
def getObjectElement(field):
# type: (SMCApi.ObjectField) -> Optional[SMCApi.ObjectElement]
if field is None or field.getValue() is None:
return None
if field.getType() == SMCApi.ObjectType.OBJECT_ARRAY:
if len(field.getValue().objects) > 0 and field.getValue().getType() == SMCApi.ObjectType.OBJECT_ELEMENT:
return field.getValue().objects[0]
elif field.getType() == SMCApi.ObjectType.OBJECT_ELEMENT:
return field.getValue()
return None
def getObjectElements(field):
# type: (SMCApi.ObjectField) -> Optional[List[SMCApi.ObjectElement]]
if field is None or field.getValue() is None:
return None
if field.getType() == SMCApi.ObjectType.OBJECT_ARRAY:
if field.getValue().getType() == SMCApi.ObjectType.OBJECT_ELEMENT:
return list(field.getValue().objects)
elif field.getType() == SMCApi.ObjectType.OBJECT_ELEMENT:
return list(field.getValue())
return None
def toString(value):
# type: (SMCApi.IValue) -> str
if value is None:
return ""
if value.getType() == SMCApi.ValueType.STRING:
return value.getValue()
elif value.getType() == SMCApi.ValueType.BYTES:
return base64.b64encode(value.getValue())
else:
return str(value.getValue())
def toStringField(field):
# type: (SMCApi.ObjectField) -> str
if field is None or field.getValue() is None:
return ""
if field.getType() == SMCApi.ObjectType.STRING:
return field.getValue()
elif field.getType() == SMCApi.ObjectType.BYTES:
return base64.b64encode(field.getValue())
else:
return str(field.getValue())
def toNumber(value):
# type: (SMCApi.IValue) -> int or long or float
if value is None:
return 0
if value.getType() == SMCApi.ValueType.STRING:
str = getString(value)
if len(str) > 0:
try:
if "." in str:
return float(str)
else:
return long(getString(value))
except:
return 0
return 0
elif value.getType() == SMCApi.ValueType.BOOLEAN:
if getBoolean(value):
return 1
else:
return 0
elif value.getType() == SMCApi.ValueType.BYTES:
return len(getBytes(value))
elif value.getType() == SMCApi.ValueType.OBJECT_ARRAY:
return getObjectArray(value).size()
else:
return value.getValue()
def toNumberField(field):
# type: (SMCApi.ObjectField) -> int or long or float
if field is None or field.getValue() is None:
return 0
if field.getType() == SMCApi.ObjectType.STRING:
try:
return float(getStringField(field))
except:
return long(getStringField(field))
elif field.getType() == SMCApi.ObjectType.BOOLEAN:
if getBooleanField(field):
return 1
else:
return 0
elif field.getType() == SMCApi.ObjectType.BYTES:
return len(getBytesField(field))
elif field.getType() == SMCApi.ObjectType.OBJECT_ARRAY:
return getObjectArrayField(field).size()
elif field.getType() == SMCApi.ObjectType.OBJECT_ARRAY:
return len(getObjectElement(field).getFields())
else:
return field.getValue()
def toBoolean(value):
# type: (SMCApi.IValue) -> bool
if value is None:
return False
if value.getType() == SMCApi.ValueType.STRING:
try:
return bool(getString(value))
except:
return False
elif value.getType() == SMCApi.ValueType.BOOLEAN:
return getBoolean(value)
elif value.getType() == SMCApi.ValueType.BYTES or value.getType() == SMCApi.ValueType.OBJECT_ARRAY:
return True
else:
return getNumber(value) > 0
def toBooleanField(field):
# type: (SMCApi.ObjectField) -> bool
if field is None or field.getValue() is None:
return False
if field.getType() == SMCApi.ObjectType.STRING:
try:
return bool(getStringField(field))
except:
return False
elif field.getType() == SMCApi.ObjectType.BOOLEAN:
return getBooleanField(field)
elif field.getType() == SMCApi.ObjectType.BYTES or field.getType() == SMCApi.ObjectType.OBJECT_ARRAY:
return True
else:
return getNumberField(field) > 0
def toObjectArray(value):
# type: (SMCApi.IValue) -> SMCApi.ObjectArray
if value is None:
return SMCApi.ObjectArray()
if value.getType() == SMCApi.ValueType.OBJECT_ARRAY:
return getObjectArray(value)
else:
return SMCApi.ObjectArray(convertToObjectType(value.getType()), [value.getValue()])
def toObjectArrayField(field):
# type: (SMCApi.ObjectField) -> SMCApi.ObjectArray
if field is None or field.getValue() is None:
return SMCApi.ObjectArray()
if field.getType() == SMCApi.ObjectType.OBJECT_ARRAY:
return getObjectArrayField(field)
elif field.getType() == SMCApi.ObjectType.OBJECT_ELEMENT:
return SMCApi.ObjectArray(SMCApi.ObjectType.OBJECT_ELEMENT, [getObjectElement(field)])
else:
return SMCApi.ObjectArray(field.getType(), [field.getValue()])
def toObjectElement(value):
# type: (SMCApi.IValue) -> SMCApi.ObjectElement
if value is None:
return SMCApi.ObjectElement()
if value.getType() == SMCApi.ValueType.OBJECT_ARRAY:
array = getObjectArray(value)
if isArrayContainObjectElements(array):
return array.get(0)
elif array.isSimple() and array.size() > 0:
element = SMCApi.ObjectElement()
for i in range(array.size()):
element.getFields().append(SMCApi.ObjectField(str(i), array.get(i), array.getType()))
else:
return SMCApi.ObjectElement()
else:
return SMCApi.ObjectElement([SMCApi.ObjectField("0", value.getValue(), convertToObjectType(value.getType()))])
def toObjectElementField(field):
# type: (SMCApi.ObjectField) -> SMCApi.ObjectElement
if field is None or field.getValue() is None:
return SMCApi.ObjectElement()
if field.getType() == SMCApi.ObjectType.OBJECT_ARRAY:
array = getObjectArrayField(field)
if isArrayContainObjectElements(array):
return array.get(0)
elif array.isSimple() and array.size() > 0:
element = SMCApi.ObjectElement()
for i in range(array.size()):
element.getFields().append(SMCApi.ObjectField(str(i), array.get(i), array.getType()))
else:
return SMCApi.ObjectElement()
elif field.getType() == SMCApi.ObjectType.OBJECT_ELEMENT:
return getObjectElement(field)
else:
return SMCApi.ObjectElement([SMCApi.ObjectField(field.getName(), field.getValue(), field.getType())])
ObjectTypePrivate = Enum('OBJECT_ARRAY', 'OBJECT_ELEMENT', 'OBJECT_ELEMENT_SIMPLE', 'VALUE_ANY', 'STRING', 'BYTE', 'SHORT', 'INTEGER', 'LONG',
'FLOAT', 'DOUBLE', 'BIG_INTEGER', 'BIG_DECIMAL', 'BYTES', 'OBJECT_ELEMENT_OPTIMIZED', 'OBJECT_ELEMENT_SIMPLE_OPTIMIZED',
'BOOLEAN',
'STRING_NULL', 'BYTE_NULL', 'SHORT_NULL', 'INTEGER_NULL', 'LONG_NULL', 'FLOAT_NULL', 'DOUBLE_NULL',
'BIG_INTEGER_NULL', 'BIG_DECIMAL_NULL', 'BYTES_NULL', 'BOOLEAN_NULL')
ObjectTypePrivate.OBJECT_ARRAY.value = 0
ObjectTypePrivate.OBJECT_ELEMENT.value = 1
ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE.value = 2
ObjectTypePrivate.VALUE_ANY.value = 3
ObjectTypePrivate.STRING.value = 4
ObjectTypePrivate.BYTE.value = 5
ObjectTypePrivate.SHORT.value = 6
ObjectTypePrivate.INTEGER.value = 7
ObjectTypePrivate.LONG.value = 8
ObjectTypePrivate.FLOAT.value = 9
ObjectTypePrivate.DOUBLE.value = 10
ObjectTypePrivate.BIG_INTEGER.value = 11
ObjectTypePrivate.BIG_DECIMAL.value = 12
ObjectTypePrivate.BYTES.value = 13
ObjectTypePrivate.OBJECT_ELEMENT_OPTIMIZED.value = 14
ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE_OPTIMIZED.value = 15
ObjectTypePrivate.BOOLEAN.value = 16
ObjectTypePrivate.STRING_NULL.value = 17
ObjectTypePrivate.BYTE_NULL.value = 18
ObjectTypePrivate.SHORT_NULL.value = 19
ObjectTypePrivate.INTEGER_NULL.value = 20
ObjectTypePrivate.LONG_NULL.value = 21
ObjectTypePrivate.FLOAT_NULL.value = 22
ObjectTypePrivate.DOUBLE_NULL.value = 23
ObjectTypePrivate.BIG_INTEGER_NULL.value = 24
ObjectTypePrivate.BIG_DECIMAL_NULL.value = 25
ObjectTypePrivate.BYTES_NULL.value = 26
ObjectTypePrivate.BOOLEAN_NULL.value = 27
class ObjectValuePrivate(object):
def __init__(self, name, type, value=None):
# type: (str, ObjectTypePrivate, object) -> None
self.name = name
self.type = type
self.value = value
def convertFromObjectTypePrivate(typePrivate):
# type: (ObjectTypePrivate) -> SMCApi.ObjectType
if typePrivate == ObjectTypePrivate.OBJECT_ELEMENT_OPTIMIZED:
return SMCApi.ObjectType.OBJECT_ELEMENT
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE_OPTIMIZED:
return SMCApi.ObjectType.OBJECT_ELEMENT
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE:
return SMCApi.ObjectType.OBJECT_ELEMENT
else:
return SMCApi.ObjectType[SMCApi.ObjectType._keys.index(typePrivate.key.replace("_NULL", ""))]
def convertToObjectTypePrivate(type, isNull=False):
# type: (SMCApi.ObjectType, bool) -> ObjectTypePrivate
if type == SMCApi.ObjectType.OBJECT_ARRAY:
return ObjectTypePrivate.OBJECT_ARRAY
elif type == SMCApi.ObjectType.OBJECT_ELEMENT:
return ObjectTypePrivate.OBJECT_ELEMENT
elif type == SMCApi.ObjectType.VALUE_ANY:
return ObjectTypePrivate.VALUE_ANY
elif type == SMCApi.ObjectType.STRING:
if isNull:
return ObjectTypePrivate.STRING_NULL
else:
return ObjectTypePrivate.STRING
elif type == SMCApi.ObjectType.BYTE:
if isNull:
return ObjectTypePrivate.BYTE_NULL
else:
return ObjectTypePrivate.BYTE
elif type == SMCApi.ObjectType.SHORT:
if isNull:
return ObjectTypePrivate.SHORT_NULL
else:
return ObjectTypePrivate.SHORT
elif type == SMCApi.ObjectType.INTEGER:
if isNull:
return ObjectTypePrivate.INTEGER_NULL
else:
return ObjectTypePrivate.INTEGER
elif type == SMCApi.ObjectType.LONG:
if isNull:
return ObjectTypePrivate.LONG_NULL
else:
return ObjectTypePrivate.LONG
elif type == SMCApi.ObjectType.FLOAT:
if isNull:
return ObjectTypePrivate.FLOAT_NULL
else:
return ObjectTypePrivate.FLOAT
elif type == SMCApi.ObjectType.DOUBLE:
if isNull:
return ObjectTypePrivate.DOUBLE_NULL
else:
return ObjectTypePrivate.DOUBLE
elif type == SMCApi.ObjectType.BIG_INTEGER:
if isNull:
return ObjectTypePrivate.BIG_INTEGER_NULL
else:
return ObjectTypePrivate.BIG_INTEGER
elif type == SMCApi.ObjectType.BIG_DECIMAL:
if isNull:
return ObjectTypePrivate.BIG_DECIMAL_NULL
else:
return ObjectTypePrivate.BIG_DECIMAL
elif type == SMCApi.ObjectType.BYTES:
if isNull:
return ObjectTypePrivate.BYTES_NULL
else:
return ObjectTypePrivate.BYTES
elif type == SMCApi.ObjectType.BOOLEAN:
if isNull:
return ObjectTypePrivate.BOOLEAN_NULL
else:
return ObjectTypePrivate.BOOLEAN
return ObjectTypePrivate[ObjectTypePrivate._keys.index(type.key)]
def deserializeToObject(messages, silent=True):
# type: (List[SMCApi.IMessage], bool) -> SMCApi.ObjectArray
"""
* deserialize messages to object array
* if first message type ObjectArray, when return it
* use object serialization format
* @formatter:off
* object serialization format:
* number - type of elements, may by: 0-ObjectArray, 1-ObjectElement, 2-ObjectElementSimple, 3-AnySimpleTypes, 4-String, 5-Byte, 6-Short, 7-Integer, 8-Long, 9-Float, 10-Double, 11-BigInteger, 12-BigDecimal, 13-byte[], 14-OBJECT_ELEMENT_OPTIMIZED, 15-OBJECT_ELEMENT_SIMPLE_OPTIMIZED, 16-Boolean
* number - number of item in array
* items. depend of type. format:
* if item type is 0 (ObjectArray): then list of arrays. each has format described above, recursion.
* if item type is 1 (ObjectElement): then list of objects:
* if all elements hase same fields and size>1 then:
* number - number of fields in object.
* list of fields. format:
* string - field name.
* number - field type, may by: 0-ObjectArray, 1-ObjectElement, 2-ObjectElementSimple, 3-AnySimpleTypes, 4-String, 5-Byte, 6-Short, 7-Integer, 8-Long, 9-Float, 10-Double, 11-BigInteger, 12-BigDecimal, 13-byte[], 16-Boolean
* list of values for each element. format:
* field value. depend of type. format:
* if item type is 0-ObjectArray: then list of arrays. each has format described above, recursion.
* if item type is 1-ObjectElement: then list of objects. each has format described above, recursion.
* if item type is 2-ObjectElementSimple: then list of simple objects. each has format described above, recursion.
* else: any type - simple value.
* else:
* number - number of fields in object.
* for each element:
* for each field. format:
* string - field name.
* number - field type, may by: 0-ObjectArray, 1-ObjectElement, 2-ObjectElementSimple, 3-AnySimpleTypes, 4-String, 5-Byte, 6-Short, 7-Integer, 8-Long, 9-Float, 10-Double, 11-BigInteger, 12-BigDecimal, 13-byte[], 16-Boolean
* field value. depend of type. format:
* if item type is 0-ObjectArray: then list of arrays. each has format described above, recursion.
* if item type is 1-ObjectElement: then list of objects. each has format described above, recursion.
* if item type is 2-ObjectElementSimple: then list of simple objects. each has format described above, recursion.
* else: any type - simple value.
* if item type is 2 (ObjectElementSimple): then list of simple objects has format:
* if all elements hase same fields and size>1 then:
* number - number of fields in object.
* list of fields. format:
* string - field name.
* list of values for each element. format:
* any type - simple value.
* else:
* number - number of fields in each object (one for all).
* list of each object fields. format:
* string - field name.
* any type - simple value.
* else: list of simple values, format:
* any type - simple value.
* @formatter:on
:param messages:
:return:
"""
message = None
if messages and len(messages) > 0:
message = messages[0]
if isObjectArray(message):
return getObjectArray(messages.pop(0))
objectArray = SMCApi.ObjectArray()
if messages is None or len(messages) < 2:
return objectArray
typeId = getNumber(message)
if typeId is None:
return objectArray
if typeId < 0 or len(ObjectTypePrivate) <= typeId:
return objectArray
typePrivate = ObjectTypePrivate[typeId]
messages.pop(0)
# type = ObjectType[typeId]
size = getNumber(messages[0])
if size is None:
messages.insert(0, message)
return objectArray
messages.pop(0)
count = size
type = convertFromObjectTypePrivate(typePrivate)
objectArray = SMCApi.ObjectArray(type)
try:
if typePrivate == ObjectTypePrivate.OBJECT_ARRAY:
for i in range(count):
objectArray.add(deserializeToObject(messages, silent))
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT:
for i in range(count):
objectArray.add(deserializeToObjectElement(messages, -1, None, silent))
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE:
if messages is None or len(messages) < 1:
return objectArray
message = messages[0]
countFields = getNumber(message)
if countFields is not None:
messages.pop(0)
for i in range(count):
objectArray.add(deserializeToObjectElement(messages, countFields, None, silent))
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT_OPTIMIZED:
if messages is None or len(messages) < 1:
return objectArray
message = messages[0]
countFields = getNumber(message)
if countFields is None or len(messages) <= countFields * 2 + 1:
return objectArray
messages.pop(0)
definedFields = []
hasErrors = False
for i in range(countFields):
message = messages[0]
fieldName = toString(message)
messages.pop(0)
message = messages[0]
fieldTypeId = getNumber(message)
if fieldTypeId is None or fieldTypeId < 0 or len(ObjectTypePrivate) <= fieldTypeId:
hasErrors = True
break
messages.pop(0)
definedFields.append(ObjectValuePrivate(fieldName, ObjectTypePrivate[fieldTypeId]))
if hasErrors:
return objectArray
for i in range(count):
objectArray.add(deserializeToObjectElement(messages, -1, definedFields, silent))
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE_OPTIMIZED:
if messages is None or len(messages) < 1:
return objectArray
message = messages[0]
countFields = getNumber(message)
if countFields is None or len(messages) <= countFields * 2 + 1:
return objectArray
messages.pop(0)
definedFields = []
for i in range(countFields):
definedFields.append(ObjectValuePrivate(toString(messages.pop(0)), ObjectTypePrivate.VALUE_ANY))
for i in range(count):
objectArray.add(deserializeToObjectElement(messages, -1, definedFields, silent))
else:
for i in range(count):
objectArray.add(messages.pop(0).getValue())
except Exception as e:
if not silent:
raise Exception(e)
return objectArray
def deserializeToObjectElement(messages, countFields=-1, definedFields=None, silent=True):
# type: (List[SMCApi.IMessage], int, List[ObjectValuePrivate], bool) -> SMCApi.ObjectElement
message = messages[0]
objectElement = SMCApi.ObjectElement()
if isObjectArray(message):
objectArray = getObjectArray(messages.pop(0))
if isArrayContainObjectElements(objectArray):
return objectArray.get(0)
return objectElement
count = -1
if messages is None or len(messages) < 1:
return objectElement
if countFields < 0:
if definedFields is None:
size = getNumber(messages[0])
if size is None:
return objectElement
messages.pop(0)
count = size
else:
count = len(definedFields)
else:
count = countFields
try:
if definedFields is not None:
for field in definedFields:
deserializeToObjectElementValue(messages, objectElement, field.getName(), field.getType(), silent)
else:
for i in range(count):
if (countFields > -1 and len(messages) < 2) or (countFields < 0 and len(messages) < 3):
break
fieldName = toString(messages.pop(0))
type = ObjectTypePrivate.VALUE_ANY
if countFields < 0:
typeId = getNumber(messages[0])
if typeId is None or typeId < 0 or len(ObjectTypePrivate) <= typeId:
break
messages.pop(0)
type = ObjectTypePrivate[typeId]
deserializeToObjectElementValue(messages, objectElement, fieldName, type, silent)
except Exception as e:
if not silent:
raise Exception(e)
return objectElement
def deserializeToObjectElementValue(messages, objectElement, fieldName, type, silent=True):
# type: (List[SMCApi.IMessage], SMCApi.ObjectElement, str, ObjectTypePrivate, bool) -> None
if type == ObjectTypePrivate.OBJECT_ARRAY:
objectElement.fields.append(SMCApi.ObjectField(fieldName, deserializeToObject(messages, silent)))
elif type == ObjectTypePrivate.OBJECT_ELEMENT:
objectElement.fields.append(SMCApi.ObjectField(fieldName, deserializeToObjectElement(messages, -1, None, silent)))
elif type == ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE:
countFields2 = getNumber(messages[0])
if countFields2 is not None:
messages.pop(0)
objectElement.fields.append(SMCApi.ObjectField(fieldName, deserializeToObjectElement(messages, countFields2, None, silent)))
elif type == ObjectTypePrivate.OBJECT_ELEMENT_OPTIMIZED:
return
elif type == ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE_OPTIMIZED:
return
elif type == ObjectTypePrivate.STRING_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.STRING))
elif type == ObjectTypePrivate.BYTE_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.BYTE))
elif type == ObjectTypePrivate.SHORT_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.SHORT))
elif type == ObjectTypePrivate.INTEGER_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.INTEGER))
elif type == ObjectTypePrivate.LONG_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.LONG))
elif type == ObjectTypePrivate.FLOAT_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.FLOAT))
elif type == ObjectTypePrivate.DOUBLE_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.DOUBLE))
elif type == ObjectTypePrivate.BIG_INTEGER_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.BIG_INTEGER))
elif type == ObjectTypePrivate.BIG_DECIMAL_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.BIG_DECIMAL))
elif type == ObjectTypePrivate.BYTES_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.BYTES))
elif type == ObjectTypePrivate.BOOLEAN_NULL:
objectElement.fields.append(SMCApi.ObjectField(fieldName, None, SMCApi.ObjectType.BOOLEAN))
else:
objectElement.fields.append(SMCApi.ObjectField(fieldName, messages.pop(0).getValue()))
def serializeFromObject(objectArray, silent=True):
# type: (SMCApi.ObjectArray, bool) -> List[object]
"""
* serialize objects to messages
* use object serialization format
* @formatter:off
* object serialization format:
* number - type of elements, may by: 0-ObjectArray, 1-ObjectElement, 2-ObjectElementSimple, 3-AnySimpleTypes, 4-String, 5-Byte, 6-Short, 7-Integer, 8-Long, 9-Float, 10-Double, 11-BigInteger, 12-BigDecimal, 13-byte[], 14-OBJECT_ELEMENT_OPTIMIZED, 15-OBJECT_ELEMENT_SIMPLE_OPTIMIZED, 16-Boolean
* number - number of item in array
* items. depend of type. format:
* if item type is 0 (ObjectArray): then list of arrays. each has format described above, recursion.
* if item type is 1 (ObjectElement): then list of objects:
* if all elements hase same fields and size>1 then:
* number - number of fields in object.
* list of fields. format:
* string - field name.
* number - field type, may by: 0-ObjectArray, 1-ObjectElement, 2-ObjectElementSimple, 3-AnySimpleTypes, 4-String, 5-Byte, 6-Short, 7-Integer, 8-Long, 9-Float, 10-Double, 11-BigInteger, 12-BigDecimal, 13-byte[], 16-Boolean
* list of values for each element. format:
* field value. depend of type. format:
* if item type is 0-ObjectArray: then list of arrays. each has format described above, recursion.
* if item type is 1-ObjectElement: then list of objects. each has format described above, recursion.
* if item type is 2-ObjectElementSimple: then list of simple objects. each has format described above, recursion.
* else: any type - simple value.
* else:
* number - number of fields in object.
* for each element:
* for each field. format:
* string - field name.
* number - field type, may by: 0-ObjectArray, 1-ObjectElement, 2-ObjectElementSimple, 3-AnySimpleTypes, 4-String, 5-Byte, 6-Short, 7-Integer, 8-Long, 9-Float, 10-Double, 11-BigInteger, 12-BigDecimal, 13-byte[], 16-Boolean
* field value. depend of type. format:
* if item type is 0-ObjectArray: then list of arrays. each has format described above, recursion.
* if item type is 1-ObjectElement: then list of objects. each has format described above, recursion.
* if item type is 2-ObjectElementSimple: then list of simple objects. each has format described above, recursion.
* else: any type - simple value.
* if item type is 2 (ObjectElementSimple): then list of simple objects has format:
* if all elements hase same fields and size>1 then:
* number - number of fields in object.
* list of fields. format:
* string - field name.
* list of values for each element. format:
* any type - simple value.
* else:
* number - number of fields in each object (one for all).
* list of each object fields. format:
* string - field name.
* any type - simple value.
* else: list of simple values, format:
* any type - simple value.
* @formatter:on
:param objectArray:
:return:
"""
result = []
if objectArray is None:
return result
count = objectArray.size()
typePrivate = convertToObjectTypePrivate(objectArray.getType())
result.append(typePrivate.index)
result.append(count)
if count == 0:
return result
try:
hasFieldWithNull = False
if typePrivate == ObjectTypePrivate.OBJECT_ELEMENT and count > 1:
isSimple = True
fieldNames = None
for obj in objectArray.objects:
if fieldNames is None:
fieldNames = []
for field in obj.fields:
fieldNames.append(field.getName())
if not obj.isSimple():
isSimple = False
break
for field in obj.fields:
if field.getValue() is None:
isSimple = False
hasFieldWithNull = True
break
else:
if len(fieldNames) != len(obj.fields) or not obj.isSimple():
isSimple = False
break
sameFields = True
for field in obj.fields:
if field.getName() not in fieldNames:
sameFields = False
break
if not sameFields:
isSimple = False
break
if isSimple:
typePrivate = ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE
if typePrivate == ObjectTypePrivate.OBJECT_ARRAY:
for obj in objectArray.objects:
listExtend(result, serializeFromObject(obj, silent))
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT:
definedFields = None
if not hasFieldWithNull and count > 1 and isSameFields(objectArray):
definedFields = []
objectElement = objectArray.get(0)
for field in objectElement.getFields():
definedFields.append(ObjectValuePrivate(field.getName(), convertToObjectTypePrivate(field.getType())))
result = [ObjectTypePrivate.OBJECT_ELEMENT_OPTIMIZED.index, count, len(definedFields)]
for field in definedFields:
result.append(field.name)
result.append(field.type.index)
for obj in objectArray.objects:
listExtend(result, serializeFromObjectElement(obj, False, definedFields, silent))
elif typePrivate == ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE:
definedFields = None
if count > 1 and isSameFields(objectArray):
definedFields = []
objectElement = objectArray.get(0)
for field in objectElement.getFields():
definedFields.append(ObjectValuePrivate(field.getName(), None))
result = [ObjectTypePrivate.OBJECT_ELEMENT_SIMPLE_OPTIMIZED.index, count, len(definedFields)]
for field in definedFields:
result.append(field.name)
else:
if count > 0:
result.append(len(objectArray.get(0).getFields()))
else:
result.append(0)
for obj in objectArray.objects:
listExtend(result, serializeFromObjectElement(obj, True, definedFields, silent))
else:
for obj in objectArray.objects:
result.append(obj)
except Exception as e:
if not silent:
raise Exception(e)
return result
def serializeFromObjectElement(objectElement, isSimple=False, definedFields=None, silent=True):
# type: (SMCApi.ObjectElement, bool, List[ObjectValuePrivate], bool) -> List[object]
result = []
if objectElement is None:
return result
try:
if definedFields is not None:
# because its check
for field in objectElement.getFields():
listExtend(result, serializeFromObjectFieldValue(field.getType(), field.getValue(), silent))
else:
if not isSimple:
result.append(len(objectElement.getFields()))
for f in objectElement.getFields():
result.append(f.getName())
if not isSimple:
result.append(convertToObjectTypePrivate(f.getType(), f.getValue() is None).index)
if f.getValue() is not None:
listExtend(result, serializeFromObjectFieldValue(f.getType(), f.getValue(), silent))
except Exception as e:
if not silent:
raise Exception(e)
return result
def serializeFromObjectFieldValue(type, value, silent=True):
# type: (SMCApi.ObjectType, object, bool) -> List[object]
if type == SMCApi.ObjectType.OBJECT_ARRAY:
return serializeFromObject(value, silent)
elif type == SMCApi.ObjectType.OBJECT_ELEMENT:
if value.isSimple():
lst = []
lst.append(len(value.getFields()))
listExtend(lst, serializeFromObjectElement(value, True, None, silent))
return lst
else:
return serializeFromObjectElement(value, False, None, silent)
else:
return [value]
def hasErrors(command):
# type: (SMCApi.ICommand) -> bool
if command is None:
return False
return len(command.getActions()) > 0 and any(not hasErrorsInAction(action) for action in command.getActions())
def hasData(command):
# type: (SMCApi.ICommand) -> bool
if command is None:
return False
return len(command.getActions()) > 0 and any(not hasDataInAction(action) for action in command.getActions())
def hasErrorsInAction(action):
# type: (SMCApi.IAction) -> bool
if action is None:
return False
return len(action.getMessages()) > 0 and any(
message.getMessageType() == SMCApi.MessageType.ERROR or message.getMessageType() == SMCApi.MessageType.ACTION_ERROR for message in
action.getMessages())
def hasDataInAction(action):
# type: (SMCApi.IAction) -> bool
if action is None:
return False
return len(action.getMessages()) > 0 and any(message.getMessageType() == SMCApi.MessageType.DATA for message in action.getMessages())
def getErrorsInAction(action):
# type: (SMCApi.IAction) -> [SMCApi.IMessage]
if action is None:
return []
return filter(lambda message: message.getMessageType() == SMCApi.MessageType.ERROR or message.getMessageType() == SMCApi.MessageType.ACTION_ERROR,
action.getMessages())
def getErrors(command):
# type: (SMCApi.ICommand) -> [SMCApi.IMessage]
if command is None:
return []
result = []
for i in range(len(command.getActions())):
lst = getErrorsInAction(command.getActions()[i])
if lst:
listExtend(result, lst)
return result
def getDataInAction(action):
# type: (SMCApi.IAction) -> [SMCApi.IMessage]
if action is None:
return []
return filter(lambda message: message.getMessageType() == SMCApi.MessageType.DATA, action.getMessages())
def getData(command):
# type: (SMCApi.ICommand) -> [SMCApi.IMessage]
if command is None:
return []
result = []
for i in range(len(command.getActions())):
lst = getDataInAction(command.getActions()[i])
if lst:
listExtend(result, lst)
return result
def isSameFields(objectArray):
# type: (SMCApi.ObjectArray) -> bool
isSame = True
fieldNames = None
fieldTypes = None
for obj in objectArray:
if fieldNames is None or fieldTypes is None:
fieldNames = []
fieldTypes = []
for field in obj.fields:
fieldNames.append(field.getName())
fieldTypes.append(field.getType())
else:
if len(fieldNames) != len(obj.fields):
isSame = False
break
sameFields = True
for i in range(len(fieldNames)):
if fieldNames[i] != obj.fields[i].getName():
sameFields = False
break
if fieldTypes[i] != obj.fields[i].getType():
sameFields = False
break
if not sameFields:
isSame = False
break
return isSame
def splitFieldNames(fieldPath):
# type: (str) -> [str]
return fieldPath.split(".")
def isArrayContainObjectElements(objectArray):
# type: (SMCApi.ObjectArray) -> bool
return objectArray is not None and len(objectArray.objects) > 0 and objectArray.getType() == SMCApi.ObjectType.OBJECT_ELEMENT