o
    $x`e                     @   s  d Z ddlmZ zddlmZ W n ey   dd ZY nw 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      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   s    z8_UnhashableComponentCounter.__init__.<locals>.<listcomp>)items_data)self	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   s6   t | jD ]\}}|d |kr| j|=  d S qt|r&   )r+   r"   KeyError)r#   r(   r-   r.   r   r   r	   __delitem__E   s   z'_UnhashableComponentCounter.__delitem__N)__name__
__module____qualname__r%   r*   r/   r1   r   r   r   r	   r   1   s
    r   c                   C   s   t tS r   )r   intr   r   r   r	   _defaultdict_intL      r6   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   r6   _cache
_utilities_utility_registrations%_UtilityRegistrations__populate_cache)r#   	utilitiesutility_registrationsr   r   r	   r%   Q   s   
z_UtilityRegistrations.__init__c                 C   s4   t | j D ]\\}}}|d }| || qd S r&   )iterr;   r!   $_UtilityRegistrations__cache_utility)r#   p_r.   r(   r   r   r	   __populate_cacheY   s   z&_UtilityRegistrations.__populate_cachec                 C   sZ   z| j | |  d7  < W d S  ty,   t| j |  }| j |< ||  d7  < Y d S w )N   )r9   	TypeErrorr   )r#   providedr(   provr   r   r	   __cache_utility^   s   z%_UtilityRegistrations.__cache_utilityc                 C   s@   | j | }|| }|d8 }|dkr||= |dkS |||< |dkS )NrD   r   )r9   )r#   rF   r(   r)   r   r   r	   __uncache_utilityg   s   
z'_UtilityRegistrations.__uncache_utilityc                 C   s*   z
| j | | dkW S  ty   Y dS w )Nr   F)r9   rE   )r#   rF   r(   r   r   r	   _is_utility_subscribedv   s
   z,_UtilityRegistrations._is_utility_subscribedc                 C   sV   |  ||}|||f| j||f< | jd||| |s#| jd|| | || d S Nr   )rJ   r;   r:   register	subscriber@   )r#   rF   namer(   infofactory
subscribedr   r   r	   registerUtility}   s   z%_UtilityRegistrations.registerUtilityc                 C   sD   | j ||f= | jd|| | ||}|s | jd|| d S d S rK   )r;   r:   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r#   rF   rN   r(   rQ   r   r   r	   unregisterUtility   s   z'_UtilityRegistrations.unregisterUtilityN)
r2   r3   r4   r%   r<   r@   rT   rJ   rR   rV   r   r   r   r	   r8   O   s    	r8   c                       sr  e Zd ZdZdId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		dJddZ		dKddZdd ZdLddZdMd d!Zd"d# Zd$d% Z		dNd&d'Z		dOd(d)Zd*d+ ZdLd,d-ZdMd.d/Z		dLd0d1ZdMd2d3Zd4d5 Z			dNd6d7Zd8d9 Z		dOd:d;Zd<d= Z 			dPd>d?Z!d@dA Z"dQdBdCZ#dDdE Z$dRdGdHZ%  Z&S )Sr   N r   c                 C   s8   t |tsJ || _|   |   t|| _d | _d S r   )
isinstancer   r2   _init_registries_init_registrationstuple	__bases___v_utility_registrations_cache)r#   rN   basesr   r   r	   r%      s   

zComponents.__init__c                 C   s   d| j j| jf S )Nz<%s %s>)	__class__r2   r#   r   r   r	   __repr__      zComponents.__repr__c                    s<   t t|  }dd |d  D }t|}||d< t|S )Nc                 S   s    i | ]\}}| d s||qS )_v_)
startswith)r   kvr   r   r	   
<dictcomp>   s    z)Components.__reduce__.<locals>.<dictcomp>   )superr   
__reduce__r!   listr[   )r#   	reductionfiltered_stater_   r   r	   rj      s
   zComponents.__reduce__c                 C   s   t  | _t  | _d S r   )r   adaptersr=   r`   r   r   r	   rY      s   zComponents._init_registriesc                 C   s   i | _ i | _g | _g | _d S r   )r;   _adapter_registrations_subscription_registrations_handler_registrationsr`   r   r   r	   rZ      s   
zComponents._init_registrationsc                 C   s>   | j }|d u s|j| jus|j| jurt| j| j }| _ |S r   )r]   r:   r=   r;   r8   )r#   cacher   r   r	   _utility_registrations_cache   s   


z'Components._utility_registrations_cachec                 C   s   | j ddS )Nr\   r   )__dict__getr`   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      g | ]}|j qS r   )ro   r   baser   r   r	   r           z(Components._setBases.<locals>.<listcomp>c                 S   rx   r   )r=   ry   r   r   r	   r       r{   r\   )r[   ro   r\   r=   ru   r#   r^   r   r   r	   	_setBases   s   zComponents._setBasesc                 C   s   |   S r   )rw   r`   r   r   r	   <lambda>   s    zComponents.<lambda>c                 C   s
   |  |S r   )r}   r|   r   r   r	   r~      s   
 Tc              	   C   s   |r|rt d| }|d u rt|}|dkrt|}| j||f}|d ur<|d d ||fkr3d S | |d || | j||||| |rWtt	t
