U
    ؒ]Z                     @   s  d Z ddlmZ zddlmZ W n ek
r<   dd ZY nX 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 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_TYPESc                   @   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>0   s     z8_UnhashableComponentCounter.__init__.<locals>.<listcomp>)items_data)selfZ	otherdictr   r   r   __init__.   s    z$_UnhashableComponentCounter.__init__c                 C   s$   | j D ]\}}||kr|  S qdS Nr   )r    )r!   key	componentcountr   r   r   __getitem__2   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__8   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__?   s
    z'_UnhashableComponentCounter.__delitem__N)__name__
__module____qualname__r"   r'   r,   r.   r   r   r   r   r   +   s   r   c                   C   s   t tS r   )r   intr   r   r   r   _defaultdict_intF   s    r3   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   r3   _cache
_utilities_utility_registrations%_UtilityRegistrations__populate_cache)r!   	utilitiesZutility_registrationsr   r   r   r"   K   s    
z_UtilityRegistrations.__init__c                 C   s4   t | j D ] \\}}}|d }| || qd S r#   )iterr7   r   $_UtilityRegistrations__cache_utility)r!   p_r+   r%   r   r   r   Z__populate_cacheS   s    z&_UtilityRegistrations.__populate_cachec                 C   s\   z| j | |  d7  < W n< tk
rV   t| j |  }| j |< ||  d7  < Y nX d S )N   )r5   	TypeErrorr   )r!   providedr%   Zprovr   r   r   Z__cache_utilityX   s
    z%_UtilityRegistrations.__cache_utilityc                 C   s:   | j | }|| }|d8 }|dkr*||= n|||< |dkS )Nr>   r   )r5   )r!   r@   r%   r&   r   r   r   Z__uncache_utilitya   s    
z'_UtilityRegistrations.__uncache_utilityc                 C   s0   z| j | | dkW S  tk
r*   Y dS X d S )Nr   F)r5   r?   )r!   r@   r%   r   r   r   _is_utility_subscribedp   s    z,_UtilityRegistrations._is_utility_subscribedc                 C   sV   |  ||}|||f| j||f< | jd||| |sF| jd|| | || d S Nr   )rA   r7   r6   register	subscriber;   )r!   r@   namer%   infofactory
subscribedr   r   r   registerUtilityw   s    z%_UtilityRegistrations.registerUtilityc                 C   s@   | j ||f= | jd|| | ||}|s<| jd|| d S rB   )r7   r6   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r!   r@   rE   r%   rH   r   r   r   unregisterUtility   s
    z'_UtilityRegistrations.unregisterUtilityN)
r/   r0   r1   r"   r8   r;   rK   rA   rI   rM   r   r   r   r   r4   I   s   	r4   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 )U
ComponentsN r   c                 C   s8   t |tst|| _|   |   t|| _d | _d S r   )	
isinstancer   AssertionErrorr/   _init_registries_init_registrationstuple	__bases___v_utility_registrations_cache)r!   rE   basesr   r   r   r"      s    
zComponents.__init__c                 C   s   d| j j| jf S )Nz<%s %s>)	__class__r/   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>   )superrN   
__reduce__r   listrT   )r!   Z	reductionZfiltered_staterX   r   r   ra      s
    zComponents.__reduce__c                 C   s   t  | _t  | _d S r   )r   adaptersr9   rY   r   r   r   rR      s    zComponents._init_registriesc                 C   s   i | _ i | _g | _g | _d S r   )r7   _adapter_registrations_subscription_registrations_handler_registrationsrY   r   r   r   rS      s    zComponents._init_registrationsc                 C   s>   | j }|d ks&|j| jk	s&|j| jk	r:t| j| j }| _ |S r   )rV   r6   r9   r7   r4   )r!   cacher   r   r   _utility_registrations_cache   s    


