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,766 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ import calendar
19
+ import datetime
20
+ import json
21
+ import uuid
22
+ import math
23
+ from collections import OrderedDict
24
+ from decimal import *
25
+ import logging
26
+ from datetime import timedelta
27
+
28
+ from aenum import Enum
29
+ from isodate import parse_duration, duration_isoformat
30
+
31
+ from gremlin_python import statics
32
+ from gremlin_python.statics import FloatType, FunctionType, ShortType, IntType, LongType, TypeType, DictType, ListType, SetType, SingleByte, ByteBufferType, SingleChar
33
+ from gremlin_python.process.traversal import Binding, Bytecode, Direction, P, TextP, Traversal, Traverser, TraversalStrategy, T
34
+ from gremlin_python.structure.graph import Edge, Property, Vertex, VertexProperty, Path
35
+ from gremlin_python.structure.io.util import HashableDict, SymbolUtil
36
+
37
+ log = logging.getLogger(__name__)
38
+
39
+ # When we fall back to a superclass's serializer, we iterate over this map.
40
+ # We want that iteration order to be consistent, so we use an OrderedDict,
41
+ # not a dict.
42
+ _serializers = OrderedDict()
43
+ _deserializers = {}
44
+
45
+
46
+ class GraphSONTypeType(type):
47
+ def __new__(mcs, name, bases, dct):
48
+ cls = super(GraphSONTypeType, mcs).__new__(mcs, name, bases, dct)
49
+ if not name.startswith('_'):
50
+ if cls.python_type:
51
+ _serializers[cls.python_type] = cls
52
+ if cls.graphson_type:
53
+ _deserializers[cls.graphson_type] = cls
54
+ return cls
55
+
56
+
57
+ class GraphSONUtil(object):
58
+ TYPE_KEY = "@type"
59
+ VALUE_KEY = "@value"
60
+
61
+ @classmethod
62
+ def typed_value(cls, type_name, value, prefix="g"):
63
+ out = {cls.TYPE_KEY: cls.format_type(prefix, type_name)}
64
+ if value is not None:
65
+ out[cls.VALUE_KEY] = value
66
+ return out
67
+
68
+ @classmethod
69
+ def format_type(cls, prefix, type_name):
70
+ return "%s:%s" % (prefix, type_name)
71
+
72
+
73
+ # Read/Write classes split to follow precedence of the Java API
74
+ class GraphSONWriter(object):
75
+ def __init__(self, serializer_map=None):
76
+ """
77
+ :param serializer_map: map from Python type to serializer instance implementing `dictify`
78
+ """
79
+ self.serializers = _serializers.copy()
80
+ if serializer_map:
81
+ self.serializers.update(serializer_map)
82
+
83
+ def write_object(self, objectData):
84
+ # to JSON
85
+ return json.dumps(self.to_dict(objectData), separators=(',', ':'))
86
+
87
+ def to_dict(self, obj):
88
+ """
89
+ Encodes python objects in GraphSON type-tagged dict values
90
+ """
91
+ try:
92
+ return self.serializers[type(obj)].dictify(obj, self)
93
+ except KeyError:
94
+ for key, serializer in self.serializers.items():
95
+ if isinstance(obj, key):
96
+ return serializer.dictify(obj, self)
97
+
98
+ if isinstance(obj, dict):
99
+ return dict((self.to_dict(k), self.to_dict(v)) for k, v in obj.items())
100
+ elif isinstance(obj, set):
101
+ return set([self.to_dict(o) for o in obj])
102
+ elif isinstance(obj, list):
103
+ return [self.to_dict(o) for o in obj]
104
+ else:
105
+ return obj
106
+
107
+
108
+ class GraphSONReader(object):
109
+ def __init__(self, deserializer_map=None):
110
+ """
111
+ :param deserializer_map: map from GraphSON type tag to deserializer instance implementing `objectify`
112
+ """
113
+ self.deserializers = _deserializers.copy()
114
+ if deserializer_map:
115
+ self.deserializers.update(deserializer_map)
116
+
117
+ def read_object(self, json_data):
118
+ # from JSON
119
+ return self.to_object(json.loads(json_data))
120
+
121
+ def to_object(self, obj):
122
+ """
123
+ Unpacks GraphSON type-tagged dict values into objects mapped in self.deserializers
124
+ """
125
+ if isinstance(obj, dict):
126
+ try:
127
+ return self.deserializers[obj[GraphSONUtil.TYPE_KEY]].objectify(obj[GraphSONUtil.VALUE_KEY], self)
128
+ except KeyError:
129
+ pass
130
+ return dict((self.to_object(k), self.to_object(v)) for k, v in obj.items())
131
+ elif isinstance(obj, set):
132
+ return set([self.to_object(o) for o in obj])
133
+ elif isinstance(obj, list):
134
+ return [self.to_object(o) for o in obj]
135
+ else:
136
+ return obj
137
+
138
+
139
+ class _GraphSONTypeIO(object, metaclass=GraphSONTypeType):
140
+ python_type = None
141
+ graphson_type = None
142
+
143
+ def dictify(self, obj, writer):
144
+ raise NotImplementedError()
145
+
146
+ def objectify(self, d, reader):
147
+ raise NotImplementedError()
148
+
149
+
150
+ class _BytecodeSerializer(_GraphSONTypeIO):
151
+ @classmethod
152
+ def _dictify_instructions(cls, instructions, writer):
153
+ out = []
154
+ for instruction in instructions:
155
+ inst = [instruction[0]]
156
+ inst.extend(writer.to_dict(arg) for arg in instruction[1:])
157
+ out.append(inst)
158
+ return out
159
+
160
+ @classmethod
161
+ def dictify(cls, bytecode, writer):
162
+ if isinstance(bytecode, Traversal):
163
+ bytecode = bytecode.bytecode
164
+ out = {}
165
+ if bytecode.source_instructions:
166
+ out["source"] = cls._dictify_instructions(bytecode.source_instructions, writer)
167
+ if bytecode.step_instructions:
168
+ out["step"] = cls._dictify_instructions(bytecode.step_instructions, writer)
169
+ return GraphSONUtil.typed_value("Bytecode", out)
170
+
171
+
172
+ class TraversalSerializer(_BytecodeSerializer):
173
+ python_type = Traversal
174
+
175
+
176
+ class BytecodeSerializer(_BytecodeSerializer):
177
+ python_type = Bytecode
178
+
179
+
180
+ class VertexSerializer(_GraphSONTypeIO):
181
+ python_type = Vertex
182
+ graphson_type = "g:Vertex"
183
+
184
+ @classmethod
185
+ def dictify(cls, vertex, writer):
186
+ return GraphSONUtil.typed_value("Vertex", {"id": writer.to_dict(vertex.id),
187
+ "label": writer.to_dict(vertex.label)})
188
+
189
+
190
+ class EdgeSerializer(_GraphSONTypeIO):
191
+ python_type = Edge
192
+ graphson_type = "g:Edge"
193
+
194
+ @classmethod
195
+ def dictify(cls, edge, writer):
196
+ return GraphSONUtil.typed_value("Edge", {"id": writer.to_dict(edge.id),
197
+ "outV": writer.to_dict(edge.outV.id),
198
+ "outVLabel": writer.to_dict(edge.outV.label),
199
+ "label": writer.to_dict(edge.label),
200
+ "inV": writer.to_dict(edge.inV.id),
201
+ "inVLabel": writer.to_dict(edge.inV.label)})
202
+
203
+
204
+ class VertexPropertySerializer(_GraphSONTypeIO):
205
+ python_type = VertexProperty
206
+ graphson_type = "g:VertexProperty"
207
+
208
+ @classmethod
209
+ def dictify(cls, vertex_property, writer):
210
+ return GraphSONUtil.typed_value("VertexProperty", {"id": writer.to_dict(vertex_property.id),
211
+ "label": writer.to_dict(vertex_property.label),
212
+ "value": writer.to_dict(vertex_property.value),
213
+ "vertex": writer.to_dict(vertex_property.vertex.id)})
214
+
215
+
216
+ class PropertySerializer(_GraphSONTypeIO):
217
+ python_type = Property
218
+ graphson_type = "g:Property"
219
+
220
+ @classmethod
221
+ def dictify(cls, property, writer):
222
+ elementDict = writer.to_dict(property.element)
223
+ if elementDict is not None:
224
+ valueDict = elementDict["@value"]
225
+ if "outVLabel" in valueDict:
226
+ del valueDict["outVLabel"]
227
+ if "inVLabel" in valueDict:
228
+ del valueDict["inVLabel"]
229
+ if "properties" in valueDict:
230
+ del valueDict["properties"]
231
+ if "value" in valueDict:
232
+ del valueDict["value"]
233
+ return GraphSONUtil.typed_value("Property", {"key": writer.to_dict(property.key),
234
+ "value": writer.to_dict(property.value),
235
+ "element": elementDict})
236
+
237
+
238
+ class TraversalStrategySerializer(_GraphSONTypeIO):
239
+ python_type = TraversalStrategy
240
+
241
+ @classmethod
242
+ def dictify(cls, strategy, writer):
243
+ configuration = {}
244
+ for key in strategy.configuration:
245
+ configuration[key] = writer.to_dict(strategy.configuration[key])
246
+ return GraphSONUtil.typed_value(strategy.strategy_name, configuration)
247
+
248
+
249
+ class TraverserIO(_GraphSONTypeIO):
250
+ python_type = Traverser
251
+ graphson_type = "g:Traverser"
252
+
253
+ @classmethod
254
+ def dictify(cls, traverser, writer):
255
+ return GraphSONUtil.typed_value("Traverser", {"value": writer.to_dict(traverser.object),
256
+ "bulk": writer.to_dict(traverser.bulk)})
257
+
258
+ @classmethod
259
+ def objectify(cls, d, reader):
260
+ return Traverser(reader.to_object(d["value"]),
261
+ reader.to_object(d["bulk"]))
262
+
263
+
264
+ class EnumSerializer(_GraphSONTypeIO):
265
+ python_type = Enum
266
+
267
+ @classmethod
268
+ def dictify(cls, enum, _):
269
+ return GraphSONUtil.typed_value(SymbolUtil.to_camel_case(type(enum).__name__),
270
+ SymbolUtil.to_camel_case(str(enum.name)))
271
+
272
+
273
+ class PSerializer(_GraphSONTypeIO):
274
+ python_type = P
275
+
276
+ @classmethod
277
+ def dictify(cls, p, writer):
278
+ out = {"predicate": p.operator,
279
+ "value": [writer.to_dict(p.value), writer.to_dict(p.other)] if p.other is not None else
280
+ writer.to_dict(p.value)}
281
+ return GraphSONUtil.typed_value("P", out)
282
+
283
+
284
+ class TextPSerializer(_GraphSONTypeIO):
285
+ python_type = TextP
286
+
287
+ @classmethod
288
+ def dictify(cls, p, writer):
289
+ out = {"predicate": p.operator,
290
+ "value": [writer.to_dict(p.value), writer.to_dict(p.other)] if p.other is not None else
291
+ writer.to_dict(p.value)}
292
+ return GraphSONUtil.typed_value("TextP", out)
293
+
294
+
295
+ class BindingSerializer(_GraphSONTypeIO):
296
+ python_type = Binding
297
+
298
+ @classmethod
299
+ def dictify(cls, binding, writer):
300
+ out = {"key": binding.key,
301
+ "value": writer.to_dict(binding.value)}
302
+ return GraphSONUtil.typed_value("Binding", out)
303
+
304
+
305
+ class LambdaSerializer(_GraphSONTypeIO):
306
+ python_type = FunctionType
307
+
308
+ @classmethod
309
+ def dictify(cls, lambda_object, writer):
310
+ lambda_result = lambda_object()
311
+ script = lambda_result if isinstance(lambda_result, str) else lambda_result[0]
312
+ language = statics.default_lambda_language if isinstance(lambda_result, str) else lambda_result[1]
313
+ out = {"script": script,
314
+ "language": language}
315
+ if language == "gremlin-groovy" and "->" in script:
316
+ # if the user has explicitly added parameters to the groovy closure then we can easily detect one or two
317
+ # arg lambdas - if we can't detect 1 or 2 then we just go with "unknown"
318
+ args = script[0:script.find("->")]
319
+ out["arguments"] = 2 if "," in args else 1
320
+ else:
321
+ out["arguments"] = -1
322
+
323
+ return GraphSONUtil.typed_value("Lambda", out)
324
+
325
+
326
+ class TypeSerializer(_GraphSONTypeIO):
327
+ python_type = TypeType
328
+
329
+ @classmethod
330
+ def dictify(cls, typ, writer):
331
+ return writer.to_dict(typ())
332
+
333
+
334
+ class UUIDIO(_GraphSONTypeIO):
335
+ python_type = uuid.UUID
336
+ graphson_type = "g:UUID"
337
+ graphson_base_type = "UUID"
338
+
339
+ @classmethod
340
+ def dictify(cls, obj, writer):
341
+ return GraphSONUtil.typed_value(cls.graphson_base_type, str(obj))
342
+
343
+ @classmethod
344
+ def objectify(cls, d, reader):
345
+ return cls.python_type(d)
346
+
347
+
348
+ class DateIO(_GraphSONTypeIO):
349
+ python_type = datetime.datetime
350
+ graphson_type = "g:Date"
351
+ graphson_base_type = "Date"
352
+
353
+ @classmethod
354
+ def dictify(cls, obj, writer):
355
+ try:
356
+ timestamp_seconds = calendar.timegm(obj.utctimetuple())
357
+ pts = timestamp_seconds * 1e3 + getattr(obj, 'microsecond', 0) / 1e3
358
+ except AttributeError:
359
+ pts = calendar.timegm(obj.timetuple()) * 1e3
360
+
361
+ ts = int(round(pts))
362
+ return GraphSONUtil.typed_value(cls.graphson_base_type, ts)
363
+
364
+ @classmethod
365
+ def objectify(cls, ts, reader):
366
+ # Python timestamp expects seconds
367
+ return datetime.datetime.utcfromtimestamp(ts / 1000.0)
368
+
369
+
370
+ # Based on current implementation, this class must always be declared before FloatIO.
371
+ # Seems pretty fragile for future maintainers. Maybe look into this.
372
+ class TimestampIO(_GraphSONTypeIO):
373
+ """A timestamp in Python is type float"""
374
+ python_type = statics.timestamp
375
+ graphson_type = "g:Timestamp"
376
+ graphson_base_type = "Timestamp"
377
+
378
+ @classmethod
379
+ def dictify(cls, obj, writer):
380
+ # Java timestamp expects milliseconds integer
381
+ # Have to use int because of legacy Python
382
+ ts = int(round(obj * 1000))
383
+ return GraphSONUtil.typed_value(cls.graphson_base_type, ts)
384
+
385
+ @classmethod
386
+ def objectify(cls, ts, reader):
387
+ # Python timestamp expects seconds
388
+ return cls.python_type(ts / 1000.0)
389
+
390
+
391
+ class _NumberIO(_GraphSONTypeIO):
392
+ @classmethod
393
+ def dictify(cls, n, writer):
394
+ if isinstance(n, bool): # because isinstance(False, int) and isinstance(True, int)
395
+ return n
396
+ return GraphSONUtil.typed_value(cls.graphson_base_type, n)
397
+
398
+ @classmethod
399
+ def objectify(cls, v, _):
400
+ return cls.python_type(v)
401
+
402
+
403
+ class ListIO(_GraphSONTypeIO):
404
+ python_type = ListType
405
+ graphson_type = "g:List"
406
+
407
+ @classmethod
408
+ def dictify(cls, l, writer):
409
+ new_list = []
410
+ for obj in l:
411
+ new_list.append(writer.to_dict(obj))
412
+ return GraphSONUtil.typed_value("List", new_list)
413
+
414
+ @classmethod
415
+ def objectify(cls, l, reader):
416
+ new_list = []
417
+ for obj in l:
418
+ new_list.append(reader.to_object(obj))
419
+ return new_list
420
+
421
+
422
+ class SetIO(_GraphSONTypeIO):
423
+ python_type = SetType
424
+ graphson_type = "g:Set"
425
+
426
+ @classmethod
427
+ def dictify(cls, s, writer):
428
+ new_list = []
429
+ for obj in s:
430
+ new_list.append(writer.to_dict(obj))
431
+ return GraphSONUtil.typed_value("Set", new_list)
432
+
433
+ @classmethod
434
+ def objectify(cls, s, reader):
435
+ """
436
+ By default, returns a python set
437
+
438
+ In case Java returns numeric values of different types which
439
+ python don't recognize, coerce and return a list.
440
+ See comments of TINKERPOP-1844 for more details
441
+ """
442
+ new_list = [reader.to_object(obj) for obj in s]
443
+ new_set = set(new_list)
444
+ if len(new_list) != len(new_set):
445
+ log.warning("Coercing g:Set to list due to java numeric values. "
446
+ "See TINKERPOP-1844 for more details.")
447
+ return new_list
448
+
449
+ return new_set
450
+
451
+
452
+ class MapType(_GraphSONTypeIO):
453
+ python_type = DictType
454
+ graphson_type = "g:Map"
455
+
456
+ @classmethod
457
+ def dictify(cls, d, writer):
458
+ l = []
459
+ for key in d:
460
+ l.append(writer.to_dict(key))
461
+ l.append(writer.to_dict(d[key]))
462
+ return GraphSONUtil.typed_value("Map", l)
463
+
464
+ @classmethod
465
+ def objectify(cls, l, reader):
466
+ new_dict = {}
467
+ if len(l) > 0:
468
+ x = 0
469
+ while x < len(l):
470
+ new_dict[HashableDict.of(reader.to_object(l[x]))] = reader.to_object(l[x + 1])
471
+ x = x + 2
472
+ return new_dict
473
+
474
+
475
+ class BulkSetIO(_GraphSONTypeIO):
476
+ graphson_type = "g:BulkSet"
477
+
478
+ @classmethod
479
+ def objectify(cls, l, reader):
480
+ new_list = []
481
+
482
+ # this approach basically mimics what currently existed in 3.3.4 and prior versions where BulkSet is
483
+ # basically just coerced to list. the limitation here is that if the value of a bulk exceeds the size of
484
+ # a list (into the long space) then stuff won't work nice.
485
+ if len(l) > 0:
486
+ x = 0
487
+ while x < len(l):
488
+ obj = reader.to_object(l[x])
489
+ bulk = reader.to_object(l[x + 1])
490
+ for y in range(bulk):
491
+ new_list.append(obj)
492
+ x = x + 2
493
+ return new_list
494
+
495
+
496
+ class FloatIO(_NumberIO):
497
+ python_type = FloatType
498
+ graphson_type = "g:Float"
499
+ graphson_base_type = "Float"
500
+
501
+ @classmethod
502
+ def dictify(cls, n, writer):
503
+ if isinstance(n, bool): # because isinstance(False, int) and isinstance(True, int)
504
+ return n
505
+ elif math.isnan(n):
506
+ return GraphSONUtil.typed_value(cls.graphson_base_type, "NaN")
507
+ elif math.isinf(n) and n > 0:
508
+ return GraphSONUtil.typed_value(cls.graphson_base_type, "Infinity")
509
+ elif math.isinf(n) and n < 0:
510
+ return GraphSONUtil.typed_value(cls.graphson_base_type, "-Infinity")
511
+ else:
512
+ return GraphSONUtil.typed_value(cls.graphson_base_type, n)
513
+
514
+ @classmethod
515
+ def objectify(cls, v, _):
516
+ if isinstance(v, str):
517
+ if v == 'NaN':
518
+ return float('nan')
519
+ elif v == "Infinity":
520
+ return float('inf')
521
+ elif v == "-Infinity":
522
+ return float('-inf')
523
+
524
+ return cls.python_type(v)
525
+
526
+
527
+ class BigDecimalIO(_NumberIO):
528
+ python_type = Decimal
529
+ graphson_type = "gx:BigDecimal"
530
+ graphson_base_type = "BigDecimal"
531
+
532
+ @classmethod
533
+ def dictify(cls, n, writer):
534
+ if isinstance(n, bool): # because isinstance(False, int) and isinstance(True, int)
535
+ return n
536
+ elif math.isnan(n):
537
+ return GraphSONUtil.typed_value(cls.graphson_base_type, "NaN", "gx")
538
+ elif math.isinf(n) and n > 0:
539
+ return GraphSONUtil.typed_value(cls.graphson_base_type, "Infinity", "gx")
540
+ elif math.isinf(n) and n < 0:
541
+ return GraphSONUtil.typed_value(cls.graphson_base_type, "-Infinity", "gx")
542
+ else:
543
+ return GraphSONUtil.typed_value(cls.graphson_base_type, str(n), "gx")
544
+
545
+ @classmethod
546
+ def objectify(cls, v, _):
547
+ if isinstance(v, str):
548
+ if v == 'NaN':
549
+ return Decimal('nan')
550
+ elif v == "Infinity":
551
+ return Decimal('inf')
552
+ elif v == "-Infinity":
553
+ return Decimal('-inf')
554
+
555
+ return Decimal(v)
556
+
557
+
558
+ class DoubleIO(FloatIO):
559
+ graphson_type = "g:Double"
560
+ graphson_base_type = "Double"
561
+
562
+
563
+ class Int64IO(_NumberIO):
564
+ python_type = LongType
565
+ graphson_type = "g:Int64"
566
+ graphson_base_type = "Int64"
567
+
568
+ @classmethod
569
+ def dictify(cls, n, writer):
570
+ # if we exceed Java long range then we need a BigInteger
571
+ if isinstance(n, bool):
572
+ return n
573
+ elif n < -9223372036854775808 or n > 9223372036854775807:
574
+ return GraphSONUtil.typed_value("BigInteger", str(n), "gx")
575
+ else:
576
+ return GraphSONUtil.typed_value(cls.graphson_base_type, n)
577
+
578
+
579
+ class BigIntegerIO(Int64IO):
580
+ graphson_type = "gx:BigInteger"
581
+
582
+
583
+ class Int32IO(Int64IO):
584
+ python_type = IntType
585
+ graphson_type = "g:Int32"
586
+ graphson_base_type = "Int32"
587
+
588
+ @classmethod
589
+ def dictify(cls, n, writer):
590
+ # if we exceed Java int range then we need a long
591
+ if isinstance(n, bool):
592
+ return n
593
+ elif n < -9223372036854775808 or n > 9223372036854775807:
594
+ return GraphSONUtil.typed_value("BigInteger", str(n), "gx")
595
+ elif n < -2147483648 or n > 2147483647:
596
+ return GraphSONUtil.typed_value("Int64", n)
597
+ else:
598
+ return GraphSONUtil.typed_value(cls.graphson_base_type, n)
599
+
600
+
601
+ class Int16IO(Int64IO):
602
+ python_type = ShortType
603
+ graphson_type = "gx:Int16"
604
+ graphson_base_type = "Int16"
605
+
606
+ @classmethod
607
+ def dictify(cls, n, writer):
608
+ # if we exceed Java int range then we need a long
609
+ if isinstance(n, bool):
610
+ return n
611
+ elif n < -9223372036854775808 or n > 9223372036854775807:
612
+ return GraphSONUtil.typed_value("BigInteger", str(n), "gx")
613
+ elif n < -2147483648 or n > 2147483647:
614
+ return GraphSONUtil.typed_value("Int64", n)
615
+ elif n < -32768 or n > 32767:
616
+ return GraphSONUtil.typed_value("Int32", n)
617
+ else:
618
+ return GraphSONUtil.typed_value(cls.graphson_base_type, n, "gx")
619
+
620
+ @classmethod
621
+ def objectify(cls, v, _):
622
+ return int.__new__(ShortType, v)
623
+
624
+
625
+ class ByteIO(_NumberIO):
626
+ python_type = SingleByte
627
+ graphson_type = "gx:Byte"
628
+ graphson_base_type = "Byte"
629
+
630
+ @classmethod
631
+ def dictify(cls, n, writer):
632
+ if isinstance(n, bool): # because isinstance(False, int) and isinstance(True, int)
633
+ return n
634
+ return GraphSONUtil.typed_value(cls.graphson_base_type, n, "gx")
635
+
636
+ @classmethod
637
+ def objectify(cls, v, _):
638
+ return int.__new__(SingleByte, v)
639
+
640
+
641
+ class ByteBufferIO(_GraphSONTypeIO):
642
+ python_type = ByteBufferType
643
+ graphson_type = "gx:ByteBuffer"
644
+ graphson_base_type = "ByteBuffer"
645
+
646
+ @classmethod
647
+ def dictify(cls, n, writer):
648
+ return GraphSONUtil.typed_value(cls.graphson_base_type, "".join(chr(x) for x in n), "gx")
649
+
650
+ @classmethod
651
+ def objectify(cls, v, _):
652
+ return cls.python_type(v, "utf8")
653
+
654
+
655
+ class CharIO(_GraphSONTypeIO):
656
+ python_type = SingleChar
657
+ graphson_type = "gx:Char"
658
+ graphson_base_type = "Char"
659
+
660
+ @classmethod
661
+ def dictify(cls, n, writer):
662
+ return GraphSONUtil.typed_value(cls.graphson_base_type, n, "gx")
663
+
664
+ @classmethod
665
+ def objectify(cls, v, _):
666
+ return str.__new__(SingleChar, v)
667
+
668
+
669
+ class DurationIO(_GraphSONTypeIO):
670
+ python_type = timedelta
671
+ graphson_type = "gx:Duration"
672
+ graphson_base_type = "Duration"
673
+
674
+ @classmethod
675
+ def dictify(cls, n, writer):
676
+ return GraphSONUtil.typed_value(cls.graphson_base_type, duration_isoformat(n), "gx")
677
+
678
+ @classmethod
679
+ def objectify(cls, v, _):
680
+ return parse_duration(v)
681
+
682
+
683
+ class VertexDeserializer(_GraphSONTypeIO):
684
+ graphson_type = "g:Vertex"
685
+
686
+ @classmethod
687
+ def objectify(cls, d, reader):
688
+ return Vertex(reader.to_object(d["id"]), d.get("label", "vertex"))
689
+
690
+
691
+ class EdgeDeserializer(_GraphSONTypeIO):
692
+ graphson_type = "g:Edge"
693
+
694
+ @classmethod
695
+ def objectify(cls, d, reader):
696
+ return Edge(reader.to_object(d["id"]),
697
+ Vertex(reader.to_object(d["outV"]), d.get("outVLabel", "vertex")),
698
+ d.get("label", "edge"),
699
+ Vertex(reader.to_object(d["inV"]), d.get("inVLabel", "vertex")))
700
+
701
+
702
+ class VertexPropertyDeserializer(_GraphSONTypeIO):
703
+ graphson_type = "g:VertexProperty"
704
+
705
+ @classmethod
706
+ def objectify(cls, d, reader):
707
+ vertex = Vertex(reader.to_object(d.get("vertex"))) if "vertex" in d else None
708
+ return VertexProperty(reader.to_object(d["id"]),
709
+ d["label"],
710
+ reader.to_object(d["value"]),
711
+ vertex)
712
+
713
+
714
+ class PropertyDeserializer(_GraphSONTypeIO):
715
+ graphson_type = "g:Property"
716
+
717
+ @classmethod
718
+ def objectify(cls, d, reader):
719
+ element = reader.to_object(d["element"]) if "element" in d else None
720
+ return Property(d["key"], reader.to_object(d["value"]), element)
721
+
722
+
723
+ class PathDeserializer(_GraphSONTypeIO):
724
+ graphson_type = "g:Path"
725
+
726
+ @classmethod
727
+ def objectify(cls, d, reader):
728
+ return Path(reader.to_object(d["labels"]), reader.to_object(d["objects"]))
729
+
730
+
731
+ class TDeserializer(_GraphSONTypeIO):
732
+ graphson_type = "g:T"
733
+
734
+ @classmethod
735
+ def objectify(cls, d, reader):
736
+ return T[d]
737
+
738
+
739
+ class DirectionIO(_GraphSONTypeIO):
740
+ graphson_type = "g:Direction"
741
+ graphson_base_type = "Direction"
742
+ python_type = Direction
743
+
744
+ @classmethod
745
+ def dictify(cls, d, writer):
746
+ return GraphSONUtil.typed_value(cls.graphson_base_type, d.name, "g")
747
+
748
+ @classmethod
749
+ def objectify(cls, d, reader):
750
+ return Direction[d]
751
+
752
+
753
+ class TraversalMetricsDeserializer(_GraphSONTypeIO):
754
+ graphson_type = "g:TraversalMetrics"
755
+
756
+ @classmethod
757
+ def objectify(cls, d, reader):
758
+ return reader.to_object(d)
759
+
760
+
761
+ class MetricsDeserializer(_GraphSONTypeIO):
762
+ graphson_type = "g:Metrics"
763
+
764
+ @classmethod
765
+ def objectify(cls, d, reader):
766
+ return reader.to_object(d)