|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2002-2018 "Neo4j," |
| 5 | +# Neo4j Sweden AB [http://neo4j.com] |
| 6 | +# |
| 7 | +# This file is part of Neo4j. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# you may not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | + |
| 21 | +""" |
| 22 | +This package contains classes for modelling the standard set of data types |
| 23 | +available within a Neo4j graph database. Most non-primitive types are |
| 24 | +represented by PackStream structures on the wire before being converted |
| 25 | +into concrete values through the PackStreamHydrant. |
| 26 | +""" |
| 27 | + |
| 28 | + |
| 29 | +from neo4j import Record |
| 30 | +from neo4j.compat import map_type, string, integer, ustr |
| 31 | + |
| 32 | +# These classes are imported in order to retain backward compatibility with 1.5. |
| 33 | +# They should be removed in 2.0. |
| 34 | +from .graph import Entity, Node, Relationship, Path |
| 35 | + |
| 36 | + |
| 37 | +INT64_MIN = -(2 ** 63) |
| 38 | +INT64_MAX = (2 ** 63) - 1 |
| 39 | + |
| 40 | + |
| 41 | +class PackStreamHydrator(object): |
| 42 | + |
| 43 | + def __init__(self, protocol_version): |
| 44 | + from .graph import Graph, hydration_functions as graph_hydration_functions |
| 45 | + |
| 46 | + super(PackStreamHydrator, self).__init__() |
| 47 | + self.graph = Graph() |
| 48 | + self.hydration_functions = {} |
| 49 | + self.hydration_functions.update(graph_hydration_functions(self.graph)) |
| 50 | + if protocol_version >= 2: |
| 51 | + from .spatial import hydration_functions as spatial_hydration_functions |
| 52 | + from .temporal import hydration_functions as temporal_hydration_functions |
| 53 | + self.hydration_functions.update(spatial_hydration_functions()) |
| 54 | + self.hydration_functions.update(temporal_hydration_functions()) |
| 55 | + |
| 56 | + def hydrate(self, values): |
| 57 | + """ Convert PackStream values into native values. |
| 58 | + """ |
| 59 | + from neobolt.packstream import Structure |
| 60 | + |
| 61 | + def hydrate_(obj): |
| 62 | + if isinstance(obj, Structure): |
| 63 | + try: |
| 64 | + f = self.hydration_functions[obj.tag] |
| 65 | + except KeyError: |
| 66 | + # If we don't recognise the structure type, just return it as-is |
| 67 | + return obj |
| 68 | + else: |
| 69 | + return f(*map(hydrate_, obj.fields)) |
| 70 | + elif isinstance(obj, list): |
| 71 | + return list(map(hydrate_, obj)) |
| 72 | + elif isinstance(obj, dict): |
| 73 | + return {key: hydrate_(value) for key, value in obj.items()} |
| 74 | + else: |
| 75 | + return obj |
| 76 | + |
| 77 | + return tuple(map(hydrate_, values)) |
| 78 | + |
| 79 | + def hydrate_records(self, keys, record_values): |
| 80 | + for values in record_values: |
| 81 | + yield Record(zip(keys, self.hydrate(values))) |
| 82 | + |
| 83 | + |
| 84 | +class PackStreamDehydrator(object): |
| 85 | + |
| 86 | + def __init__(self, protocol_version, supports_bytes=False): |
| 87 | + from .graph import Graph, dehydration_functions as graph_dehydration_functions |
| 88 | + self.supports_bytes = supports_bytes |
| 89 | + self.dehydration_functions = {} |
| 90 | + self.dehydration_functions.update(graph_dehydration_functions()) |
| 91 | + if protocol_version >= 2: |
| 92 | + from .spatial import dehydration_functions as spatial_dehydration_functions |
| 93 | + from .temporal import dehydration_functions as temporal_dehydration_functions |
| 94 | + self.dehydration_functions.update(spatial_dehydration_functions()) |
| 95 | + self.dehydration_functions.update(temporal_dehydration_functions()) |
| 96 | + |
| 97 | + def dehydrate(self, values): |
| 98 | + """ Convert native values into PackStream values. |
| 99 | + """ |
| 100 | + |
| 101 | + def dehydrate_(obj): |
| 102 | + try: |
| 103 | + f = self.dehydration_functions[type(obj)] |
| 104 | + except KeyError: |
| 105 | + pass |
| 106 | + else: |
| 107 | + return f(obj) |
| 108 | + if obj is None: |
| 109 | + return None |
| 110 | + elif isinstance(obj, bool): |
| 111 | + return obj |
| 112 | + elif isinstance(obj, integer): |
| 113 | + if INT64_MIN <= obj <= INT64_MAX: |
| 114 | + return obj |
| 115 | + raise ValueError("Integer out of bounds (64-bit signed integer values only)") |
| 116 | + elif isinstance(obj, float): |
| 117 | + return obj |
| 118 | + elif isinstance(obj, string): |
| 119 | + return ustr(obj) |
| 120 | + elif isinstance(obj, (bytes, bytearray)): # order is important here - bytes must be checked after string |
| 121 | + if self.supports_bytes: |
| 122 | + return obj |
| 123 | + else: |
| 124 | + raise TypeError("This PackSteam channel does not support BYTES (consider upgrading to Neo4j 3.2+)") |
| 125 | + elif isinstance(obj, (list, map_type)): |
| 126 | + return list(map(dehydrate_, obj)) |
| 127 | + elif isinstance(obj, dict): |
| 128 | + return {key: dehydrate_(value) for key, value in obj.items()} |
| 129 | + else: |
| 130 | + raise TypeError(obj) |
| 131 | + |
| 132 | + return tuple(map(dehydrate_, values)) |
0 commit comments