| 1 |
# Copyright DataStax, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
__all__ = ['Element', 'Vertex', 'Edge', 'VertexProperty', 'Path', 'T'] |
| 16 |
|
| 17 |
|
| 18 |
class Element(object): |
| 19 |
|
| 20 |
element_type = None |
| 21 |
|
| 22 |
_attrs = ('id', 'label', 'type', 'properties') |
| 23 |
|
| 24 |
def __init__(self, id, label, type, properties): |
| 25 |
if type != self.element_type: |
| 26 |
raise TypeError("Attempted to create %s from %s element", (type, self.element_type)) |
| 27 |
|
| 28 |
self.id = id |
| 29 |
self.label = label |
| 30 |
self.type = type |
| 31 |
self.properties = self._extract_properties(properties) |
| 32 |
|
| 33 |
@staticmethod |
| 34 |
def _extract_properties(properties): |
| 35 |
return dict(properties) |
| 36 |
|
| 37 |
def __eq__(self, other): |
| 38 |
return all(getattr(self, attr) == getattr(other, attr) for attr in self._attrs) |
| 39 |
|
| 40 |
def __str__(self): |
| 41 |
return str(dict((k, getattr(self, k)) for k in self._attrs)) |
| 42 |
|
| 43 |
|
| 44 |
class Vertex(Element): |
| 45 |
""" |
| 46 |
Represents a Vertex element from a graph query. |
| 47 |
|
| 48 |
Vertex ``properties`` are extracted into a ``dict`` of property names to list of :class:`~VertexProperty` (list |
| 49 |
because they are always encoded that way, and sometimes have multiple cardinality; VertexProperty because sometimes |
| 50 |
the properties themselves have property maps). |
| 51 |
""" |
| 52 |
|
| 53 |
element_type = 'vertex' |
| 54 |
|
| 55 |
@staticmethod |
| 56 |
def _extract_properties(properties): |
| 57 |
# vertex properties are always encoded as a list, regardless of Cardinality |
| 58 |
return dict((k, [VertexProperty(k, p['value'], p.get('properties')) for p in v]) for k, v in properties.items()) |
| 59 |
|
| 60 |
def __repr__(self): |
| 61 |
properties = dict((name, [{'label': prop.label, 'value': prop.value, 'properties': prop.properties} for prop in prop_list]) |
| 62 |
for name, prop_list in self.properties.items()) |
| 63 |
return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, |
| 64 |
self.id, self.label, |
| 65 |
self.type, properties) |
| 66 |
|
| 67 |
|
| 68 |
class VertexProperty(object): |
| 69 |
""" |
| 70 |
Vertex properties have a top-level value and an optional ``dict`` of properties. |
| 71 |
""" |
| 72 |
|
| 73 |
label = None |
| 74 |
""" |
| 75 |
label of the property |
| 76 |
""" |
| 77 |
|
| 78 |
value = None |
| 79 |
""" |
| 80 |
Value of the property |
| 81 |
""" |
| 82 |
|
| 83 |
properties = None |
| 84 |
""" |
| 85 |
dict of properties attached to the property |
| 86 |
""" |
| 87 |
|
| 88 |
def __init__(self, label, value, properties=None): |
| 89 |
self.label = label |
| 90 |
self.value = value |
| 91 |
self.properties = properties or {} |
| 92 |
|
| 93 |
def __eq__(self, other): |
| 94 |
return isinstance(other, VertexProperty) and self.label == other.label and self.value == other.value and self.properties == other.properties |
| 95 |
|
| 96 |
def __repr__(self): |
| 97 |
return "%s(%r, %r, %r)" % (self.__class__.__name__, self.label, self.value, self.properties) |
| 98 |
|
| 99 |
|
| 100 |
class Edge(Element): |
| 101 |
""" |
| 102 |
Represents an Edge element from a graph query. |
| 103 |
|
| 104 |
Attributes match initializer parameters. |
| 105 |
""" |
| 106 |
|
| 107 |
element_type = 'edge' |
| 108 |
|
| 109 |
_attrs = Element._attrs + ('inV', 'inVLabel', 'outV', 'outVLabel') |
| 110 |
|
| 111 |
def __init__(self, id, label, type, properties, |
| 112 |
inV, inVLabel, outV, outVLabel): |
| 113 |
super(Edge, self).__init__(id, label, type, properties) |
| 114 |
self.inV = inV |
| 115 |
self.inVLabel = inVLabel |
| 116 |
self.outV = outV |
| 117 |
self.outVLabel = outVLabel |
| 118 |
|
| 119 |
def __repr__(self): |
| 120 |
return "%s(%r, %r, %r, %r, %r, %r, %r, %r)" %\ |
| 121 |
(self.__class__.__name__, |
| 122 |
self.id, self.label, |
| 123 |
self.type, self.properties, |
| 124 |
self.inV, self.inVLabel, |
| 125 |
self.outV, self.outVLabel) |
| 126 |
|
| 127 |
|
| 128 |
class Path(object): |
| 129 |
""" |
| 130 |
Represents a graph path. |
| 131 |
|
| 132 |
Labels list is taken verbatim from the results. |
| 133 |
|
| 134 |
Objects are either :class:`~.Result` or :class:`~.Vertex`/:class:`~.Edge` for recognized types |
| 135 |
""" |
| 136 |
|
| 137 |
labels = None |
| 138 |
""" |
| 139 |
List of labels in the path |
| 140 |
""" |
| 141 |
|
| 142 |
objects = None |
| 143 |
""" |
| 144 |
List of objects in the path |
| 145 |
""" |
| 146 |
|
| 147 |
def __init__(self, labels, objects): |
| 148 |
# TODO fix next major |
| 149 |
# The Path class should not do any deserialization by itself. To fix in the next major. |
| 150 |
from cassandra.datastax.graph.query import _graph_object_sequence |
| 151 |
self.labels = labels |
| 152 |
self.objects = list(_graph_object_sequence(objects)) |
| 153 |
|
| 154 |
def __eq__(self, other): |
| 155 |
return self.labels == other.labels and self.objects == other.objects |
| 156 |
|
| 157 |
def __str__(self): |
| 158 |
return str({'labels': self.labels, 'objects': self.objects}) |
| 159 |
|
| 160 |
def __repr__(self): |
| 161 |
return "%s(%r, %r)" % (self.__class__.__name__, self.labels, [o.value for o in self.objects]) |
| 162 |
|
| 163 |
|
| 164 |
class T(object): |
| 165 |
""" |
| 166 |
Represents a collection of tokens for more concise Traversal definitions. |
| 167 |
""" |
| 168 |
|
| 169 |
name = None |
| 170 |
val = None |
| 171 |
|
| 172 |
# class attributes |
| 173 |
id = None |
| 174 |
""" |
| 175 |
""" |
| 176 |
|
| 177 |
key = None |
| 178 |
""" |
| 179 |
""" |
| 180 |
label = None |
| 181 |
""" |
| 182 |
""" |
| 183 |
value = None |
| 184 |
""" |
| 185 |
""" |
| 186 |
|
| 187 |
def __init__(self, name, val): |
| 188 |
self.name = name |
| 189 |
self.val = val |
| 190 |
|
| 191 |
def __str__(self): |
| 192 |
return self.name |
| 193 |
|
| 194 |
def __repr__(self): |
| 195 |
return "T.%s" % (self.name, ) |
| 196 |
|
| 197 |
|
| 198 |
T.id = T("id", 1) |
| 199 |
T.id_ = T("id_", 2) |
| 200 |
T.key = T("key", 3) |
| 201 |
T.label = T("label", 4) |
| 202 |
T.value = T("value", 5) |
| 203 |
|
| 204 |
T.name_to_value = { |
| 205 |
'id': T.id, |
| 206 |
'id_': T.id_, |
| 207 |
'key': T.key, |
| 208 |
'label': T.label, |
| 209 |
'value': T.value |
| 210 |
} |