3
8ßL]Ù  ã               @   s¼   d Z ddlmZ yddlmZmZ W n$ ek
rH   ddlmZmZ Y nX ddlZddl	m
Z
 ddlZddlmZ ejeƒZd	Zd
ZdZG dd„ deƒZG dd„ deƒZeƒ Zddd„ZdS )z" An attempt at type-safe casting.
é    )Úabsolute_import)ÚgetfullargspecÚgetmro)Ú
getargspecr   N)ÚFunctionTypeé   )Ú	HasTraitszZThe '%s' class signature for the '%s' method is different from that of the '%s' interface.zHThe '%s' class does not implement the '%s' method of the '%s' interface.zHThe '%s' class does not implement the %s trait(s) of the '%s' interface.c               @   s   e Zd ZdZdS )ÚInterfaceErrorzM The exception raised if a class does not really implement an interface.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú</tmp/pip-install-5sebvq2j/traits/traits/interface_checker.pyr	   E   s   r	   c               @   sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚInterfaceCheckerz6 Checks that interfaces are actually implemented.
    c             C   sx   yt |ƒ W n tk
r&   |g}Y nX t|tƒrTx@|D ]}| j|||ƒs8dS q8W n x|D ]}| j|||ƒsZdS qZW dS )z‘ Checks that the class implements the specified interfaces.

            'interfaces' can be a single interface or a list of interfaces.
        FT)ÚiterÚ	TypeErrorÚ
issubclassr   Ú_check_has_traits_classÚ_check_non_has_traits_class)ÚselfÚclsÚ
interfacesÚ
error_modeÚ	interfacer   r   r   Úcheck_implementsY   s    





z!InterfaceChecker.check_implementsc             C   s   | j |||ƒo| j|||ƒS )zB Checks that a 'HasTraits' class implements an interface.
        )Ú_check_traitsÚ_check_methods)r   r   r   r   r   r   r   r   |   s    
z(InterfaceChecker._check_has_traits_classc             C   s   | j |||ƒS )zF Checks that a non-'HasTraits' class implements an interface.
        )r   )r   r   r   r   r   r   r   r   ƒ   s    z,InterfaceChecker._check_non_has_traits_classc       	      C   s’   | j |ƒ}| j |ƒ}xx|D ]p}||krH| jt| j|ƒ|| j|ƒf |ƒS t|| ƒ}t|| ƒ}||kr| jt| j|ƒ|| j|ƒf |ƒS qW dS )zE Checks that a class implements the methods on an interface.
        T)Ú_get_public_methodsÚ_handle_errorÚMISSING_METHODÚ_class_namer   ÚBAD_SIGNATURE)	r   r   r   r   Zcls_methodsZinterface_methodsÚnameZcls_argspecZinterface_argspecr   r   r   r   ˆ   s(    



zInterfaceChecker._check_methodsc             C   s\   t |jƒ ƒjt |jƒ ƒƒ}t|ƒdkrX| jt| j|ƒtt|ƒƒdd… | j|ƒf |ƒS dS )zD Checks that a class implements the traits on an interface.
        r   r   Téÿÿÿÿ)	ÚsetZclass_traitsÚ
differenceÚlenr   ÚMISSING_TRAITr!   ÚreprÚlist)r   r   r   r   Úmissingr   r   r   r   «   s    zInterfaceChecker._check_traitsc             C   sd   i }xZt |ƒD ]N}|tkrP x>|jjƒ D ]0\}}|jdƒ r(t|ƒtkr(||kr(|||< q(W qW |S )z Returns all public methods on a class.

            Returns a dictionary containing all public methods keyed by name.
        Ú_)r   r   Ú__dict__ÚitemsÚ
startswithÚtyper   )r   r   Zpublic_methodsÚcr#   Úvaluer   r   r   r   ¿   s    z$InterfaceChecker._get_public_methodsc             C   s   |j S )N)r
   )r   r   r   r   r   r!   Ó   s    zInterfaceChecker._class_namec             C   s&   |dkrt |ƒ‚|dkr"tj|ƒ dS )Nr   F)r	   ÚloggerÚwarning)r   Úmsgr   r   r   r   r   Ö   s
    
zInterfaceChecker._handle_errorN)r
   r   r   r   r   r   r   r   r   r   r!   r   r   r   r   r   r   Q   s   ##r   c             C   s   t j| ||ƒS )z‰ Checks that the class implements the specified interfaces.

        'interfaces' can be a single interface or a list of interfaces.
    )Úcheckerr   )r   r   r   r   r   r   r   ä   s    r   )r   )r   Ú
__future__r   Úinspectr   r   ÚImportErrorr   ÚloggingÚtypesr   ÚsixZ
has_traitsr   Ú	getLoggerr
   r3   r"   r    r(   Ú	Exceptionr	   r   r6   r   r   r   r   r   Ú<module>   s$   
	 