|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2002-2016 "Neo Technology," |
| 5 | +# Network Engine for Objects in Lund AB [http://neotechnology.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 | +import json |
| 22 | +import re |
| 23 | +from neo4j.v1 import Node, Relationship, Path |
| 24 | +from tck_util import TestValue |
| 25 | + |
| 26 | + |
| 27 | +def parse_values_to_comparable(row): |
| 28 | + return _parse(row, True) |
| 29 | + |
| 30 | + |
| 31 | +def parse_values(row): |
| 32 | + return _parse(row, False) |
| 33 | + |
| 34 | + |
| 35 | +def _parse(row, comparable=False): |
| 36 | + if is_array(row): |
| 37 | + values = get_array(row) |
| 38 | + result = [] |
| 39 | + for val in values: |
| 40 | + result.append(_parse(val, comparable)) |
| 41 | + return result |
| 42 | + elif is_map(row): |
| 43 | + if comparable: |
| 44 | + raise ValueError("No support for pasing maps of Node/Rels/paths atm") |
| 45 | + return get_map(row) |
| 46 | + else: |
| 47 | + if comparable: |
| 48 | + return value_to_comparable_object(row) |
| 49 | + else: |
| 50 | + return value_to_object(row) |
| 51 | + |
| 52 | + |
| 53 | +def is_array(val): |
| 54 | + return val[0] == '[' and val[-1] == ']' and val[1] != ':' |
| 55 | + |
| 56 | + |
| 57 | +def get_array(val): |
| 58 | + return val[1:-1].split(',') |
| 59 | + |
| 60 | + |
| 61 | +def is_map(val): |
| 62 | + return val[0] == '{' and val[-1] == '}' |
| 63 | + |
| 64 | + |
| 65 | +def get_map(val): |
| 66 | + return json.loads(val) |
| 67 | + |
| 68 | + |
| 69 | +def value_to_comparable_object(val): |
| 70 | + return TestValue(value_to_object(val)) |
| 71 | + |
| 72 | + |
| 73 | +def value_to_object(val): |
| 74 | + val = val.strip() |
| 75 | + PATH = '^(<\().*(\)>)$' |
| 76 | + NODE = '^(\().*(\))$' |
| 77 | + RELATIONSHIP = '^(\[:).*(\])$' |
| 78 | + INTEGER = '^(-?[0-9]+)$' |
| 79 | + STRING = '^(").*(")$' |
| 80 | + NULL = '^null$' |
| 81 | + BOOLEAN = '^(false|true)$' |
| 82 | + # TEST FLOAT AFTER INT |
| 83 | + FLOAT = '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$' |
| 84 | + if re.match(PATH, val): |
| 85 | + return get_path(val)[0] |
| 86 | + if re.match(RELATIONSHIP, val): |
| 87 | + return get_relationship(val)[0] |
| 88 | + if re.match(NODE, val): |
| 89 | + return get_node(val)[0] |
| 90 | + if re.match(NULL, val): |
| 91 | + return None |
| 92 | + if re.match(BOOLEAN, val): |
| 93 | + return bool(val) |
| 94 | + if re.match(STRING, val): |
| 95 | + return val[1:-1] |
| 96 | + if re.match(INTEGER, val): |
| 97 | + return int(val) |
| 98 | + if re.match(FLOAT, val): |
| 99 | + return float(val) |
| 100 | + raise TypeError("String value does not match any type. Got: %s" % val) |
| 101 | + |
| 102 | + |
| 103 | +def get_properties(prop): |
| 104 | + split_at = prop.find('{') |
| 105 | + if split_at != -1: |
| 106 | + n_start, prop = split_two(prop, split_at) |
| 107 | + split_at = prop.find('}') |
| 108 | + if split_at == -1: |
| 109 | + raise ValueError( |
| 110 | + "Node properties not correctly representetd. Found starrting '{' but no ending '}' in : %s" % prop) |
| 111 | + prop, n_end = split_two(prop, split_at + 1) |
| 112 | + n = n_start + n_end |
| 113 | + properties = json.loads(prop) |
| 114 | + return properties, n |
| 115 | + else: |
| 116 | + return {}, prop |
| 117 | + |
| 118 | + |
| 119 | +def get_labels(labels): |
| 120 | + labels = labels.split(':') |
| 121 | + if len(labels) == 1: |
| 122 | + return [], labels[0] |
| 123 | + n = labels[0] |
| 124 | + labels = labels[1:] |
| 125 | + if labels[-1].find(' ') != -1: |
| 126 | + labels[-1], n_end = split_two(labels[-1], labels[-1].find(' ')) |
| 127 | + n += n_end |
| 128 | + return labels, n |
| 129 | + |
| 130 | + |
| 131 | +def get_labels_and_properties(n): |
| 132 | + prop, n = get_properties(n) |
| 133 | + labels, n = get_labels(n) |
| 134 | + return labels, prop, n |
| 135 | + |
| 136 | + |
| 137 | +def split_two(string_entity, split_at): |
| 138 | + return string_entity[:split_at], string_entity[split_at:] |
| 139 | + |
| 140 | + |
| 141 | +def get_node(string_entity): |
| 142 | + if string_entity[0] != "(": |
| 143 | + raise ValueError("Excpected node which shuld start with '('. Got: %s" % string_entity[0]) |
| 144 | + split_at = string_entity.find(')') |
| 145 | + if split_at == -1: |
| 146 | + raise ValueError("Excpected node which shuld end with ')'. Found no such character in: %s" % string_entity) |
| 147 | + n, string_entity = split_two(string_entity, split_at + 1) |
| 148 | + n = n[1:-1] |
| 149 | + labels, properties, n = get_labels_and_properties(n) |
| 150 | + node = Node(labels=labels, properties=properties) |
| 151 | + return node, string_entity |
| 152 | + |
| 153 | + |
| 154 | +def get_relationship(string_entity): |
| 155 | + point_up = None |
| 156 | + if string_entity[:3] == "<-[": |
| 157 | + point_up = False |
| 158 | + string_entity = string_entity[3:] |
| 159 | + rel = string_entity[:string_entity.find(']-')] |
| 160 | + string_entity = string_entity[string_entity.find(']-') + 2:] |
| 161 | + elif string_entity[:2] == "-[": |
| 162 | + point_up = True |
| 163 | + string_entity = string_entity[2:] |
| 164 | + rel = string_entity[:string_entity.find(']-')] |
| 165 | + string_entity = string_entity[string_entity.find(']->') + 3:] |
| 166 | + elif string_entity[0] == "[": |
| 167 | + string_entity = string_entity[1:] |
| 168 | + rel = string_entity[:string_entity.find(']')] |
| 169 | + string_entity = string_entity[string_entity.find(']') + 1:] |
| 170 | + else: |
| 171 | + raise ValueError("Cant identify relationship from: %s" % string_entity) |
| 172 | + type, properties, str = get_labels_and_properties(rel) |
| 173 | + if len(type) > 1: |
| 174 | + raise ValueError("Relationship can only have one type. Got: %s" % type) |
| 175 | + relationship = Relationship(1, 2, type[0], properties=properties) |
| 176 | + return relationship, string_entity, point_up |
| 177 | + |
| 178 | + |
| 179 | +def get_path(string_path): |
| 180 | + id = 0 |
| 181 | + string_path = string_path[1:-1] |
| 182 | + n, string_path = get_node(string_path) |
| 183 | + list_of_nodes_and_rel = [n] |
| 184 | + n.identity = ++id |
| 185 | + while string_path != '': |
| 186 | + r, string_path, point_up = get_relationship(string_path) |
| 187 | + n, string_path = get_node(string_path) |
| 188 | + n.identity = ++id |
| 189 | + if point_up: |
| 190 | + r.start = list_of_nodes_and_rel[-1].identity |
| 191 | + r.end = n.identity |
| 192 | + r.identity = 0 |
| 193 | + else: |
| 194 | + r.start = n.identity |
| 195 | + r.end = list_of_nodes_and_rel[-1].identity |
| 196 | + r.identity = 0 |
| 197 | + list_of_nodes_and_rel.append(r) |
| 198 | + list_of_nodes_and_rel.append(n) |
| 199 | + path = Path(list_of_nodes_and_rel[0], *list_of_nodes_and_rel[1:]) |
| 200 | + return path, string_path |
0 commit comments