U
    ؒ]S                     @   s  d Z ddlmZ ddlZddlmZ ddlmZ ddlZddlZddl	m
Z
 ddlmZ dZd	Zd
Ze Zdd Zdd ZG dd deZG dd deZeZzddlmZ W n ek
r   Y nX e ZG dd deZeZzddlmZ W n ek
r   Y nX g ZzddlmZ W n ek
r0   Y nX G dd deZG dd deeeZedddZG dd  d eZ G d!d" d"e Z!d-d#d$Z"d.d%d&Z#d'd( Z$dd)l%m&Z& dd*l%m'Z' dd+l	m(Z( dd,l	m)Z) dS )/z Interface object implementation
    )
generatorsN)
MethodType)FunctionType)Invalid)ro      Z__interface_tagged_values__c                 C   s2   t dj}|ti }|dg }||  tS )N   
invariants)sys	_getframef_locals
setdefaultTAGGED_DATAappend_decorator_non_return)callr   Ztagsr
    r   :/usr/lib/python3/dist-packages/zope/interface/interface.py	invariant"   s
    
r   c                 C   s$   t dj}|ti }||| < tS )z;Attaches a tagged value to an interface at definition time.r	   )r   r   r   r   r   r   )keyvaluer   Ztagged_valuesr   r   r   taggedValue*   s    r   c                   @   sL   e Zd ZdZdddZdd Zdd Zd	d
 ZdddZdd Z	dd Z
dS )ElementzI
    Default implementation of `zope.interface.interfaces.IElement`.
     c                 C   s0   |s| ddkr|}d }|| _|| _i | _d S )N r   )find__name____doc___Element__tagged_values)selfr   r   r   r   r   __init__<   s    zElement.__init__c                 C   s   | j S )z! Returns the name of the object. r   r    r   r   r   getNameE   s    zElement.getNamec                 C   s   | j S )z+ Returns the documentation for the object. )r   r#   r   r   r   getDocI   s    zElement.getDocc                 C   s
   | j | S * Returns the value associated with 'tag'. r   )r    tagr   r   r   getTaggedValueM   s    zElement.getTaggedValueNc                 C   s   | j ||S r&   )r   get)r    r)   defaultr   r   r   queryTaggedValueQ   s    zElement.queryTaggedValuec                 C   s
   | j  S z Returns a list of all tags. )r   keysr#   r   r   r   getTaggedValueTagsU   s    zElement.getTaggedValueTagsc                 C   s   || j |< dS )z  Associates 'value' with 'key'. Nr(   r    r)   r   r   r   r   setTaggedValueY   s    zElement.setTaggedValue)r   )N)r   
__module____qualname__r   r!   r$   r%   r*   r-   r0   r2   r   r   r   r   r   2   s   	
	
r   c                   @   s(   e Zd Zdd Zdd Zdd ZeZdS )SpecificationBasePyc                 C   s   t |}| |jkS )z2Is the interface implemented by an object
        )
providedBy_implied)r    obspecr   r   r   r6   _   s    zSpecificationBasePy.providedByc                 C   s   t |}| |jkS )zTest whether the specification is implemented by a class or factory.

        Raise TypeError if argument is neither a class nor a callable.
        )implementedByr7   )r    clsr9   r   r   r   r:   e   s    z!SpecificationBasePy.implementedByc                 C   s
   || j kS )zCIs the interface the same as or extend the given interface
        r7   )r    	interfacer   r   r   isOrExtendsm   s    zSpecificationBasePy.isOrExtendsN)r   r3   r4   r6   r:   r>   __call__r   r   r   r   r5   ]   s   r5   )SpecificationBasec                   @   s$   e Zd ZdZefddZdd ZdS )InterfaceBasePyz:Base class that wants to be replaced with a C base :)
    c                 C   s\   t |dd}|dk	r*| |}|dk	r*|S | |}|dk	r@|S |tk	rL|S td|| dS )z)Adapt an object to the interface
        Z__conform__NzCould not adapt)getattr_call_conform	__adapt___marker	TypeError)r    objZ	alternateconformadapterr   r   r   r?      s    

zInterfaceBasePy.__call__c                 C   s6   |  |r|S tD ]}|| |}|dk	r|  S qdS )z(Adapt an object to the reciever
        N)r6   adapter_hooks)r    rG   hookrI   r   r   r   rD      s    

