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   w/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/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)self	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      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    	utilitiesutility_registrationsr   r   r	   r"   T   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	   __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   )r6   	TypeErrorr   )r    providedr%   provr   r   r	   __cache_utilitya   s   z%_UtilityRegistrations.__cache_utilityc                 C   s@   | j | }|| }|d8 }|dkr||= |dkS |||< |dkS )NrA   r   )r6   )r    rC   r%   r&   r   r   r	   __uncache_utilityl   s   
z'_UtilityRegistrations.__uncache_utilityc                 C   s*   z
| j | | dkW S  ty   Y dS w )Nr   F)r6   rB   )r    rC   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   )rG   r8   r7   register	subscriber=   )r    rC   namer%   infofactory
subscribedr   r   r	   registerUtility   s   z%_UtilityRegistrations.registerUtilityc                 C   sD   | j ||f= | jd|| | ||}|s | jd|| d S d S rH   )r8   r7   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r    rC   rK   r%   rN   r   r   r	   unregisterUtility   s   z'_UtilityRegistrations.unregisterUtilityN)
r/   r0   r1   r"   r9   r=   rQ   rG   rO   rS   r   r   r   r	   r5   R   s    r5   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    rK   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 )_v_)
startswith)r   kvr   r   r	   
<dictcomp>   s    z)Components.__reduce__.<locals>.<dictcomp>   )super
__reduce__r   listrY   )r    	reductionfiltered_stater`   r   r	   rj      s
   
zComponents.__reduce__c                 C   s   t  | _t  | _d S r   )r
   adaptersr:   ra   r   r   r	   rW      s   zComponents._init_registriesc                 C   s   i | _ i | _g | _g | _d S r   )r8   _adapter_registrations_subscription_registrations_handler_registrationsra   r   r   r	   rX      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[   r7   r:   r8   r5   )r    cacher   r   r	   _utility_registrations_cache   s   z'Components._utility_registrations_cachec                 C   s   | j ddS )NrZ   r   )__dict__getra   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{   rZ   )rY   ro   rZ   r:   ru   r    r\   r   r   r	   	_setBases   s   zComponents._setBasesc                 C   s   |   S r   )rw   ra   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.rT   rh   r   )rB   _getUtilityProvided_getNamer8   rv   rS   rt   rO   r   r   UtilityRegistration)r    r%   rC   rK   rL   eventrM   regr   r   r	   rO      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   FrA   T)	rB   r   r8   rv   rt   rS   r   r   r   )r    r%   rC   rK   rM   oldr   r   r	   rS     s*   zComponents.unregisterUtilityc                 c   s8    t | j D ]\\}}}t| ||g|R  V  qd S r   )r<   r8   r   r   )r    rC   rK   r+   r   r   r	   registeredUtilities$  s
   zComponents.registeredUtilitiesc                 C   s   | j d|||S rH   )r:   lookup)r    rC   rK   defaultr   r   r	   queryUtility)     zComponents.queryUtilityc                 C   s&   | j d||}|d u rt|||S rH   )r:   r   r   )r    rC   rK   utilityr   r   r	   
getUtility,  s   
zComponents.getUtilityc                 c   s    | j d|E d H  d S rH   )r:   	lookupAllr    	interfacer   r   r	   getUtilitiesFor2  s   zComponents.getUtilitiesForc                 C   s   | j d|S rH   )r:   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 NrT   )	_getAdapterProvided_getAdapterRequiredr   rp   ro   rI   r   r   AdapterRegistrationr    rM   requiredrC   rK   rL   r   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)
rB   r   r   rp   rv   ro   rP   r   r   r   )r    rM   r   rC   rK   r   r   r   r	   unregisterAdapterJ  s"   
zComponents.unregisterAdapterc                 c   s<    t | j D ]\\}}}\}}t| |||||V  qd S r   )r<   rp   r   r   )r    r   rC   rK   r%   rL   r   r   r	   registeredAdaptersd  s   
zComponents.registeredAdaptersc                 C      | j ||||S r   )ro   queryAdapter)r    objectr   rK   r   r   r   r	   r   j  r   zComponents.queryAdapterc                 C   (   | j |||}|d u rt||||S r   )ro   r   r   )r    r   r   rK   adapterr   r   r	   
getAdapterm     zComponents.getAdapterc                 C   r   r   )ro   queryMultiAdapter)r    objectsr   rK   r   r   r   r	   r   s  s   zComponents.queryMultiAdapterc                 C   r   r   )ro   r   r   )r    r   r   rK   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   )ro   r   rk   mapr   )r    r   rC   rK   rM   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)
rB   r   r   rq   r)   ro   rJ   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   rr>   nfr*   )rC   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   rM   rC   r   r   r	   r     s
    FrT   T)
rB   r   r   rq   lenro   rR   r   r   r   )r    rM   r   rC   rK   newr   r   r	   unregisterSubscriptionAdapter  s4   
z(Components.unregisterSubscriptionAdapterc                 C   s   | j ||S r   ro   subscribers)r    r   rC   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)	rB   r   rr   r)   ro   rJ   r   r   HandlerRegistration)r    rM   r   rK   rL   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   rM   r   r   r	   r     s
    

FrT   T)	rB   r   rr   r   ro   rR   r   r   r   )r    rM   r   rK   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	   r~   (      zAComponents.rebuildUtilityRegistryFromLocalCache.<locals>.<lambda>c                  W   r   r   r   )argsr   r   r	   r~   .  r   r   rA   N)needed_registereddid_not_registerneeded_subscribeddid_not_subscribe)
dictr8   r:   ru   r   rI   rJ   r   
registeredrN   )r    rebuildregsutilsr   r   r   r   rI   rJ   rC   rK   value_info_factoryr   r   r	   $rebuildUtilityRegistryFromLocalCache  sH   




z/Components.rebuildUtilityRegistryFromLocalCache)rT   r   )NNrT   rT   TN)NNrT   N)rT   N)rT   )NNrT   rT   T)NNNrT   )NrT   rT   T)NNrT   )F)'r/   r0   r1   r[   r"   rb   rj   rW   rX   propertyrt   rw   r}   rZ   rO   rS   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      sp    












	

*

"c                 C   s   z| j W S  ty   Y dS w r   )__component_name__AttributeError)r%   r   r   r	   r   J  s
   r   c                 C   (   t t| }t|dkr|d S td)NrA   r   zWThe utility doesn't provide a single interface and no provided interface was specified.)rk   r   r   rB   )r%   rC   r   r   r	   r   Q     r   c                 C   r   )NrA   r   zaThe adapter factory doesn't implement a single interface and no provided interface was specified.)rk   r   r   rB   )rM   rC   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)__component_adapts__r   rB   r   r   r   rU   typer   r)   rY   )rM   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 )r   Nc                 C   s(   || _ || _|| _|| _|| _|| _d S r   )registryrC   rK   r%   rL   rM   )r    r   rC   rK   r%   docrM   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/   )formatr`   r/   r   getattrrC   rK   r%   reprrM   rL   ra   r   r   r	   rb     s   zUtilityRegistration.__repr__c                 C      t | S r   idra   r   r   r	   __hash__  r4   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/   r0   r1   r"   rb   r   r   r   r   r   r   r   r   r   r   r	   r     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   s,   ||||||f\| _ | _| _| _| _| _d S r   )r   r   rC   rK   rM   rL   )r    r   r   rC   rK   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   rx   r   r/   r   r   r   r   r	   r         z0AdapterRegistration.__repr__.<locals>.<listcomp>]r/   )r   r`   r/   r   joinr   r   rC   rK   rM   r   rL   ra   r   r   r	   rb     s   zAdapterRegistration.__repr__c                 C   r   r   r   ra   r   r   r	   r     r4   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/   r0   r1   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   rK   handlerrL   )r    r   r   rK   r   r   r   r   r	   r"     s   zHandlerRegistration.__init__c                 C   s   | j S r   )r   ra   r   r   r	   rM     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   rx   r   r   r   r   r   r	   r     r   z0HandlerRegistration.__repr__.<locals>.<listcomp>r   r/   )r   r`   r/   r   r   r   rK   r   rM   r   rL   ra   r   r   r	   rb     s   zHandlerRegistration.__repr__)r/   r0   r1   r"   r   rM   rC   rb   r   r   r   r	   r     s    
r   N)&__doc__collectionsr   
zope.eventr   ImportErrorzope.interface.adapterr
   zope.interface.declarationsr   r   r   r   zope.interface.interfacer   zope.interface.interfacesr   r   r   r   r   r   r   r   r   __all__r   r3   r5   r   r   r   r   r   r   r   r   r   r   r   r   r	   <module>   sX   G   3

)&