ó B×Oc@s#dZddlZddlmZmZddlmZmZddlm Z m Z m Z m Z ddlm Z mZmZddlmZmZe Ze Ze Ze ZdZd Zd Zd Zd efd „ƒYZdefd„ƒYZde fd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZ defd„ƒYZ!defd„ƒYZ"de"fd„ƒYZ#defd„ƒYZ$d e$efd!„ƒYZ%d"„Z&e d e&ƒd#„Z'e d$e'ƒd%„Z(e d&e(ƒd'd+d(„ƒYZ)d)d,d*„ƒYZ*dS(-sî This module represents flavors of remotely acessible 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 iÿÿÿÿN(t implementst Interface(tlogtreflect(tsetUnjellyableForClasstsetUnjellyableForClassTreetsetUnjellyableFactoryForClasstunjellyableRegistry(t Jellyablet Unjellyablet _newDummyLike(tsetInstanceStatetgetInstanceStatetcopytcachetcachedtremotet NoSuchMethodcBseZdZRS(s(Raised if there is no such remote method(t__name__t __module__t__doc__(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR0stIPBRootcBseZdZd„ZRS(s6Factory for root Referenceable objects for PB servers.cCsdS(s%Return root Referenceable for broker.N((tbroker((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt rootObject7s(RRRR(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR4st SerializablecBseZdZd„ZRS(sxAn 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. cCs t|ƒS(sÿReturn 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). (tid(tself((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytprocessUniqueIDOs(RRRR(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR;st ReferenceablecBs eZdZd„Zd„ZRS(cCs­|j|ƒ}|j|ƒ}t|d|dƒ}|dkrVtd|fƒ‚ny|||Ž}Wn.tk r™tjd|||fƒ‚nX|j||jƒS(sÐA 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. s remote_%ssNo such method: remote_%ss%s didn't accept %s and %sN( t unserializetgetattrtNoneRt TypeErrorRtmsgt serializet perspective(RRtmessagetargstkwtmethodtstate((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytremoteMessageReceivedfs  cCsd|jj|ƒgS(sw(internal) Return a tuple which will be used as the s-expression to serialize this to a peer. R(tinvokertregisterReference(Rtjellier((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytjellyForxsN(RRRR#R)R-(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRYs tRootcBs!eZdZeeƒd„ZRS(sþI provide a root object to L{pb.Broker}s for a L{pb.BrokerFactory}. When a L{pb.BrokerFactory} produces a L{pb.Broker}, it supplies that L{pb.Broker} with an object named "root". That object is obtained by calling my rootObject method. cCs|S(sA L{pb.BrokerFactory} is requesting to publish me as a root object. When a L{pb.BrokerFactory} is sending me as the root object, this method will be invoked to allow per-broker versions of an object. By default I return myself. ((RR((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRŒs(RRRRRR(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR.‚s t ViewPointcBs)eZdZd„Zd„Zd„ZRS(sD I act as an indirect reference to an object accessed through a L{pb.Perspective}. Simply put, I combine an object with a perspective so that when a peer calls methods on the object I refer to, the method will be invoked with that perspective as a first argument, so that it can know who is calling it. While L{Viewable} objects will be converted to ViewPoints by default when they are returned from or sent as arguments to a remote method, any object may be manually proxied as well. (XXX: Now that this class is no longer named C{Proxy}, this is the only occourance of the term 'proxied' in this docstring, and may be unclear.) This can be useful when dealing with L{pb.Perspective}s, L{Copyable}s, and L{Cacheable}s. It is legal to implement a method as such on a perspective:: | def perspective_getViewPointForOther(self, name): | defr = self.service.getPerspectiveRequest(name) | defr.addCallbacks(lambda x, self=self: ViewPoint(self, x), log.msg) | return defr This will allow you to have references to Perspective objects in two different ways. One is through the initial 'attach' call -- each peer will have a L{pb.RemoteReference} to their perspective directly. The other is through this method; each peer can get a L{pb.RemoteReference} to all other perspectives in the service; but that L{pb.RemoteReference} will be to a L{ViewPoint}, not directly to the object. The practical offshoot of this is that you can implement 2 varieties of remotely callable methods on this Perspective; view_xxx and C{perspective_xxx}. C{view_xxx} methods will follow the rules for ViewPoint methods (see ViewPoint.L{remoteMessageReceived}), and C{perspective_xxx} methods will follow the rules for Perspective methods. cCs||_||_dS(s8Initialize me with a Perspective and an Object. N(R#tobject(RR#R0((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt__init__¾s cCst|jƒt|jƒfS(sPReturn an ID unique to a proxy for this perspective+object combination. (RR#R0(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRÄscCs°|j||jƒ}|j||jƒ}t|jd|ƒ}y||jf||Ž}Wn.tk rtjd|||fƒ‚nX|j||j|||ƒ}|S(s7A remote message has been received. Dispatch it appropriately. The default implementation is to dispatch to a method called 'C{view_messagename}' to my Object and call it on my object with the same arguments, modified by inserting my Perspective as the first argument. sview_%ss%s didn't accept %s and %s(RR#RR0R RR!R"(RRR$R%R&R'R(trv((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR)És (RRRR1RR)(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR/–s&  tViewablecBseZdZd„ZRS(sBI 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. cCst|jj|ƒj|ƒS(sQSerialize a L{ViewPoint} for me and the perspective of the given broker. (R/R*tserializingPerspectiveR-(RR,((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR-æs(RRRR-(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR3ÝstCopyablecBs;eZdZd„Zd„Zd„Zd„Zd„ZRS(s§Subclass 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. cCs|jS(s¥Gather state to send when I am serialized for a peer. I will default to returning self.__dict__. Override this to customize this behavior. (t__dict__(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytgetStateToCopyùscCs |jƒS(sÊ 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. (R7(RR#((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytgetStateToCopyFors cCstj|jƒS(sÛDetermine 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. (Rtqualt __class__(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt getTypeToCopy scCs |jƒS(s±Determine 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. (R;(RR#((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytgetTypeToCopyForscCs|jdkrt||ƒS|jj}|j|ƒ}|j|ƒ}|j|ƒ}|j||j|ƒgƒ|j ||ƒS(sÀAssemble 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. N( R*RR R4R<R8tpreparetextendtjellytpreserve(RR,tpttR(tsxp((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR- s  (RRRR7R8R;R<R-(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR5ís   t CacheablecBs)eZdZd„Zd„Zd„ZRS(s‡A 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. cCs |j|ƒS(s. Get state to cache on the client and client-cache reference to observe locally. This is similiar 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 propogated. (R8(RR#tobserver((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytgetStateToCacheAndObserveFor=sc Csã|jdkrt||ƒS|jj|dƒ}|dkrÕ|jj|ƒ}|jj}|j|ƒ}t|j||ƒ}|j||ƒ}|j |ƒ}|j |ƒ}|j |||gƒ|j ||ƒSt |fSdS(sÆReturn 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. iN(R*RR tcachedRemotelyAst cacheRemotelyR4R<tRemoteCacheObserverRFR=R?R>R@t cached_atom( RR,tluidRAttype_RER(tltjstate((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR-Ms   cCsdS(s®This method is called when a client has stopped observing me. The 'observer' argument is the same as that passed in to getStateToCacheAndObserveFor. N((RR#RE((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytstoppedObservingcs(RRRRFR-RO(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRD1s   t RemoteCopycBs eZdZd„Zd„ZRS(sI 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}! cCs ||_dS(s(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). N(R6(RR(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytsetCopyableStateys cCs=|jdkrt|||ƒS|j|j|dƒƒ|S(Ni(R*RR RQtunjelly(Rt unjelliert jellyList((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt unjellyFor„s(RRRRQRU(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRPls  t RemoteCachecBsPeZdZd„Zd„Zd„Zd„Zd„ZdZ dZ d„Z RS(sÂA 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. cCsŽ|j|ƒ}|j|ƒ}t|d|ƒ}y|||Ž}Wn.tk rttjd|||fƒ‚nX|j|d|||ƒS(sÛA 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. s observe_%ss%s didn't accept %s and %sN(RRR RR!R"R(RRR$R%R&R'R(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR)™s cCsG|jdkrt||ƒS|j|jks:tdƒ‚d|jfS(sTserialize me (only for the broker I'm for) as the original cached reference s3You cannot exchange cached proxies between brokers.tlcacheN(R*RR RtAssertionErrorRK(RR,((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR-ªs cCsÀ|jdkrt|||ƒS|j|_|d|_t|ƒ}t|ddƒ}|rf|ƒn|jj|d|ƒ|j|j |dƒƒ|j |_ |j|_|d|_|S(NiR1i( R*RR RRKR Rt cacheLocallyRQRRR6(RRSRTtcProxytinit((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRU³s       cCsKt||jƒr1tt|jƒt|jƒƒStt|jƒ|ƒSdS(s,Compare me [to another RemoteCache. N(t isinstanceR:tcmpRR6(Rtother((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt__cmp__ÌscCstt|jƒtjƒS(sHash me. (tintRR6tsystmaxint(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt__hash__ÔscCs;y#|jr"|jj|jƒnWntjƒnXdS(s7Do distributed reference counting on finalize. N(Rt decCacheRefRKRtdeferr(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt__del__Üs  N( RRRR)R-RUR_RcRRRKRf(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRVŒs     cCs,|d}|jj|ƒ}t|ƒ}|S(Ni(R*tcachedLocallyAsR (RSt unjellyListRKt cNotProxyRZ((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt unjellyCachedæs  cCs |d}|jj|ƒ}|S(Ni(R*tremotelyCachedForLUID(RSRhRKtobj((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt unjellyLCacheîs RWcCs|jj|dƒ}|S(Ni(R*tlocalObjectForID(RSRhRl((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt unjellyLocalõstlocaltRemoteCacheMethodcBs2eZdZd„Zd„Zd„Zd„ZRS(s1A method on a reference to a L{RemoteCache}. cCs(||_||_||_||_dS(s(internal) initialize. N(tnameRR#R(RRrRRR#((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR1ÿs   cCs%t|j|j|j|jf|ƒS(N(R]RrRR#R(RR^((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR_scCs"t|j|j|j|jfƒS(N(thashRrRR#R(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRc scOse|jj|jƒ}|dkr@ddlm}|dƒ‚n|jjd|j||j||ƒS(s"(internal) action method. iÿÿÿÿ(t ProtocolErrorsQYou can't call a cached method when the object hasn't been given to the peer yet.RN( RRGRRtpbRtt _sendMessageR#Rr(RR%R&tcacheIDRt((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt__call__ s  (RRRR1R_RcRx(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRqûs    RIcBsDeZdZd„Zd„Zd„Zd„Zd„Zd„ZRS(s6I am a reverse-reference to the peer's L{RemoteCache}. I am generated automatically when a cache is serialized. I represent a reference to the client's L{RemoteCache} object that will represent a particular L{Cacheable}; I am the additional object passed to getStateToCacheAndObserveFor. cCs||_||_||_dS(s(internal) Initialize me. @param broker: a L{pb.Broker} instance. @param cached: a L{Cacheable} instance that this L{RemoteCacheObserver} corresponds to. @param perspective: a reference to the perspective who is observing this. N(RRR#(RRRR#((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR1s  cCs#d|j|j|jt|ƒfS(Ns'(RRR#R(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt__repr__.scCs3t|jƒdt|jƒdt|jƒdS(siGenerate a hash unique to all L{RemoteCacheObserver}s for this broker/perspective/cached triplet ii iii(RsRR#R(R((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRc2scCst|j|j|jf|ƒS(s6Compare me to another L{RemoteCacheObserver}. (R]RR#R(RR^((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyR_:scOsb|jj|jƒ}|dkr@ddlm}|dƒ‚n|jjd|j||||ƒS(s"(internal) action method. iÿÿÿÿ(RtsQYou can't call a cached method when the object hasn't been given to the peer yet.RN(RRGRRRuRtRvR#(Rt_nameR%R&RwRt((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt callRemote@s  cCst||j|j|jƒS(s/Get a L{pb.RemoteMethod} for this key. (RqRRR#(Rtkey((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyt remoteMethodKs( RRRR1RyRcR_R{R}(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pyRIs     (((+RRatzope.interfaceRRttwisted.pythonRRR?RRRRRR R R R tsetCopierForClasstsetCopierForClassTreetsetFactoryForClasstcopyTagst copy_atomt cache_atomRJt remote_atomtAttributeErrorRRRRR.R/R3R5RDRPRVRjRmRoRqRI(((s:/usr/lib/python2.7/dist-packages/twisted/spread/flavors.pytsB ")GD; Z