Ϫf[dZddlZddlmZmZddlmZmZddlm Z m Z ddl m Z m Z mZmZmZmZmZmZmZeZeZeZeZdZd Zd Zd ZGd d eZGddeZGdde Z Gdde Z!eeGdde!Z"Gdde!Z#Gdde Z$Gdde Z%Gdde%Z&Gdde Z'Gd d!e'e Z(d"Z)ed#e)d$Z*ed%e*d&Z+ed'e+e Gd(d)Z,e Gd*d+Z-y),a This module represents flavors of remotely accessible objects. Currently this is only objects accessible through Perspective Broker, but will hopefully encompass all forms of remote access which can emulate subsets of PB (such as XMLRPC or SOAP). Future Plans: Optimization. Exploitation of new-style object model. Optimizations to this module should not affect external-use semantics at all, but may have a small impact on users who subclass and override methods. @author: Glyph Lefkowitz N) Interface implementer)logreflect)cmp comparable) Jellyable Unjellyable _createBlankgetInstanceStatesetInstanceStatesetUnjellyableFactoryForClasssetUnjellyableForClasssetUnjellyableForClassTreeunjellyableRegistryscopycachescachedremoteceZdZdZy) NoSuchMethodz(Raised if there is no such remote methodN)__name__ __module__ __qualname____doc__8/usr/lib/python3/dist-packages/twisted/spread/flavors.pyrr7s2rrceZdZdZdZy)IPBRootz6Factory for root Referenceable objects for PB servers.cy)z%Return root Referenceable for broker.Nr)brokers r rootObjectzIPBRoot.rootObject>rNrrrrr"rrrrr;s @4rrceZdZdZdZy) SerializableaxAn object that can be passed remotely. I am a style of object which can be serialized by Perspective Broker. Objects which wish to be referenceable or copied remotely have to subclass Serializable. However, clients of Perspective Broker will probably not want to directly subclass Serializable; the Flavors of transferable objects are listed below. What it means to be "Serializable" is that an object can be passed to or returned from a remote method. Certain basic types (dictionaries, lists, tuples, numbers, strings) are serializable by default; however, classes need to choose a specific serialization style: L{Referenceable}, L{Viewable}, L{Copyable} or L{Cacheable}. You may also pass C{[lists, dictionaries, tuples]} of L{Serializable} instances to or return them from remote methods, as many levels deep as you like. ct|S)zReturn an ID which uniquely represents this object for this process. By default, this uses the 'id' builtin, but can be overridden to indicate that two values are identity-equivalent (such as proxies for the same object). )idselfs rprocessUniqueIDzSerializable.processUniqueIDVs $xrN)rrrrr+rrrr&r&Bs &rr&ceZdZdZ dZdZy) ReferenceableNc@|j|}|j|}|jDcgc]}t|ts|c}r2|j Dcic]\}}|j d|}}}t|t s|j d}t|d|zd}|td| ||i|} |j| |jScc}wcc}}w#t$rtj|d|d|wxYw)zA remote message has been received. Dispatch it appropriately. The default implementation is to dispatch to a method called 'remote_messagename' and call it with the same arguments. utf8z remote_%sNzNo such method: remote_ didn't accept  and ) unserializekeys isinstancebytesitemsdecodestrgetattrr TypeErrorrmsg serialize perspective) r*r!messageargskwkeykvmethodstates rremoteMessageReceivedz#Referenceable.remoteMessageReceivedns !!$'    #779 ?C 3(>C ?24((*=$!Q!((6"A%=B='3'nnV,G{W4d; >!8 BC C D'B'Et'7'788 @=  GGvhodV5= >  sC* C*%C/C55(Dc<d|jj|gS)zw(internal) Return a tuple which will be used as the s-expression to serialize this to a peer. r)invokerregisterReferencer*jelliers rjellyForzReferenceable.jellyFors7??<r?r@rDrErvs rrFzViewPoint.remoteMessageReceiveds!!$(8(89   D$4$4 5'3'nnV,Gi'&9: T--/$6="=E  eT%5%5vtR H   GGvhodV5= >  s 4B++(CN)rrrrrTr+rFrrrrQrQs%N 7rrQceZdZdZdZy)ViewableaBI will be converted to a L{ViewPoint} when passed to or returned from a remote method. The beginning of a peer's interaction with a PB Service is always through a perspective. However, if a C{perspective_xxx} method returns a Viewable, it will be serialized to the peer as a response to that method. c`t|jj|j|S)zHSerialize a L{ViewPoint} for me and the perspective of the given broker.)rQrHserializingPerspectiverLrJs rrLzViewable.jellyFors%??FOOPWXXrN)rrrrrLrrrrYrYs YrrYc.eZdZdZdZdZdZdZdZy)CopyableaSubclass me to get copied each time you are returned from or passed to a remote method. When I am returned from or passed to a remote method call, I will be converted into data via a set of callbacks (see my methods for more info). That data will then be serialized using Jelly, and sent to the peer. The peer will then look up the type to represent this with; see L{RemoteCopy} for details. c|jS)zGather state to send when I am serialized for a peer. I will default to returning self.__dict__. Override this to customize this behavior. )__dict__r)s rgetStateToCopyzCopyable.getStateToCopy s}}rc"|jS)z Gather state to send when I am serialized for a particular perspective. I will default to calling L{getStateToCopy}. Override this to customize this behavior. )r`r*r=s rgetStateToCopyForzCopyable.getStateToCopyFors""$$rc^tj|jjdS)zDetermine what type tag to send for me. By default, send the string representation of my class (package.module.Class); normally this is adequate, but you may override this to change it. utf-8)rqual __class__encoder)s r getTypeToCopyzCopyable.getTypeToCopys"||DNN+227;;rc"|jS)zDetermine what type tag to send for me. By default, defer to self.L{getTypeToCopy}() normally this is adequate, but you may override this to change it. )rirbs rgetTypeToCopyForzCopyable.getTypeToCopyFor(s!!##rc,|j t||S|jj}|j|}|j |}|j |}|j ||j|g|j||S)zAssemble type tag and state to copy for this broker. This will call L{getTypeToCopyFor} and L{getStateToCopy}, and return an appropriate s-expression to represent me. ) rHr r[rkrcprepareextendjellypreserve)r*rKptrEsxps rrLzCopyable.jellyFor1s ?? "#D'2 2 OO 2 2  ! !! $&&q)ood# Aw}}U+,-c**rN) rrrrr`rcrirkrLrrrr]r]s   %<$+rr]c"eZdZdZdZdZdZy) CacheableaA cached instance. This means that it's copied; but there is some logic to make sure that it's only copied once. Additionally, when state is retrieved, it is passed a "proto-reference" to the state as it will exist on the client. XXX: The documentation for this class needs work, but it's the most complex part of PB and it is inherently difficult to explain. c$|j|S)a- Get state to cache on the client and client-cache reference to observe locally. This is similar to getStateToCopyFor, but it additionally passes in a reference to the client-side RemoteCache instance that will be created when it is unserialized. This allows Cacheable instances to keep their RemoteCaches up to date when they change, such that no changes can occur between the point at which the state is initially copied and the client receives it that are not propagated. )rcr*r=observers rgetStateToCacheAndObserveForz&Cacheable.getStateToCacheAndObserveForNs%%k22rc|j t||S|jj|d}||jj|}|jj}|j |}t |j||}|j||}|j|}|j|}|j|||g|j||St|fS)zReturn an appropriate tuple to serialize me. Depending on whether this broker has cached me or not, this may return either a full state or a reference to an existing cache. r ) rHr cachedRemotelyAs cacheRemotelyr[rkRemoteCacheObserverryrmrornrp cached_atom) r*rKluidrqtype_rxrEljstates rrLzCacheable.jellyFor^s ?? "#D'2 2//a8 <??006D66A))!,E*7??D!DH55aBE%A]]5)F HHeT6* +##D!, ,$ $rcy)zThis method is called when a client has stopped observing me. The 'observer' argument is the same as that passed in to getStateToCacheAndObserveFor. Nrrws rstoppedObservingzCacheable.stoppedObservingtr#rN)rrrrryrLrrrrruruBs 3 %, rruceZdZdZdZdZy) RemoteCopyaI am a remote copy of a Copyable object. When the state from a L{Copyable} object is received, an instance will be created based on the copy tags table (see setUnjellyableForClass) and sent the L{setCopyableState} message. I provide a reasonable default implementation of that message; subclass me if you wish to serve as a copier for remote data. NOTE: copiers are invoked with no arguments. Do not implement a constructor which requires args in a subclass of L{RemoteCopy}! c|si}|jDcic])\}}t|tr|jdn||+}}}||_ycc}}w)a(I will be invoked with the state to copy locally. 'state' is the data returned from the remote object's 'getStateToCopyFor' method, which will often be the remote object's dictionary (or a filtered approximation of it depending on my peer's perspective). r/N)r6r4r5r7r_)r*rExys rsetCopyableStatezRemoteCopy.setCopyableStatesWEKP;;= CG1a 1e 4AHHV !Q >    s.Ac~|j t|||S|j|j|d|SNr )rHrrunjelly)r* unjellier jellyLists r unjellyForzRemoteCopy.unjellyFors?    $#D)Y? ? i// ! => rN)rrrrrrrrrrr|s  rrcBeZdZdZdZdZdZdZdZdZ dZ dZ d Z y) RemoteCacheaA cache is a local representation of a remote L{Cacheable} object. This represents the last known state of this object. It may also have methods invoked on it -- in order to update caches, the cached class generates a L{pb.RemoteReference} to this object as it is originally sent. Much like copy, I will be invoked with no arguments. Do not implement a constructor that requires arguments in one of my subclasses. c8t|ts|jd}|j|}|j|}t |d|z} ||i|}|j|d|||S#t $rt j|d|d|wxYw)zA remote message has been received. Dispatch it appropriately. The default implementation is to dispatch to a method called 'C{observe_messagename}' and call it on my with the same arguments. r/z observe_%sr0r1N) r4r8r7r2r9r:rr;r<)r*r!r>r?r@rDrEs rrFz!RemoteCache.remoteMessageReceiveds '3'nnV,G!!$'    #|g56 D'B'EtVT2>>  GGvhodV5= >  s A11(Bc|j t||S|j|jusJdd|jfS)zKserialize me (only for the broker I'm for) as the original cached referencez3You cannot exchange cached proxies between brokers.slcache)rHr r!rrJs rrLzRemoteCache.jellyForsJ ?? "#D'2 2 OOt{{ * A @ A *$))##rc|j t|||S|j|_|d|_|j }t |dd}|r||jj |d||j|j|d|j|_ |j|_|d|_|S)Nr rT) rHrr!r_borgifyr9 cacheLocallyrrr_)r*rrborgCopyinits rrzRemoteCache.unjellyFors    $#D)Y? ?'' aL ==?xT2  F&&y|T:!!)"3"3IaL"AB!))  '' aL rct||jr2tt|jt|jStt|j|S)z#Compare me [to another RemoteCache.)r4rgrr(r_r*others r__cmp__zRemoteCache.__cmp__sE eT^^ ,r$--("U^^*<= =r$--(%0 0rc`tt|jtjzS)zHash me.)intr(r_sysmaxsizer)s r__hash__zRemoteCache.__hash__s2dmm$s{{233rNc |jr&|jj|jyy#t$rt j YywxYw)z.Do distributed reference counting on finalize.N)r! decCacheRefr BaseExceptionrdeferrr)s r__del__zRemoteCache.__del__sA {{ '' 2  JJL s15AAcRt|j}|j|_|S)a Create a new object that shares its state (i.e. its C{__dict__}) and type with this object, but does not share its identity. This is an instance of U{the Borg design pattern } originally described by Alex Martelli, but unlike the example given there, this is not a replacement for a Singleton. Instead, it is for lifecycle tracking (and distributed garbage collection). The purpose of these separate objects is to have a separate object tracking each application-level reference to the root L{RemoteCache} object being tracked by the broker, and to have their C{__del__} methods be invoked. This may be achievable via a weak value dictionary to track the root L{RemoteCache} instances instead, but this implementation strategy predates the availability of weak references in Python. @return: The new instance. @rtype: C{self.__class__} )r rgr_)r*blanks rrzRemoteCache._borgifys"*T^^, r) rrrrrFrLrrrr!rrrrrrrrs6 ?&$214F Drrc^|d}|jj|jSr)rHcachedLocallyAsr)r unjellyListrs r unjellyCachedrs+ q>D    , ,T 2 ; ; ==rcachedcF|d}|jj|}|Sr)rHremotelyCachedForLUID)rrrobjs r unjellyLCachers& q>D    1 1$ 7C JrlcachecB|jj|d}|Sr)rHlocalObjectForID)rrrs r unjellyLocalr#s!    , ,[^ )formatr!rr=r(r)s r__repr__zRemoteCacheObserver.__repr__cs38?? KK KK    tH   rct|jdzt|jdzzt|jdzzS)z`Generate a hash unique to all L{RemoteCacheObserver}s for this broker/perspective/cached tripleti)rr!r=rr)s rrzRemoteCacheObserver.__hash__ksH$++  &D$$%- /DKK 5( * rc\t|j|j|jf|S)z-Compare me to another L{RemoteCacheObserver}.)rr!r=rrs rrzRemoteCacheObserver.__cmp__ts&DKK!1!14;;?GGrc|jj|j}t|tr|j d}|ddlm}|d|jjd|j||||S)rrerrrr) r!r{rr4r8rhrrrr=)r*_namer?r@rrs r callRemotezRemoteCacheObserver.callRemoteysx++..t{{; eS !LL)E ? 7< {{'' d&&b  rcZt||j|j|jS)z&Get a L{pb.RemoteMethod} for this key.)rr!rr=)r*rAs r remoteMethodz RemoteCacheObserver.remoteMethods! dkk4;;@P@PQQrN) rrrrrTr8rrrrrrrrr}r}Js. ' #  H  Rrr}).rrzope.interfacerrtwisted.pythonrrtwisted.python.compatrrror r r r rrrrrsetCopierForClasssetCopierForClassTreesetFactoryForClasscopyTags copy_atom cache_atomr~ remote_atomAttributeErrorrrr&r-rNrQrYr]rurrrrrrr}rrrrsi ( 1'1   +22      3>34i49>/DL/Dd W=&F FR Y| YA+|A+H7 7 t  Fp*lpf> x/ x/ w -     < @R@R @Rr