| ||||| d S d S )N$Can't specify factory and component.rW   rh   r   )rE   _getUtilityProvided_getNamer;   rv   rV   rt   rR   r   r   UtilityRegistration)r#   r(   rF   rN   rO   eventrP   regr   r   r	   rR      s.   

zComponents.registerUtilityc                 C   s   |r|rt d| }|d u r|d u rt dt|}| j||f}|d u s1|d ur3||d kr3dS |d u r;|d }| j||| ttt| |||g|dd  R   dS )Nr   z3Must specify one of component, factory and providedr   FrD   T)	rE   r   r;   rv   rt   rV   r   r   r   )r#   r(   rF   rN   rP   oldr   r   r	   rV      s,   
zComponents.unregisterUtilityc                 c   s8    t | j D ]\\}}}t| ||g|R  V  qd S r   )r?   r;   r!   r   )r#   rF   rN   r.   r   r   r	   registeredUtilities  s
   zComponents.registeredUtilitiesc                 C   s   | j d|||S rK   )r=   lookup)r#   rF   rN   defaultr   r   r	   queryUtility  rb   zComponents.queryUtilityc                 C   s&   | j d||}|d u rt|||S rK   )r=   r   r   )r#   rF   rN   utilityr   r   r	   
getUtility!  s   
zComponents.getUtilityc                 c   s(    | j d|D ]	\}}||fV  qd S rK   )r=   	lookupAll)r#   	interfacerN   r   r   r   r	   getUtilitiesFor'  s   zComponents.getUtilitiesForc                 C   s   | j d|S rK   )r=   subscriptions)r#   r   r   r   r	   getAllUtilitiesRegisteredFor+     z'Components.getAllUtilitiesRegisteredForc              	   C   sv   |d u rt |}t||}|dkrt|}||f| j|||f< | j|||| |r9ttt| ||||| d S d S NrW   )	_getAdapterProvided_getAdapterRequiredr   rp   ro   rL   r   r   AdapterRegistrationr#   rP   requiredrF   rN   rO   r   r   r   r	   registerAdapter.  s   


zComponents.registerAdapterc                 C   s   |d u r|d u rt dt|}|d u r|d u rt dt||}| j|||f}|d u s8|d ur:||d kr:d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)
rE   r   r   rp   rv   ro   rS   r   r   r   )r#   rP   r   rF   rN   r   r   r   r	   unregisterAdapter@  s(   

zComponents.unregisterAdapterc                 c   s<    t | j D ]\\}}}\}}t| |||||V  qd S r   )r?   rp   r!   r   )r#   r   rF   rN   r(   rO   r   r   r	   registeredAdapters[  s   
zComponents.registeredAdaptersc                 C      | j ||||S r   )ro   queryAdapter)r#   objectr   rN   r   r   r   r	   r   a  rb   zComponents.queryAdapterc                 C   (   | j |||}|d u rt||||S r   )ro   r   r   )r#   r   r   rN   adapterr   r   r	   
getAdapterd     zComponents.getAdapterc                 C   r   r   )ro   queryMultiAdapter)r#   objectsr   rN   r   r   r   r	   r   j  s   zComponents.queryMultiAdapterc                 C   r   r   )ro   r   r   )r#   r   r   rN   r   r   r   r	   getMultiAdaptero  r   zComponents.getMultiAdapterc                 c   sB    | j ttt||D ]\}}|| }|d ur||fV  qd S r   )ro   r   rk   mapr   )r#   r   rF   rN   rP   r   r   r   r	   getAdaptersu  s   
zComponents.getAdaptersc              	   C   sr   |rt d|d u rt|}t||}| j|||||f | j||| |r7ttt	| ||||| d S d S )N'Named subscribers are not yet supported)
rE   r   r   rq   r,   ro   rM   r   r   SubscriptionRegistrationr   r   r   r	   registerSubscriptionAdapter}  s    


