U
     T‚g,  ã                   @   sd   d Z ddlZddlmZ ddlmZmZmZ G dd„ dejƒZ	G dd„ dej
ƒZd	d
„ Zdd„ ZdS )z]
Persistently cached objects for PB.

Maintainer: Glyph Lefkowitz

Future Plans: None known.
é    N)Údefer)ÚbananaÚflavorsÚjellyc                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚPublishablez6An object whose cached state persists across sessions.c                 C   s   |   ¡  || _d S )N)Ú	republishÚpublishedID)Úselfr   © r
   ú:/tmp/pip-unpacked-wheel-1_7di8ic/twisted/spread/publish.pyÚ__init__   s    zPublishable.__init__c                 C   s   t   ¡ | _dS )z=Set the timestamp to current and (TODO) update all observers.N)ÚtimeÚ	timestamp©r	   r
   r
   r   r      s    zPublishable.republishc                 C   s
   |   |¡S )ú
(internal))ÚgetStateToPublishFor©r	   Úperspectiver
   r
   r   Úview_getStateToPublish   s    z"Publishable.view_getStateToPublishc                 C   s   |   ¡ S )z:Implement me to special-case your state for a perspective.)ÚgetStateToPublishr   r
   r
   r   r   #   s    z Publishable.getStateToPublishForc                 C   s   t d| j ƒ‚dS )zBImplement me to return state to copy as part of the publish phase.z%s.getStateToPublishForN)ÚNotImplementedErrorÚ	__class__r   r
   r
   r   r   '   s    zPublishable.getStateToPublishc                 C   s:   |r|j }| ¡ j}nd}d}t || ¡| j||| jdœS )z6Get all necessary metadata to keep a clientside cache.ÚNone)Úremoter   r   Úservicer   )ZperspectiveNameZ
getServiceZserviceNamer   Z	ViewPointr   r   )r	   r   ZobserverZpnameZsnamer
   r
   r   ÚgetStateToCacheAndObserveFor+   s    
ûz(Publishable.getStateToCacheAndObserveForN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r
   r
   r
   r   r      s   r   c                   @   sJ   e Zd ZdZdZdZddd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ ZdS )ÚRemotePublishedz6The local representation of remote Publishable object.r   Úpubc                 C   s   d  | j| jt| jƒ|¡S )Nz{}-{}-{}.{})Úformatr   r   Ústrr   )r	   Úextr
   r
   r   ÚgetFileNameC   s    üzRemotePublished.getFileNamec              	   C   sž   | j  |¡ g | _z&t|  ¡ dƒ�}| ¡ }W 5 Q R X W n tk
rP   d}Y nX t t	 
|¡¡}|j| jk}|r†|  |j ¡ d| _n| j d¡ | j¡ d S )NÚrbr   é   r   )Ú__dict__ÚupdateÚ_activationListenersÚopenr%   ÚreadÚOSErrorr   Zunjellyr   Údecoder   Ú_cbGotUpdateÚ_wasCleanWhenLoadedr   Z
callRemoteÚaddCallbacks)r	   ÚstateÚdataFileÚdataZrecentÚnewselfr
   r
   r   ÚsetCopyableStateK   s    
z RemotePublished.setCopyableStatec                 C   s,   | j  ¡ }|d= |d= |d= |d= |d= |S )NZbrokerr   Zluidr*   ÚisActivated)r(   Úcopy)r	   Úotherr
   r
   r   Ú__getstate__\   s    
zRemotePublished.__getstate__c              	   C   sh   | j  |¡ d| _| jD ]}|| ƒ qg | _|  ¡  t|  ¡ dƒ�}| t 	t
 
| ¡¡¡ W 5 Q R X d S )Nr'   Úwb)r(   r)   r7   r*   Ú	activatedr+   r%   Úwriter   Úencoder   )r	   ZnewStateZlistenerr3   r
   r
   r   r/   g   s    

zRemotePublished._cbGotUpdatec                 C   s   dS )znImplement this method if you want to be notified when your
        publishable subclass is activated.
        Nr
   r   r
   r
   r   r<   r   s    zRemotePublished.activatedc                 C   s    | j r|| ƒ n| j |¡ dS )zZExternally register for notification when this publishable has received all relevant data.N)r7   r*   Úappend)r	   Úcallbackr
   r
   r   ÚcallWhenActivatedw   s    
z!RemotePublished.callWhenActivatedN)r!   )r   r   r   r   r7   r0   r%   r6   r:   r/   r<   rA   r
   r
   r
   r   r    =   s   
r    c                 C   s    t  ¡ }| jt|j|fd� |S )aD  
    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)
    )ZcallbackArgs)r   ZDeferredr1   Ú	_pubReadyZerrback)ÚdÚd2r
   r
   r   Ú	whenReady   s    
rE   c                 C   s   |   |j¡ dS )r   N)rA   r@   )ÚresultrD   r
   r
   r   rB   Ž   s    rB   )r   r   Ztwisted.internetr   Ztwisted.spreadr   r   r   Z	Cacheabler   ZRemoteCacher    rE   rB   r
   r
   r
   r   Ú<module>   s   	)B