/
client.py
1513 lines (1313 loc) · 53.9 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
try:
import cPickle as pickle
except:
import pickle
import json
import urllib
import weakref
import warnings
from lucenequerybuilder import Q
import options
from constants import (BREADTH_FIRST, DEPTH_FIRST,
STOP_AT_END_OF_GRAPH,
NODE_GLOBAL, NODE_PATH, NODE_RECENT,
RELATIONSHIP_GLOBAL, RELATIONSHIP_PATH,
RELATIONSHIP_RECENT, NONE,
NODE, RELATIONSHIP, PATH, POSITION,
INDEX_FULLTEXT, TX_GET, TX_PUT, TX_POST, TX_DELETE,
RELATIONSHIPS_ALL, RELATIONSHIPS_IN, RELATIONSHIPS_OUT)
from request import (Request, NotFoundError, StatusException,
TransactionException)
__all__ = ["GraphDatabase", "Incoming", "Outgoing", "Undirected",
"StopAtDepth", "NotFoundError", "StatusException", "Q"]
class StopAtDepth(object):
"""
Only traverse to a certain depth.
"""
def __init__(self, depth):
self.depth = depth
def __get__(self):
return self.depth
class MetaTraversal(type):
"""
Metaclass for adding default attributes to Traversal class.
"""
def __new__(cls, name, bases, dic):
for attr in ["types", "order", "stop", "returnable", "uniqueness",
"paginated", "page_size", "time_out", "returns"]:
dic[attr] = dic.get(attr, None)
dic["is_returnable"] = dic.get("is_returnable",
dic.get("isReturnable", None))
dic["is_stop_node"] = dic.get("is_stop_node",
dic.get("isStopNode", None))
return type.__new__(cls, name, bases, dic)
class GraphDatabase(object):
"""
Main class for connection to Ne4j standalone REST server.
"""
def __init__(self, url):
self._transactions = {}
self.url = None
if url.endswith("/"):
self.url = url
else:
self.url = "%s/" % url
response, content = None, None
try:
response, content = Request().get(url)
except Exception:
raise NotFoundError(result="Unable get root")
if response.status == 200:
response_json = json.loads(content)
self._relationship_index = response_json['relationship_index']
self._node = response_json['node']
self._node_index = response_json['node_index']
self._reference_node = response_json['reference_node']
self._extensions_info = response_json['extensions_info']
self._extensions = response_json['extensions']
self.extensions = ExtensionsProxy(self._extensions)
self.nodes = NodesProxy(self._node, self._reference_node,
self._node_index)
# Backward compatibility. The current style is more pythonic
self.node = self.nodes
# HACK: Neo4j doesn't provide the URLs to access to relationships
url_parts = self._node.rpartition("node")
self._relationship = "%s%s%s" % (url_parts[0], RELATIONSHIP,
url_parts[2])
self.relationships = RelationshipsProxy(self._relationship,
self._relationship_index)
self.Traversal = self._get_traversal_class()
self._batch = "%sbatch" % self.url
else:
raise NotFoundError(response.status, "Unable get root")
def _get_reference_node(self):
return Node(self._reference_node)
reference_node = property(_get_reference_node)
def traverse(self, *args, **kwargs):
return self.reference_node.traverse(*args, **kwargs)
def _get_traversal_class(self):
cls = self
class Traversal(object):
__metaclass__ = MetaTraversal
def __init__(self, start_node=None):
if start_node and isinstance(start_node, Node):
self.start_node = start_node
else:
self.start_node = cls.reference_node
is_returnable = self.is_returnable
is_stop_node = self.is_stop_node
results = self.start_node.traverse(types=self.types,
order=self.order,
stop=self.stop,
returnable=self.returnable,
uniqueness=self.uniqueness,
is_stop_node=is_stop_node,
is_returnable=is_returnable,
paginated=self.paginated,
page_size=self.page_size,
time_out=self.time_out,
returns=self.returns)
self._items = results
self._index = len(results)
def __iter__(self):
return self
def next(self):
if self._index == 0:
raise StopIteration
self._index = self._index - 1
return self._items[self._index]
return Traversal
def transaction(self, using_globals=True, commit=True, update=True,
transaction_id=None, context=None):
if transaction_id not in self._transactions:
transaction_id = len(self._transactions.keys())
self._transactions[transaction_id] = Transaction(self, transaction_id,
context or {},
commit=commit,
update=update)
if using_globals:
globals()[options.TX_NAME] = self._transactions[transaction_id]
return self._transactions[transaction_id]
class TransactionOperationProxy(dict, object):
"""
TransactionOperationProxy class. A proxy to convert transaction operations
into final instances of Node or Relationship.
"""
def __init__(self, obj=None, attr=None, **kwargs):
self._self = None
if obj:
self._object_ref = weakref.ref(obj)
else:
self._object_ref = None
self._attribute = attr
super(TransactionOperationProxy, self).__init__(kwargs)
def __getattribute__(self, attr):
try:
if attr != "_self":
return getattr(object.__getattribute__(self, "_self"), attr)
except AttributeError:
pass
return object.__getattribute__(self, attr)
def __setattribute__(self, attr, val):
if attr in ("_object_ref", "_attribute"):
object.__setattribute__(self, attr, val)
elif attr != "_self":
setattr(self._self, attr, val)
else:
dict.__setattribute__(self, attr, val)
def __repr__(self):
return self.__unicode__()
def __str__(self):
return self.__unicode__()
def __unicode__(self):
attr = "__unicode__"
try:
return getattr(object.__getattribute__(self, "_self"), attr)()
except AttributeError:
pass
return object.__repr__(self)
def __getitem__(self, key):
attr = "__getitem__"
try:
return getattr(object.__getattribute__(self, "_self"), attr)(key)
except AttributeError:
pass
return dict.__getitem__(self, key)
def get_object(self):
if self._object_ref:
return self._object_ref()
else:
return None
def get_attribute(self):
return self._attribute
def change(self, cls, url, data=None):
self._self = cls(url, update_dict=data)
class Transaction(object):
"""
Transaction class.
"""
def __init__(self, cls, transaction_id, context, commit=True, update=True):
self._class = cls
self.url = self._class._batch
self.id = transaction_id
self.context = context
self.auto_commit = commit
self.auto_update = update
self.operations = []
self.references = []
self._value = None
self._attribute = None
def __call__(self, value):
"""
Call method in order to allow expresions like
>>> with gdb.transaction(using_globals=False) as tx:
...: n[key] = tx(value)
"""
self._value = value
return self
def get_value(self):
return self._value
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
del self._class._transactions[self.id]
if options.TX_NAME in globals():
del globals()[options.TX_NAME]
if self.auto_commit:
return self.commit(type, value, traceback)
return True
def _results_list_to_dict(self, results):
result_dict = {}
for result in results:
result_id = result.pop("id")
if "body" in result and "self" in result["body"]:
if NODE in result["body"]["self"]:
result["returns"] = Node
elif RELATIONSHIP in result["body"]["self"]:
result["returns"] = Relationship
if "returns" in result:
result_dict[result_id] = result
return result_dict
def _batch(self):
response, content = Request().post(self.url, data=self.operations)
if response.status == 200:
results_list = json.loads(content)
results_dict = self._results_list_to_dict(results_list)
return results_dict
else:
raise TransactionException(response.status)
def commit(self, *args, **kwargs):
results = self._batch()
# Objects to update
if self.auto_update:
for operation in self.operations:
on_object = operation.get_object()
if hasattr(on_object, "update"):
on_object.update(extensions=False, delete_on_not_found=True)
# Objects to return
for referenced_object in self.references:
ref_object = referenced_object()
result = results[ref_object["id"]]
if "returns" in result:
if "location" in result:
cls = result["returns"]
url = result["location"]
elif "body" in result:
cls = result["returns"]
url = result["body"]["self"]
if cls and url:
ref_object.change(cls, url, data=result)
self.references = []
self.operations = []
# Destroy the object after commit
self = None
if "type" in kwargs and isinstance(kwargs["type"], Exception):
raise kwargs["type"]
else:
return True
def subscribe(self, method, url, data=None, obj=None):
params = {
"method": method,
"to": "/%s" % url.replace(self._class.url, ""),
"id": len(self.operations),
}
if data:
params.update({"body": data})
# Reunify PUT methods in just one
transaction_operation = None
for i, operation in enumerate(self.operations):
if (operation["method"] == params["method"] == TX_PUT
and operation["to"] == params["to"]):
if "body" in operation:
self.operations[i]["body"].update(params["body"])
else:
self.operations[i]["body"] = params["body"]
transaction_operation = operation
break
if not transaction_operation:
transaction_operation = TransactionOperationProxy(obj=obj,
**params)
self.operations.append(transaction_operation)
if method in (TX_POST, TX_GET):
self.references.append(weakref.ref(transaction_operation))
return transaction_operation
@staticmethod
def get_transaction(tx=None):
if not tx and options.TX_NAME in globals():
return globals()[options.TX_NAME]
elif isinstance(tx, Transaction):
return tx
elif (isinstance(tx, (list, tuple)) and len(tx) > 1
and isinstance(tx[-1], Transaction)):
return tx[-1]
else:
return None
class Base(object):
"""
Base class.
"""
def __init__(self, url, create=False, data={}, update_dict={}):
self._dic = {}
self.url = None
# TODO: Allow update an object using only a new data dict of properties
self._update_dict = update_dict
if url.endswith("/"):
url = url[:-1]
if create:
response, content = Request().post(url, data=data)
if response.status == 201:
self._dic.update(data.copy())
self._update_dict_data()
self.url = response.get("location")
else:
raise NotFoundError(response.status, "Invalid data sent")
if not self.url:
self.url = url
self.update()
def _update_dict_data(self):
if "data" in self._dic:
self._dic["data"] = dict((self._safe_string(k),
self._safe_string(v))
for k, v in self._dic["data"].items())
def _safe_string(self, s):
return unicode(s.decode("utf-8"))
def update(self, extensions=True, delete_on_not_found=False):
response, content = Request().get(self.url)
if response.status == 200:
self._dic.update(json.loads(content).copy())
if extensions:
self._extensions = self._dic.get('extensions', {})
if self._extensions:
self.extensions = ExtensionsProxy(self._extensions)
elif delete_on_not_found and response.status == 404:
self.url = None
self._dic = {}
self = None
del self
else:
raise NotFoundError(response.status, "Unable get object")
def delete(self, key=None, tx=None):
if key:
return self.__delitem__(key, tx=tx)
tx = Transaction.get_transaction(tx)
if tx:
return tx.subscribe(TX_DELETE, self.url, obj=self)
response, content = Request().delete(self.url)
if response.status == 204:
del self
elif response.status == 404:
raise NotFoundError(response.status, "Node or property not found")
else:
raise StatusException(response.status, "Node could not be "\
"deleted (still has " \
"relationships?)")
def __getitem__(self, key, tx=None):
property_url = self._dic["property"].replace("{key}", smart_quote(key))
tx = Transaction.get_transaction(tx)
if tx:
return tx.subscribe(TX_GET, property_url, obj=self)
response, content = Request().get(property_url)
if response.status == 200:
self._dic["data"][key] = json.loads(content)
else:
if options.SMART_ERRORS:
raise KeyError()
else:
raise NotFoundError(response.status,
"Node or propery not found")
return self._dic["data"][key]
def get(self, key, *args, **kwargs):
tx = kwargs.get("tx", None)
try:
return self.__getitem__(key, tx=tx)
except (KeyError, NotFoundError, StatusException):
if args:
return args[0]
elif "default" in kwargs:
return kwargs["default"]
elif options.SMART_ERRORS:
raise KeyError(key)
else:
raise NotFoundError()
def __contains__(self, obj):
return obj in self._dic["data"]
def __setitem__(self, key, value, tx=None):
if isinstance(key, (list, tuple)):
tx = tx or key[1]
key = key[0]
if isinstance(value, Transaction):
tx = tx or value
value = value.get_value()
property_url = self._dic["property"].replace("{key}", smart_quote(key))
tx = Transaction.get_transaction(tx)
if tx:
transaction_url = self._dic["property"].replace("{key}", "")
return tx.subscribe(TX_PUT, transaction_url, {key: value}, obj=self)
response, content = Request().put(property_url, data=value)
if response.status == 204:
self._dic["data"].update({key: value})
elif response.status == 404:
raise NotFoundError(response.status, "Node or property not found")
else:
raise StatusException(response.status, "Invalid data sent")
def set(self, key, value, tx=None):
tx = Transaction.get_transaction(tx)
if tx:
return self.__setitem__(key, value, tx=tx)
self.__setitem__(key, value)
def __delitem__(self, key, tx=None):
property_url = self._dic["property"].replace("{key}", smart_quote(key))
tx = Transaction.get_transaction(tx)
if tx:
return tx.subscribe(TX_DELETE, property_url, obj=self)
response, content = Request().delete(property_url)
if response.status == 204:
del self._dic["data"][key]
elif response.status == 404:
if options.SMART_ERRORS:
raise KeyError()
else:
raise NotFoundError(response.status,
"Node or propery not found")
else:
raise StatusException(response.status, "Node or propery not found")
def __len__(self):
return len(self._dic["data"])
def __iter__(self):
return self._dic["data"].__iter__()
def __eq__(self, obj):
if not self.url and not self._dic:
return (obj == None)
else:
return (hasattr(obj, "url")
and self.url == obj.url
and hasattr(obj, "__class__")
and self.__class__ == obj.__class__)
def __ne__(self, obj):
return not self.__cmp__(obj)
def __nonzero__(self):
return bool(self._dic)
def __repr__(self):
return self.__unicode__()
def __str__(self):
return self.__unicode__()
def __unicode__(self):
if not self.url and not self._dic:
return None
else:
return u"<Neo4j %s: %s>" % (self.__class__.__name__, self.url)
def _get_properties(self):
return self._dic["data"]
def _set_properties(self, props={}):
if not props:
return None
properties_url = self._dic["properties"]
response, content = Request().put(properties_url, data=props)
if response.status == 204:
self._dic["data"] = props.copy()
self._update_dict_data()
return props
elif response.status == 400:
raise StatusException(response.status, "Invalid data sent")
else:
raise NotFoundError(response.status, "Properties not found")
def _del_properties(self):
properties_url = self._dic["properties"]
response, content = Request().delete(properties_url)
if response.status == 204:
self._dic["data"] = {}
else:
raise NotFoundError(response.status, "Properties not found")
# TODO: Create an own Property class to handle transactions
properties = property(_get_properties, _set_properties, _del_properties)
class Iterable(list):
"""
Class to iterate among returned objects.
"""
def __init__(self, cls, lst, attr):
self._list = lst
self._index = len(lst)
self._class = cls
self._attribute = attr
super(Iterable, self).__init__(lst)
def __getslice__(self, *args, **kwargs):
eltos = super(Iterable, self).__getslice__(*args, **kwargs)
if self._attribute:
return [self._class(elto[self._attribute]) for elto in eltos]
else:
return [self._class(elto) for elto in eltos]
def __getitem__(self, index):
elto = super(Iterable, self).__getitem__(index)
if self._attribute:
return self._class(elto[self._attribute])
else:
return self._class(elto)
def __repr__(self):
return self.__unicode__()
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u"<Neo4j %s: %s>" % (self.__class__.__name__,
self._class.__name__)
def __contains__(self, value):
if isinstance(value, Base) and hasattr(value, "url"):
if self._attribute:
return value.url in [elto[self._attribute]
for elto in self._list]
else:
return value.url in self._list
return False
def __iter__(self):
return self
def next(self):
if self._index == 0:
raise StopIteration
self._index = self._index - 1
return self.__getitem__(self._index)
class NodesProxy(dict):
"""
Class proxy for node in order to allow get a node by id and
create new nodes through calling.
"""
def __init__(self, node, reference_node=None, node_index=None):
self._node = node
self._reference_node = reference_node
self._node_index = node_index
def __call__(self, **kwargs):
tx = Transaction.get_transaction(kwargs.get("tx", None))
reference = kwargs.pop("reference", False)
if reference and self._reference_node:
return self.__getitem__(self._reference_node, tx=tx)
else:
return self.create(**kwargs)
def __getitem__(self, key, tx=None):
tx = Transaction.get_transaction(tx)
if tx:
if isinstance(key, (str, unicode)) and key.startswith(self._node):
return tx.subscribe(TX_GET, key, obj=self)
else:
return tx.subscribe(TX_GET, "%s/%s/" % (self._node, key), obj=self)
else:
if isinstance(key, (str, unicode)) and key.startswith(self._node):
return Node(key)
else:
return Node("%s/%s/" % (self._node, key))
def get(self, key, *args, **kwargs):
tx = Transaction.get_transaction(kwargs.get("tx", None))
try:
return self.__getitem__(key, tx=tx)
except (KeyError, NotFoundError, StatusException):
if args:
return args[0]
elif "default" in kwargs:
return kwargs["default"]
else:
raise NotFoundError()
def create(self, **kwargs):
tx = Transaction.get_transaction(kwargs.get("tx", None))
if tx:
if "tx" in kwargs and isinstance(kwargs["tx"], Transaction):
x = kwargs.pop("tx", None)
del x # Makes pyflakes happy
return tx.subscribe(TX_POST, self._node, data=kwargs, obj=self)
else:
return Node(self._node, create=True, data=kwargs)
def delete(self, key, tx=None):
tx = Transaction.get_transaction(tx)
if tx:
return self.__getitem__(key, tx=tx)
else:
node = self.__getitem__(key)
del node
def _indexes(self):
if self._node_index:
return IndexesProxy(self._node_index, NODE)
indexes = property(_indexes)
class Node(Base):
"""
Node class.
"""
def __getattr__(self, relationship_name, *args, **kwargs):
"""
HACK: Allow to set node relationship
"""
warnings.warn("Deprecated, in favor of pythonic style to declare "
"relationships: n2.relationships.create(rel_name, n2). "
"This is needed in order to handle pickling in nodes.",
DeprecationWarning)
def relationship(to, *args, **kwargs):
tx = Transaction.get_transaction(kwargs.get("tx", None))
create_relationship_url = self._dic["create_relationship"]
data = {
"to": to.url,
"type": relationship_name,
}
if "tx" in kwargs and isinstance(kwargs["tx"], Transaction):
x = kwargs.pop("tx", None)
del x # Makes pyflakes happy
if kwargs:
data.update({"data": kwargs})
if tx:
return tx.subscribe(TX_POST, create_relationship_url,
data=data, obj=self)
response, content = Request().post(create_relationship_url,
data=data)
if response.status == 201:
return Relationship(response.get("location"))
elif response.status == 404:
raise NotFoundError(response.status, "Node specified by the " \
"URI not of \"to\" node" \
"not found")
else:
raise StatusException(response.status, "Invalid data sent")
return relationship
# HACK: Special methods for handle pickling manually
def __getstate__(self):
data = {}
for key, value in self.__dict__.items():
try:
encoded = pickle.dumps(value)
except pickle.PicklingError:
encoded = pickle.dumps(pickle.Unpickable())
data[key] = encoded
return data
def __setstate__(self, state):
for key, value in state.items():
self.__dict__[key] = pickle.loads(value)
def _get_relationships(self):
"""
HACK: Return a 3-methods class: incoming, outgoing and all.
"""
return Relationships(self)
relationships = property(_get_relationships)
def _get_id(self):
return int(self.url.split("/")[-1])
id = property(_get_id)
def traverse(self, types=None, order=None, stop=None, returnable=None,
uniqueness=None, is_stop_node=None, is_returnable=None,
paginated=False, page_size=None, time_out=None,
returns=None):
data = {}
if order in (BREADTH_FIRST, DEPTH_FIRST):
data.update({"order": order})
if isinstance(stop, (int, float)):
data.update({"max_depth": stop})
elif stop == STOP_AT_END_OF_GRAPH:
data.update({'prune_evaluator': {
'language': 'javascript',
'body': 'false',
}})
if returnable in (BREADTH_FIRST, DEPTH_FIRST):
data.update({"return_filter": {
"language": "builtin",
"name": returnable,
}})
if uniqueness in (NODE_GLOBAL, NODE_PATH, NODE_RECENT, NODE,
RELATIONSHIP_GLOBAL, RELATIONSHIP_PATH,
RELATIONSHIP_RECENT, NONE):
data.update({"uniqueness": uniqueness})
if types:
if not isinstance(types, (list, tuple)):
types = [types]
relationships = []
for relationship in types:
if relationship.direction == "both":
relationships.append({"type": relationship.type})
else:
relationships.append({"type": relationship.type,
"direction": relationship.direction})
if relationships:
data.update({"relationships": relationships})
if returns not in (NODE, RELATIONSHIP, PATH, POSITION):
returns = NODE
if ((paginated or page_size or time_out)
and "paged_traverse" in self._dic):
traverse_params = []
if page_size:
traverse_params.append("pageSize=%s" % page_size)
if time_out:
traverse_params.append("leaseTime=%s" % time_out)
traverse_url = self._dic["paged_traverse"].replace("{returnType}",
returns)
traverse_url = traverse_url.replace("{?pageSize,leaseTime}", "")
if traverse_params:
traverse_url = "%s?%s" % (traverse_url,
"&".join(traverse_params))
return PaginatedTraversal(traverse_url, returns, data=data)
else:
traverse_url = self._dic["traverse"].replace("{returnType}",
returns)
response, content = Request().post(traverse_url, data=data)
if response.status == 200:
results_list = json.loads(content)
if returns == NODE:
return Iterable(Node, results_list, "self")
elif returns == RELATIONSHIP:
return Iterable(Relationship, results_list, "self")
elif returns == PATH:
return Iterable(Path, results_list)
elif returns == POSITION:
return Iterable(Position, results_list)
elif response.status == 404:
raise NotFoundError(response.status, "Node or relationship " \
"not found")
else:
raise StatusException(response.status, "Invalid data sent")
class PaginatedTraversal(object):
"""
Class for paged traversals.
"""
def __init__(self, url, returns, data=None):
self.url = url
self.returns = returns
self.data = data
self._results = []
response, content = Request().post(self.url, data=self.data)
if response.status == 201:
self._results = json.loads(content)
self._next_url = response.get("location")
else:
self._next_url = None
def __iter__(self):
return self
def next(self):
if self._results:
self._item = False
if self.returns == NODE:
results = Iterable(Node, self._results, "self")
elif self.returns == RELATIONSHIP:
results = Iterable(Relationship, self._results, "self")
elif self.returns == PATH:
results = Iterable(Path, self._results)
elif self.returns == POSITION:
results = Iterable(Position, self._results)
self._results = []
if self._next_url:
response, content = Request().get(self._next_url)
if response.status == 200:
self._results = json.loads(content)
self._next_url = response.get("location")
else:
self._next_url = None
return results
else:
raise StopIteration
class IndexesProxy(dict):
"""
Class proxy for indexes (nodes and relationships).
"""
def __init__(self, index_url, index_for=NODE):
self.url = index_url
self._index_for = index_for
self._dict = self._get_dict()
def __getitem__(self, attr):
return self._dict[attr]
def __repr__(self):
return self.__unicode__()
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return unicode(self._dict)
def _get_dict(self):
indexes_dict = {}
response, content = Request().get(self.url)
if response.status == 200:
indexes_dict = json.loads(content)
for index_name, index_properties in indexes_dict.items():
index_props = {}
for key, val in index_properties.items():
index_props[str(key)] = val
indexes_dict[index_name] = Index(self._index_for, index_name,
**index_props)
return indexes_dict
elif response.status == 404:
raise NotFoundError(response.status, "Indexes not found")
elif response.status == 204:
return indexes_dict
else:
raise StatusException(response.status,
"Error requesting indexes with GET %s" \
% self.url)
def create(self, name, **kwargs):
data = {
'name': name,
'config': {
'type': kwargs.get("type", INDEX_FULLTEXT),
'provider': kwargs.get("provider", "lucene"),
}
}
if name not in self._dict:
response, content = Request().post(self.url, data=data)
if response.status == 201:
loaded_dict = json.loads(content)
result_dict = {}
for key, val in loaded_dict.items():
result_dict[str(key)] = val
self._dict[name] = Index(self._index_for, name, **result_dict)
else:
raise StatusException(response.status, "Invalid data sent")
return self._dict[name]
def get(self, attr, *args, **kwargs):
if attr in self._dict.keys():
return self.__getitem__(attr)
else:
if args:
return args[0]
elif "default" in kwargs:
return kwargs["default"]
else:
if options.SMART_ERRORS:
raise KeyError()
else:
raise NotFoundError()
def items(self):
return self._dict.items()
def values(self):
return self._dict.values()
def keys(self):
return self._dict.keys()
class Index(object):
"""
key/value indexed lookups. Create an index object with GraphDatabase.index.
The returned object supports dict style lookups, eg index[key][value].
"""
@staticmethod
def _get_results(url, node_or_rel):
response, content = Request().get(url)
if response.status == 200:
data_list = json.loads(content)
if node_or_rel == NODE:
return Iterable(Node, data_list, "self")
else:
return Iterable(Relationship, data_list, "self")
elif response.status == 404:
raise NotFoundError(response.status,
"Node or relationship not found")
else:
raise StatusException(response.status,
"Error requesting index with GET %s" \
% url)
class IndexKey(object):
"""
Intermediate object so that lookups can be done like:
index[key][value]
Lookups are formated as http://.../{index_name}/{key}/{value}, so this
is the object that gets returned by index[key]. The REST request will
be sent when the value is specified.
"""
def __init__(self, index_for, url):
self._index_for = index_for
if url[-1] == '/':
url = url[:-1]
self.url = url
def __getitem__(self, value):
url = "%s/%s" % (self.url, smart_quote(value))
return Index._get_results(url, self._index_for)
def __setitem__(self, value, item):
# Neo4j hardly crush if you try to index a relationship in a
# node index and viceversa.
is_node_index = self._index_for == NODE and isinstance(item, Node)
is_relationship_index = (self._index_for == RELATIONSHIP
and isinstance(item, Relationship))
if not (is_node_index or is_relationship_index):
raise TypeError("%s is a %s and the index is for %ss"
% (item, self._index_for.capitalize(),