a
    ¨ˆc³  ã                   @   sN   d Z ddlZddlZddlZe 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   sn   g }t  | j¡D ]P}|j|j }}|dv r,qz
|j}W n tyL   d}Y n0 | ||f|f¡ q| ¡  |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__éÿÿÿÿ)	ÚinspectÚgetmroÚ	__class__Ú__name__Ú
__module__Ú__version__ÚAttributeErrorÚappendÚreverse)ÚobjÚresÚclsÚ
class_nameÚmoduleÚversion© r   úh/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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__<   s    zHandlerRegistry.__init__c                 C   sB   ||f}|| j v r0d|d |d f }t |¡ || 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   é   N)r   ÚloggerÚwarn)r   r   r   ÚhandlerÚkeyÚmsgr   r   r   ÚregisterA   s
    

zHandlerRegistry.registerc                 C   s   | j  ||f¡ dS )z0Unregisters any handlers for a class and module.N)r   Úpop)r   r   r   r   r   r   Ú
unregisterL   s    zHandlerRegistry.unregisterc              	   C   s`   | j rt|dƒsdS |jd }|D ]8}|d }z| j | ||d ƒ W q" tyX   Y q"0 q"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   ÚupdateP   s    
zHandlerRegistry.updateN)r   r   Ú__qualname__Ú__doc__r   r    r"   r)   r   r   r   r   r   2   s
   	r   c                  C   sH   d} t j ¡ D ]*}d|v rt j| }t|dƒr|j}  q:q| sDtƒ } | S )z*Creates a reload safe, singleton registry.NZversion_registryÚregistry)ÚsysÚmodulesÚkeysr$   r,   r   )r,   r   Úmodr   r   r   Ú_create_registry_   s    

r1   )r+   r-   r   ÚloggingÚ	getLoggerr   r   r   r   r1   r,   r   r   r   r   Ú<module>
   s   
-