gremlinpython 3.6.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. gremlin_python/__init__.py +20 -0
  2. gremlin_python/__version__.py +20 -0
  3. gremlin_python/driver/__init__.py +20 -0
  4. gremlin_python/driver/aiohttp/__init__.py +18 -0
  5. gremlin_python/driver/aiohttp/transport.py +242 -0
  6. gremlin_python/driver/client.py +206 -0
  7. gremlin_python/driver/connection.py +106 -0
  8. gremlin_python/driver/driver_remote_connection.py +183 -0
  9. gremlin_python/driver/protocol.py +259 -0
  10. gremlin_python/driver/remote_connection.py +84 -0
  11. gremlin_python/driver/request.py +25 -0
  12. gremlin_python/driver/resultset.py +100 -0
  13. gremlin_python/driver/serializer.py +294 -0
  14. gremlin_python/driver/transport.py +45 -0
  15. gremlin_python/driver/useragent.py +37 -0
  16. gremlin_python/process/__init__.py +20 -0
  17. gremlin_python/process/anonymous_traversal.py +64 -0
  18. gremlin_python/process/graph_traversal.py +2301 -0
  19. gremlin_python/process/strategies.py +239 -0
  20. gremlin_python/process/translator.py +297 -0
  21. gremlin_python/process/traversal.py +875 -0
  22. gremlin_python/statics.py +117 -0
  23. gremlin_python/structure/__init__.py +20 -0
  24. gremlin_python/structure/graph.py +134 -0
  25. gremlin_python/structure/io/__init__.py +20 -0
  26. gremlin_python/structure/io/graphbinaryV1.py +1153 -0
  27. gremlin_python/structure/io/graphsonV2d0.py +646 -0
  28. gremlin_python/structure/io/graphsonV3d0.py +766 -0
  29. gremlin_python/structure/io/util.py +60 -0
  30. gremlinpython-3.6.8.data/data/LICENSE +202 -0
  31. gremlinpython-3.6.8.data/data/NOTICE +5 -0
  32. gremlinpython-3.6.8.dist-info/LICENSE +202 -0
  33. gremlinpython-3.6.8.dist-info/METADATA +147 -0
  34. gremlinpython-3.6.8.dist-info/NOTICE +5 -0
  35. gremlinpython-3.6.8.dist-info/RECORD +37 -0
  36. gremlinpython-3.6.8.dist-info/WHEEL +5 -0
  37. gremlinpython-3.6.8.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1153 @@