z'Components._utility_registrations_cachec                 C   s   | j ddS )NrU   r   )__dict__getrY   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   )rd   r   baser   r   r   r      s    z(Components._setBases.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   )r9   rm   r   r   r   r      s    rU   )rT   rd   rU   r9   rj   r!   rW   r   r   r   	_setBases   s    zComponents._setBasesc                 C   s   |   S r   )rl   rY   r   r   r   <lambda>   r	   zComponents.<lambda>c                 C   s
   |  |S r   )rp   ro   r   r   r   rq      r	   Tc              	   C   s   |r|rt d| }|d kr&t|}|dkr6t|}| j||f}|d k	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_getNamer7   rk   rM   ri   rI   r   r   UtilityRegistration)r!   r%   r@   rE   rF   eventrG   Zregr   r   r   rI      s4        zComponents.registerUtilityc                 C   s   |r|rt d| }|d kr6|d kr.t dt|}| j||f}|d ksb|d k	rf||d krfdS |d krv|d }| j||| ttt| |||f|dd    dS )Nrr   z3Must specify one of component, factory and providedr   Fr>   T)	r?   rs   r7   rk   ri   rM   r   r   ru   )r!   r%   r@   rE   rG   oldr   r   r   rM      s0    
  zComponents.unregisterUtilityc                 c   s4   t | j D ] \\}}}t| ||f| V  qd S r   )r:   r7   r   ru   )r!   r@   rE   r+   r   r   r   registeredUtilities  s    zComponents.registeredUtilitiesc                 C   s   | j d|||S rB   )r9   lookup)r!   r@   rE   defaultr   r   r   queryUtility  s    zComponents.queryUtilityc                 C   s&   | j d||}|d kr"t|||S rB   )r9   ry   r   )r!   r@   rE   utilityr   r   r   
getUtility  s    
zComponents.getUtilityc                 c   s&   | j d|D ]\}}||fV  qd S rB   )r9   	lookupAll)r!   	interfacerE   r|   r   r   r   getUtilitiesFor!  s    zComponents.getUtilitiesForc                 C   s   | j d|S rB   )r9   Zsubscriptions)r!   r   r   r   r   getAllUtilitiesRegisteredFor%  s    z'Components.getAllUtilitiesRegisteredForc              	   C   sr   |d krt |}t||}|dkr*t|}||f| j|||f< | j|||| |rnttt| ||||| d S NrO   )	_getAdapterProvided_getAdapterRequiredrt   re   rd   rC   r   r   AdapterRegistrationr!   rG   requiredr@   rE   rF   rv   r   r   r   registerAdapter(  s    

 zComponents.registerAdapterc                 C   s   |d kr |d krt dt|}|d kr8|d kr8t dt||}| j|||f}|d ksp|d k	rt||d krtdS | j|||f= | j||| ttt	| |||f|  dS )N(Must specify one of factory and provided(Must specify one of factory and requiredr   FT)
r?   r   r   re   rk   rd   rJ   r   r   r   )r!   rG   r   r@   rE   rw   r   r   r   unregisterAdapter:  s(    

zComponents.unregisterAdapterc                 c   s:   t | j D ]&\\}}}\}}t| |||||V  qd S r   )r:   re   r   r   )r!   r   r@   rE   r%   rF   r   r   r   registeredAdaptersU  s    
 zComponents.registeredAdaptersc                 C   s   | j ||||S r   )rd   queryAdapter)r!   objectr   rE   rz   r   r   r   r   [  s    zComponents.queryAdapterc                 C   s(   | j |||}|d kr$t||||S r   )rd   r   r   )r!   r   r   rE   adapterr   r   r   
getAdapter^  s    zComponents.getAdapterc                 C   s   | j ||||S r   )rd   queryMultiAdapter)r!   objectsr   rE   rz   r   r   r   r   d  s       zComponents.queryMultiAdapterc                 C   s(   | j |||}|d kr$t||||S r   )rd   r   r   )r!   r   r   rE   r   r   r   r   getMultiAdapteri  s    zComponents.getMultiAdapterc                 c   s@   | j ttt||D ]"\}}|| }|d k	r||fV  qd S r   )rd   r~   rb   mapr   )r!   r   r@   rE   rG   r   r   r   r   getAdapterso  s    zComponents.getAdaptersc              	   C   sn   |rt d|d krt|}t||}| j|||||f | j||| |rjttt	| ||||| d S )N'Named subscribers are not yet supported)
r?   r   r   rf   r)   rd   rD   r   r   SubscriptionRegistrationr   r   r   r   registerSubscriptionAdapterw  s     

 z&Components.registerSubscriptionAdapterc                 c   s    | j D ]}t| f| V  qd S r   )rf   r   r!   r+   r   r   r   registeredSubscriptionAdapters  s    
z)Components.registeredSubscriptionAdaptersc              	      s   |rt dd kr, d kr$t dt d krD d krDt dt  d k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*   )r@   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   rG   r@   r   r   r   r     s
     FrO   T)
r?   r   r   rf   lenrd   rL   r   r   r   )r!   rG   r   r@   rE   newr   r   r   unregisterSubscriptionAdapter  s6    

 z(Components.unregisterSubscriptionAdapterc                 C   s   | j ||S r   rd   subscribers)r!   r   r@   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   rg   r)   rd   rD   r   r   HandlerRegistration)r!   rG   r   rE   rF   rv   r   r   r   registerHandler  s    