zInterfaceBasePy.__adapt__N)r   r3   r4   r   rE   r?   rD   r   r   r   r   rA   {   s   rA   )InterfaceBase)rJ   c                   @   sz   e Zd ZdZejZejZdddZdd Zdd Z	d	d
 Z
edd e
Zdd Zdd ZdddZdddZdddZdS )Specificationaw  Specifications

    An interface specification is used to track interface declarations
    and component registrations.

    This class is a base class for both interfaces themselves and for
    interface specifications (declarations).

    Specifications are mutable.  If you reassign their bases, their
    relations with other specifications are adjusted accordingly.
    r   c                 C   s   i | _ t | _t|| _d S N)r7   weakrefWeakKeyDictionary
dependentstuple	__bases__)r    basesr   r   r   r!      s    
zSpecification.__init__c                 C   s   | j |dd | j |< d S Nr   r	   )rQ   r+   )r    	dependentr   r   r   	subscribe   s    zSpecification.subscribec                 C   s@   | j |dd }|s | j |= n|dkr4|| j |< nt|d S rU   )rQ   r+   KeyError)r    rV   nr   r   r   unsubscribe   s    
zSpecification.unsubscribec                 C   sB   | j D ]}||  q|| jd< |D ]}||  q$| |  d S )NrS   )rS   rZ   __dict__rW   changed)r    rT   br   r   r   Z
__setBases   s    

zSpecification.__setBasesc                 C   s   | j ddS )NrS   r   )r[   r+   r#   r   r   r   <lambda>       zSpecification.<lambda>c                 C   s   z| ` W n tk
r   Y nX | j}|  t| }zt|krH|t W n tk
r^   Y nX t|| _	tdd |D | _
|D ]}d||< qt| j D ]}|| qdS )z4We, or something we depend on, have changed
        c                 S   s   g | ]}t |tr|qS r   )
isinstanceInterfaceClass).0ancestorr   r   r   
<listcomp>   s    
z)Specification.changed.<locals>.<listcomp>r   N)_v_attrsAttributeErrorr7   clearr   	Interfacer   	NameErrorrR   Z__sro____iro__rQ   r/   r\   )r    Zoriginally_changedZimpliedZ	ancestorsrc   rV   r   r   r   r\      s$    

zSpecification.changedc                 c   s8   i }| j D ](}| D ]}||krd||< |V  qq
dS )DReturn an iterator for the interfaces in the specification.
        r	   N)rS   
interfaces)r    seenbaser=   r   r   r   rl      s    
zSpecification.interfacesTc                 C   s   || j ko| p| |kS )zDoes the specification extend the given interface?

        Test whether an interface in the specification extends the
        given interface
        r<   )r    r=   strictr   r   r   extends  s    
zSpecification.extendsNc                 C   s   t | |S rN   )rO   ref)r    callbackr   r   r   rO     s    zSpecification.weakrefc                 C   sz   z
| j }W n tk
r(   i  }| _ Y nX ||}|dkrf| jD ]"}||}|dk	rB|||<  qfqB|dkrr|S |S dS )z+Query for an attribute description
        N)re   rf   r+   rj   direct)r    namer,   attrsattrZifacer   r   r   r+     s    



zSpecification.get)r   )T)N)N)r   r3   r4   r   r@   r>   r6   r!   rW   rZ   Z_Specification__setBasespropertyrS   r\   rl   rp   rO   r+   r   r   r   r   rM      s    
	!

