From 385a36b5329dce78659cb8ebc7745e1d717497fc Mon Sep 17 00:00:00 2001 From: Javier de la Rosa Date: Tue, 5 Jul 2011 20:09:41 -0400 Subject: [PATCH] Adding more functions for transactions --- neo4jrestclient/client.py | 54 +++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/neo4jrestclient/client.py b/neo4jrestclient/client.py index d02d618..16d82f6 100644 --- a/neo4jrestclient/client.py +++ b/neo4jrestclient/client.py @@ -179,19 +179,20 @@ def commit(self, *args, **kwargs): for operation in self.operations: # TODO: Make the real request method = operation["method"] - if method = TX_GET: + if method == TX_GET: pass - elif method = TX_PUT: + elif method == TX_PUT: pass - elif method = TX_POST: + elif method == TX_POST: pass - elif method = TX_DELETE: + elif method == TX_DELETE: pass self.operations = [] del self.cls._transactions[self.id] if options.TX_VARIABLE in globals(): del globals()[options.TX_VARIABLE] - self = None + # TODO: Destroy the object after commit? + # self = None if "type" in kwargs: return isinstance(kwargs["type"], Exception) else: @@ -205,12 +206,14 @@ def subscribe(self, method, url, data=None): "id": len(self.operations), "variable": self.variable, } + transaction_operation = TransactionOperation(**params) self.variable = None - self.operations.append(TransactionOperation(**params)) + self.operations.append(transaction_operation) + return transaction_operation @staticmethod def get_transaction(tx=None): - if not tx: + if not tx and options.TX_VARIABLE in globals(): return globals()[options.TX_VARIABLE] if (isinstance(tx, Transaction) or (isinstance(tx, (list, tuple)) and len(tx) > 1 @@ -225,12 +228,12 @@ class Base(object): """ def __init__(self, url, create=False, data={}, tx=None): + tx = Transaction.get_transaction(tx) self._dic = {} self.url = None if url.endswith("/"): url = url[:-1] if create: - tx = Transaction.get_transaction(tx) if tx: return tx.subscribe(TX_POST, url, data) response, content = Request().post(url, data=data) @@ -241,6 +244,8 @@ def __init__(self, url, create=False, data={}, tx=None): raise NotFoundError(response.status, "Invalid data sent") if not self.url: self.url = url + if tx: + return tx.subscribe(TX_GET, self.url) response, content = Request().get(self.url) if response.status == 200: self._dic.update(json.loads(content).copy()) @@ -256,7 +261,7 @@ def delete(self, key=None, tx=None): return tx = Transaction.get_transaction(tx) if tx: - return tx.subscribe(TX_DELETE, self.url, self) + return tx.subscribe(TX_DELETE, self.url) response, content = Request().delete(self.url) if response.status == 204: del self @@ -267,8 +272,11 @@ def delete(self, key=None, tx=None): "deleted (still has " \ "relationships?)") - def __getitem__(self, key): + def __getitem__(self, key, tx=None): property_url = self._dic["property"].replace("{key}", key) + tx = Transaction.get_transaction(tx) + if tx: + return tx.subscribe(TX_GET, url) response, content = Request().get(property_url) if response.status == 200: self._dic["data"][key] = json.loads(content) @@ -280,21 +288,28 @@ def __getitem__(self, key): "Node or propery not found") return self._dic["data"][key] - def get(self, key, *args): - if args: - default = args[0] - try: - return self.__getitem__(key) - except (KeyError, NotFoundError, StatusException): - return default - else: - return self.__getitem__(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): property_url = self._dic["property"].replace("{key}", key) + # tx = Transaction.get_transaction(tx) + # if tx: + # return tx.subscribe(TX_GET, url) response, content = Request().put(property_url, data=value) if response.status == 204: self._dic["data"].update({key: value}) @@ -373,6 +388,7 @@ def _del_properties(self): 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)