a
    _[                     @   s  d Z ddlmZ zddlmZ W n ey:   dd ZY n0 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 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 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 ddlmZ ddlmZ dgZG dd deZdd ZG dd deZ ee
G dd deZ!dd  Z"d!d" Z#d#d$ Z$d%d& Z%eeG d'd( d(eZ&ee	G d)d* d*eZ'eeG d+d, d,e'Z(eeG d-d. d.e'Z)d/S )0zBasic components support
    )defaultdict)notifyc                  O   s   d S N )argkwr   r   9/usr/lib/python3/dist-packages/zope/interface/registry.pyr          r   )ISpecification)ComponentLookupError)IAdapterRegistration)IComponents)IHandlerRegistration) ISubscriptionAdapterRegistration)IUtilityRegistration)
Registered)Unregistered)	Interface)implementedBy)implementer)implementer_only)
providedBy)AdapterRegistry)CLASS_TYPES)STRING_TYPES
Componentsc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_UnhashableComponentCounterc                 C   s   dd |  D | _d S )Nc                 S   s   g | ]}|qS r   r   ).0itemr   r   r   
<listcomp>6   r	   z8_UnhashableComponentCounter.__init__.<locals>.<listcomp>)items_data)selfZ	otherdictr   r   r   __init__4   s    z$_UnhashableComponentCounter.__init__c                 C   s$   | j D ]\}}||kr|  S qdS Nr   )r!   )r"   key	componentcountr   r   r   __getitem__8   s    
z'_UnhashableComponentCounter.__getitem__c                 C   sH   t | jD ](\}}|d |kr
||f| j|<  d S q
| j||f d S r$   )	enumerater!   append)r"   r&   r'   idatar   r   r   __setitem__>   s
    z'_UnhashableComponentCounter.__setitem__c                 C   s:   t | jD ]"\}}|d |kr
| j|=  d S q
t|d S r$   )r)   r!   KeyError)r"   r&   r+   r,   r   r   r   __delitem__E   s
    z'_UnhashableComponentCounter.__delitem__N)__name__
__module____qualname__r#   r(   r-   r/   r   r   r   r   r   1   s   r   c                   C   s   t tS r   )r   intr   r   r   r   _defaultdict_intL   s    r4   c                   @   sD   e 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S )_UtilityRegistrationsc                 C   s"   t t| _|| _|| _|   d S r   )r   r4   _cache
_utilities_utility_registrations%_UtilityRegistrations__populate_cache)r"   	utilitiesZutility_registrationsr   r   r   r#   Q   s    
z_UtilityRegistrations.__init__c                 C   s4   t | j D ] \\}}}|d }| || qd S r$   )iterr8   r    $_UtilityRegistrations__cache_utility)r"   p_r,   r&   r   r   r   Z__populate_cacheY   s    z&_UtilityRegistrations.__populate_cachec                 C   sZ   z| j | |  d7  < W n: tyT   t| j |  }| j |< ||  d7  < Y n0 d S )N   )r6   	TypeErrorr   )r"   providedr&   Zprovr   r   r   Z__cache_utility^   s
    z%_UtilityRegistrations.__cache_utilityc                 C   s:   | j | }|| }|d8 }|dkr*||= n|||< |dkS )Nr?   r   )r6   )r"   rA   r&   r'   r   r   r   Z__uncache_utilityg   s    
z'_UtilityRegistrations.__uncache_utilityc                 C   s.   z| j | | dkW S  ty(   Y dS 0 d S )Nr   F)r6   r@   )r"   rA   r&   r   r   r   _is_utility_subscribedv   s    z,_UtilityRegistrations._is_utility_subscribedc                 C   sV   |  ||}|||f| j||f< | jd||| |sF| jd|| | || d S Nr   )rB   r8   r7   register	subscriber<   )r"   rA   namer&   infofactory
subscribedr   r   r   registerUtility}   s    z%_UtilityRegistrations.registerUtilityc                 C   s@   | j ||f= | jd|| | ||}|s<| jd|| d S rC   )r8   r7   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r"   rA   rF   r&   rI   r   r   r   unregisterUtility   s
    z'_UtilityRegistrations.unregisterUtilityN)
