3
6ßL]uR  ã               @   sÀ   d Z ddlZddlZddlmZ ddlmZ ejd dkrHddlm	Z
 nddlm
Z
 ddlmZmZ dd	lmZmZ ejeƒZd
ZdZdZdd„ Zdd„ ZG dd„ de
ƒZG dd„ deeƒZdS )a   An unpickler that is tolerant of class refactorings, and 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.
é    N)Úpath)ÚGeneratorTypeé   )Ú
_Unpickler)Ú	Unpickler)ÚUnpicklingErrorÚBUILD)Ú	HasTraitsÚInstanceZ,__enthought_sweet_pickle_original_setstate__Ú__setstate__Z$__enthought_sweet_pickle_unpickler__c             C   sD   t | tƒ\}}}|j| |||ƒ}|dk	r@tjd|ƒ | jj|ƒ dS )zI Called to enable an unpickler to modify the state of this instance.
    NzFinal state: %s)ÚgetattrÚ_UNPICKLER_DATAÚmodify_stateÚloggerÚdebugÚ__dict__Úupdate)ÚselfÚstateZ	unpicklerÚmoduleÚname© r   úO/tmp/pip-install-5sebvq2j/apptools/apptools/sweet_pickle/versioned_unpickler.pyÚ__replacement_setstate__:   s
    r   c             C   sF   | j d }t|ƒtkr>t| jdƒr2tj| jjƒ}nd}||d< | jƒ S )zy Called prior to the actual load_build() unpickling method which primes
        the state dictionary with meta-data.
    é   r   Ú Z_pickle_file_nameéÿÿÿÿ)	ÚstackÚtypeÚdictÚhasattrÚ_filer   Úabspathr   Ú
load_build)r   r   Zpickle_file_namer   r   r   Úload_build_with_meta_dataN   s    
r$   c               @   s.   e Zd ZdZddd„Zdd„ Zedd„ ƒ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.
    r   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%   r#   r   r   ÚloadÚ
initialize)r   Úmax_passr'   Úretr   r   r   r(   p   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>�   s    z+NewUnpickler.initialize.<locals>.<listcomp>z—Reached maximum pass count %s.  You may have
                         a deadlock!  The following objects are
                         uninitialized: %s)r&   r    Úcallabler,   Ú
isinstancer   Úappendr   Ú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é   éþÿÿÿ)r1   r%   r&   r2   r   r   r#   )Úclsr8   r   r   r   r#   ®   s    
zNewUnpickler.load_buildNr   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r)   Úclassmethodr#   r   r   r   r   r%   i   s   
)r%   c                   sp   e Zd ZdZedƒZ‡ fdd„Z‡ fdd„Zdd„ Zd	d
„ Z	dd„ Z
‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚVersionedUnpicklera'   An unpickler that is tolerant of class refactorings.

        This class reads in a pickled file and applies the transforms
        specified in its updater to generate a new hierarchy of objects
        which are at the current version of the classes they are instances
        of.

        Note that the creation of an updater is kept out of this class to
        ensure that the class can be reused in different situations.
        However, if no updater is provided during construction, then the
        global registry updater will be used.
    z%apptools.sweet_pickle.updater.Updaterc                sH   t t| ƒj|ƒ || _| jd kr4ddlm} |ƒ | _tjd| | jƒ d S )Nr   )Úget_global_registryz*VersionedUnpickler [%s] using Updater [%s])	ÚsuperrE   Ú__init__r!   ÚupdaterZglobal_registryrF   r   r   )r   ÚfileÚkwsrF   )Ú	__class__r   r   rH   Ø   s    
zVersionedUnpickler.__init__c                s.  |j ƒ }|j ƒ }ytt| ƒj||ƒ}W n   Y nX || }}| jdk	rˆ| jj||ƒrˆ| j||ƒ\}}||ksv||krˆtjd||||ƒ ytt| ƒj||ƒ}W nb t	k
�r  } zDddl
m} tjd||f dd� |d||t|ƒ| jjf ƒ‚W Y dd}~X nX | jdk	�r*| j||ƒ�r*| j|||ƒ |S )	ax   Returns the class definition for the named class within the
            specified module.

            Overridden here to:

            - Allow updaters to redirect to a different class, possibly
              within a different module.
            - Ensure that any setstate hooks for the class are called
              when the instance of this class is unpickled.
        NzUnpickling [%s.%s] as [%s.%s]r   )r   z%Traceback when finding class [%s.%s]:T)Úexc_infozCUnable to load class [%s.%s]. Original exception was, "%s".  map:%s)ÚstriprG   rE   Ú
find_classrI   Úhas_class_mappingÚ_get_target_classr   r   Ú	ExceptionÚapptools.sweet_pickler   ÚstrÚ	class_mapÚ_has_state_functionÚ_add_unpickler)r   r   r   ÚklassÚoriginal_moduleÚoriginal_nameÚer   )rL   r   r   rO   ò   s2    


*zVersionedUnpickler.find_classc          
   C   s¼  | j |jƒ | jj||ƒ}|j|dƒ}| jj|jj|jjƒ}t||dƒ}|}	|	d }