1
+ """
2
+ Licensed to the Apache Software Foundation (ASF) under one
3
+ or more contributor license agreements. See the NOTICE file
4
+ distributed with this work for additional information
5
+ regarding copyright ownership. The ASF licenses this file
6
+ to you under the Apache License, Version 2.0 (the
7
+ "License"); you may not use this file except in compliance
8
+ with the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing,
13
+ software distributed under the License is distributed on an
14
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ KIND, either express or implied. See the License for the
16
+ specific language governing permissions and limitations
17
+ under the License.
18
+ """
19
+
20
+ import datetime
21
+ import calendar
22
+ import uuid
23
+ import math
24
+ import io
25
+ import struct
26
+ from collections import OrderedDict
27
+ import logging
28
+
29
+ from struct import pack, unpack
30
+ from aenum import Enum
31
+ from datetime import timedelta
32
+ from gremlin_python import statics
33
+ from gremlin_python.statics import FloatType, BigDecimal, FunctionType, ShortType, IntType, LongType, BigIntType, \
34
+ TypeType, DictType, ListType, SetType, SingleByte, ByteBufferType, GremlinType, \
35
+ SingleChar
36
+ from gremlin_python.process.traversal import Barrier, Binding, Bytecode, Cardinality, Column, Direction, Merge, \
37
+ Operator, Order, Pick, Pop, P, Scope, TextP, Traversal, Traverser, \
38
+ TraversalStrategy, T
39
+ from gremlin_python.process.graph_traversal import GraphTraversal
40
+ from gremlin_python.structure.graph import Graph, Edge, Property, Vertex, VertexProperty, Path
41
+ from gremlin_python.structure.io.util import HashableDict, SymbolUtil
42
+
43
+ log = logging.getLogger(__name__)
44
+
45
+ # When we fall back to a superclass's serializer, we iterate over this map.
46
+ # We want that iteration order to be consistent, so we use an OrderedDict,
47
+ # not a dict.
48
+ _serializers = OrderedDict()
49
+ _deserializers = {}
50
+
51
+
52
+ class DataType(Enum):
53
+ null = 0xfe
54
+ int = 0x01
55
+ long = 0x02
56
+ string = 0x03
57
+ date = 0x04
58
+ timestamp = 0x05
59
+ clazz = 0x06
60
+ double = 0x07
61
+ float = 0x08
62
+ list = 0x09
63
+ map = 0x0a
64
+ set = 0x0b
65
+ uuid = 0x0c
66
+ edge = 0x0d
67
+ path = 0x0e
68
+ property = 0x0f
69
+ graph = 0x10 # not supported - no graph object in python yet
70
+ vertex = 0x11
71
+ vertexproperty = 0x12
72
+ barrier = 0x13
73
+ binding = 0x14
74
+ bytecode = 0x15
75
+ cardinality = 0x16
76
+ column = 0x17
77
+ direction = 0x18
78
+ operator = 0x19
79
+ order = 0x1a
80
+ pick = 0x1b
81
+ pop = 0x1c
82
+ lambda_ = 0x1d
83
+ p = 0x1e
84
+ scope = 0x1f
85
+ t = 0x20
86
+ traverser = 0x21
87
+ bigdecimal = 0x22
88
+ biginteger = 0x23
89
+ byte = 0x24
90
+ bytebuffer = 0x25
91
+ short = 0x26
92
+ boolean = 0x27
93
+ textp = 0x28
94
+ traversalstrategy = 0x29
95
+ bulkset = 0x2a
96
+ tree = 0x2b # not supported - no tree object in Python yet
97
+ metrics = 0x2c
98
+ traversalmetrics = 0x2d
99
+ merge = 0x2e
100
+ char = 0x80
101
+ duration = 0x81
102
+ inetaddress = 0x82 # todo
103
+ instant = 0x83 # todo
104
+ localdate = 0x84 # todo
105
+ localdatetime = 0x85 # todo
106
+ localtime = 0x86 # todo
107
+ monthday = 0x87 # todo
108
+ offsetdatetime = 0x88 # todo
109
+ offsettime = 0x89 # todo
110
+ period = 0x8a # todo
111
+ year = 0x8b # todo
112
+ yearmonth = 0x8c # todo
113
+ zonedatetime = 0x8d # todo
114
+ zoneoffset = 0x8e # todo
115
+ custom = 0x00 # todo
116
+
117
+
118
+ NULL_BYTES = [DataType.null.value, 0x01]
119
+
120
+
121
+ def _make_packer(format_string):
122
+ packer = struct.Struct(format_string)
123
+ pack = packer.pack
124
+ unpack = lambda s: packer.unpack(s)[0]
125
+ return pack, unpack
126
+
127
+
128
+ int64_pack, int64_unpack = _make_packer('>q')
129
+ int32_pack, int32_unpack = _make_packer('>i')
130
+ int16_pack, int16_unpack = _make_packer('>h')
131
+ int8_pack, int8_unpack = _make_packer('>b')
132
+ uint64_pack, uint64_unpack = _make_packer('>Q')
133
+ uint8_pack, uint8_unpack = _make_packer('>B')
134
+ float_pack, float_unpack = _make_packer('>f')
135
+ double_pack, double_unpack = _make_packer('>d')
136
+
137
+
138
+ class GraphBinaryTypeType(type):
139
+ def __new__(mcs, name, bases, dct):
140
+ cls = super(GraphBinaryTypeType, mcs).__new__(mcs, name, bases, dct)
141
+ if not name.startswith('_'):
142
+ if cls.python_type:
143
+ _serializers[cls.python_type] = cls
144
+ if cls.graphbinary_type:
145
+ _deserializers[cls.graphbinary_type] = cls
146
+ return cls
147
+
148
+
149
+ class GraphBinaryWriter(object):
150
+ def __init__(self, serializer_map=None):
151
+ self.serializers = _serializers.copy()
152
+ if serializer_map:
153
+ self.serializers.update(serializer_map)
154
+
155
+ def write_object(self, object_data):
156
+ return self.to_dict(object_data)
157
+
158
+ def to_dict(self, obj, to_extend=None):
159
+ if to_extend is None:
160
+ to_extend = bytearray()
161
+
162
+ if obj is None:
163
+ to_extend.extend(NULL_BYTES)
164
+ return
165
+
166
+ try:
167
+ t = type(obj)
168
+ return self.serializers[t].dictify(obj, self, to_extend)
169
+ except KeyError:
170
+ for key, serializer in self.serializers.items():
171
+ if isinstance(obj, key):
172
+ return serializer.dictify(obj, self, to_extend)
173
+
174
+ if isinstance(obj, dict):
175
+ return dict((self.to_dict(k, to_extend), self.to_dict(v, to_extend)) for k, v in obj.items())
176
+ elif isinstance(obj, set):
177
+ return set([self.to_dict(o, to_extend) for o in obj])
178
+ elif isinstance(obj, list):
179
+ return [self.to_dict(o, to_extend) for o in obj]
180
+ else:
181
+ return obj
182
+
183
+
184
+ class GraphBinaryReader(object):
185
+ def __init__(self, deserializer_map=None):
186
+ self.deserializers = _deserializers.copy()
187
+ if deserializer_map:
188
+ self.deserializers.update(deserializer_map)
189
+
190
+ def read_object(self, b):
191
+ if isinstance(b, bytearray):
192
+ return self.to_object(io.BytesIO(b))
193
+ elif isinstance(b, io.BufferedIOBase):
194
+ return self.to_object(b)
195
+
196
+ def to_object(self, buff, data_type=None, nullable=True):
197
+ if data_type is None:
198
+ bt = uint8_unpack(buff.read(1))
199
+ if bt == DataType.null.value:
200
+ if nullable:
201
+ buff.read(1)
202
+ return None
203
+ return self.deserializers[DataType(bt)].objectify(buff, self, nullable)
204
+ else:
205
+ return self.deserializers[data_type].objectify(buff, self, nullable)
206
+
207
+
208
+ class _GraphBinaryTypeIO(object, metaclass=GraphBinaryTypeType):
209
+ python_type = None
210
+ graphbinary_type = None
211
+
212
+ @classmethod
213
+ def prefix_bytes(cls, graphbin_type, as_value=False, nullable=True, to_extend=None):
214
+ if to_extend is None:
215
+ to_extend = bytearray()
216
+
217
+ if not as_value:
218
+ to_extend += uint8_pack(graphbin_type.value)
219
+
220
+ if nullable:
221
+ to_extend += int8_pack(0)
222
+
223
+ return to_extend
224
+
225
+ @classmethod
226
+ def read_int(cls, buff):
227
+ return int32_unpack(buff.read(4))
228
+
229
+ @classmethod
230
+ def is_null(cls, buff, reader, else_opt, nullable=True):
231
+ return None if nullable and buff.read(1)[0] == 0x01 else else_opt(buff, reader)
232
+
233
+ def dictify(self, obj, writer, to_extend, as_value=False, nullable=True):
234
+ raise NotImplementedError()
235
+
236
+ def objectify(self, d, reader, nullable=True):
237
+ raise NotImplementedError()
238
+
239
+
240
+ class LongIO(_GraphBinaryTypeIO):
241
+
242
+ python_type = LongType
243
+ graphbinary_type = DataType.long
244
+ byte_format_pack = int64_pack
245
+ byte_format_unpack = int64_unpack
246
+
247
+ @classmethod
248
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
249
+ if obj < -9223372036854775808 or obj > 9223372036854775807:
250
+ raise Exception("Value too big, please use bigint Gremlin type")
251
+ else:
252
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
253
+ to_extend.extend(cls.byte_format_pack(obj))
254
+ return to_extend
255
+
256
+ @classmethod
257
+ def objectify(cls, buff, reader, nullable=True):
258
+ return cls.is_null(buff, reader, lambda b, r: int64_unpack(buff.read(8)), nullable)
259
+
260
+
261
+ class IntIO(LongIO):
262
+
263
+ python_type = IntType
264
+ graphbinary_type = DataType.int
265
+ byte_format_pack = int32_pack
266
+ byte_format_unpack = int32_unpack
267
+
268
+ @classmethod
269
+ def objectify(cls, buff, reader, nullable=True):
270
+ return cls.is_null(buff, reader, lambda b, r: cls.read_int(b), nullable)
271
+
272
+
273
+ class ShortIO(LongIO):
274
+
275
+ python_type = ShortType
276
+ graphbinary_type = DataType.short
277
+ byte_format_pack = int16_pack
278
+ byte_format_unpack = int16_unpack
279
+
280
+ @classmethod
281
+ def objectify(cls, buff, reader, nullable=True):
282
+ return cls.is_null(buff, reader, lambda b, r: int16_unpack(buff.read(2)), nullable)
283
+
284
+
285
+ class BigIntIO(_GraphBinaryTypeIO):
286
+
287
+ python_type = BigIntType
288
+ graphbinary_type = DataType.biginteger
289
+
290
+ @classmethod
291
+ def write_bigint(cls, obj, to_extend):
292
+ length = (obj.bit_length() + 7) // 8
293
+ if obj > 0:
294
+ b = obj.to_bytes(length, byteorder='big')
295
+ to_extend.extend(int32_pack(length + 1))
296
+ to_extend.extend(int8_pack(0))
297
+ to_extend.extend(b)
298
+ else:
299
+ # handle negative
300
+ b = obj.to_bytes(length, byteorder='big', signed=True)
301
+ to_extend.extend(int32_pack(length))
302
+ to_extend.extend(b)
303
+ return to_extend
304
+
305
+ @classmethod
306
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
307
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
308
+ return cls.write_bigint(obj, to_extend)
309
+
310
+ @classmethod
311
+ def read_bigint(cls, buff):
312
+ size = cls.read_int(buff)
313
+ return int.from_bytes(buff.read(size), byteorder='big', signed=True)
314
+
315
+ @classmethod
316
+ def objectify(cls, buff, reader, nullable=False):
317
+ return cls.is_null(buff, reader, lambda b, r: cls.read_bigint(b), nullable)
318
+
319
+
320
+ class DateIO(_GraphBinaryTypeIO):
321
+
322
+ python_type = datetime.datetime
323
+ graphbinary_type = DataType.date
324
+
325
+ @classmethod
326
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
327
+ try:
328
+ timestamp_seconds = calendar.timegm(obj.utctimetuple())
329
+ pts = timestamp_seconds * 1e3 + getattr(obj, 'microsecond', 0) / 1e3
330
+ except AttributeError:
331
+ pts = calendar.timegm(obj.timetuple()) * 1e3
332
+
333
+ ts = int(round(pts))
334
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
335
+ to_extend.extend(int64_pack(ts))
336
+ return to_extend
337
+
338
+ @classmethod
339
+ def objectify(cls, buff, reader, nullable=True):
340
+ return cls.is_null(buff, reader,
341
+ lambda b, r: datetime.datetime.utcfromtimestamp(int64_unpack(b.read(8)) / 1000.0),
342
+ nullable)
343
+
344
+
345
+ # Based on current implementation, this class must always be declared before FloatIO.
346
+ # Seems pretty fragile for future maintainers. Maybe look into this.
347
+ class TimestampIO(_GraphBinaryTypeIO):
348
+ python_type = statics.timestamp
349
+ graphbinary_type = DataType.timestamp
350
+
351
+ @classmethod
352
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
353
+ # Java timestamp expects milliseconds integer - Have to use int because of legacy Python
354
+ ts = int(round(obj * 1000))
355
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
356
+ to_extend.extend(int64_pack(ts))
357
+ return to_extend
358
+
359
+ @classmethod
360
+ def objectify(cls, buff, reader, nullable=True):
361
+ # Python timestamp expects seconds
362
+ return cls.is_null(buff, reader, lambda b, r: statics.timestamp(int64_unpack(b.read(8)) / 1000.0),
363
+ nullable)
364
+
365
+
366
+ def _long_bits_to_double(bits):
367
+ return unpack('d', pack('Q', bits))[0]
368
+
369
+
370
+ NAN = _long_bits_to_double(0x7ff8000000000000)
371
+ POSITIVE_INFINITY = _long_bits_to_double(0x7ff0000000000000)
372
+ NEGATIVE_INFINITY = _long_bits_to_double(0xFff0000000000000)
373
+
374
+
375
+ class FloatIO(LongIO):
376
+
377
+ python_type = FloatType
378
+ graphbinary_type = DataType.float
379
+ graphbinary_base_type = DataType.float
380
+ byte_format_pack = float_pack
381
+ byte_format_unpack = float_unpack
382
+
383
+ @classmethod
384
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
385
+ if math.isnan(obj):
386
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
387
+ to_extend.extend(cls.byte_format_pack(NAN))
388
+ elif math.isinf(obj) and obj > 0:
389
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
390
+ to_extend.extend(cls.byte_format_pack(POSITIVE_INFINITY))
391
+ elif math.isinf(obj) and obj < 0:
392
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
393
+ to_extend.extend(cls.byte_format_pack(NEGATIVE_INFINITY))
394
+ else:
395
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
396
+ to_extend.extend(cls.byte_format_pack(obj))
397
+
398
+ return to_extend
399
+
400
+ @classmethod
401
+ def objectify(cls, buff, reader, nullable=True):
402
+ return cls.is_null(buff, reader, lambda b, r: float_unpack(b.read(4)), nullable)
403
+
404
+
405
+ class DoubleIO(FloatIO):
406
+ """
407
+ Floats basically just fall through to double serialization.
408
+ """
409
+
410
+ graphbinary_type = DataType.double
411
+ graphbinary_base_type = DataType.double
412
+ byte_format_pack = double_pack
413
+ byte_format_unpack = double_unpack
414
+
415
+ @classmethod
416
+ def objectify(cls, buff, reader, nullable=True):
417
+ return cls.is_null(buff, reader, lambda b, r: double_unpack(b.read(8)), nullable)
418
+
419
+
420
+ class BigDecimalIO(_GraphBinaryTypeIO):
421
+
422
+ python_type = BigDecimal
423
+ graphbinary_type = DataType.bigdecimal
424
+
425
+ @classmethod
426
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
427
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
428
+ to_extend.extend(int32_pack(obj.scale))
429
+ return BigIntIO.write_bigint(obj.unscaled_value, to_extend)
430
+
431
+ @classmethod
432
+ def _read(cls, buff):
433
+ scale = int32_unpack(buff.read(4))
434
+ unscaled_value = BigIntIO.read_bigint(buff)
435
+ return BigDecimal(scale, unscaled_value)
436
+
437
+ @classmethod
438
+ def objectify(cls, buff, reader, nullable=False):
439
+ return cls.is_null(buff, reader, lambda b, r: cls._read(b), nullable)
440
+
441
+
442
+ class CharIO(_GraphBinaryTypeIO):
443
+ python_type = SingleChar
444
+ graphbinary_type = DataType.char
445
+
446
+ @classmethod
447
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
448
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
449
+ to_extend.extend(obj.encode("utf-8"))
450
+ return to_extend
451
+
452
+ @classmethod
453
+ def objectify(cls, buff, reader, nullable=True):
454
+ return cls.is_null(buff, reader, cls._read_char, nullable)
455
+
456
+ @classmethod
457
+ def _read_char(cls, b, r):
458
+ max_bytes = 4
459
+ x = b.read(1)
460
+ while max_bytes > 0:
461
+ max_bytes = max_bytes - 1
462
+ try:
463
+ return x.decode("utf-8")
464
+ except UnicodeDecodeError:
465
+ x += b.read(1)
466
+
467
+
468
+ class StringIO(_GraphBinaryTypeIO):
469
+
470
+ python_type = str
471
+ graphbinary_type = DataType.string
472
+
473
+ @classmethod
474
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
475
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
476
+ str_bytes = obj.encode("utf-8")
477
+ to_extend += int32_pack(len(str_bytes))
478
+ to_extend += str_bytes
479
+ return to_extend
480
+
481
+ @classmethod
482
+ def objectify(cls, buff, reader, nullable=True):
483
+ return cls.is_null(buff, reader, lambda b, r: b.read(cls.read_int(b)).decode("utf-8"), nullable)
484
+
485
+
486
+ class ListIO(_GraphBinaryTypeIO):
487
+
488
+ python_type = list
489
+ graphbinary_type = DataType.list
490
+
491
+ @classmethod
492
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
493
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
494
+ to_extend.extend(int32_pack(len(obj)))
495
+ for item in obj:
496
+ writer.to_dict(item, to_extend)
497
+
498
+ return to_extend
499
+
500
+ @classmethod
501
+ def objectify(cls, buff, reader, nullable=True):
502
+ return cls.is_null(buff, reader, cls._read_list, nullable)
503
+
504
+ @classmethod
505
+ def _read_list(cls, b, r):
506
+ size = cls.read_int(b)
507
+ the_list = []
508
+ while size > 0:
509
+ the_list.append(r.read_object(b))
510
+ size = size - 1
511
+
512
+ return the_list
513
+
514
+
515
+ class SetDeserializer(ListIO):
516
+
517
+ python_type = SetType
518
+ graphbinary_type = DataType.set
519
+
520
+ @classmethod
521
+ def objectify(cls, buff, reader, nullable=True):
522
+ return set(ListIO.objectify(buff, reader, nullable))
523
+
524
+
525
+ class MapIO(_GraphBinaryTypeIO):
526
+
527
+ python_type = DictType
528
+ graphbinary_type = DataType.map
529
+
530
+ @classmethod
531
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
532
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
533
+
534
+ to_extend.extend(int32_pack(len(obj)))
535
+ for k, v in obj.items():
536
+ writer.to_dict(k, to_extend)
537
+ writer.to_dict(v, to_extend)
538
+
539
+ return to_extend
540
+
541
+ @classmethod
542
+ def objectify(cls, buff, reader, nullable=True):
543
+ return cls.is_null(buff, reader, cls._read_map, nullable)
544
+
545
+ @classmethod
546
+ def _read_map(cls, b, r):
547
+ size = cls.read_int(b)
548
+ the_dict = {}
549
+ while size > 0:
550
+ k = HashableDict.of(r.read_object(b))
551
+ v = r.read_object(b)
552
+ the_dict[k] = v
553
+ size = size - 1
554
+
555
+ return the_dict
556
+
557
+
558
+ class UuidIO(_GraphBinaryTypeIO):
559
+
560
+ python_type = uuid.UUID
561
+ graphbinary_type = DataType.uuid
562
+
563
+ @classmethod
564
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
565
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
566
+ to_extend.extend(obj.bytes)
567
+ return to_extend
568
+
569
+ @classmethod
570
+ def objectify(cls, buff, reader, nullable=True):
571
+ return cls.is_null(buff, reader, lambda b, r: uuid.UUID(bytes=b.read(16)), nullable)
572
+
573
+
574
+ class EdgeIO(_GraphBinaryTypeIO):
575
+
576
+ python_type = Edge
577
+ graphbinary_type = DataType.edge
578
+
579
+ @classmethod
580
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
581
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
582
+
583
+ writer.to_dict(obj.id, to_extend)
584
+ StringIO.dictify(obj.label, writer, to_extend, True, False)
585
+ writer.to_dict(obj.inV.id, to_extend)
586
+ StringIO.dictify(obj.inV.label, writer, to_extend, True, False)
587
+ writer.to_dict(obj.outV.id, to_extend)
588
+ StringIO.dictify(obj.outV.label, writer, to_extend, True, False)
589
+ to_extend.extend(NULL_BYTES)
590
+ to_extend.extend(NULL_BYTES)
591
+
592
+ return to_extend
593
+
594
+ @classmethod
595
+ def objectify(cls, buff, reader, nullable=True):
596
+ return cls.is_null(buff, reader, cls._read_edge, nullable)
597
+
598
+ @classmethod
599
+ def _read_edge(cls, b, r):
600
+ edgeid = r.read_object(b)
601
+ edgelbl = r.to_object(b, DataType.string, False)
602
+ inv = Vertex(r.read_object(b), r.to_object(b, DataType.string, False))
603
+ outv = Vertex(r.read_object(b), r.to_object(b, DataType.string, False))
604
+ edge = Edge(edgeid, outv, edgelbl, inv)
605
+ b.read(4)
606
+ return edge
607
+
608
+
609
+ class PathIO(_GraphBinaryTypeIO):
610
+
611
+ python_type = Path
612
+ graphbinary_type = DataType.path
613
+
614
+ @classmethod
615
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
616
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
617
+ writer.to_dict(obj.labels, to_extend)
618
+ writer.to_dict(obj.objects, to_extend)
619
+ return to_extend
620
+
621
+ @classmethod
622
+ def objectify(cls, buff, reader, nullable=True):
623
+ return cls.is_null(buff, reader, lambda b, r: Path(r.read_object(b), r.read_object(b)), nullable)
624
+
625
+
626
+ class PropertyIO(_GraphBinaryTypeIO):
627
+
628
+ python_type = Property
629
+ graphbinary_type = DataType.property
630
+
631
+ @classmethod
632
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
633
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
634
+ StringIO.dictify(obj.key, writer, to_extend, True, False)
635
+ writer.to_dict(obj.value, to_extend)
636
+ to_extend.extend(NULL_BYTES)
637
+ return to_extend
638
+
639
+ @classmethod
640
+ def objectify(cls, buff, reader, nullable=True):
641
+ return cls.is_null(buff, reader, cls._read_property, nullable)
642
+
643
+ @classmethod
644
+ def _read_property(cls, b, r):
645
+ p = Property(r.to_object(b, DataType.string, False), r.read_object(b), None)
646
+ b.read(2)
647
+ return p
648
+
649
+
650
+ class TinkerGraphIO(_GraphBinaryTypeIO):
651
+
652
+ python_type = Graph
653
+ graphbinary_type = DataType.graph
654
+
655
+ @classmethod
656
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
657
+ raise AttributeError("TinkerGraph serialization is not currently supported by gremlin-python")
658
+
659
+ @classmethod
660
+ def objectify(cls, b, reader, as_value=False):
661
+ raise AttributeError("TinkerGraph deserialization is not currently supported by gremlin-python")
662
+
663
+
664
+ class VertexIO(_GraphBinaryTypeIO):
665
+
666
+ python_type = Vertex
667
+ graphbinary_type = DataType.vertex
668
+
669
+ @classmethod
670
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
671
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
672
+ writer.to_dict(obj.id, to_extend)
673
+ StringIO.dictify(obj.label, writer, to_extend, True, False)
674
+ to_extend.extend(NULL_BYTES)
675
+ return to_extend
676
+
677
+ @classmethod
678
+ def objectify(cls, buff, reader, nullable=True):
679
+ return cls.is_null(buff, reader, cls._read_vertex, nullable)
680
+
681
+ @classmethod
682
+ def _read_vertex(cls, b, r):
683
+ vertex = Vertex(r.read_object(b), r.to_object(b, DataType.string, False))
684
+ b.read(2)
685
+ return vertex
686
+
687
+
688
+ class VertexPropertyIO(_GraphBinaryTypeIO):
689
+
690
+ python_type = VertexProperty
691
+ graphbinary_type = DataType.vertexproperty
692
+
693
+ @classmethod
694
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
695
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
696
+ writer.to_dict(obj.id, to_extend)
697
+ StringIO.dictify(obj.label, writer, to_extend, True, False)
698
+ writer.to_dict(obj.value, to_extend)
699
+ to_extend.extend(NULL_BYTES)
700
+ to_extend.extend(NULL_BYTES)
701
+ return to_extend
702
+
703
+ @classmethod
704
+ def objectify(cls, buff, reader, nullable=True):
705
+ return cls.is_null(buff, reader, cls._read_vertexproperty, nullable)
706
+
707
+ @classmethod
708
+ def _read_vertexproperty(cls, b, r):
709
+ vp = VertexProperty(r.read_object(b), r.to_object(b, DataType.string, False), r.read_object(b), None)
710
+ b.read(4)
711
+ return vp
712
+
713
+
714
+ class _EnumIO(_GraphBinaryTypeIO):
715
+
716
+ @classmethod
717
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
718
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
719
+ StringIO.dictify(SymbolUtil.to_camel_case(str(obj.name)), writer, to_extend)
720
+ return to_extend
721
+
722
+ @classmethod
723
+ def objectify(cls, buff, reader, nullable=True):
724
+ return cls.is_null(buff, reader, cls._read_enumval, nullable)
725
+
726
+ @classmethod
727
+ def _read_enumval(cls, b, r):
728
+ enum_name = r.to_object(b)
729
+ return cls.python_type[SymbolUtil.to_snake_case(enum_name)]
730
+
731
+
732
+ class BarrierIO(_EnumIO):
733
+ graphbinary_type = DataType.barrier
734
+ python_type = Barrier
735
+
736
+
737
+ class CardinalityIO(_EnumIO):
738
+ graphbinary_type = DataType.cardinality
739
+ python_type = Cardinality
740
+
741
+
742
+ class ColumnIO(_EnumIO):
743
+ graphbinary_type = DataType.column
744
+ python_type = Column
745
+
746
+
747
+ class DirectionIO(_EnumIO):
748
+ graphbinary_type = DataType.direction
749
+ python_type = Direction
750
+
751
+ @classmethod
752
+ def _read_enumval(cls, b, r):
753
+ # Direction needs to retain all CAPS. note that to_/from_ are really just aliases of IN/OUT
754
+ # so they don't need to be accounted for in serialization
755
+ enum_name = r.to_object(b)
756
+ return cls.python_type[enum_name]
757
+
758
+
759
+ class OperatorIO(_EnumIO):
760
+ graphbinary_type = DataType.operator
761
+ python_type = Operator
762
+
763
+
764
+ class OrderIO(_EnumIO):
765
+ graphbinary_type = DataType.order
766
+ python_type = Order
767
+
768
+
769
+ class PickIO(_EnumIO):
770
+ graphbinary_type = DataType.pick
771
+ python_type = Pick
772
+
773
+
774
+ class PopIO(_EnumIO):
775
+ graphbinary_type = DataType.pop
776
+ python_type = Pop
777
+
778
+
779
+ class BindingIO(_GraphBinaryTypeIO):
780
+
781
+ python_type = Binding
782
+ graphbinary_type = DataType.binding
783
+
784
+ @classmethod
785
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
786
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
787
+ StringIO.dictify(obj.key, writer, to_extend, True, False)
788
+ writer.to_dict(obj.value, to_extend)
789
+ return to_extend
790
+
791
+ @classmethod
792
+ def objectify(cls, buff, reader, nullable=True):
793
+ return cls.is_null(buff, reader, lambda b, r: Binding(r.to_object(b, DataType.string, False),
794
+ reader.read_object(b)), nullable)
795
+
796
+
797
+ class BytecodeIO(_GraphBinaryTypeIO):
798
+ python_type = Bytecode
799
+ graphbinary_type = DataType.bytecode
800
+
801
+ @classmethod
802
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
803
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
804
+ bc = obj.bytecode if isinstance(obj, Traversal) else obj
805
+ to_extend.extend(int32_pack(len(bc.step_instructions)))
806
+ for inst in bc.step_instructions:
807
+ inst_name, inst_args = inst[0], inst[1:] if len(inst) > 1 else []
808
+ StringIO.dictify(inst_name, writer, to_extend, True, False)
809
+ to_extend.extend(int32_pack(len(inst_args)))
810
+ for arg in inst_args:
811
+ writer.to_dict(arg, to_extend)
812
+
813
+ to_extend.extend(int32_pack(len(bc.source_instructions)))
814
+ for inst in bc.source_instructions:
815
+ inst_name, inst_args = inst[0], inst[1:] if len(inst) > 1 else []
816
+ StringIO.dictify(inst_name, writer, to_extend, True, False)
817
+ to_extend.extend(int32_pack(len(inst_args)))
818
+ for arg in inst_args:
819
+ if isinstance(arg, TypeType):
820
+ writer.to_dict(GremlinType(arg().fqcn), to_extend)
821
+ else:
822
+ writer.to_dict(arg, to_extend)
823
+ return to_extend
824
+
825
+ @classmethod
826
+ def objectify(cls, buff, reader, nullable=True):
827
+ return cls.is_null(buff, reader, cls._read_bytecode, nullable)
828
+
829
+ @classmethod
830
+ def _read_bytecode(cls, b, r):
831
+ bytecode = Bytecode()
832
+
833
+ step_count = cls.read_int(b)
834
+ ix = 0
835
+ while ix < step_count:
836
+ inst = [r.to_object(b, DataType.string, False)]
837
+ inst_ct = cls.read_int(b)
838
+ iy = 0
839
+ while iy < inst_ct:
840
+ inst.append(r.read_object(b))
841
+ iy += 1
842
+ bytecode.step_instructions.append(inst)
843
+ ix += 1
844
+
845
+ source_count = cls.read_int(b)
846
+ ix = 0
847
+ while ix < source_count:
848
+ inst = [r.to_object(b, DataType.string, False)]
849
+ inst_ct = cls.read_int(b)
850
+ iy = 0
851
+ while iy < inst_ct:
852
+ inst.append(r.read_object(b))
853
+ iy += 1
854
+ bytecode.source_instructions.append(inst)
855
+ ix += 1
856
+
857
+ return bytecode
858
+
859
+
860
+ class TraversalIO(BytecodeIO):
861
+ python_type = GraphTraversal
862
+
863
+
864
+ class LambdaSerializer(_GraphBinaryTypeIO):
865
+
866
+ python_type = FunctionType
867
+ graphbinary_type = DataType.lambda_
868
+
869
+ @classmethod
870
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
871
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
872
+
873
+ lambda_result = obj()
874
+ script = lambda_result if isinstance(lambda_result, str) else lambda_result[0]
875
+ language = statics.default_lambda_language if isinstance(lambda_result, str) else lambda_result[1]
876
+
877
+ StringIO.dictify(language, writer, to_extend, True, False)
878
+
879
+ script_cleaned = script
880
+ script_args = -1
881
+
882
+ if language == "gremlin-groovy" and "->" in script:
883
+ # if the user has explicitly added parameters to the groovy closure then we can easily detect one or two
884
+ # arg lambdas - if we can't detect 1 or 2 then we just go with "unknown"
885
+ args = script[0:script.find("->")]
886
+ script_args = 2 if "," in args else 1
887
+
888
+ StringIO.dictify(script_cleaned, writer, to_extend, True, False)
889
+ to_extend.extend(int32_pack(script_args))
890
+
891
+ return to_extend
892
+
893
+
894
+ class PSerializer(_GraphBinaryTypeIO):
895
+ graphbinary_type = DataType.p
896
+ python_type = P
897
+
898
+ @classmethod
899
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
900
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
901
+
902
+ StringIO.dictify(obj.operator, writer, to_extend, True, False)
903
+
904
+ args = []
905
+ if obj.other is None:
906
+ if isinstance(obj.value, ListType):
907
+ args = obj.value
908
+ else:
909
+ args.append(obj.value)
910
+ else:
911
+ args.append(obj.value)
912
+ args.append(obj.other)
913
+
914
+ to_extend.extend(int32_pack(len(args)))
915
+ for a in args:
916
+ writer.to_dict(a, to_extend)
917
+
918
+ return to_extend
919
+
920
+
921
+ class MergeIO(_EnumIO):
922
+ graphbinary_type = DataType.merge
923
+ python_type = Merge
924
+
925
+
926
+ class ScopeIO(_EnumIO):
927
+ graphbinary_type = DataType.scope
928
+ python_type = Scope
929
+
930
+
931
+ class TIO(_EnumIO):
932
+ graphbinary_type = DataType.t
933
+ python_type = T
934
+
935
+
936
+ class TraverserIO(_GraphBinaryTypeIO):
937
+ graphbinary_type = DataType.traverser
938
+ python_type = Traverser
939
+
940
+ @classmethod
941
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
942
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
943
+ to_extend.extend(int64_pack(obj.bulk))
944
+ writer.to_dict(obj.object, to_extend)
945
+ return to_extend
946
+
947
+ @classmethod
948
+ def objectify(cls, buff, reader, nullable=True):
949
+ return cls.is_null(buff, reader, cls._read_traverser, nullable)
950
+
951
+ @classmethod
952
+ def _read_traverser(cls, b, r):
953
+ bulk = int64_unpack(b.read(8))
954
+ obj = r.read_object(b)
955
+ return Traverser(obj, bulk=bulk)
956
+
957
+
958
+ class ByteIO(_GraphBinaryTypeIO):
959
+ python_type = SingleByte
960
+ graphbinary_type = DataType.byte
961
+
962
+ @classmethod
963
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
964
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
965
+ to_extend.extend(int8_pack(obj))
966
+ return to_extend
967
+
968
+ @classmethod
969
+ def objectify(cls, buff, reader, nullable=True):
970
+ return cls.is_null(buff, reader,
971
+ lambda b, r: int.__new__(SingleByte, int8_unpack(b.read(1))),
972
+ nullable)
973
+
974
+
975
+ class ByteBufferIO(_GraphBinaryTypeIO):
976
+ python_type = ByteBufferType
977
+ graphbinary_type = DataType.bytebuffer
978
+
979
+ @classmethod
980
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
981
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
982
+ to_extend.extend(int32_pack(len(obj)))
983
+ to_extend.extend(obj)
984
+ return to_extend
985
+
986
+ @classmethod
987
+ def objectify(cls, buff, reader, nullable=True):
988
+ return cls.is_null(buff, reader, cls._read_bytebuffer, nullable)
989
+
990
+ @classmethod
991
+ def _read_bytebuffer(cls, b, r):
992
+ size = cls.read_int(b)
993
+ return ByteBufferType(b.read(size))
994
+
995
+
996
+ class BooleanIO(_GraphBinaryTypeIO):
997
+ python_type = bool
998
+ graphbinary_type = DataType.boolean
999
+
1000
+ @classmethod
1001
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
1002
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
1003
+ to_extend.extend(int8_pack(0x01 if obj else 0x00))
1004
+ return to_extend
1005
+
1006
+ @classmethod
1007
+ def objectify(cls, buff, reader, nullable=True):
1008
+ return cls.is_null(buff, reader,
1009
+ lambda b, r: True if int8_unpack(b.read(1)) == 0x01 else False,
1010
+ nullable)
1011
+
1012
+
1013
+ class TextPSerializer(_GraphBinaryTypeIO):
1014
+ graphbinary_type = DataType.textp
1015
+ python_type = TextP
1016
+
1017
+ @classmethod
1018
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
1019
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
1020
+
1021
+ StringIO.dictify(obj.operator, writer, to_extend, True, False)
1022
+
1023
+ args = []
1024
+ if obj.other is None:
1025
+ if isinstance(obj.value, ListType):
1026
+ args = obj.value
1027
+ else:
1028
+ args.append(obj.value)
1029
+ else:
1030
+ args.append(obj.value)
1031
+ args.append(obj.other)
1032
+
1033
+ to_extend.extend(int32_pack(len(args)))
1034
+ for a in args:
1035
+ writer.to_dict(a, to_extend)
1036
+
1037
+ return to_extend
1038
+
1039
+
1040
+ class BulkSetDeserializer(_GraphBinaryTypeIO):
1041
+
1042
+ graphbinary_type = DataType.bulkset
1043
+
1044
+ @classmethod
1045
+ def objectify(cls, buff, reader, nullable=True):
1046
+ return cls.is_null(buff, reader, cls._read_bulkset, nullable)
1047
+
1048
+ @classmethod
1049
+ def _read_bulkset(cls, b, r):
1050
+ size = cls.read_int(b)
1051
+ the_list = []
1052
+ while size > 0:
1053
+ itm = r.read_object(b)
1054
+ bulk = int64_unpack(b.read(8))
1055
+ for y in range(bulk):
1056
+ the_list.append(itm)
1057
+ size = size - 1
1058
+
1059
+ return the_list
1060
+
1061
+
1062
+ class MetricsDeserializer(_GraphBinaryTypeIO):
1063
+
1064
+ graphbinary_type = DataType.metrics
1065
+
1066
+ @classmethod
1067
+ def objectify(cls, buff, reader, nullable=True):
1068
+ return cls.is_null(buff, reader, cls._read_metrics, nullable)
1069
+
1070
+ @classmethod
1071
+ def _read_metrics(cls, b, r):
1072
+ metricid = r.to_object(b, DataType.string, False)
1073
+ name = r.to_object(b, DataType.string, False)
1074
+ duration = r.to_object(b, DataType.long, nullable=False)
1075
+ counts = r.to_object(b, DataType.map, nullable=False)
1076
+ annotations = r.to_object(b, DataType.map, nullable=False)
1077
+ metrics = r.to_object(b, DataType.list, nullable=False)
1078
+
1079
+ return {"id": metricid,
1080
+ "name": name,
1081
+ "dur": duration,
1082
+ "counts": counts,
1083
+ "annotations": annotations,
1084
+ "metrics": metrics}
1085
+
1086
+
1087
+ class TraversalMetricsDeserializer(_GraphBinaryTypeIO):
1088
+
1089
+ graphbinary_type = DataType.traversalmetrics
1090
+
1091
+ @classmethod
1092
+ def objectify(cls, buff, reader, nullable=True):
1093
+ return cls.is_null(buff, reader, cls._read_traversalmetrics, nullable)
1094
+
1095
+ @classmethod
1096
+ def _read_traversalmetrics(cls, b, r):
1097
+ duration = r.to_object(b, DataType.long, nullable=False)
1098
+ metrics = r.to_object(b, DataType.list, nullable=False)
1099
+
1100
+ return {"dur": duration,
1101
+ "metrics": metrics}
1102
+
1103
+
1104
+ class ClassSerializer(_GraphBinaryTypeIO):
1105
+ graphbinary_type = DataType.clazz
1106
+ python_type = GremlinType
1107
+
1108
+ @classmethod
1109
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
1110
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
1111
+ StringIO.dictify(obj.gremlin_type, writer, to_extend, True, False)
1112
+ return to_extend
1113
+
1114
+
1115
+ class TraversalStrategySerializer(_GraphBinaryTypeIO):
1116
+ graphbinary_type = DataType.traversalstrategy
1117
+ python_type = TraversalStrategy
1118
+
1119
+ @classmethod
1120
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
1121
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
1122
+
1123
+ ClassSerializer.dictify(GremlinType(obj.fqcn), writer, to_extend, True, False)
1124
+ conf = {k: cls._convert(v) for k, v in obj.configuration.items()}
1125
+ MapIO.dictify(conf, writer, to_extend, True, False)
1126
+
1127
+ return to_extend
1128
+
1129
+ @classmethod
1130
+ def _convert(cls, v):
1131
+ return v.bytecode if isinstance(v, Traversal) else v
1132
+
1133
+
1134
+ class DurationIO(_GraphBinaryTypeIO):
1135
+ python_type = timedelta
1136
+ graphbinary_type = DataType.duration
1137
+
1138
+ @classmethod
1139
+ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
1140
+ cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
1141
+ LongIO.dictify(obj.seconds, writer, to_extend, True, False)
1142
+ IntIO.dictify(obj.microseconds * 1000, writer, to_extend, True, False)
1143
+ return to_extend
1144
+
1145
+ @classmethod
1146
+ def objectify(cls, buff, reader, nullable=True):
1147
+ return cls.is_null(buff, reader, cls._read_duration, nullable)
1148
+
1149
+ @classmethod
1150
+ def _read_duration(cls, b, r):
1151
+ seconds = r.to_object(b, DataType.long, False)
1152
+ nanos = r.to_object(b, DataType.int, False)
1153
+ return timedelta(seconds=seconds, microseconds=nanos / 1000)