ó
[³XMc           @   s†   d  Z  d d l Z d d l m Z d d l m Z m Z m Z d e j f d „  ƒ  YZ	 d e j
 f d „  ƒ  YZ d	 „  Z d
 „  Z d S(   s]   
Persistently cached objects for PB.

Maintainer: Glyph Lefkowitz

Future Plans: None known.
iÿÿÿÿN(   t   defer(   t   bananat   jellyt   flavorst   Publishablec           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s;   An object whose cached state persists across sessions.
    c         C   s   |  j  ƒ  | |  _ d  S(   N(   t	   republisht   publishedID(   t   selfR   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   __init__   s    
c         C   s   t  j  ƒ  |  _ d S(   sF   Set the timestamp to current and (TODO) update all observers.
        N(   t   timet	   timestamp(   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyR      s    c         C   s   |  j  | ƒ S(   s
   (internal)(   t   getStateToPublishFor(   R   t   perspective(    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   view_getStateToPublish   s    c         C   s
   |  j  ƒ  S(   sC   Implement me to special-case your state for a perspective.
        (   t   getStateToPublish(   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyR   #   s    c         C   s   t  d |  j ƒ ‚ d S(   sK   Implement me to return state to copy as part of the publish phase.
        s   %s.getStateToPublishForN(   t   NotImplementedErrort	   __class__(   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyR   (   s    c         C   sf   | r! | j  } | j ƒ  j } n d } d } i t j | |  ƒ d 6|  j d 6| d 6| d 6|  j d 6S(   s?   Get all necessary metadata to keep a clientside cache.
        t   Nonet   remoteR   R   t   serviceR
   (   t   perspectiveNamet
   getServicet   serviceNameR   t	   ViewPointR   R
   (   R   R   t   observert   pnamet   sname(    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   getStateToCacheAndObserveFor-   s    	
(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyR      s   					t   RemotePublishedc           B   sS   e  Z d  Z d Z d Z d d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s;   The local representation of remote Publishable object.
    i    t   pubc         C   s#   d |  j  |  j t |  j ƒ | f S(   Ns   %s-%s-%s.%s(   R   R   t   strR   (   R   t   ext(    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   getFileNameB   s    c         C   sÎ   |  j  j | ƒ g  |  _ y/ t |  j ƒ  d ƒ } | j ƒ  } | j ƒ  Wn t k
 ra d } n+ Xt j	 t
 j | ƒ ƒ } | j |  j k } | r® |  j | j  ƒ d |  _ n |  j j d ƒ j |  j ƒ d  S(   Nt   rbi    i   R   (   t   __dict__t   updatet   _activationListenerst   fileR#   t   readt   closet   IOErrorR   t   unjellyR   t   decodeR
   t   _cbGotUpdatet   _wasCleanWhenLoadedR   t
   callRemotet   addCallbacks(   R   t   statet   dataFilet   datat   recentt   newself(    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   setCopyableStateF   s    	
c         C   s6   |  j  j ƒ  } | d =| d =| d =| d =| d =| S(   Nt   brokerR   t   luidR'   t   isActivated(   R%   t   copy(   R   t   other(    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   __getstate__X   s    c         C   sŒ   |  j  j | ƒ d |  _ x |  j D] } | |  ƒ q# Wg  |  _ |  j ƒ  t |  j ƒ  d ƒ } | j t j	 t
 j
 |  ƒ ƒ ƒ | j ƒ  d  S(   Ni   t   wb(   R%   R&   R:   R'   t	   activatedR(   R#   t   writeR   t   encodeR   R*   (   R   t   newStatet   listenerR3   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyR.   c   s    		
c         C   s   d S(   sn   Implement this method if you want to be notified when your
        publishable subclass is activated.
        N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyR?   p   s    c         C   s*   |  j  r | |  ƒ n |  j j | ƒ d S(   sc   Externally register for notification when this publishable has received all relevant data.
        N(   R:   R'   t   append(   R   t   callback(    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   callWhenActivatedu   s    	(   R   R   R   R:   R/   R#   R7   R=   R.   R?   RF   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyR   =   s   				c         C   s,   t  j ƒ  } |  j t | j d | f ƒ| S(   sD  
    Wrap a deferred returned from a pb method in another deferred that
    expects a RemotePublished as a result.  This will allow you to wait until
    the result is really available.

    Idiomatic usage would look like::

        publish.whenReady(serverObject.getMeAPublishable()).addCallback(lookAtThePublishable)
    t   callbackArgs(   R    t   DeferredR1   t	   _pubReadyt   errback(   t   dt   d2(    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt	   whenReady}   s    

c         C   s   |  j  | j ƒ d S(   s
   (internal)N(   RF   RE   (   t   resultRL   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyRI   Œ   s    (   R   R	   t   twisted.internetR    t   twisted.spreadR   R   R   t	   CacheableR   t   RemoteCacheR   RM   RI   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/spread/publish.pyt   <module>   s   *@	