<s> """<STR_LIT>""" <EOL> import sys <EOL> from zope . interface import implements , Interface <EOL> from twisted . python import log , reflect <EOL> from jelly import setUnjellyableForClass , setUnjellyableForClassTree , setUnjellyableFactoryForClass , unjellyableRegistry <EOL> from jelly import Jellyable , Unjellyable , _Dummy , _DummyNewStyle <EOL> from jelly import setInstanceState , getInstanceState <EOL> setCopierForClass = setUnjellyableForClass <EOL> setCopierForClassTree = setUnjellyableForClassTree <EOL> setFactoryForClass = setUnjellyableFactoryForClass <EOL> copyTags = unjellyableRegistry <EOL> copy_atom = "<STR_LIT>" <EOL> cache_atom = "<STR_LIT>" <EOL> cached_atom = "<STR_LIT>" <EOL> remote_atom = "<STR_LIT>" <EOL> class NoSuchMethod ( AttributeError ) : <EOL> """<STR_LIT>""" <EOL> class IPBRoot ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def rootObject ( broker ) : <EOL> """<STR_LIT>""" <EOL> class Serializable ( Jellyable ) : <EOL> """<STR_LIT>""" <EOL> def processUniqueID ( self ) : <EOL> """<STR_LIT>""" <EOL> return id ( self ) <EOL> class Referenceable ( Serializable ) : <EOL> perspective = None <EOL> """<STR_LIT>""" <EOL> def remoteMessageReceived ( self , broker , message , args , kw ) : <EOL> """<STR_LIT>""" <EOL> args = broker . unserialize ( args ) <EOL> kw = broker . unserialize ( kw ) <EOL> method = getattr ( self , "<STR_LIT>" % message , None ) <EOL> if method is None : <EOL> raise NoSuchMethod ( "<STR_LIT>" % ( message , ) ) <EOL> try : <EOL> state = method ( * args , ** kw ) <EOL> except TypeError : <EOL> log . msg ( "<STR_LIT>" % ( method , args , kw ) ) <EOL> raise <EOL> return broker . serialize ( state , self . perspective ) <EOL> def jellyFor ( self , jellier ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" , jellier . invoker . registerReference ( self ) <EOL> class Root ( Referenceable ) : <EOL> """<STR_LIT>""" <EOL> implements ( IPBRoot ) <EOL> def rootObject ( self , broker ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> class ViewPoint ( Referenceable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , perspective , object ) : <EOL> """<STR_LIT>""" <EOL> self . perspective = perspective <EOL> self . object = object <EOL> def processUniqueID ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( id ( self . perspective ) , id ( self . object ) ) <EOL> def remoteMessageReceived ( self , broker , message , args , kw ) : <EOL> """<STR_LIT>""" <EOL> args = broker . unserialize ( args , self . perspective ) <EOL> kw = broker . unserialize ( kw , self . perspective ) <EOL> method = getattr ( self . object , "<STR_LIT>" % message ) <EOL> try : <EOL> state = apply ( method , ( self . perspective , ) + args , kw ) <EOL> except TypeError : <EOL> log . msg ( "<STR_LIT>" % ( method , args , kw ) ) <EOL> raise <EOL> rv = broker . serialize ( state , self . perspective , method , args , kw ) <EOL> return rv <EOL> class Viewable ( Serializable ) : <EOL> """<STR_LIT>""" <EOL> def jellyFor ( self , jellier ) : <EOL> """<STR_LIT>""" <EOL> return ViewPoint ( jellier . invoker . serializingPerspective , self ) . jellyFor ( jellier ) <EOL> class Copyable ( Serializable ) : <EOL> """<STR_LIT>""" <EOL> def getStateToCopy ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dict__ <EOL> def getStateToCopyFor ( self , perspective ) : <EOL> """<STR_LIT>""" <EOL> return self . getStateToCopy ( ) <EOL> def getTypeToCopy ( self ) : <EOL> """<STR_LIT>""" <EOL> return reflect . qual ( self . __class__ ) <EOL> def getTypeToCopyFor ( self , perspective ) : <EOL> """<STR_LIT>""" <EOL> return self . getTypeToCopy ( ) <EOL> def jellyFor ( self , jellier ) : <EOL> """<STR_LIT>""" <EOL> if jellier . invoker is None : <EOL> return getInstanceState ( self , jellier ) <EOL> p = jellier . invoker . serializingPerspective <EOL> t = self . getTypeToCopyFor ( p ) <EOL> state = self . getStateToCopyFor ( p ) <EOL> sxp = jellier . prepare ( self ) <EOL> sxp . extend ( [ t , jellier . jelly ( state ) ] ) <EOL> return jellier . preserve ( self , sxp ) <EOL> class Cacheable ( Copyable ) : <EOL> """<STR_LIT>""" <EOL> def getStateToCacheAndObserveFor ( self , perspective , observer ) : <EOL> """<STR_LIT>""" <EOL> return self . getStateToCopyFor ( perspective ) <EOL> def jellyFor ( self , jellier ) : <EOL> """<STR_LIT>""" <EOL> if jellier . invoker is None : <EOL> return getInstanceState ( self , jellier ) <EOL> luid = jellier . invoker . cachedRemotelyAs ( self , <NUM_LIT:1> ) <EOL> if luid is None : <EOL> luid = jellier . invoker . cacheRemotely ( self ) <EOL> p = jellier . invoker . serializingPerspective <EOL> type_ = self . getTypeToCopyFor ( p ) <EOL> observer = RemoteCacheObserver ( jellier . invoker , self , p ) <EOL> state = self . getStateToCacheAndObserveFor ( p , observer ) <EOL> l = jellier . prepare ( self ) <EOL> jstate = jellier . jelly ( state ) <EOL> l . extend ( [ type_ , luid , jstate ] ) <EOL> return jellier . preserve ( self , l ) <EOL> else : <EOL> return cached_atom , luid <EOL> def stoppedObserving ( self , perspective , observer ) : <EOL> """<STR_LIT>""" <EOL> class RemoteCopy ( Unjellyable ) : <EOL> """<STR_LIT>""" <EOL> def setCopyableState ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ = state <EOL> def unjellyFor ( self , unjellier , jellyList ) : <EOL> if unjellier . invoker is None : <EOL> return setInstanceState ( self , unjellier , jellyList ) <EOL> self . setCopyableState ( unjellier . unjelly ( jellyList [ <NUM_LIT:1> ] ) ) <EOL> return self <EOL> class RemoteCache ( RemoteCopy , Serializable ) : <EOL> """<STR_LIT>""" <EOL> def remoteMessageReceived ( self , broker , message , args , kw ) : <EOL> """<STR_LIT>""" <EOL> args = broker . unserialize ( args ) <EOL> kw = broker . unserialize ( kw ) <EOL> method = getattr ( self , "<STR_LIT>" % message ) <EOL> try : <EOL> state = apply ( method , args , kw ) <EOL> except TypeError : <EOL> log . msg ( "<STR_LIT>" % ( method , args , kw ) ) <EOL> raise <EOL> return broker . serialize ( state , None , method , args , kw ) <EOL> def jellyFor ( self , jellier ) : <EOL> """<STR_LIT>""" <EOL> if jellier . invoker is None : <EOL> return getInstanceState ( self , jellier ) <EOL> assert jellier . invoker is self . broker , "<STR_LIT>" <EOL> return '<STR_LIT>' , self . luid <EOL> def unjellyFor ( self , unjellier , jellyList ) : <EOL> if unjellier . invoker is None : <EOL> return setInstanceState ( self , unjellier , jellyList ) <EOL> self . broker = unjellier . invoker <EOL> self . luid = jellyList [ <NUM_LIT:1> ] <EOL> if isinstance ( self . __class__ , type ) : <EOL> cProxy = _DummyNewStyle ( ) <EOL> else : <EOL> cProxy = _Dummy ( ) <EOL> cProxy . __class__ = self . __class__ <EOL> cProxy . __dict__ = self . __dict__ <EOL> init = getattr ( cProxy , "<STR_LIT>" , None ) <EOL> if init : <EOL> init ( ) <EOL> unjellier . invoker . cacheLocally ( jellyList [ <NUM_LIT:1> ] , self ) <EOL> cProxy . setCopyableState ( unjellier . unjelly ( jellyList [ <NUM_LIT:2> ] ) ) <EOL> self . __dict__ = cProxy . __dict__ <EOL> self . broker = unjellier . invoker <EOL> self . luid = jellyList [ <NUM_LIT:1> ] <EOL> return cProxy <EOL> def __cmp__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , self . __class__ ) : <EOL> return cmp ( id ( self . __dict__ ) , id ( other . __dict__ ) ) <EOL> else : <EOL> return cmp ( id ( self . __dict__ ) , other ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( id ( self . __dict__ ) % sys . maxint ) <EOL> broker = None <EOL> luid = None <EOL> def __del__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . broker : <EOL> self . broker . decCacheRef ( self . luid ) <EOL> except : <EOL> log . deferr ( ) <EOL> def unjellyCached ( unjellier , unjellyList ) : <EOL> luid = unjellyList [ <NUM_LIT:1> ] <EOL> cNotProxy = unjellier . invoker . cachedLocallyAs ( luid ) <EOL> cProxy = _Dummy ( ) <EOL> cProxy . __class__ = cNotProxy . __class__ <EOL> cProxy . __dict__ = cNotProxy . __dict__ <EOL> return cProxy <EOL> setUnjellyableForClass ( "<STR_LIT>" , unjellyCached ) <EOL> def unjellyLCache ( unjellier , unjellyList ) : <EOL> luid = unjellyList [ <NUM_LIT:1> ] <EOL> obj = unjellier . invoker . remotelyCachedForLUID ( luid ) <EOL> return obj <EOL> setUnjellyableForClass ( "<STR_LIT>" , unjellyLCache ) <EOL> def unjellyLocal ( unjellier , unjellyList ) : <EOL> obj = unjellier . invoker . localObjectForID ( unjellyList [ <NUM_LIT:1> ] ) <EOL> return obj <EOL> setUnjellyableForClass ( "<STR_LIT>" , unjellyLocal ) <EOL> class RemoteCacheMethod : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , broker , cached , perspective ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . broker = broker <EOL> self . perspective = perspective <EOL> self . cached = cached <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( ( self . name , self . broker , self . perspective , self . cached ) , other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . name , self . broker , self . perspective , self . cached ) ) <EOL> def __call__ ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> cacheID = self . broker . cachedRemotelyAs ( self . cached ) <EOL> if cacheID is None : <EOL> from pb import ProtocolError <EOL> raise ProtocolError ( "<STR_LIT>" ) <EOL> return self . broker . _sendMessage ( '<STR_LIT>' , self . perspective , cacheID , self . name , args , kw ) <EOL> class RemoteCacheObserver : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , broker , cached , perspective ) : <EOL> """<STR_LIT>""" <EOL> self . broker = broker <EOL> self . cached = cached <EOL> self . perspective = perspective <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . broker , self . cached , self . perspective , id ( self ) ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( ( hash ( self . broker ) % <NUM_LIT:2> ** <NUM_LIT:10> ) <EOL> + ( hash ( self . perspective ) % <NUM_LIT:2> ** <NUM_LIT:10> ) <EOL> + ( hash ( self . cached ) % <NUM_LIT:2> ** <NUM_LIT:10> ) ) <EOL> def __cmp__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return cmp ( ( self . broker , self . perspective , self . cached ) , other ) <EOL> def callRemote ( self , _name , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> cacheID = self . broker . cachedRemotelyAs ( self . cached ) <EOL> if cacheID is None : <EOL> from pb import ProtocolError <EOL> raise ProtocolError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return self . broker . _sendMessage ( '<STR_LIT>' , self . perspective , cacheID , <EOL> _name , args , kw ) <EOL> def remoteMethod ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return RemoteCacheMethod ( key , self . broker , self . cached , self . perspective ) </s>
