3
8ßL]$=  ã               @   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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 )z% 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	   úH/tmp/pip-install-5sebvq2j/traits/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 )ÚAdaptationManagerz% Manages all registered adaptations. c             C   sP   t j| |ƒsdS tj| ƒdd… }d}x$|D ]}t j||ƒrF|d7 }q,P q,W |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 )ag   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   N   s    z#AdaptationManager.provides_protocolc             C   sJ   | j |j|ƒr|}n| j||ƒ}|dkrF|tkrBtd||f ƒ‚n|}|S )a£   Attempt to adapt an object to a given protocol.

        Parameters
        ----------
        adaptee :
          The object that we want to adapt.

        to_protocol :
          The protocol that the want to adapt the object to.

        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 (if default=Adaptation error), or `default` is returned (as
        in the default value passed to 'getattr' etc).

        NzCould not adapt %r to %r)r   Ú	__class__Ú_adaptr   )Úselfr
   r   ÚdefaultÚresultr	   r	   r   Úadaptd   s    zAdaptationManager.adaptc             C   s   | j j|jg ƒ}|j|ƒ dS )z: 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} | j||||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   | j t||ƒ dS )z, Register that a protocol provides another. N)r(   r   )r   Úprovider_protocolr   r	   r	   r   Úregister_provides®   s    
z#AdaptationManager.register_providesc             C   s   | j ||dƒdk	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 jƒ }ddt|ƒfg t|ƒfg}xØt|ƒdkrút|ƒ\}}}| j||ƒ}tjr^|j	t
d� n|j	tjt
ƒd� x†|D ]~\}	}
||
g }| j|
j|ƒrÂ|}xX|D ]}
|
j|ƒ}|dkr P q W |S qv|\}}}|d ||	 t|ƒf}t||||
jfƒ qvW q$W dS )zy Returns an adapter that adapts an object to the target class.

        Returns None if no such adapter exists.

        r   )Úcmp)ÚkeyNr   )Ú	itertoolsÚcountÚnextÚtypeÚlenr   Ú_get_applicable_offersÚsixÚPY2ÚsortÚ)_by_weight_then_from_protocol_specificityÚ	functoolsÚ
cmp_to_keyr   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   È   s4    ,
	




zAdaptationManager._adaptc       	      C   sb   g }xX| j jƒ D ]J\}}|d j}| j||ƒ}|dk	rx"|D ]}||kr<|j||fƒ q<W qW |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   r4   <  s    

z(AdaptationManager._get_applicable_offersN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   r$   r(   r*   r,   r   r   r   r   r   r4   r	   r	   r	   r   r   &   s   #,
	
tr   c             C   s`   | \}}|\}}||k rdS ||kr(dS |j |j k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   éÿÿÿÿrI   )r'   r   )Zedge_1Zedge_2Zmro_distance_1Zoffer_1Zmro_distance_2Zoffer_2r	   r	   r   r8   [  s    r8   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    rK   c               C   s
   t ƒ adS )zL Set the global adaptation manager to a new AdaptationManager instance.
    N)r   rJ   r	   r	   r	   r   Úreset_global_adaptation_manager�  s    rL   c               C   s   t S )z3 Set a reference to the global adaptation manager. )rJ   r	   r	   r	   r   Úget_global_adaptation_manager—  s    rM   c             C   s   t ƒ }|j| ||ƒS )z1 Attempt to adapt an object to a given protocol. )rM   r   )r
   r   r   Úmanagerr	   r	   r   r   £  s    r   c             C   s   t ƒ }|j| ||ƒS )z Register an adapter factory. )rM   r(   )r&   r'   r   rN   r	   r	   r   r(   ©  s    r(   c             C   s   t ƒ }|j| ƒS )z: Register an offer to adapt from one protocol to another. )rM   r$   )r"   rN   r	   r	   r   r$   ¯  s    r$   c             C   s   t ƒ }|j| |ƒS )z, Register that a protocol provides another. )rM   r*   )r)   r   rN   r	   r	   r   r*   µ  s    r*   c             C   s   t ƒ }|j| |ƒS )z+ Does the object support a given protocol? )rM   r,   )r+   r   rN   r	   r	   r   r,   »  s    r,   c             C   s   t j| |ƒS )z? Does the given type provide (i.e implement) a given protocol? )r   r   )r   r   r	   r	   r   r   Á  s    r   )rG   Úheapqr   r   r   r/   r9   r5   Z"traits.adaptation.adaptation_errorr   Ztraits.has_traitsr   Ztraits.trait_typesr   r   r   r   r   r8   rJ   rK   rL   rM   r   r(   r$   r*   r,   r   r	   r	   r	   r   Ú<module>   s.   
  7,