xÚ| jj||ƒ}x~|||
f| jjkrâ| jj|||
f }x,|D ]$}t	j
d
|||	|||
|ƒ ||ƒ}q�W |j||	ƒ}||	krÔ|	d }|}	|	d }
qfW | jj||ƒ�r*|| }}| jj||f \}}t	j
d|||	|||	ƒ qVP qVW t|tdƒ}|dk	�r^||ƒ d}t||ƒ}	n|}||jjk�sˆ||jjk�sˆ|	|k�r¸ddlm} |dd	|||	|jj|jj|f  ƒ‚|S )zÔ Called to update the specified state dictionary, which represents
            the class of the specified name within the specified module, to
            complete the unpickling of the specified object.
        r   r   z'Modifying state from [%s.%s (v.%s)] to z [%s.%s (v.%s)] using function %sz[%s.%s (v.%s)]N)r   zUnexpected state! Got z&[%s.%s (v.%s)] expected [%s.%s (v.%s)]zGModifying state from [%s.%s (v.%s)] to [%s.%s (v.%s)] using function %sz5Modifying state from [%s.%s (v.%s)] to [%s.%s (v.%s)])Ú_remove_unpicklerrL   rI   Zget_version_attributeÚgetrA   r@   r   Zstate_functionsr   r   rP   rU   Ú_SETSTATE_NAMErS   r   )r   r8   r   r   r   Z
source_keyZsource_versionZ
target_keyZtarget_versionÚversionZnext_versionÚkeyZ	functionsÚfZnew_versionrY   rZ   ÚfnÚresultr   r   r   r   r   .  sZ    




zVersionedUnpickler.modify_statec             C   sD   t jd|ƒ | j|ƒ tjd|ƒ}t|t|ƒ t|t| ||fƒ dS )zƒ Modifies the specified class so that our 'modify_state' method
            is called when its next instance is unpickled.
        zAdding unpickler hook to [%s]N)r   r   Ú_backup_setstater   Ú__get__Úsetattrr^   r   )r   rX   r   r   Úmr   r   r   rW   ƒ  s
    
z!VersionedUnpickler._add_unpicklerc             C   s@   t |tdƒ}|dk	r<tjdtt|ƒ |jd|ƒ}t|t|ƒ dS )z9 Backs up the specified class's setstate method.
        Nz&Backing up method [%s] to [%s] on [%s])r   r^   r   r   Ú_BACKUP_NAMEre   rf   )r   rX   Úmethodrg   r   r   r   rd   ’  s    
z#VersionedUnpickler._backup_setstatec          
      s¶   || }}g }xž| j j||ƒr¬||f|krRddlm} |dd||||| j f  ƒ‚|j||fƒ | j j||f \}}ytt| ƒj||ƒ W q   t	j
d||f ƒ Y qX qW ||fS )aÃ   Returns the class info that the class, within the specified module
            and with the specified name, should be instantiated as according to
            our associated updater.

            This is done in a manner that allows for chaining of class mappings
            but is tolerant of the fact that a mapping away from an
            intermediate class may not be registered until an attempt is made
            to load that class.
        r   )r   z Detected infinite loop in class z3mapping from [%s.%s] to [%s.%s] within Updater [%s]z _get_target_class can't find: %s)rI   rP   rS   r   r2   rU   rG   rE   rO   r   Ú	exception)r   r   r   rY   rZ   Úvisitedr   )rL   r   r   rQ   ž  s"    

z$VersionedUnpickler._get_target_classc             C   sJ   d}x@|sD| j j||ƒ}|s| j j||ƒr@| j j||f \}}qP qW |S )aZ   Returns True if the updater contains any state functions that could
            be called by unpickling an instance of the class identified by the
            specified module and name.

            Note: If we had a version number we could tell for sure, but we
            don't have one so we'll have to settle for 'could' be called.
        F)rI   Zhas_state_functionrP   rU   )r   r   r   rc   r   r   r   rV   Ã  s    	z&VersionedUnpickler._has_state_functionc             C   s$   t jd|ƒ | j|ƒ t|tƒ dS )z� Restores the specified class to its unmodified state.  Meaning
            we won't get called when its next instance is unpickled.
        z!Removing unpickler hook from [%s]N)r   r   Ú_restore_setstateÚdelattrr   )r   rX   r   r   r   r\   ß  s    
z$VersionedUnpickler._remove_unpicklerc             C   sV   t |tdƒ}|dk	rHtjdtt|ƒ t|tƒ |jd|ƒ}t|t|ƒ n
t|tƒ dS )zK Restores the original setstate method back to its rightful place.
        Nz%Restoring method [%s] to [%s] on [%s])r   rh   r   r   r^   rm   re   rf   )r   rX   ri   rg   r   r   r   rl   í  s    

z$VersionedUnpickler._restore_setstate)r@   rA   rB   rC   r
   rI   rH   rO   r   rW   rd   rQ   rV   r\   rl   Ú__classcell__r   r   )rL   r   rE   º   s   		<U%rE   )rC   ÚsysÚloggingÚosr   Útypesr   Úversion_infoÚpickler   r   r   r   Z
traits.apir	   r
   Ú	getLoggerr@   r   rh   r^   r   r   r$   r%   rE   r   r   r   r   Ú<module>   s"   
Q