File size: 2,539 Bytes
b63c816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#  Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import zookeeper, time, threading

f = open("out.log","w")
zookeeper.set_log_stream(f)

connected = False
conn_cv = threading.Condition( )

def my_connection_watcher(handle,type,state,path):
    global connected, conn_cv
    print("Connected, handle is ", handle)
    conn_cv.acquire()
    connected = True
    conn_cv.notifyAll()
    conn_cv.release()
    
conn_cv.acquire()
print("Connecting to localhost:2181 -- ")
handle = zookeeper.init("localhost:2181", my_connection_watcher, 10000, 0)
while not connected:
    conn_cv.wait()
conn_cv.release()

def my_getc_watch( handle, type, state, path ):
    print("Watch fired -- ")
    print(type, state, path)

ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"};

try:
    zookeeper.create(handle, "/zk-python", "data", [ZOO_OPEN_ACL_UNSAFE], 0)
    zookeeper.get_children(handle, "/zk-python", my_getc_watch)
    for i in xrange(5):
        print("Creating sequence node ", i, " ", zookeeper.create(handle, "/zk-python/sequencenode", "data", [ZOO_OPEN_ACL_UNSAFE], zookeeper.SEQUENCE ))
except:
    pass

def pp_zk(handle,root, indent = 0):
    """Pretty print(a zookeeper tree, starting at root)"""
    def make_path(child):
        if root == "/":
            return "/" + child
        return root + "/" + child
    children = zookeeper.get_children(handle, root, None)
    out = ""
    for i in xrange(indent):
        out += "\t"
    out += "|---"+root + " :: " + zookeeper.get(handle, root, None)[0]
    print(out)
    for child in children:
        pp_zk(handle,make_path(child),indent+1)

print("ZNode tree -- ")
pp_zk(handle,"/")

print("Getting ACL / Stat for /zk-python --")
(stat, acl) =  zookeeper.get_acl(handle, "/zk-python")
print("Stat:: ", stat)
print("Acl:: ", acl)