r0   r1   r2   r#   r9   r<   rL   rB   rJ   rN   r   r   r   r   r5   O   s   	r5   c                       sD  e Zd ZdZdFddZdd Z fdd	Zd
d Zdd Ze	dd Z
dd Zdd Ze	dd dd ZdGddZdHddZdd ZdIddZdJd d!Zd"d# Zd$d% ZdKd&d'ZdLd(d)Zd*d+ ZdMd,d-ZdNd.d/ZdOd0d1ZdPd2d3Zd4d5 ZdQd6d7Zd8d9 ZdRd:d;Zd<d= Z dSd>d?Z!d@dA Z"dTdBdCZ#dDdE Z$  Z%S )Ur   N r   c                 C   s8   t |tsJ || _|   |   t|| _d | _d S r   )
isinstancer   r0   _init_registries_init_registrationstuple	__bases___v_utility_registrations_cache)r"   rF   basesr   r   r   r#      s    
zComponents.__init__c                 C   s   d| j j| jf S )Nz<%s %s>)	__class__r0   r"   r   r   r   __repr__   s    zComponents.__repr__c                    s<   t t|  }dd |d  D }t|}||d< t|S )Nc                 S   s    i | ]\}}| d s||qS )Z_v_)
startswith)r   kvr   r   r   
<dictcomp>   s   
z)Components.__reduce__.<locals>.<dictcomp>   )superr   
__reduce__r    listrS   )r"   Z	reductionZfiltered_staterW   r   r   r`      s
    zComponents.__reduce__c                 C   s   t  | _t  | _d S r   )r   adaptersr:   rX   r   r   r   rQ      s    zComponents._init_registriesc                 C   s   i | _ i | _g | _g | _d S r   )r8   _adapter_registrations_subscription_registrations_handler_registrationsrX   r   r   r   rR      s    zComponents._init_registrationsc                 C   s>   | j }|d u s&|j| jus&|j| jur:t| j| j }| _ |S r   )rU   r7   r:   r8   r5   )r"   cacher   r   r   _utility_registrations_cache   s    


z'Components._utility_registrations_cachec                 C   s   | j ddS )NrT   r   )__dict__getrX   r   r   r   	_getBases   s    zComponents._getBasesc                 C   s>   t dd |D | j_t dd |D | j_t || jd< d S )Nc                 S   s   g | ]
}|j qS r   )rc   r   baser   r   r   r      s   z(Components._setBases.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   )r:   rl   r   r   r   r      s   rT   )rS   rc   rT   r:   ri   r"   rV   r   r   r   	_setBases   s    zComponents._setBasesc                 C   s   |   S r   )rk   rX   r   r   r   <lambda>   r	   zComponents.<lambda>c                 C   s
   |  |S r   )ro   rn   r   r   r   rp      r	   Tc              	   C   s   |r|rt d| }|d u r&t|}|dkr6t|}| j||f}|d urx|d d ||fkrfd S | |d || | j||||| |rtt	t