z&Components.registerSubscriptionAdapterc                 c   $    | j D ]}t| g|R  V  qd S r   )rq   r   r#   r.   r   r   r	   registeredSubscriptionAdapters     
z)Components.registeredSubscriptionAdaptersc              	      s   |rt dd u r d u rt dt d u r" d u r"t dt  d u r7fdd| jD }n fdd| jD }t|t| jkrNd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   rrA   nfr-   )rF   r   r   r	   r      s    z<Components.unregisterSubscriptionAdapter.<locals>.<listcomp>c                    s<   g | ]\}}}}}|kr|kr| ks|||||fqS r   r   r   rP   rF   r   r   r	   r      s    FrW   T)
rE   r   r   rq   lenro   rU   r   r   r   )r#   rP   r   rF   rN   newr   r   r	   unregisterSubscriptionAdapter  s4   

z(Components.unregisterSubscriptionAdapterc                 C   s   | j ||S r   ro   subscribers)r#   r   rF   r   r   r	   r     r   zComponents.subscribersc                 C   s^   |rt dt||}| j||||f | j|d | |r-ttt| |||| d S d S )Nz$Named handlers are not yet supported)	rE   r   rr   r,   ro   rM   r   r   HandlerRegistration)r#   rP   r   rN   rO   r   r   r   r	   registerHandler  s   


zComponents.registerHandlerc                 c   r   r   )rr   r   r   r   r   r	   registeredHandlers  r   zComponents.registeredHandlersc                    s   |rt dd u r d u rt dt  d u r&fdd| jD }n fdd| jD }t|t| jkr<d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   rP   r   r   r	   r      s    
FrW   T)	rE   r   rr   r   ro   rU   r   r   r   )r#   rP   r   rN   r   r   r   r	   unregisterHandler  s(   

zComponents.unregisterHandlerc                 G   s   | j |d  d S r   r   )r#   r   r   r   r	   handle  rb   zComponents.handleFc              	   C   s"  t | j}| j}d}d}d}d}d|jvsJ dd |_|r%|j}|j}	ndd  }}	zN| D ]:\\}
}\}}}|d|
||krN|d|
|| |d7 }n|d7 }|	d|
|du rf|d7 }|	d|
| q0|d7 }q0W |`|ry|st|ry|| n|`|r|s|r|| w w w ||||d	S )
a  
        Emergency maintenance method to rebuild the ``.utilities``
        registry from the local copy maintained in this object, or
        detect the need to do so.

        Most users will never need to call this, but it can be helpful
        in the event of suspected corruption.

        By default, this method only checks for corruption. To make it
        actually rebuild the registry, pass `True` for *rebuild*.

        :param bool rebuild: If set to `True` (not the default),
           this method will actually register and subscribe utilities
           in the registry as needed to synchronize with the local cache.

        :return: A dictionary that's meant as diagnostic data. The keys
           and values may change over time. When called with a false *rebuild*,
           the keys ``"needed_registered"`` and ``"needed_subscribed"`` will be
           non-zero if any corruption was detected, but that will not be corrected.

        .. versionadded:: 5.3.0
        r   changedc                 S   r   r   r   )rB   r   r   r	   r~     r
   zAComponents.rebuildUtilityRegistryFromLocalCache.<locals>.<lambda>c                  W   r   r   r   )argsr   r   r	   r~   $  r
   r   rD   N)needed_registereddid_not_registerneeded_subscribeddid_not_subscribe)
dictr;   r=   ru   r   rL   rM   r!   
registeredrQ   )r#   rebuildregsutilsr   r   r   r   rL   rM   rF   rN   value_info_factoryr   r   r	   $rebuildUtilityRegistryFromLocalCache  sH   




z/Components.rebuildUtilityRegistryFromLocalCache)rW   r   )NNrW   rW   TN)NNrW   N)rW   N)rW   )NNrW   rW   T)NNNrW   )NrW   rW   T)NNrW   )F)'r2   r3   r4   r]   r%   ra   rj   rY   rZ   propertyrt   rw   r}   r\   rR   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   rn   r	   r      sr    












	

