3
AAd]¤  ã               @   sN   d Z ddlZddlZddlZejeƒZdd„ ZG dd„ dƒZdd„ Z	e	ƒ Z
dS )	zGA version registry that manages handlers for different state
versions.
é    Nc             C   st   g }xbt j| jƒD ]R}|j|j }}|dkr.qy
|j}W n tk
rP   d}Y nX |j||f|fƒ qW |jƒ  |S )z¼Walks the class hierarchy and obtains the versions of the
    various classes and returns a list of tuples of the form
    ((class_name, module), version) in reverse order of the MRO.
    Ú__builtin__é   )r   éÿÿÿÿ)	ÚinspectÚgetmroÚ	__class__Ú__name__Ú
__module__Ú__version__ÚAttributeErrorÚappendÚreverse)ÚobjÚresÚclsÚ
class_nameÚmoduleÚversion© r   ú�/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/persistence/version_registry.pyÚget_version   s    

r   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚHandlerRegistrya»  A simple version conversion handler registry.  Classes register
    handlers in order to convert the state version to the latest
    version.  When an object's state is about to be set, the `update`
    method of the registy is called.  This in turn calls any handlers
    registered for the class/module and this handler is then called
    with the state and the version of the state.  The state is
    modified in-place by the handlers.
    c             C   s
   i | _ d S )N)Úhandlers)Úselfr   r   r   Ú__init__5   s    zHandlerRegistry.__init__c             C   sB   ||f}|| j kr0d|d |d f }tj|ƒ || j ||f< dS )zÑRegister `handler` that handles versioning for class having
        class name (`class_name`) and module name (`module`).  The
        handler function will be passed the state and its version to fix.
        z(Overwriting version handler for (%s, %s)r   r   N)r   ÚloggerÚwarn)r   r   r   ÚhandlerÚkeyÚmsgr   r   r   Úregister:   s
    

zHandlerRegistry.registerc             C   s   | j j||fƒ dS )z9Unregisters any handlers for a class and module.
        N)r   Úpop)r   r   r   r   r   r   Ú
unregisterE   s    zHandlerRegistry.unregisterc             C   sj   | j  st|dƒ rdS |jd }xB|D ]:}|d }y| j | ||d ƒ W q( tk
r`   Y q(X q(W dS )zgUpdates the given state using the handlers.  Note that the
        state is modified in-place.
        Ú__metadata__Nr   r   r   )r   Úhasattrr#   ÚKeyError)r   ÚstateÚversionsÚverr   r   r   r   ÚupdateJ   s    

zHandlerRegistry.updateN)r   r	   Ú__qualname__Ú__doc__r   r    r"   r)   r   r   r   r   r   +   s
   r   c              C   sN   d} x:t tjjƒ ƒD ](}d|krtj| }t|dƒr|j} P qW | sJtƒ } | S )z/Creates a reload safe, singleton registry.
    NZversion_registryÚregistry)ÚlistÚsysÚmodulesÚkeysr$   r,   r   )r,   r   Úmodr   r   r   Ú_create_registryY   s    

r2   )r+   r.   r   ÚloggingÚ	getLoggerr   r   r   r   r2   r,   r   r   r   r   Ú<module>   s   
.