rM   c                   @   s   e Zd ZdZd9ddZdd Zdd	 Zd
d Zd:ddZdd Z	d;ddZ
dd ZeZdd Zdd Zd<ddZd=ddZd>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-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 ZdS )?ra   z0Prototype (scarecrow) Interfaces Implementation.r   Nc              	   C   s  |d kri }|d kr^| d}t|tr0|d= n.ztdjd }W n ttfk
r\   Y nX || _| d}|d k	rt|t	s|d kr|}|d= |d krd}t
| || |td }|d k	r| D ]\}}	| ||	 q|D ]}
t|
tstdqt| | t| D ]~\}}|dkr.||= qt|t	rP| |_|js||_n>t|trpt|| |d||< n|tkr||= ntd	| q|| _d
| j| jf | _d S )Nr3   r	   r   r   r   zExpected base interfaces)Z
__locals__r4   __annotations__)rt   zConcrete attribute, %s.%s)r+   r`   strr   r   	f_globalsrf   rX   r3   	Attributer   r!   popr   itemsr2   ra   rF   rM   listr=   r   r   fromFunctionr   InvalidInterface_InterfaceClass__attrsZ__identifier__)r    rt   rT   ru   r   r3   dZtagged_datar   valrn   rv   r   r   r   r!   5  sV    







zInterfaceClass.__init__c                 c   s
   | V  dS )rk   Nr   r#   r   r   r   rl   y  s    zInterfaceClass.interfacesc                 C   s   | j S rN   )rS   r#   r   r   r   getBases~  s    zInterfaceClass.getBasesc                 C   s   | |kp| | S )zSame interface or extends?)rp   )r    otherr   r   r   isEqualOrExtendedBy  s    z"InterfaceClass.isEqualOrExtendedByFc                 C   sB   |s| j  S | j  }| jD ]}|t|| q| S )z4Return the attribute names defined by the interface.)r   r/   copyrS   updatedictfromkeysnamesr    allrrn   r   r   r   r     s    


zInterfaceClass.namesc                 C   s   t | jddS )NT)r   )iterr   r#   r   r   r   __iter__  s    zInterfaceClass.__iter__c                 C   sP   |s| j  S i }| jddd D ]}|t|| q"|| j  | S )z=Return attribute names and descriptions defined by interface.N)r   r~   rS   r   r   namesAndDescriptionsr   r   r   r   r     s    
z#InterfaceClass.namesAndDescriptionsc                 C   s"   |  |}|dk	r|S t|dS )z4Return the attribute description for the given name.N)r+   rX   )r    rt   r   r   r   r   getDescriptionFor  s    
z InterfaceClass.getDescriptionForc                 C   s   |  |d k	S rN   r+   r    rt   r   r   r   __contains__  s    zInterfaceClass.__contains__c                 C   s   | j |S rN   )r   r+   r   r   r   r   rs     s    zInterfaceClass.directc                 C   s   |  ||S rN   r   )r    rt   r,   r   r   r   queryDescriptionFor  s    z"InterfaceClass.queryDescriptionForc                 C   s   |  dg D ]J}z|| W q tk
rT } z|dkr: n
|| W 5 d}~X Y qX q| jD ]4}z||| W q^ tk
r   |dkr Y q^X q^|rt|dS )z&validate object to defined invariants.r
   N)r-   r   r   rS   validateInvariants)r    rG   errorsr   ern   r   r   r   r     s    

z!InterfaceClass.validateInvariantsc                 C   sJ   t j| |td}|tk	r|S | jD ]"}|j|td}|tk	r"|  S q"|S r'   )r,   )r   r-   rE   rS   )r    r)   r,   r   rn   r   r   r   r-     s    

zInterfaceClass.queryTaggedValuec                 C   s"   | j |td}|tkrt||S r   )r-   rE   rX   r1   r   r   r   r*     s    zInterfaceClass.getTaggedValuec                 C   s>   t t| }| jD ]$}| D ]}||kr || q q|S r.   )r   r   r0   rS   r   )r    r/   rn   r   r   r   r   r0     s    
z!InterfaceClass.getTaggedValueTagsc                 C   sX   z| j W S  tk
rR   | j}| j}|r4d||f }d| jj|f }|| _ | Y S X d S )Nry   z<%s %s>)Z_v_reprrf   r   r3   	__class__)r    rt   mr   r   r   r   __repr__  s    zInterfaceClass.__repr__c                 C   s8   z
|| W S  t k
r2   t d jd k	r. Y nX d S )N   )rF   r   exc_infotb_next)r    rH   r   r   r   rC     s    
zInterfaceClass._call_conformc                 C   s   | j S rN   r"   r#   r   r   r   
__reduce__  s    zInterfaceClass.__reduce__c                 C   sL   |dkrdS t | ddt | ddf}t |ddt |ddf}||k||k  S )a  Make interfaces sortable

        TODO: It would ne nice if:

           More specific interfaces should sort before less specific ones.
           Otherwise, sort on name and module.

           But this is too complicated, and we're going to punt on it
           for now.

        For now, sort on interface and module name.

        None is treated as a pseudo interface that implies the loosest
        contact possible, no contract. For that reason, all interfaces
        sort before None.

        Nr   r   r   r3   )rB   )r    r   Zn1Zn2r   r   r   Z__cmp  s
    zInterfaceClass.__cmpc                 C   s4   | j }d|ksd|kr$td dS t| j| jfS )Nr3   r   z-Hashing uninitialized InterfaceClass instancer	   )r[   warningswarnhashr   r3   )r    r   r   r   r   __hash__#  s
    
zInterfaceClass.__hash__c                 C   s   |  |}|dkS Nr   _InterfaceClass__cmpr    r   cr   r   r   __eq__*  s    
zInterfaceClass.__eq__c                 C   s   |  |}|dkS r   r   r   r   r   r   __ne__.  s    
zInterfaceClass.__ne__c                 C   s   |  |}|dk S r   r   r   r   r   r   __lt__2  s    
zInterfaceClass.__lt__c                 C   s   |  |}|dkS r   r   r   r   r   r   __le__6  s    
zInterfaceClass.__le__c                 C   s   |  |}|dkS r   r   r   r   r   r   __gt__:  s    
zInterfaceClass.__gt__c                 C   s   |  |}|dkS r   r   r   r   r   r   __ge__>  s    
zInterfaceClass.__ge__)r   NNN)F)F)N)N)N)r   r3   r4   r   r!   rl   r   r   r   r   r   r   __getitem__r   rs   r   r   r-   r*   r0   r   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   ra   -  s<     
D




	ra   rh   zzope.interface)r3   c                   @   s   e Zd ZdZdZdS )r|   zAttribute descriptions
    N)r   r3   r4   r   r=   r   r   r   r   r|   E  s   r|   c                   @   s`   e Zd ZdZd ZZd Z ZZdd Z	dd Z