| ||||| d S )N$Can't specify factory and component.rO   r^   r   )r@   _getUtilityProvided_getNamer8   rj   rN   rh   rJ   r   r   UtilityRegistration)r"   r&   rA   rF   rG   eventrH   Zregr   r   r   rJ      s,    
zComponents.registerUtilityc                 C   s   |r|rt d| }|d u r6|d u r.t dt|}| j||f}|d u sb|d urf||d krfdS |d u rv|d }| j||| ttt| |||g|dd  R   dS )Nrq   z3Must specify one of component, factory and providedr   Fr?   T)	r@   rr   r8   rj   rh   rN   r   r   rt   )r"   r&   rA   rF   rH   oldr   r   r   rN      s,    
zComponents.unregisterUtilityc                 c   s6   t | j D ]"\\}}}t| ||g|R  V  qd S r   )r;   r8   r    rt   )r"   rA   rF   r,   r   r   r   registeredUtilities  s    zComponents.registeredUtilitiesc                 C   s   | j d|||S rC   )r:   lookup)r"   rA   rF   defaultr   r   r   queryUtility  s    zComponents.queryUtilityc                 C   s&   | j d||}|d u r"t|||S rC   )r:   rx   r   )r"   rA   rF   utilityr   r   r   
getUtility!  s    
zComponents.getUtilityc                 c   s&   | j d|D ]\}}||fV  qd S rC   )r:   	lookupAll)r"   	interfacerF   r{   r   r   r   getUtilitiesFor'  s    zComponents.getUtilitiesForc                 C   s   | j d|S rC   )r:   Zsubscriptions)r"   r~   r   r   r   getAllUtilitiesRegisteredFor+  s    z'Components.getAllUtilitiesRegisteredForc              	   C   sr   |d u rt |}t||}|dkr*t|}||f| j|||f< | j|||| |rnttt| ||||| d S NrO   )	_getAdapterProvided_getAdapterRequiredrs   rd   rc   rD   r   r   AdapterRegistrationr"   rH   requiredrA   rF   rG   ru   r   r   r   registerAdapter.  s    

zComponents.registerAdapterc                 C   s   |d u r |d u rt dt|}|d u r8|d u r8t dt||}| j|||f}|d u sp|d urt||d krtdS | j|||f= | j||| ttt	| |||g|R   dS )N(Must specify one of factory and provided(Must specify one of factory and requiredr   FT)
r@   r   r   rd   rj   rc   rK   r   r   r   )r"   rH   r   rA   rF   rv   r   r   r   unregisterAdapter@  s(    

zComponents.unregisterAdapterc                 c   s:   t | j D ]&\\}}}\}}t| |||||V  qd S r   )r;   rd   r    r   )r"   r   rA   rF   r&   rG   r   r   r   registeredAdapters[  s
    
zComponents.registeredAdaptersc                 C   s   | j ||||S r   )rc   queryAdapter)r"   objectr~   rF   ry   r   r   r   r   a  s    zComponents.queryAdapterc                 C   s(   | j |||}|d u r$t||||S r   )rc   r   r   )r"   r   r~   rF   adapterr   r   r   
getAdapterd  s    zComponents.getAdapterc                 C   s   | j ||||S r   )rc   queryMultiAdapter)r"   objectsr~   rF   ry   r   r   r   r   j  s    zComponents.queryMultiAdapterc                 C   s(   | j |||}|d u r$t||||S r   )rc   r   r   )r"   r   r~   rF   r   r   r   r   getMultiAdaptero  s    zComponents.getMultiAdapterc                 c   s@   | j ttt||D ]"\}}|| }|d ur||fV  qd S r   )rc   r}   ra   mapr   )r"   r   rA   rF   rH   r   r   r   r   getAdaptersu  s    zComponents.getAdaptersc              	   C   sn   |rt d|d u rt|}t||}| j|||||f | j||| |rjttt	| ||||| d S )N'Named subscribers are not yet supported)
r@   r   r   re   r*   rc   rE   r   r   SubscriptionRegistrationr   r   r   r   registerSubscriptionAdapter}  s    

z&Components.registerSubscriptionAdapterc                 c   s"   | j D ]}t| g|R  V  qd S r   )re   r   r"   r,   r   r   r   registeredSubscriptionAdapters  s    
z)Components.registeredSubscriptionAdaptersc              	      s   |rt dd u r, d u r$t dt d u rD d u rDt dt  d u rnfdd| jD }n fdd| jD }t|t| jkrdS || jd d < | j  ttt	| | d d	S )
Nr   r   r   c                    s4   g | ],\}}}}}|kr"| ks|||||fqS r   r   r   rr=   nfr+   )rA   r   r   r   r     s   z<Components.unregisterSubscriptionAdapter.<locals>.<listcomp>c                    s<   g | ]4\}}}}}|kr*|kr*| ks|||||fqS r   r   r   rH   rA   r   r   r   r     s   FrO   T)
r@   r   r   re   lenrc   rM   r   r   r   )r"   rH   r   rA   rF   newr   r   r   unregisterSubscriptionAdapter  s4    

