Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add makepath feature to KazooClient.create()

Creates parent path if missing
  • Loading branch information...
commit 5ffa429fabdb2615837c7c15309031c1ede55440 1 parent 769bbf3
@labisso labisso authored
Showing with 47 additions and 3 deletions.
  1. +23 −3 kazoo/client.py
  2. +24 −0 kazoo/test/test_client.py
View
26 kazoo/client.py
@@ -110,7 +110,8 @@ def close(self):
"""
self.zk.close()
- def create(self, path, value, acl=None, ephemeral=False, sequence=False):
+ def create(self, path, value, acl=None, ephemeral=False, sequence=False,
+ makepath=False):
"""Create a ZNode
@param path: path of node
@@ -118,13 +119,32 @@ def create(self, path, value, acl=None, ephemeral=False, sequence=False):
@param acl: permissions for node
@param ephemeral: boolean indicating whether node is ephemeral (tied to this session)
@param sequence: boolean indicating whether path is suffixed with a unique index
+ @param makepath: boolean indicating whether to create path if it doesn't exist
@return: real path of the new node
"""
self._assure_namespace()
path = self.namespace_path(path)
- realpath = self.zk.create(path, value, acl=acl, ephemeral=ephemeral,
- sequence=sequence)
+ try:
+ realpath = self.zk.create(path, value, acl=acl,
+ ephemeral=ephemeral, sequence=sequence)
+
+ except NoNodeException:
+ # some or all of the parent path doesn't exist. if makepath is set
+ # we will create it and retry. If it fails again, someone must be
+ # actively deleting ZNodes and we'd best bail out.
+ if not makepath:
+ raise
+
+ parent, _ = split(path)
+
+ # using the inner call directly because path is already namespaced
+ self._inner_ensure_path(parent)
+
+ # now retry
+ realpath = self.zk.create(path, value, acl=acl,
+ ephemeral=ephemeral, sequence=sequence)
+
return self.unnamespace_path(realpath)
def exists(self, path, watch=None):
View
24 kazoo/test/test_client.py
@@ -5,6 +5,7 @@
from kazoo.client import KazooClient, KazooState
from kazoo.zkclient import EventType
from kazoo.test import get_hosts_or_skip
+from kazoo.exceptions import NoNodeException
class ZooKeeperClientTests(unittest.TestCase):
def setUp(self):
@@ -108,3 +109,26 @@ def listener(state):
self.assertEqual(len(states), 1)
self.assertEqual(states[0], KazooState.CONNECTED)
+ def test_create_no_makepath(self):
+ namespace = "/" + uuid.uuid4().hex
+ client = KazooClient(self.hosts, namespace=namespace)
+
+ client.connect()
+
+ self.assertRaises(NoNodeException, client.create, "/1/2", "val1")
+ self.assertRaises(NoNodeException, client.create, "/1/2", "val1",
+ makepath=False)
+
+ def test_create_makepath(self):
+ namespace = "/" + uuid.uuid4().hex
+ client = KazooClient(self.hosts, namespace=namespace)
+ client.connect()
+
+ client.create("/1/2", "val1", makepath=True)
+ data, stat = client.get("/1/2")
+ self.assertEqual(data, "val1")
+
+ client.create("/1/2/3/4/5", "val2", makepath=True)
+ data, stat = client.get("/1/2/3/4/5")
+ self.assertEqual(data, "val2")
+
Please sign in to comment.
Something went wrong with that request. Please try again.