o
    hd                     @   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gZG dd dZdd ZG dd dZe
eG dd dZdd Zdd  Zd!d" Z d#d$ Z!e
eG d%d& d&Z"e
eG d'd( d(Z#eeG d)d* d*e#Z$eeG d+d, d,e#Z%d-S ).zBasic components support
    )defaultdict)notifyc                  O      d S N )argkwr   r   J/var/www/html/venv/lib/python3.10/site-packages/zope/interface/registry.pyr      s   r   )AdapterRegistry)implementedBy)implementer)implementer_only)
providedBy)	Interface)ComponentLookupError)IAdapterRegistration)IComponents)IHandlerRegistration)ISpecification) ISubscriptionAdapterRegistration)IUtilityRegistration)
Registered)Unregistered
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>7   s    z8_UnhashableComponentCounter.__init__.<locals>.<listcomp>)items_data)selfZ	otherdictr   r   r	   __init__5   s   z$_UnhashableComponentCounter.__init__c                 C   s$   | j D ]\}}||kr|  S qdS Nr   )r   )r    key	componentcountr   r   r	   __getitem__9   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__F   s   z'_UnhashableComponentCounter.__delitem__N)__name__
__module____qualname__r!   r&   r+   r-   r   r   r   r	   r   2   s
    r   c                   C   s   t tS r   )r   intr   r   r   r	   _defaultdict_intN      r2   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   r2   _cache
_utilities_utility_registrations%_UtilityRegistrations__populate_cache)r    	utilitiesZutility_registrationsr   r   r	   r!   T   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_cache\   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   )r5   	TypeErrorr   )r    providedr$   Zprovr   r   r	   Z__cache_utilitya   s   z%_UtilityRegistrations.__cache_utilityc                 C   s@   | j | }|| }|d8 }|dkr||= |dkS |||< |dkS )Nr>   r   )r5   )r    r@   r$   r%   r   r   r	   Z__uncache_utilityl   s   
z'_UtilityRegistrations.__uncache_utilityc                 C   s*   z
| j | | dkW S  ty   Y dS w )Nr   F)r5   r?   )r    r@   r$   r   r   r	   _is_utility_subscribed{   s
   z,_UtilityRegistrations._is_utility_subscribedc                 C   sV   |  ||}|||f| j||f< | jd||| |s#| jd|| | || d S Nr   )rA   r7   r6   register	subscriber;   )r    r@   namer$   infofactory
subscribedr   r   r	   registerUtility   s   z%_UtilityRegistrations.registerUtilityc                 C   sD   | j ||f= | jd|| | ||}|s | jd|| d S d S rB   )r7   r6   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r    r@   rE   r$   rH   r   r   r	   unregisterUtility   s   z'_UtilityRegistrations.unregisterUtilityN)
r.   r/   r0   r!   r8   r;   rK   rA   rI   rM   r   r   r   r	   r4   R   s    r4   c                       sp  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   )
isinstancestrr.   _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 d| j dS )N< >)	__class__r.   r    r   r   r	   __repr__   s   zComponents.__repr__c                    s8   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>   )super
__reduce__r   listrS   )r    Z	reductionZfiltered_staterZ   r   r	   rc      s
   
zComponents.__reduce__c                 C   s   t  | _t  | _d S r   )r
   adaptersr9   r[   r   r   r	   rQ      s   zComponents._init_registriesc                 C   s   i | _ i | _g | _g | _d S r   )r7   _adapter_registrations_subscription_registrations_handler_registrationsr[   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   r6   r9   r7   r4   )r    cacher   r   r	   _utility_registrations_cache   s   z'Components._utility_registrations_cachec                 C   s   | j ddS )NrT   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   )rf   r   baser   r   r	   r          z(Components._setBases.<locals>.<listcomp>c                 S   ro   r   )r9   rp   r   r   r	   r      rr   rT   )rS   rf   rT   r9   rl   r    rV   r   r   r	   	_setBases   s   zComponents._setBasesc                 C   s   |   S r   )rn   r[   r   r   r	   <lambda>   s    zComponents.<lambda>c                 C   s
   |  |S r   )rt   rs   r   r   r	   ru      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.rN   ra   r   )r?   _getUtilityProvided_getNamer7   rm   rM   rk   rI   r   r   UtilityRegistration)r    r$   r@   rE   rF   eventrG   regr   r   r	   rI      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 )Nrv   z3Must specify one of component, factory and providedr   Fr>   T)	r?   rw   r7   rm   rk   rM   r   r   ry   )r    r$   r@   rE   rG   oldr   r   r	   rM     s*   zComponents.unregisterUtilityc                 c   s8    t | j D ]\\}}}t| ||g|R  V  qd S r   )r:   r7   r   ry   )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)     zComponents.queryUtilityc                 C   s&   | j d||}|d u rt|||S rB   )r9   r~   r   )r    r@   rE   Zutilityr   r   r	   