zComponents.registerHandlerc                 c   s    | j D ]}t| f| V  qd S r   )rg   r   r   r   r   r   registeredHandlers  s    
zComponents.registeredHandlersc                    s   |rt dd kr$ d kr$t dt  d k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   rG   r   r   r   r     s   
 FrO   T)	r?   r   rg   r   rd   rL   r   r   r   )r!   rG   r   rE   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   )&r/   r0   r1   rV   r"   rZ   ra   rR   rS   propertyri   rl   rp   rU   rI   rM   rx   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   rc   r   rN      s   


      
  


      
      


  

	       
      
*     

"rN   c                 C   s$   z| j W S  tk
r   Y dS X d S r   )Z__component_name__AttributeError)r%   r   r   r   rt     s    rt   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.)rb   r   r   r?   )r%   r@   r   r   r   rs     s    rs   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.)rb   r   r   r?   )rG   r@   r   r   r   r     s    r   c                 C   s   |d kr2z
| j }W qD tk
r.   tdY qDX nt|rDtdg }|D ]B}|d kr^t}n&t|st|tr|t|}ntd|	| qLt
|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 interfacez8Required specification must be a specification or class.)Z__component_adapts__r   r?   r
   r   r   rP   r   r   r)   rT   )rG   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 )ru   Nc                 C   s,   ||||||f\| _ | _| _| _| _| _d S r   )registryr@   rE   r%   rF   rG   )r!   r   r@   rE   r%   docrG   r   r   r   r"   ,  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)r/   )
rX   r/   r   getattrr@   rE   r%   reprrG   rF   rY   r   r   r   rZ   1  s      zUtilityRegistration.__repr__c                 C   s   t | S r   idrY   r   r   r   __hash__:  s    zUtilityRegistration.__hash__c                 C   s   t | t |kS r   r   r!   otherr   r   r   __eq__=  s    zUtilityRegistration.__eq__c                 C   s   t | t |kS r   r   r   r   r   r   __ne__@  s    zUtilityRegistration.__ne__c                 C   s   t | t |k S r   r   r   r   r   r   __lt__C  s    zUtilityRegistration.__lt__c                 C   s   t | t |kS r   r   r   r   r   r   __le__F  s    zUtilityRegistration.__le__c                 C   s   t | t |kS r   r   r   r   r   r   __gt__I  s    zUtilityRegistration.__gt__c                 C   s   t | t |kS r   r   r   r   r   r   __ge__L  s    zUtilityRegistration.__ge__)Nr/   r0   r1   r"   rZ   r   r   r   r   r   r   r   r   r   r   r   ru   )  s   
	ru   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   r@   rE   rG   rF   )r!   r   r   r@   rE   r%   r   r   r   r   r"   R  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   r/   r   r   r   r   r   r   [  s     z0AdapterRegistration.__repr__.<locals>.<listcomp>]r/   )rX   r/   r   joinr   r   r@   rE   rG   r   rF   rY   r   r   r   rZ   W  s      zAdapterRegistration.__repr__c                 C   s   t | S r   r   rY   r   r   r   r   `  s    zAdapterRegistration.__hash__c                 C   s   t | t |kS r   r   r   r   r   r   r   c  s    zAdapterRegistration.__eq__c                 C   s   t | t |kS r   r   r   r   r   r   r   f  s    zAdapterRegistration.__ne__c                 C   s   t | t |k S r   r   r   r   r   r   r   i  s    zAdapterRegistration.__lt__c                 C   s   t | t |kS r   r   r   r   r   r   r   l  s    zAdapterRegistration.__le__c                 C   s   t | t |kS r   r   r   r   r   r   r   o  s    zAdapterRegistration.__gt__c                 C   s   t | t |kS r   r   r   r   r   r   r   r  s    zAdapterRegistration.__ge__Nr   r   r   r   r   r   O  s   	r   c                   @   s   e Zd ZdS )r   N)r/   r0   r1   r   r   r   r   r   u  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   rE   handlerrF   )r!   r   r   rE   r   r   r   r   r   r"   }  s    zHandlerRegistration.__init__c                 C   s   | j S r   )r   rY   r   r   r   rG     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     s     z0HandlerRegistration.__repr__.<locals>.<listcomp>r   r/   )
rX   r/   r   r   r   rE   r   rG   r   rF   rY   r   r   r   rZ     s     zHandlerRegistration.__repr__)r/   r0   r1   r"   r   rG   r@   rZ   r   r   r   r   r   z  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   r   r   r3   r4   rN   rt   rs   r   r   ru   r   r   r   r   r   r   r   <module>   sR   C  l%%