z(Components.unregisterSubscriptionAdapterc                 C   s   | j ||S r   rc   subscribers)r"   r   rA   r   r   r   r     s    zComponents.subscribersc                 C   sZ   |rt dt||}| j||||f | j|d | |rVttt| |||| d S )Nz$Named handlers are not yet supported)	r@   r   rf   r*   rc   rE   r   r   HandlerRegistration)r"   rH   r   rF   rG   ru   r   r   r   registerHandler  s    

zComponents.registerHandlerc                 c   s"   | j D ]}t| g|R  V  qd S r   )rf   r   r   r   r   r   registeredHandlers  s    
zComponents.registeredHandlersc                    s   |rt dd u r$ d u r$t dt  d u rLfdd| jD }n fdd| jD }t|t| jkrxdS || jd d < | jd   ttt| | d dS )	Nr   r   c                    s(   g | ] \}}}}| kr||||fqS r   r   r   r   r   r   r+   )r   r   r   r     s   
z0Components.unregisterHandler.<locals>.<listcomp>c                    s0   g | ](\}}}}|kr | ks||||fqS r   r   r   rH   r   r   r   r     s   
FrO   T)	r@   r   rf   r   rc   rM   r   r   r   )r"   rH   r   rF   r   r   r   r   unregisterHandler  s(    

zComponents.unregisterHandlerc                 G   s   | j |d  d S r   r   )r"   r   r   r   r   handle  s    zComponents.handle)rO   r   )NNrO   rO   TN)NNrO   N)rO   N)rO   )NNrO   rO   T)NNNrO   )rO   N)rO   )rO   N)rO   )NNrO   rO   T)NNNrO   )NrO   rO   T)NNrO   )&r0   r1   r2   rU   r#   rY   r`   rQ   rR   propertyrh   rk   ro   rT   rJ   rN   rw   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   rb   r   r      sn   


  
  


  
  


  

	   
  
*   

"c                 C   s"   z| j W S  ty   Y dS 0 d S r   )Z__component_name__AttributeError)r&   r   r   r   rs     s    rs   c                 C   s,   t t| }t|dkr |d S tdd S )Nr?   r   zWThe utility doesn't provide a single interface and no provided interface was specified.)ra   r   r   r@   )r&   rA   r   r   r   rr     s    rr   c                 C   s,   t t| }t|dkr |d S tdd S )Nr?   r   zaThe adapter factory doesn't implement a single interface and no provided interface was specified.)ra   r   r   r@   )rH   rA   r   r   r   r     s    r   c                 C   s   |d u r0z
| j }W qB ty,   tdY qB0 nt|rBtdg }|D ]J}|d u r\t}n.t|st|trzt|}ntdt	| |
| qJt|S )NzoThe adapter factory doesn't have a __component_adapts__ attribute and no required specifications were specifiedzLthe required argument should be a list of interfaces, not a single interfacez?Required specification must be a specification or class, not %r)Z__component_adapts__r   r@   r
   r   r   rP   r   r   typer*   rS   )rH   r   resultr   r   r   r   r     s*    




r   c                   @   sV   e Z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S )rt   Nc                 C   s,   ||||||f\| _ | _| _| _| _| _d S r   )registryrA   rF   r&   rG   rH   )r"   r   rA   rF   r&   docrH   r   r   r   r#   2  s    zUtilityRegistration.__init__c              
   C   s<   d| j j| jt| jdd | jt| jdt| j| j| j	f S )Nz%s(%r, %s, %r, %s, %r, %r)r0   )
