a
    ¦ˆco<  ã                   @   sÎ   d Z ddlmZmZ ddlZddlZddlZddlmZ ddl	m
Z
 ddlmZmZmZ dd„ ZG d	d
„ d
e
ƒZdd„ Zeƒ add„ Zdd„ Zdd„ Zefdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ú% Manages all registered adaptations. é    )ÚheappopÚheappushN)ÚAdaptationError)Ú	HasTraits)ÚDictÚListÚStrc                 C   s   | S )zv An adapter factory used to register that a protocol provides another.

    See 'register_provides' for details.

    © )Úadapteer
   r
   úg/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/adaptation/adaptation_manager.pyÚno_adapter_necessary   s    r   c                   @   sn   e Zd ZdZedd„ ƒZedd„ ƒZefdd„Zdd	„ Z	d
d„ Z
dd„ Zdd„ ZeeeƒZdd„ Zdd„ ZdS )ÚAdaptationManagerr   c                 C   sN   t  | |¡sdS t | ¡dd… }d}|D ]}t  ||¡rD|d7 }q* qJq*|S )a…   Return the distance in the MRO from 'from_type' to 'to_protocol'.

        If `from_type` provides `to_protocol`, returns the distance between
        `from_type` and the super-most class in the MRO hierarchy providing
        `to_protocol` (that's where the protocol was provided in the first
        place).

        If `from_type` does not provide `to_protocol`, return None.

        Né   r   )r   Úprovides_protocolÚinspectÚgetmro)Z	from_typeÚto_protocolZ
supertypesZdistanceÚtr
   r
   r   Úmro_distance_to_protocol'   s    
z*AdaptationManager.mro_distance_to_protocolc                 C   s
   t | |ƒS )ah   Does the given type provide (i.e implement) a given protocol?

        Parameters
        ----------
        type_
            Python 'type'.
        protocol
            Either a regular Python class or a traits Interface.

        Returns
        -------
        result : bool
            True if the object provides the protocol, otherwise False.

        )Ú
issubclass©Útype_Úprotocolr
   r
   r   r   J   s    z#AdaptationManager.provides_protocolc                 C   sL   |   t|ƒ|¡r|}n|  ||¡}|du rH|tu rDtd||f ƒ‚n|}|S )a   Attempt to adapt an object to a given protocol.

        If *adaptee* already provides (i.e. implements) the given protocol
        then it is simply returned unchanged.

        Otherwise, we try to build a chain of adapters that adapt *adaptee* to
        *to_protocol*. If no such adaptation is possible then either an
        AdaptationError is raised, or *default* is returned.

        Parameters
        ----------
        adaptee : object
            The object that we want to adapt.
        to_protocol : type or interface
            The protocol that the want to adapt *adaptee* to.
        default : object, optional
            Object to return if no adaptation is possible. If no default is
            provided, and adaptation fails, an ``AdaptationError`` is raised.

        Returns
        -------
        adapted_object : to_protocol
            The original adaptee adapted to the target protocol.

        Raises
        ------
        AdaptationError
            If adaptation is not possible, and no default is given.
        NzCould not adapt %r to %r)r   ÚtypeÚ_adaptr   )Úselfr   r   ÚdefaultÚresultr
   r
   r   Úadapt_   s    !
ÿzAdaptationManager.adaptc                 C   s   | j  |jg ¡}| |¡ dS )ú: Register an offer to adapt from one protocol to another. N)Ú_adaptation_offersÚ
setdefaultÚfrom_protocol_nameÚappend)r   ÚofferÚoffersr
   r
   r   Úregister_offer‘   s    ÿz AdaptationManager.register_offerc                 C   s$   ddl m} |  ||||d�¡ dS )z¨ Register an adapter factory.

        This is a simply a convenience method that creates and registers an
        'AdaptationOffer' from the given arguments.

        r   )ÚAdaptationOffer)ÚfactoryÚfrom_protocolr   N)Z"traits.adaptation.adaptation_offerr(   r'   )r   r)   r*   r   r(   r
   r
   r   Úregister_factory™   s    ýÿz"AdaptationManager.register_factoryc                 C   s   |   t||¡ dS )ú, Register that a protocol provides another. N)r+   r   )r   Úprovider_protocolr   r
   r
   r   Úregister_provides«   s    ÿz#AdaptationManager.register_providesc                 C   s   |   ||d¡duS )z¦ Does the object support a given protocol?

        An object "supports" a protocol if either it "provides" it directly,
        or it can be adapted to it.

        N)r   )r   Úobjr   r
   r
   r   Úsupports_protocol²   s    z#AdaptationManager.supports_protocolc                 C   sæ   t  ¡ }ddt|ƒfg t|ƒfg}t|ƒdkrât|ƒ\}}}|  ||¡}|jt 	t
