a
    ¦ˆcù  ã                   @   s~   d Z ddlmZmZ ddlZddlmZ ddlmZ e 	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.
é    )ÚgetfullargspecÚgetmroN)Ú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   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/interface_checker.pyr   (   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   sr   zt |ƒ W n ty$   |g}Y n0 t|tƒrP|D ]}|  |||¡s4 dS q4n|D ]}|  |||¡sT dS qTd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_implements3   s     
ÿ
ÿz!InterfaceChecker.check_implementsc                 C   s   |   |||¡o|  |||¡S )zB Checks that a 'HasTraits' class implements an interface.
        )Ú_check_traitsÚ_check_methods©r   r   r   r   r   r   r   r   R   s
    ÿþz(InterfaceChecker._check_has_traits_classc                 C   s   |   |||¡S )zF Checks that a non-'HasTraits' class implements an interface.
        )r   r   r   r   r   r   Y   s    z,InterfaceChecker._check_non_has_traits_classc           	   	   C   s–   |   |¡}|   |¡}|D ]x}||vrJ|  t|  |¡||  |¡f |¡  S t|| ƒ}t|| ƒ}||kr|  t|  |¡||  |¡f |¡  S q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   ^   s4    

ýÿùýÿù

zInterfaceChecker._check_methodsc                 C   s\   t | ¡ ƒ t | ¡ ƒ¡}t|ƒdkrX|  t|  |¡tt|ƒƒdd… |  |¡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   s\   i }t |ƒD ]J}|tu r qX|j ¡ D ].\}}| d¡s&t|ƒtu r&||vr&|||< q&q|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 |¡ 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   /   s   #r   c                 C   s   t  | ||¡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   Úinspectr   r   ÚloggingÚtypesr   Z
has_traitsr   Ú	getLoggerr   r2   r!   r   r'   Ú	Exceptionr   r   r5   r   r   r   r   r   Ú<module>   s    
ÿÿÿ 	