rW   r0   r   getattrrA   rF   r&   reprrH   rG   rX   r   r   r   rY   7  s    zUtilityRegistration.__repr__c                 C   s   t | S r   idrX   r   r   r   __hash__@  s    zUtilityRegistration.__hash__c                 C   s   t | t |kS r   r   r"   otherr   r   r   __eq__C  s    zUtilityRegistration.__eq__c                 C   s   t | t |kS r   r   r   r   r   r   __ne__F  s    zUtilityRegistration.__ne__c                 C   s   t | t |k S r   r   r   r   r   r   __lt__I  s    zUtilityRegistration.__lt__c                 C   s   t | t |kS r   r   r   r   r   r   __le__L  s    zUtilityRegistration.__le__c                 C   s   t | t |kS r   r   r   r   r   r   __gt__O  s    zUtilityRegistration.__gt__c                 C   s   t | t |kS r   r   r   r   r   r   __ge__R  s    zUtilityRegistration.__ge__)Nr0   r1   r2   r#   rY   r   r   r   r   r   r   r   r   r   r   r   rt   /  s   
	rt   c                   @   sT   e 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d ZdS )r   c                 C   s,   ||||||f\| _ | _| _| _| _| _d S r   )r   r   rA   rF   rH   rG   )r"   r   r   rA   rF   r&   r   r   r   r   r#   X  s    zAdapterRegistration.__init__c                 C   sT   d| j j| jdddd | jD  d t| jdd | jt| jdt	| j| j
f S )Nz%s(%r, %s, %s, %r, %s, %r)[, c                 S   s   g | ]
}|j qS r   r0   r   r   r   r   r   r   a  r	   z0AdapterRegistration.__repr__.<locals>.<listcomp>]r0   )rW   r0   r   joinr   r   rA   rF   rH   r   rG   rX   r   r   r   rY   ]  s    zAdapterRegistration.__repr__c                 C   s   t | S r   r   rX   r   r   r   r   f  s    zAdapterRegistration.__hash__c                 C   s   t | t |kS r   r   r   r   r   r   r   i  s    zAdapterRegistration.__eq__c                 C   s   t | t |kS r   r   r   r   r   r   r   l  s    zAdapterRegistration.__ne__c                 C   s   t | t |k S r   r   r   r   r   r   r   o  s    zAdapterRegistration.__lt__c                 C   s   t | t |kS r   r   r   r   r   r   r   r  s    zAdapterRegistration.__le__c                 C   s   t | t |kS r   r   r   r   r   r   r   u  s    zAdapterRegistration.__gt__c                 C   s   t | t |kS r   r   r   r   r   r   r   x  s    zAdapterRegistration.__ge__Nr   r   r   r   r   r   U  s   	r   c                   @   s   e Zd ZdS )r   N)r0   r1   r2   r   r   r   r   r   {  s   r   c                   @   s,   e Zd Zdd Zedd ZdZdd ZdS )r   c                 C   s&   |||||f\| _ | _| _| _| _d S r   )r   r   rF   handlerrG   )r"   r   r   rF   r   r   r   r   r   r#     s    zHandlerRegistration.__init__c                 C   s   | j S r   )r   rX   r   r   r   rH     s    zHandlerRegistration.factoryNc              
   C   sH   d| j j| jdddd | jD  d | jt| jdt| j| j	f S )Nz%s(%r, %s, %r, %s, %r)r   r   c                 S   s   g | ]
}|j qS r   r   r   r   r   r   r     r	   z0HandlerRegistration.__repr__.<locals>.<listcomp>r   r0   )
rW   r0   r   r   r   rF   r   rH   r   rG   rX   r   r   r   rY     s    zHandlerRegistration.__repr__)r0   r1   r2   r#   r   rH   rA   rY   r   r   r   r   r     s
   
r   N)*__doc__collectionsr   Z
zope.eventr   ImportErrorZzope.interface.interfacesr
   r   r   r   r   r   r   r   r   Zzope.interface.interfacer   Zzope.interface.declarationsr   r   r   r   Zzope.interface.adapterr   Zzope.interface._compatr   r   __all__r   r   r4   r5   r   rs   rr   r   r   rt   r   r   r   r   r   r   r   <module>   sV   C  l%%