Skip to content
Browse files

Better ensure_path() behavior

  • Loading branch information...
1 parent 410324a commit aa23b7b749436f60a55ca270eec3b1c8c7dadf1b @labisso labisso committed
Showing with 17 additions and 13 deletions.
  1. +17 −13 kazoo/client.py
View
30 kazoo/client.py
@@ -1,4 +1,5 @@
import logging
+from os.path import split
from kazoo.zkclient import ZooKeeperClient, WatchedEvent, KeeperState,\
EventType, NodeExistsException, NoNodeException
@@ -196,20 +197,21 @@ def ensure_path(self, path):
"""Recursively create a path if it doesn't exist
"""
path = self.namespace_path(path)
+ self._inner_ensure_path(path)
- so_far = ''
- for part in path.split('/'):
- if not part:
- continue
- so_far += '/' + part
+ def _inner_ensure_path(self, path):
+ if self.zk.exists(path):
+ return
+
+ parent, node = split(path)
- # using the internal zk client
- if not self.zk.exists(so_far):
- try:
- self.zk.create(so_far, "")
- except NodeExistsException:
- # someone else created the node. how sweet!
- pass
+ if parent != "/":
+ self._inner_ensure_path(parent)
+ try:
+ self.zk.create(path, "")
+ except NodeExistsException:
+ # someone else created the node. how sweet!
+ pass
def recursive_delete(self, path):
"""Recursively delete a ZNode and all of its children
@@ -232,7 +234,9 @@ def namespace_path(self, path):
if not self.namespace:
return path
validate_path(path)
- return self.namespace + path
+ path = self.namespace + path
+ path = path.rstrip('/')
+ return path
def unnamespace_path(self, path):
if not self.namespace:

0 comments on commit aa23b7b

Please sign in to comment.
Something went wrong with that request. Please try again.