*

"c                 C   s   z| j W S  ty   Y dS w r   )__component_name__AttributeError)r(   r   r   r	   r   ?  s
   r   c                 C   (   t t| }t|dkr|d S td)NrD   r   zWThe utility doesn't provide a single interface and no provided interface was specified.)rk   r   r   rE   )r(   rF   r   r   r	   r   E     r   c                 C   r   )NrD   r   zaThe adapter factory doesn't implement a single interface and no provided interface was specified.)rk   r   r   rE   )rP   rF   r   r   r	   r   M  r   r   c                 C   s   |d u rz| j }W n ty   tdw t|rtdg }|D ]%}|d u r+t}nt|sBt|tr:t|}ntdt	| |
| q"t|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)__component_adapts__r   rE   r   r   r   rX   r   r   typer,   r[   )rP   r   resultr   r   r   r	   r   U  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 )r   Nc                 C   ,   ||||||f\| _ | _| _| _| _| _d S r   )registryrF   rN   r(   rO   rP   )r#   r   rF   rN   r(   docrP   r   r   r	   r%   t  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)r2   )
r_   r2   r   getattrrF   rN   r(   reprrP   rO   r`   r   r   r	   ra   y  s   zUtilityRegistration.__repr__c                 C      t | S r   idr`   r   r   r	   __hash__  r7   zUtilityRegistration.__hash__c                 C      t | t |kS r   r   r#   otherr   r   r	   __eq__     zUtilityRegistration.__eq__c                 C      t | t |kS r   r   r   r   r   r	   __ne__  r   zUtilityRegistration.__ne__c                 C      t | t |k S r   r   r   r   r   r	   __lt__  r   zUtilityRegistration.__lt__c                 C      t | t |kS r   r   r   r   r   r	   __le__  r   zUtilityRegistration.__le__c                 C      t | t |kS r   r   r   r   r   r	   __gt__  r   zUtilityRegistration.__gt__c                 C      t | t |kS r   r   r   r   r   r	   __ge__  r   zUtilityRegistration.__ge__r   r2   r3   r4   r%   ra   r   r   r   r   r   r   r   r   r   r   r	   r   q  s    
	r   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   r   r   )r   r   rF   rN   rP   rO   )r#   r   r   rF   rN   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   rx   r   r2   r   r   r   r   r	   r          z0AdapterRegistration.__repr__.<locals>.<listcomp>]r2   )r_   r2   r   joinr   r   rF   rN   rP   r   rO   r`   r   r   r	   ra     s   zAdapterRegistration.__repr__c                 C   r   r   r   r`   r   r   r	   r     r7   zAdapterRegistration.__hash__c                 C   r   r   r   r   r   r   r	   r     r   zAdapterRegistration.__eq__c                 C   r   r   r   r   r   r   r	   r     r   zAdapterRegistration.__ne__c                 C   r   r   r   r   r   r   r	   r     r   zAdapterRegistration.__lt__c                 C   r   r   r   r   r   r   r	   r     r   zAdapterRegistration.__le__c                 C   r   r   r   r   r   r   r	   r     r   zAdapterRegistration.__gt__c                 C   r   r   r   r   r   r   r	   r     r   zAdapterRegistration.__ge__Nr   r   r   r   r	   r     s    	r   c                   @   s   e Zd ZdS )r   N)r2   r3   r4   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   rN   handlerrO   )r#   r   r   rN   r   r   r   r   r	   r%     s   zHandlerRegistration.__init__c                 C   s   | j S r   )r   r`   r   r   r	   rP     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   rx   r   r   r   r   r   r	   r      r   z0HandlerRegistration.__repr__.<locals>.<listcomp>r   r2   )
r_   r2   r   r   r   rN   r   rP   r   rO   r`   r   r   r	   ra     s   zHandlerRegistration.__repr__)r2   r3   r4   r%   r   rP   rF   ra   r   r   r   r	   r     s    
r   N)*__doc__collectionsr   
zope.eventr   ImportErrorzope.interface.interfacesr   r   r   r   r   r   r   r   r   zope.interface.interfacer   zope.interface.declarationsr   r   r   r   zope.interface.adapterr   zope.interface._compatr   r   __all__r   r   r6   r8   r   r   r   r   r   r   r   r   r   r   r   r   r	   <module>   s\   C   /%%