<s> import zc . zk <EOL> import zookeeper <EOL> import threading <EOL> import sys <EOL> import traceback <EOL> import pettingzoo . testing <EOL> class Deleted ( zc . zk . NodeInfo ) : <EOL> """<STR_LIT>""" <EOL> event_type = zookeeper . DELETED_EVENT <EOL> def __init__ ( self , session , path , callbacks = [ ] ) : <EOL> zc . zk . ZooKeeper . _ZooKeeper__zkfuncs [ zookeeper . DELETED_EVENT ] = '<STR_LIT>' <EOL> self . session = session <EOL> self . path = path <EOL> self . callbacks = callbacks <EOL> self . _set_watch ( ) <EOL> def _set_watch ( self ) : <EOL> """<STR_LIT>""" <EOL> self . key = ( self . event_type , self . path ) <EOL> if self . session . watches . add ( self . key , self ) : <EOL> try : <EOL> self . _watch_key ( ) <EOL> except zookeeper . ConnectionLossException : <EOL> watches = set ( self . session . watches . pop ( self . key ) ) <EOL> for w in watches : <EOL> w . _deleted ( ) <EOL> if self . session in watches : <EOL> watches . remove ( self . session ) <EOL> if watches : <EOL> zc . zk . logger . critical ( '<STR_LIT>' % watches ) <EOL> raise <EOL> pass <EOL> else : <EOL> self . _notify ( self . session . exists ( self . path ) ) <EOL> def _watch_key ( self ) : <EOL> """<STR_LIT>""" <EOL> zkfunc = getattr ( zookeeper , '<STR_LIT>' ) <EOL> def handler ( h , t , state , p , reraise = False ) : <EOL> if state != zookeeper . CONNECTED_STATE : <EOL> zc . zk . logger . warning ( <EOL> "<STR_LIT>" , <EOL> t , state ) <EOL> return <EOL> try : <EOL> assert h == self . session . handle <EOL> assert state == zookeeper . CONNECTED_STATE <EOL> assert p == self . path <EOL> if self . key not in self . session . watches : <EOL> return <EOL> assert t == self . event_type or t == self . event_type | pettingzoo . testing . TESTING_FLAG <EOL> try : <EOL> v = zkfunc ( self . session . handle , self . path , handler ) <EOL> except zookeeper . NoNodeException : <EOL> if t & pettingzoo . testing . TESTING_FLAG : <EOL> v = None <EOL> for watch in self . session . watches . watches ( self . key ) : <EOL> watch . _notify ( v ) <EOL> else : <EOL> self . _rewatch ( ) <EOL> else : <EOL> for watch in self . session . watches . watches ( self . key ) : <EOL> watch . _notify ( v ) <EOL> except : <EOL> exc_class , exc , tb = sys . exc_info ( ) <EOL> sys . stderr . write ( str ( exc_class ) + "<STR_LIT:\n>" ) <EOL> traceback . print_tb ( tb ) <EOL> zc . zk . logger . exception ( <EOL> "<STR_LIT>" , '<STR_LIT>' , self . path ) <EOL> if reraise : <EOL> raise exc_class , exc , tb <EOL> handler ( <EOL> self . session . handle , <EOL> self . event_type , <EOL> self . session . state , <EOL> self . path , <EOL> True ) <EOL> def _rewatch ( self ) : <EOL> """<STR_LIT>""" <EOL> for watch in self . session . watches . pop ( self . key ) : <EOL> try : <EOL> self . path = self . session . resolve ( self . path ) <EOL> except ( zookeeper . NoNodeException , zc . zk . LinkLoop ) : <EOL> zc . zk . logger . exception ( "<STR_LIT>" , watch ) <EOL> watch . _deleted ( ) <EOL> else : <EOL> self . _set_watch ( ) <EOL> def _notify ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if data == None : <EOL> for callback in list ( self . callbacks ) : <EOL> try : <EOL> callback ( self ) <EOL> except Exception , v : <EOL> self . callbacks . remove ( callback ) <EOL> if isinstance ( v , zc . zk . CancelWatch ) : <EOL> zc . zk . logger . debug ( <EOL> "<STR_LIT>" , self , callback ) <EOL> else : <EOL> zc . zk . logger . exception ( "<STR_LIT>" , self , callback ) </s>