¡d� |D ]€\}	}
||
g }|  |
j|¡r¬|}|D ]}
|
 |¡}|du r† qÞq†|  S q^|\}}}|d ||	 t|ƒf}t||||
jfƒ q^q"dS )zy Returns an adapter that adapts an object to the target class.

        Returns None if no such adapter exists.

        r   )ÚkeyNr   )Ú	itertoolsÚcountÚnextr   Úlenr   Ú_get_applicable_offersÚsortÚ	functoolsÚ
cmp_to_keyÚ)_by_weight_then_from_protocol_specificityr   r   r)   r   )r   r   r   ÚcounterZoffer_queueZweightÚpathÚcurrent_protocolÚedgesÚmro_distancer%   Únew_pathÚadapterZadapter_weightZ
mro_weightÚ_Z
new_weightr
   r
   r   r   Ã   s8    ,ÿÿ



ýÿzAdaptationManager._adaptc           	      C   sZ   g }| j  ¡ D ]F\}}|d j}|  ||¡}|dur|D ]}||vr8| ||f¡ q8q|S )a´   Find all adaptation offers that can be applied to a protocol.

        Return all the applicable offers together with the number of steps
        up the MRO hierarchy that need to be taken from the protocol
        to the offer's from_protocol.
        The returned object is a list of tuples (mro_distance, offer) .

        In terms of our graph algorithm, we're looking for all outgoing edges
        from the current node.
        r   N)r!   Úitemsr*   r   r$   )	r   r=   r<   r>   r#   r&   r*   r?   r%   r
   r
   r   r6   3  s    
ÿz(AdaptationManager._get_applicable_offersN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   r'   r+   r.   r0   r   r	   r   r!   r   r6   r
   r
   r
   r   r   "   s   
"
2
pr   c                 C   s`   | \}}|\}}||k rdS ||kr(dS |j |j u r8dS t|j |j ƒrJdS t|j |j ƒr\dS dS )af   Comparison function for graph edges.

    Each edge is of the form (mro distance, adaptation offer).

    Comparison is done by mro distance first, and by offer's from_protocol
    issubclass next.

    If two edges have the same mro distance, and the from_protocols of the
    two edges are not subclasses of one another, they are considered "equal".

    éÿÿÿÿr   r   )r*   r   )Zedge_1Zedge_2Zmro_distance_1Zoffer_1Zmro_distance_2Zoffer_2r
   r
   r   r:   R  s    r:   c                 C   s   | a dS )z: Set the global adaptation manager to the given instance. N©Úadaptation_manager)Znew_adaptation_managerr
   r
   r   Úset_global_adaptation_manager�  s    rL   c                   C   s
   t ƒ adS )zL Set the global adaptation manager to a new AdaptationManager instance.
    N)r   rK   r
   r
   r
   r   Úreset_global_adaptation_manager‡  s    rM   c                   C   s   t S )z3 Set a reference to the global adaptation manager. rJ   r
   r
   r
   r   Úget_global_adaptation_managerŽ  s    rN   c                 C   s   t ƒ }| | ||¡S )z1 Attempt to adapt an object to a given protocol. )rN   r   )r   r   r   Úmanagerr
   r
   r   r   š  s    r   c                 C   s   t ƒ }| | ||¡S )z Register an adapter factory. )rN   r+   )r)   r*   r   rO   r
   r
   r   r+      s    r+   c                 C   s   t ƒ }| | ¡S )r    )rN   r'   )r%   rO   r
   r
   r   r'   ¦  s    r'   c                 C   s   t ƒ }| | |¡S )r,   )rN   r.   )r-   r   rO   r
   r
   r   r.   ¬  s    r.   c                 C   s   t ƒ }| | |¡S )z+ Does the object support a given protocol? )rN   r0   )r/   r   rO   r
   r
   r   r0   ²  s    r0   c                 C   s   t  | |¡S )z? Does the given type provide (i.e implement) a given protocol? )r   r   r   r
   r
   r   r   ¸  s    r   )rG   Úheapqr   r   r   r2   r8   Z"traits.adaptation.adaptation_errorr   Ztraits.has_traitsr   Ztraits.trait_typesr   r   r	   r   r   r:   rK   rL   rM   rN   r   r+   r'   r.   r0   r   r
   r
   r
   r   Ú<module>   s,   
  2,