31
31
CheckVersion ,
32
32
CloseInstance ,
33
33
Create ,
34
+ Create2 ,
34
35
Delete ,
35
36
Exists ,
36
37
GetChildren ,
68
69
string_types = six .string_types
69
70
bytes_types = (six .binary_type ,)
70
71
71
- CLOSED_STATES = (KeeperState .EXPIRED_SESSION , KeeperState .AUTH_FAILED ,
72
- KeeperState .CLOSED )
72
+ CLOSED_STATES = (
73
+ KeeperState .EXPIRED_SESSION ,
74
+ KeeperState .AUTH_FAILED ,
75
+ KeeperState .CLOSED
76
+ )
73
77
ENVI_VERSION = re .compile (r'([\d\.]*).*' , re .DOTALL )
74
78
ENVI_VERSION_KEY = 'zookeeper.version'
75
79
log = logging .getLogger (__name__ )
@@ -856,7 +860,7 @@ def sync(self, path):
856
860
return self .sync_async (path ).get ()
857
861
858
862
def create (self , path , value = b"" , acl = None , ephemeral = False ,
859
- sequence = False , makepath = False ):
863
+ sequence = False , makepath = False , include_data = False ):
860
864
"""Create a node with the given value as its data. Optionally
861
865
set an ACL on the node.
862
866
@@ -904,7 +908,13 @@ def create(self, path, value=b"", acl=None, ephemeral=False,
904
908
with a unique index.
905
909
:param makepath: Whether the path should be created if it
906
910
doesn't exist.
907
- :returns: Real path of the new node.
911
+ :param include_data:
912
+ Include the :class:`~kazoo.protocol.states.ZnodeStat` of
913
+ the node in addition to its real path. This option changes
914
+ the return value to be a tuple of (path, stat).
915
+
916
+ :returns: Real path of the new node, or tuple if `include_data`
917
+ is `True`
908
918
:rtype: str
909
919
910
920
:raises:
@@ -923,21 +933,28 @@ def create(self, path, value=b"", acl=None, ephemeral=False,
923
933
:exc:`~kazoo.exceptions.ZookeeperError` if the server
924
934
returns a non-zero error code.
925
935
936
+ .. versionadded:: 1.1
937
+ The `makepath` option.
938
+ .. versionadded:: 2.7
939
+ The `include_data` option.
926
940
"""
927
941
acl = acl or self .default_acl
928
- return self .create_async (path , value , acl = acl , ephemeral = ephemeral ,
929
- sequence = sequence , makepath = makepath ).get ()
942
+ return self .create_async (
943
+ path , value , acl = acl , ephemeral = ephemeral ,
944
+ sequence = sequence , makepath = makepath , include_data = include_data
945
+ ).get ()
930
946
931
947
def create_async (self , path , value = b"" , acl = None , ephemeral = False ,
932
- sequence = False , makepath = False ):
948
+ sequence = False , makepath = False , include_data = False ):
933
949
"""Asynchronously create a ZNode. Takes the same arguments as
934
950
:meth:`create`.
935
951
936
952
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
937
953
938
954
.. versionadded:: 1.1
939
955
The makepath option.
940
-
956
+ .. versionadded:: 2.7
957
+ The `include_data` option.
941
958
"""
942
959
if acl is None and self .default_acl :
943
960
acl = self .default_acl
@@ -956,6 +973,8 @@ def create_async(self, path, value=b"", acl=None, ephemeral=False,
956
973
raise TypeError ("Invalid type for 'sequence' (bool expected)" )
957
974
if not isinstance (makepath , bool ):
958
975
raise TypeError ("Invalid type for 'makepath' (bool expected)" )
976
+ if not isinstance (include_data , bool ):
977
+ raise TypeError ("Invalid type for 'include_data' (bool expected)" )
959
978
960
979
flags = 0
961
980
if ephemeral :
@@ -970,7 +989,9 @@ def create_async(self, path, value=b"", acl=None, ephemeral=False,
970
989
@capture_exceptions (async_result )
971
990
def do_create ():
972
991
result = self ._create_async_inner (
973
- path , value , acl , flags , trailing = sequence )
992
+ path , value , acl , flags ,
993
+ trailing = sequence , include_data = include_data
994
+ )
974
995
result .rawlink (create_completion )
975
996
976
997
@capture_exceptions (async_result )
@@ -981,7 +1002,11 @@ def retry_completion(result):
981
1002
@wrap (async_result )
982
1003
def create_completion (result ):
983
1004
try :
984
- return self .unchroot (result .get ())
1005
+ if include_data :
1006
+ new_path , stat = result .get ()
1007
+ return self .unchroot (new_path ), stat
1008
+ else :
1009
+ return self .unchroot (result .get ())
985
1010
except NoNodeError :
986
1011
if not makepath :
987
1012
raise
@@ -994,10 +1019,16 @@ def create_completion(result):
994
1019
do_create ()
995
1020
return async_result
996
1021
997
- def _create_async_inner (self , path , value , acl , flags , trailing = False ):
1022
+ def _create_async_inner (self , path , value , acl , flags ,
1023
+ trailing = False , include_data = False ):
998
1024
async_result = self .handler .async_result ()
1025
+ if include_data :
1026
+ opcode = Create2
1027
+ else :
1028
+ opcode = Create
1029
+
999
1030
call_result = self ._call (
1000
- Create (_prefix_root (self .chroot , path , trailing = trailing ),
1031
+ opcode (_prefix_root (self .chroot , path , trailing = trailing ),
1001
1032
value , acl , flags ), async_result )
1002
1033
if call_result is False :
1003
1034
# We hit a short-circuit exit on the _call. Because we are
0 commit comments