o
    $x`{                     @   s  d Z ddlZddlmZ ddlmZ ddlZddlmZ ddlmZ	 ddl
mZ ddlmZ ddlmZ g d	Zd
ZdZdZdZe Ze Zdd Zdd ZG dd deZeZeG dd deZG dd deZeG dd deeZeg dZG dd deZ G dd de!Z"e"dee efd d!iZ#d"d# Z$G d$d de#Z%e%d%d&d'Z&d(d) e&_'e&(e& e&j)e&fksJ e&e _*e&e_*G d*d+ d+e%Z+G d,d- d-eZ,G d.d/ d/e,Z-d;d0d1Z.d<d2d3Z/d4d5 Z0dd6l1m2Z2 dd7l1m3Z3 dd8l
m4Z4 dd9l
m5Z5 dd:l1m6Z6 e (e6e6 dS )=z Interface object implementation
    N)
MethodType)FunctionType)_use_c_impl)PYTHON2)Invalid)ro)InterfaceClassSpecificationadapter_hooks      __interface_tagged_values____interface_methods__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   tagsr    r   :/usr/lib/python3/dist-packages/zope/interface/interface.py	invariant2   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   tagged_valuesr   r   r   taggedValue:   s   r    c                   @   sX   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
eZeZe	ZdS )ElementzI
    Default implementation of `zope.interface.interfaces.IElement`.
     c                 C   s0   |s| ddkr|}d }|| _|| _d | _d S )N r   )find__name____doc___Element__tagged_values)selfr%   r&   r   r   r   __init__L   s   
zElement.__init__c                 C      | j S )z! Returns the name of the object. r%   r(   r   r   r   getNameW      zElement.getNamec                 C   r*   )z+ Returns the documentation for the object. )r&   r,   r   r   r   getDoc[   r.   zElement.getDocc                 C   s   | j st|| j | S * Returns the value associated with 'tag'. )r'   KeyError)r(   tagr   r   r   getTaggedValuef   s   
zElement.getTaggedValueNc                 C   s   | j r
| j ||S |S r0   )r'   get)r(   r3   defaultr   r   r   queryTaggedValuel   s   zElement.queryTaggedValuec                 C   s   | j r| j  S dS )z# Returns a collection of all tags. r   )r'   keysr,   r   r   r   getTaggedValueTagsp   s   zElement.getTaggedValueTagsc                 C   s   | j du ri | _ || j |< dS )z  Associates 'value' with 'key'. N)r'   r(   r3   r   r   r   r   setTaggedValuet   s   
zElement.setTaggedValue)r"   N)r%   
__module____qualname__r&   r)   r-   r/   r4   r7   r9   r;   queryDirectTaggedValuegetDirectTaggedValuegetDirectTaggedValueTagsr   r   r   r   r!   B   s    
	
r!   c                   @   s,   e Zd ZdZdd Zdd Zdd ZeZdS )	SpecificationBase)_implied_dependents_bases_v_attrs__iro____sro____weakref__c                 C      t |}| |jv S )z2Is the interface implemented by an object
        )
providedByrC   )r(   obspecr   r   r   rK      s   
zSpecificationBase.providedByc                 C   rJ   )zTest whether the specification is implemented by a class or factory.

        Raise TypeError if argument is neither a class nor a callable.
        )implementedByrC   )r(   clsrM   r   r   r   rN      s   
zSpecificationBase.implementedByc                 C   s
   || j v S )zCIs the interface the same as or extend the given interface
        rC   )r(   	interfacer   r   r   isOrExtends   s   
zSpecificationBase.isOrExtendsN)r%   r=   r>   	__slots__rK   rN   rR   __call__r   r   r   r   rB      s    rB   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )NameAndModuleComparisonMixinr   c                 C   s\   || u rdS |du rdS | j | jf}z|j |jf}W n ty%   t Y S w ||k||k  S )a  
        Compare *self* to *other* based on ``__name__`` and ``__module__``.

        Return 0 if they are equal, return 1 if *self* is
        greater than *other*, and return -1 if *self* is less than
        *other*.

        If *other* does not have ``__name__`` or ``__module__``, then
        return ``NotImplemented``.

        .. caution::
           This allows comparison to things well outside the type hierarchy,
           perhaps not symmetrically.

           For example, ``class Foo(object)`` and ``class Foo(Interface)``
           in the same file would compare equal, depending on the order of
           operands. Writing code like this by hand would be unusual, but it could
           happen with dynamic creation of types and interfaces.

        None is treated as a pseudo interface that implies the loosest
        contact possible, no contract. For that reason, all interfaces
        sort before None.
        r   N)r%   r=   AttributeErrorNotImplemented)r(   othern1n2r   r   r   _compare   s   z%NameAndModuleComparisonMixin._comparec                 C   s   |  |}|tu r|S |dk S Nr   r\   rX   r(   rY   cr   r   r   __lt__      
z#NameAndModuleComparisonMixin.__lt__c                 C   s   |  |}|tu r|S |dkS r]   r^   r_   r   r   r   __le__   rb   z#NameAndModuleComparisonMixin.__le__c                 C   s   |  |}|tu r|S |dkS r]   r^   r_   r   r   r   __gt__   rb   z#NameAndModuleComparisonMixin.__gt__c                 C   s   |  |}|tu r|S |dkS r]   r^   r_   r   r   r   __ge__   rb   z#NameAndModuleComparisonMixin.__ge__N)	r%   r=   r>   rS   r\   ra   rc   rd   re   r   r   r   r   rU      s    'rU   c                   @   s^   e Zd ZdZdZdddZdd Zedd	 Ze	fd
dZ
dd Zdd Zdd Zdd ZdS )InterfaceBasez:Base class that wants to be replaced with a C base :)
    )r%   __ibmodule___v_cached_hashNc                 C   s   || _ || _d S r<   r%   rg   )r(   namemoduler   r   r   r)   
  s   
zInterfaceBase.__init__c                 C   s   t r<   )NotImplementedErrorr(   conformr   r   r   _call_conform  s   zInterfaceBase._call_conformc                 C   r*   r<   )rg   r,   r   r   r   __module_property__  s   z!InterfaceBase.__module_property__c                 C   sn   z|j }W n ty   d}Y nw |dur | |}|dur |S | |}|dur+|S |tur1|S td|| )z)Adapt an object to the interface
        NzCould not adapt)__conform__rW   ro   	__adapt___marker	TypeError)r(   obj	alternatern   adapterr   r   r   rT     s   


zInterfaceBase.__call__c                 C   s6   |  |r|S tD ]}|| |}|dur|  S q	dS )z(Adapt an object to the receiver
        N)rK   r
   )r(   ru   hookrw   r   r   r   rr   +  s   

zInterfaceBase.__adapt__c                 C   s2   z| j W S  ty   t| j| jf| _ Y | j S w r<   )rh   rW   hashr%   r=   r,   r   r   r   __hash__8  s   zInterfaceBase.__hash__c                 C   s   |  |}|tu r|S |dkS r]   r^   r_   r   r   r   __eq__@  rb   zInterfaceBase.__eq__c                 C   s*   || u rdS |  |}|tu r|S |dkS )NFr   r^   r_   r   r   r   __ne__F  s   
zInterfaceBase.__ne__NN)r%   r=   r>   r&   rS   r)   ro   propertyrp   rs   rT   rr   rz   r{   r|   r   r   r   r   rf      s    

rf   r
   c                   @   s   e Zd ZdZdZdZejZejZdddZ	e
dd Zdd	 Zd
d Zdd Ze
dd eZeZdd Zdd Zdd ZdddZdddZdddZdS ) r	   aw  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   Nc                 C   s2   d | _ d| _i | _d | _d| _d| _t|| _d S )Nr   )rD   rE   rC   rF   rG   rH   tuple	__bases__)r(   basesr   r   r   r)   h  s   	zSpecification.__init__c                 C   s   | j d u r
t | _ | j S r<   )rD   weakrefWeakKeyDictionaryr,   r   r   r   
dependentsz  s   

zSpecification.dependentsc                 C   s   | j |dd | j|< d S )Nr   r   )r   r5   rD   )r(   	dependentr   r   r   	subscribe  s   zSpecification.subscribec                 C   sX   z| j | }W n ty   t|w |d8 }|s| j|= d S |dks%J || j|< d S )Nr   r   )rD   rt   r2   r   )r(   r   nr   r   r   unsubscribe  s   zSpecification.unsubscribec                 C   s>   | j D ]}||  q|| _|D ]}||  q| |  d S r<   )r   r   rE   r   changed)r(   r   br   r   r   
__setBases  s   
zSpecification.__setBasesc                 C   r*   r<   )rE   r,   r   r   r   <lambda>      zSpecification.<lambda>c                    sV   | j dd | jD d}| j  dur)|r)|d  ur) fdd|D }|  |S )z
        Calculate and return the resolution order for this object, using its ``__bases__``.

        Ensures that ``Interface`` is always the last (lowest priority) element.
        c                 S   s   i | ]}||j qS r   )rH   .0r   r   r   r   
<dictcomp>  s    z0Specification._calculate_sro.<locals>.<dictcomp>)	base_mrosNrV   c                    s   g | ]}| ur|qS r   r   )r   xrootr   r   
<listcomp>  s    z0Specification._calculate_sro.<locals>.<listcomp>)_do_calculate_ror   _ROOTr   )r(   sror   r   r   _calculate_sro  s   
#


zSpecification._calculate_sroc                 C   s~   d| _ | j}|  |  }t|| _tdd |D | _|D ]}d||< qt| jr/| j ndD ]}|	| q2d| _ dS )z
        We, or something we depend on, have changed.

        By the time this is called, the things we depend on,
        such as our bases, should themselves be stable.
        Nc                 S   s   g | ]	}t |tr|qS r   
isinstancer   )r   ancestorr   r   r   r     s    
z)Specification.changed.<locals>.<listcomp>r   )
rF   rC   clearr   r   rH   rG   rD   r8   r   )r(   originally_changedimplied	ancestorsr   r   r   r   r   r     s   


zSpecification.changedc                 c   s:    i }| j D ]}| D ]}||vrd||< |V  qqdS )DReturn an iterator for the interfaces in the specification.
        r   N)r   
interfaces)r(   seenbaserQ   r   r   r   r      s   
zSpecification.interfacesTc                 C   s   || j v o| p| |kS )zDoes the specification extend the given interface?

        Test whether an interface in the specification extends the
        given interface
        rP   )r(   rQ   strictr   r   r   extends
  s   
zSpecification.extendsc                 C   s   t | |S r<   )r   ref)r(   callbackr   r   r   r        zSpecification.weakrefc                 C   sd   | j }|du ri  }| _ ||}|du r*| jD ]}||}|dur)|||<  nq|du r0|S |S )z+Query for an attribute description
        N)rF   r5   rG   direct)r(   rj   r6   attrsattrifacer   r   r   r5     s   



zSpecification.get)r   )Tr<   )r%   r=   r>   r&   rS   r   rB   rR   rK   r)   r~   r   r   r   _Specification__setBasesr   calculate_ror   r   r   r   r   r   r5   r   r   r   r   r	   R  s.    


9 


r	   c                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	_InterfaceMetaClassr   c                 C   sP   t djd }tjd }t|trtjd }||d< t| |||}||_	|S )Nr   r%   r=   rp   )
r   r   	f_globalsrf   __dict__r   strtype__new___InterfaceMetaClass__module)rO   rj   r   r   r=   moduledescrkindr   r   r   r   t  s   


z_InterfaceMetaClass.__new__c                 C   r*   r<   )r   rO   r   r   r   r=     r.   z_InterfaceMetaClass.__module__c                 C   s   d| j | jf S )Nz<class '%s.%s'>)r   r%   r   r   r   r   __repr__  s   z_InterfaceMetaClass.__repr__N)r%   r=   r>   rS   r   r~   r   r   r   r   r   r   )  s    I
r   r   rS   r   c                 C   s&   t dj}|ti }| || j< tS )a  
    Convert a method specification to an actual method of the interface.

    This is a decorator that functions like `staticmethod` et al.

    The primary use of this decorator is to allow interface definitions to
    define the ``__adapt__`` method, but other interface methods can be
    overridden this way too.

    .. seealso:: `zope.interface.interfaces.IInterfaceDeclaration.interfacemethod`
    r   )r   r   r   r   INTERFACE_METHODSr%   r   )funcr   methodsr   r   r   interfacemethod  s   
r   c                   @   s   e Zd ZdZ		d/ddZ		d0ddZdd	 Zd
d Zdd Zdd Z	d1ddZ
dd Zd1ddZdd ZeZdd Zdd Zd2ddZd2dd Zd2d!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. ZdS )3r   z
    Prototype (scarecrow) Interfaces Implementation.

    Note that it is not possible to change the ``__name__`` or ``__module__``
    after an instance of this object has been constructed.
    Nr   c           	      C   s   t |tsJ |p
i }|td }|rK|d|v r d|dini  d|v r+d|d< t| tr4| f}n| tu r<tf}n| tf}t| |d ||} n t	rk|rkt
|dkrktdd |D }|rkt|d |i jd } t| S )N__classcell__rr   r   _CALL_CUSTOM_ADAPTz<WithCustomMethods>c                 s   s&    | ]}t t|trt|V  qd S r<   )
issubclassr    _InterfaceClassWithCustomMethodsr   r   r   r   	<genexpr>  s    
z)InterfaceClass.__new__.<locals>.<genexpr>)r   r   popr   updater   r   r   r   PY2len__mro___InterfaceClassBaser   )	rO   rj   r   r   r&   r=   needs_custom_class	cls_basesbases_with_custom_methodsr   r   r   r     sD   

zInterfaceClass.__new__c           
   	   C   s(  t dd |D std|d u ri }|d u r;|d}t|tr%|d= nz
tdjd }W n tt	fy:   Y nw t
| || |d}|d urYt|tsY|d u rV|}|d= |d u r_d}t| || |td }|d ur| D ]
\}}	| ||	 qtt| | | || _d	||f | _d S )
Nc                 s   s    | ]}t |tV  qd S r<   r   )r   r   r   r   r   r     s    z*InterfaceClass.__init__.<locals>.<genexpr>zExpected base interfacesr=   r   r%   r&   r"   %s.%s)allrt   r5   r   r   r   r   r   rW   r2   rf   r)   	Attributer!   r   r   itemsr;   r	   _InterfaceClass__compute_attrs_InterfaceClass__attrs__identifier__)
r(   rj   r   r   r&   r=   dtagged_datar   valr   r   r   r)     s<   



zInterfaceClass.__init__c                    s"    fddfdd|  D S )Nc                    sH   t |tr |_|js| |_|S t |trt| | d}|S td|  )N)rj   zConcrete attribute, )r   r   rQ   r%   r   fromFunctionInvalidInterface)anameavalr,   r   r   update_value!  s   

z4InterfaceClass.__compute_attrs.<locals>.update_valuec                    s,   i | ]\}}|d vr|t ur| ||qS ))
__locals__r>   __annotations__)r   )r   r   r   )r   r   r   r   ,  s    
z2InterfaceClass.__compute_attrs.<locals>.<dictcomp>)r   )r(   r   r   )r(   r   r   __compute_attrs  s   
zInterfaceClass.__compute_attrsc                 c   s    | V  dS )r   Nr   r,   r   r   r   r   :  s   
zInterfaceClass.interfacesc                 C   r*   r<   )r   r,   r   r   r   getBases?     zInterfaceClass.getBasesc                 C   s   | |kp| | S )zSame interface or extends?)r   )r(   rY   r   r   r   isEqualOrExtendedByB  s   z"InterfaceClass.isEqualOrExtendedByFc                 C   sB   |s| j  S | j  }| jD ]}|t|| q| S )z4Return the attribute names defined by the interface.)r   r8   copyr   r   dictfromkeysnamesr(   r   rr   r   r   r   r   F  s   


zInterfaceClass.namesc                 C   s   t | jddS )NT)r   )iterr   r,   r   r   r   __iter__R  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.NrV   )r   r   r   r   r   namesAndDescriptionsr   r   r   r   r   U  s   
z#InterfaceClass.namesAndDescriptionsc                 C   s   |  |}|dur|S t|)z4Return the attribute description for the given name.N)r5   r2   r(   rj   r   r   r   r   getDescriptionForb  s   
z InterfaceClass.getDescriptionForc                 C   s   |  |d uS r<   r5   r(   rj   r   r   r   __contains__l     zInterfaceClass.__contains__c                 C   s   | j |S r<   )r   r5   r   r   r   r   r   o  r   zInterfaceClass.directc                 C   s   |  ||S r<   r   )r(   rj   r6   r   r   r   queryDescriptionForr  r   z"InterfaceClass.queryDescriptionForc                 C   st   | j D ].}|ddD ]%}z|| W q ty0 } z|dur%|| n W Y d}~qd}~ww q|r8t|dS )z&validate object to defined invariants.r   r   N)rG   r?   r   r   )r(   ru   errorsr   r   errorr   r   r   validateInvariantsu  s   
	z!InterfaceClass.validateInvariantsc                 C   s,   | j D ]}||t}|tur|  S q|S )z
        Queries for the value associated with *tag*, returning it from the nearest
        interface in the ``__iro__``.

        If not found, returns *default*.
        )rG   r?   rs   )r(   r3   r6   r   r   r   r   r   r7     s   
zInterfaceClass.queryTaggedValuec                 C   s"   | j |td}|tu rt||S )r1   )r6   )r7   rs   r2   r:   r   r   r   r4     s   zInterfaceClass.getTaggedValuec                 C   s$   t  }| jD ]	}||  q|S )z Returns a list of all tags. )setrG   r   rA   )r(   r8   r   r   r   r   r9     s   
z!InterfaceClass.getTaggedValueTagsc                 C   s>   z| j W S  ty   t| }d| jj|f }|| _ | Y S w )Nz<%s %s>)_v_reprrW   r   	__class__r%   r   r   r   r   r     s   zInterfaceClass.__repr__c                 C   s    | j }| j}|rd||f }|S )Nr   ri   )r(   rj   mr   r   r   __str__  s
   zInterfaceClass.__str__c                 C   s4   z|| W S  t y   t d jd ur Y d S w )N   )rt   r   exc_infotb_nextrm   r   r   r   ro     s   
zInterfaceClass._call_conformc                 C   r*   r<   r+   r,   r   r   r   
__reduce__  r   zInterfaceClass.__reduce__)Nr   NNN)r   NNN)Fr<   )r%   r=   r>   r&   r   r)   r   r   r   r   r   r   r   r   __getitem__r   r   r   r   r7   r4   r9   r   r   ro   r   r   r   r   r   r     s8    
3
3




		Interfacezzope.interface)r=   c                   C   s   t fS r<   )r   r   r   r   r   r     r   r   c                   @   s   e Zd ZdZdS )r   z_
    Marker class for interfaces with custom methods that override InterfaceClass methods.
    N)r%   r=   r>   r&   r   r   r   r   r     s    r   c                   @   s,   e Zd ZdZdZdd Zdd Zdd ZdS )	r   zAttribute descriptions
    Nc                 C   s   dS )z/Return extra data to put at the end of __str__.r"   r   r,   r   r   r   _get_str_info  s   zAttribute._get_str_infoc                 C   s<   d}| j d ur| j jd | j j d }|| jpd |   S )Nr"   .z	<unknown>)rQ   r=   r%   r   )r(   ofr   r   r   r     s   
zAttribute.__str__c                 C   s    dt | jt | jt| | f S )Nz<%s.%s object at 0x%x %s>)r   r=   r%   idr,   r   r   r   r     s   zAttribute.__repr__)r%   r=   r>   r&   rQ   r   r   r   r   r   r   r   r     s    r   c                   @   sd   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e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 u ri S | j S r<   	_optionalr,   r   r   r   _get_optional  s   
zMethod._get_optionalc                 C   s
   || _ d S r<   r  )r(   optr   r   r   _set_optional     
zMethod._set_optionalc                 C   s
   d | _ d S r<   r  r,   r   r   r   _del_optional  r
  zMethod._del_optionalc                 O   s   t | j| jr<   )BrokenImplementationrQ   r%   )r(   argskwr   r   r   rT     r   zMethod.__call__c                 C   s   | j | j| j| j| jdS )N
positionalrequiredoptionalvarargskwargsr  r,   r   r   r   getSignatureInfo  s   zMethod.getSignatureInfoc                 C   s   g }| j D ]}|| || j v r"|d  dt| j|  7  < q| jr.|d| j  | jr9|d| j  dd| S )NrV   =*z**z(%s)z, )r  r   r  r8   reprr  r  join)r(   sigvr   r   r   getSignatureString  s   

zMethod.getSignatureString)r%   r=   r>   r&   r  r  r  r  r  r  r	  r  r~   r  rT   r  r  r   r   r   r   r   r    s    r  c                 C   s.  |p| j }t|| j}t| dd pd}| j}|j| }|j|d  }i }	t|}
|
s0t| dd}
||
 }|dk rA|| d  }d}|	t	t
||d  | |d | |_|d | |_|	|_|}|jt@ rq|| |_|d }nd |_|jt@ r|| |_nd |_||_| j D ]
\}}||| q|S )N__defaults__r   __defaults_count__r   r   )r%   r  r&   getattr__code__co_argcountco_varnamesr   r   r   zipr  r  r  co_flags
CO_VARARGSr  CO_VARKEYWORDSr  rQ   r   r   r;   )r   rQ   imlevelrj   methoddefaultscodenar   r  defaults_countnrargnor   r   r   r   r   r   &  s<   





r   c                 C   s&   t | tr	| j}n| }t||d|dS )Nr   )r'  rj   )r   r   __func__r   )methrQ   rj   r   r   r   r   
fromMethodZ  s   
r1  c                  C   s~   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dlm} | t| d S )Nr   )classImplements)IElement)
IAttribute)IMethod)ISpecification)
IInterface)zope.interface.declarationsr2  zope.interface.interfacesr3  r!   r4  r   r5  r  r6  r	   r7  r   )r2  r3  r4  r5  r6  r7  r   r   r   _wirec  s   



r:  )rN   )rK   )r   )r  )_empty)Nr   Nr}   )7r&   r   typesr   r   r   zope.interface._compatr   r   r   zope.interface.exceptionsr   zope.interface.ror   r   zope.interface__all__r%  r&  r   r   objectr   rs   r   r    r!   SpecificationBasePyrB   rU   rf   r
   r	   r   r   r   r   r   r   r   r   rH   r   r   r   r  r   r1  r:  r8  rN   rK   r   r  r;  r   r   r   r   <module>   sr   
=.N
O Xh  


3
4	