dd	 Zee	e
eZd
d Zdd Zdd ZdS )MethodzMethod interfaces

    The idea here is that you have objects that describe methods.
    This provides an opportunity for rich meta-data.
    r   Nc                 C   s   | j d kri S | j S rN   	_optionalr#   r   r   r   _get_optional_  s    
zMethod._get_optionalc                 C   s
   || _ d S rN   r   )r    optr   r   r   _set_optionalc  s    zMethod._set_optionalc                 C   s
   d | _ d S rN   r   r#   r   r   r   _del_optionale  s    zMethod._del_optionalc                 O   s   t | j| jd S rN   )BrokenImplementationr=   r   )r    argskwr   r   r   r?   i  s    zMethod.__call__c                 C   s   | j | j| j| j| jdS )N
positionalrequiredoptionalvarargskwargsr   r#   r   r   r   getSignatureInfol  s    zMethod.getSignatureInfoc                 C   s   g }| j D ]:}|| || j kr
|d  dt| j|  7  < q
| jr\|d| j  | jrr|d| j  dd| S )Nr   =*z**z(%s)z, )r   r   r   r/   reprr   r   join)r    Zsigvr   r   r   getSignatureStringt  s    

 zMethod.getSignatureString)r   r3   r4   r   r   r   r   r   r   r   r   r   rw   r   r?   r   r   r   r   r   r   r   Q  s   r   c                 C   s  |p| j }t|| j}t| dd p$d}| j}|j| }|j|d  }i }	|t| }
|
dk rn||
 d  }d}
|	t	t
||
d  | |d | |_|d |
 |_|	|_|}|jt@ r|| |_|d }nd |_|jt@ r|| |_nd |_||_| j D ]\}}||| q |S )N__defaults__r   r   r	   )r   r   r   rB   __code__co_argcountco_varnameslenr   r   zipr   r   r   co_flags
CO_VARARGSr   CO_VARKEYWORDSr   r=   r[   r~   r2   )funcr=   imlevelrt   methoddefaultscodeZnar   r   ZnrZargnor   r   r   r   r   r     s6    





r   c                 C   s&   t | tr| j}n| }t||d|dS )Nr	   )r   rt   )r`   r   __func__r   )methr=   rt   r   r   r   r   
fromMethod  s    
r   c                  C   sh   ddl m}  ddlm} | t| ddlm} | t| ddlm} | t| ddlm	} | t
| d S )Nr   )classImplements)
IAttribute)IMethod)
IInterface)ISpecification)zope.interface.declarationsr   Zzope.interface.interfacesr   r|   r   r   r   ra   r   rM   )r   r   r   r   r   r   r   r   _wire  s    


r   )r:   )r6   )r   )r   )Nr   N)NN)*r   Z
__future__r   r   typesr   r   r   rO   Zzope.interface.exceptionsr   Zzope.interface.ror   r   r   r   objectr   r   r   r   r5   r@   Z-zope.interface._zope_interface_coptimizationsImportErrorrE   rA   rL   rJ   rM   ra   rh   r|   r   r   r   r   r   r:   r6   r   r   r   r   r   r   <module>   s`   +"   0
-
	