getUtility,  s   
zComponents.getUtilityc                 c   s    | j d|E d H  d S rB   )r9   	lookupAllr    	interfacer   r   r	   getUtilitiesFor2  s   zComponents.getUtilitiesForc                 C   s   | j d|S rB   )r9   Zsubscriptionsr   r   r   r	   getAllUtilitiesRegisteredFor5     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 NrN   )	_getAdapterProvided_getAdapterRequiredrx   rg   rf   rC   r   r   AdapterRegistrationr    rG   requiredr@   rE   rF   rz   r   r   r	   registerAdapter8  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)
r?   r   r   rg   rm   rf   rJ   r   r   r   )r    rG   r   r@   rE   r|   r   r   r	   unregisterAdapterJ  s"   
zComponents.unregisterAdapterc                 c   s<    t | j D ]\\}}}\}}t| |||||V  qd S r   )r:   rg   r   r   )r    r   r@   rE   r$   rF   r   r   r	   registeredAdaptersd  s   
zComponents.registeredAdaptersc                 C      | j ||||S r   )rf   queryAdapter)r    objectr   rE   r   r   r   r	   r   j  r   zComponents.queryAdapterc                 C   (   | j |||}|d u rt||||S r   )rf   r   r   )r    r   r   rE   adapterr   r   r	   
getAdapterm     zComponents.getAdapterc                 C   r   r   )rf   queryMultiAdapter)r    objectsr   rE   r   r   r   r	   r   s  s   zComponents.queryMultiAdapterc                 C   r   r   )rf   r   r   )r    r   r   rE   r   r   r   r	   getMultiAdapterx  r   zComponents.getMultiAdapterc                 c   sB    | j ttt||D ]\}}|| }|d ur||fV  qd S r   )rf   r   rd   mapr   )r    r   r@   rE   rG   r   r   r   r	   getAdapters~  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)
r?   r   r   rh   r(   rf   rD   r   r   SubscriptionRegistrationr   r   r   r	   registerSubscriptionAdapter  s    

z&Components.registerSubscriptionAdapterc                 c   $    | j D ]}t| g|R  V  qd S r   )rh   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   rr<   nfr)   )r@   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   rG   r@   r   r   r	   r     s
    FrN   T)
r?   r   r   rh   lenrf   rL   r   r   r   )r    rG   r   r@   rE   newr   r   r	   unregisterSubscriptionAdapter  s4   
z(Components.unregisterSubscriptionAdapterc                 C   s   | j ||S r   rf   subscribers)r    r   r@   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)	r?   r   ri   r(   rf   rD   r   r   HandlerRegistration)r    rG   r   rE   rF   rz   r   r   r	   registerHandler  s   


zComponents.registerHandlerc                 c   r   r   )ri   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   rG   r   r   r	   r     s
    

FrN   T)	r?   r   ri   r   rf   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  r   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   )r=   r   r   r	   ru   (      zAComponents.rebuildUtilityRegistryFromLocalCache.<locals>.<lambda>c                  W   r   r   r   )argsr   r   r	   ru   .  r   r   r>   N)needed_registereddid_not_registerneeded_subscribeddid_not_subscribe)
dictr7   r9   rl   r   rC   rD   r   
registeredrH   )r    Zrebuildregsutilsr   r   r   r   rC   rD   r@   rE   value_info_factoryr   r   r	   $rebuildUtilityRegistryFromLocalCache  sH   




z/Components.rebuildUtilityRegistryFromLocalCache)rN   r   )NNrN   rN   TN)NNrN   N)rN   N)rN   )NNrN   rN   T)NNNrN   )NrN   rN   T)NNrN   )F)'r.   r/   r0   rU   r!   r\   rc   rQ   rR   propertyrk   rn   rt   rT   rI   rM   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   re   r	   r      sp    












	

*

"c                 C   s   z| j W S  ty   Y dS w r   )Z__component_name__AttributeError)r$   r   r   r	   rx   J  s
   rx   c                 C   (   t t| }t|dkr|d S td)Nr>   r   zWThe utility doesn't provide a single interface and no provided interface was specified.)rd   r   r   r?   )r$   r@   r   r   r	   rw   Q     rw   c                 C   r   )Nr>   r   zaThe adapter factory doesn't implement a single interface and no provided interface was specified.)rd   r   r   r?   )rG   r@   r   r   r	   r   [  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)Z__component_adapts__r   r?   r   r   r   rO   typer   r(   rS   )rG   r   resultr   r   r   r	   r   e  s4   




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 )ry   Nc                 C   s(   || _ || _|| _|| _|| _|| _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
S )Nz"{}({!r}, {}, {!r}, {}, {!r}, {!r})r.   )formatrZ   r.   r   getattrr@   rE   r$   reprrG   rF   r[   r   r   r	   r\     s   zUtilityRegistration.__repr__c                 C      t | S r   idr[   r   r   r	   __hash__  r3   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   r.   r/   r0   r!   r\   r   r   r   r   r   r   r   r   r   r   r	   ry     s    
	ry   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!     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S )Nz {}({!r}, {}, {}, {!r}, {}, {!r})[, c                 S   ro   r   r.   r   r   r   r   r	   r         z0AdapterRegistration.__repr__.<locals>.<listcomp>]r.   )r   rZ   r.   r   joinr   r   r@   rE   rG   r   rF   r[   r   r   r	   r\     s   zAdapterRegistration.__repr__c                 C   r   r   r   r[   r   r   r	   r     r3   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)r.   r/   r0   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   rE   handlerrF   )r    r   r   rE   r   r   r   r   r	   r!     s   zHandlerRegistration.__init__c                 C   s   | j S r   )r   r[   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
S )Nz{}({!r}, {}, {!r}, {}, {!r})r   r   c                 S   ro   r   r   r   r   r   r	   r     r   z0HandlerRegistration.__repr__.<locals>.<listcomp>r   r.   )r   rZ   r.   r   r   r   rE   r   rG   r   rF   r[   r   r   r	   r\     s   zHandlerRegistration.__repr__)r.   r/   r0   r!   r   rG   r@   r\   r   r   r   r	   r     s    
r   N)&__doc__collectionsr   Z
zope.eventr   ImportErrorZzope.interface.adapterr
   Zzope.interface.declarationsr   r   r   r   Zzope.interface.interfacer   Zzope.interface.interfacesr   r   r   r   r   r   r   r   r   __all__r   r2   r4   r   rx   rw   r   r   ry   r   r   r   r   r   r   r	   <module>   sX   G   3

)&