3
AAd]€  ã               @   sf   d dl T d dlZd dlZd dlZd dlmZ d dlmZ eje	ƒZ
G dd„ deƒZG dd„ deƒZdS )	é    )Ú*N)ÚGeneratorType)Ú__replacement_setstate__c               @   s2   e Zd ZdZddd„Zdd„ Zdd„ ZeeƒZd	S )ÚNewUnpicklerzõ An unpickler that implements a two-stage pickling process to make it
    possible to unpickle complicated Python object hierarchies where the
    unserialized state of an object depends on the state of other objects in
    the same pickle.
    é   c             C   sF   g | _ | j}tj|td < tj| ƒ}| j|ƒ g | _ tj|td < |S )z‹Read a pickled object representation from the open file.

        Return the reconstituted object hierarchy specified in the file.
        r   )ÚobjectsÚdispatchr   Ú
load_buildÚBUILDÚ	UnpicklerÚloadÚ
initialize)ÚselfÚmax_passr   Úret© r   ú„/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/persistence/versioned_unpickler.pyr      s    

zNewUnpickler.loadc       
      C   s
  g }x\| j D ]R}t|dƒrt|jƒr|jƒ }t|tƒrF|j||fƒ q|d k	rtd||f ƒ‚qW |dk rrt|ƒ}d}xŽt|ƒdk�r|d7 }||kr¸dd„ |D ƒ}d||f }t|ƒ‚xH|d d … D ]8\}}	yt	|	ƒ W qÆ t
k
rü   |j||	fƒ Y qÆX qÆW qxW d S )NÚ__initialize__z<Unexpected return value from __initialize__.  %s returned %sr   r   c             S   s   g | ]}|d  ‘qS )r   r   )Ú.0Úxr   r   r   ú
<listcomp>D   s    z+NewUnpickler.initialize.<locals>.<listcomp>z—Reached maximum pass count %s.  You may have
                         a deadlock!  The following objects are
                         uninitialized: %s)r   ÚhasattrÚcallabler   Ú
isinstancer   ÚappendÚUnpicklingErrorÚlenÚnextÚStopIterationÚremove)
r   r   Ú
generatorsÚobjr   ÚcountZnot_doneÚmsgÚoÚgr   r   r   r   ,   s0    


zNewUnpickler.initializec             C   s*   t |tƒr|jj|jd ƒ tj|ƒ d S )Né   éþÿÿÿ)r   r   r   r   Ústackr   r	   )Úclsr!   r   r   r   r	   U   s    
zNewUnpickler.load_buildNéÿÿÿÿ)r*   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r	   Úclassmethodr   r   r   r   r      s
   
)r   c               @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚVersionedUnpicklera   This class reads in a pickled file created at revision version 'n'
    and then applies the transforms specified in the updater class to
    generate a new set of objects which are at revision version 'n+1'.

    I decided to keep the loading of the updater out of this generic class
    because we will want updaters to be generated for each plugin's type
    of project.

    This ensures that the VersionedUnpickler can remain ignorant about the
    actual version numbers - all it needs to do is upgrade one release.
    Nc             C   s   t j| |ƒ || _d S )N)r   Ú__init__Úupdater)r   Úfiler2   r   r   r   r1   k   s    zVersionedUnpickler.__init__c          
   C   s¨   | j r>|| }}| j j||ƒ\}}| j||ƒ}| j|||ƒ nfytj| ||ƒ}W n(   tjd||f ƒ tjdƒ Y nX t	|ddƒ}|r¤t
j|d|ƒ}t|d|ƒ |S )z` Overridden method from Unpickler.

        NB  __setstate__ is not called until later.
        zLooking for [%s] [%s]z,Problem using default unpickle functionalityÚ__setstate_original__FNÚ__setstate__)r2   Z
get_latestÚimport_nameÚadd_updaterr   Ú
find_classÚloggerÚerrorÚ	exceptionÚgetattrÚnewÚinstancemethodÚsetattr)r   ÚmoduleÚnameZoriginal_moduleÚoriginal_nameÚklassÚfnÚmr   r   r   r8   q   s    
zVersionedUnpickler.find_classc             C   s^   | j jj||fdƒ}|rZ| j||ƒ tj|d|ƒ}t|d|ƒ tjtd|ƒ}t|d|ƒ n dS )zw If there is an updater defined for this class we will add it to the
        class as the __setstate__ method.
        FNZ__updater__r5   )r2   Z	setstatesÚgetÚbackup_setstater=   r>   r?   r   )r   r@   rA   rC   rD   rE   r   r   r   r7   š   s    zVersionedUnpickler.add_updaterc             C   sT   t |ddƒrPt |ddƒr&d| jj }nd}t |dƒ}tj|d|ƒ}t|||ƒ n dS )zE If the class has a user defined __setstate__ we back it up.
        r5   Fr4   z__setstate__%sN)r<   r2   Ú	__class__r=   r>   r?   )r   r@   rC   rA   ÚmethodrE   r   r   r   rG   ´   s    
z"VersionedUnpickler.backup_setstatec             C   s    t |tƒ tƒ |gƒ}t|ƒ| S )a¼  
        If the class is needed for the latest version of the application then
        it should presumably exist.

        If the class no longer exists then we should perhaps return
        a proxy of the class.

        If the persisted file is at v1 say and the application is at v3 then
        objects that are required for v1 and v2 do not have to exist they only
        need to be placeholders for the state during an upgrade.
        )Ú
__import__ÚglobalsÚlocalsÚvars)r   r@   rA   r   r   r   r6   Í   s    zVersionedUnpickler.import_name)N)	r+   r,   r-   r.   r1   r8   r7   rG   r6   r   r   r   r   r0   ]   s   
)r0   )ÚpickleÚsysr=   ÚloggingÚtypesr   Zapptools.persistence.updaterr   Ú	getLoggerr+   r9   r   r   r0   r   r   r   r   Ú<module>   s   
M