o
    gHdd                     @   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   ^/var/www/html/facialservice/flask-venv/lib/python3.10/site-packages/zope/interface/registry.pyr          r   )ISpecification)ComponentLookupError)IAdapterRegistration)IComponents)IHandlerRegistration) ISubscriptionAdapterRegistration)IUtilityRegistration)
Registered)Unregistered)	Interface)implementedBy)implementer)implementer_only)
providedBy)AdapterRegistry
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>4   s    z8_UnhashableComponentCounter.__init__.<locals>.<listcomp>)items_data)self	otherdictr   r   r	   __init__2   s   z$_UnhashableComponentCounter.__init__c                 C   s$   | j D ]\}}||kr|  S qdS Nr   )r    )r!   key	componentcountr   r   r	   __getitem__6   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__C   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_intJ      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!   	utilitiesutility_registrationsr   r   r	   r#   O   s   
z_UtilityRegistrations.__init__c                 C   s4   t | j D ]\\}}}|d }| || qd S r$   )iterr9   r   $_UtilityRegistrations__cache_utility)r!   p_r,   r&   r   r   r	   __populate_cacheW   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   )r7   	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 )NrB   r   )r7   )r!   rD   r&   r'   r   r   r	   __uncache_utilitye   s   
z'_UtilityRegistrations.__uncache_utilityc                 C   s*   z
| j | | dkW S  ty   Y dS w )Nr   F)r7   rC   )r!   rD   r&   r   r   r	   _is_utility_subscribedt   s
   z,_UtilityRegistrations._is_utility_subscribedc                 C   sV   |  ||}|||f| j||f< | jd||| |s#| jd|| | || d S Nr   )rH   r9   r8   register	subscriber>   )r!   rD   namer&   infofactory
subscribedr   r   r	   registerUtility{   s   z%_UtilityRegistrations.registerUtilityc                 C   sD   | j ||f= | jd|| | ||}|s | jd|| d S d S rI   )r9   r8   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r!   rD   rL   r&   rO   r   r   r	   unregisterUtility   s   z'_UtilityRegistrations.unregisterUtilityN)
r0   r1   r2   r#   r:   r>   rR   rH   rP   rT   r   r   r   r	   r6   M   s    	r6   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   )
isinstancestrr0   _init_registries_init_registrationstuple	__bases___v_utility_registrations_cache)r!   rL   basesr   r   r	   r#      s   

zComponents.__init__c                 C   s   d | jj| jS )Nz<{} {}>)format	__class__r0   r!   r   r   r	   __repr__      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   listrZ   )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	   rX      s   zComponents._init_registriesc                 C   s   i | _ i | _g | _g | _d S r   )r9   _adapter_registrations_subscription_registrations_handler_registrationsr`   r   r   r	   rY      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\   r8   r;   r9   r6   )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[   )rZ   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.rU   rh   r   )rC   _getUtilityProvided_getNamer9   rv   rT   rt   rP   r   r   UtilityRegistration)r!   r&   rD   rL   rM   eventrN   regr   r   r	   rP      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   FrB   T)	rC   r   r9   rv   rt   rT   r   r   r   )r!   r&   rD   rL   rN   oldr   r   r	   rT      s,   
zComponents.unregisterUtilityc                 c   s8    t | j D ]\\}}}t| ||g|R  V  qd S r   )r=   r9   r   r   )r!   rD   rL   r,   r   r   r	   registeredUtilities  s
   zComponents.registeredUtilitiesc                 C   s   | j d|||S rI   )r;   lookup)r!   rD   rL   defaultr   r   r	   queryUtility  rb   zComponents.queryUtilityc                 C   s&   | j d||}|d u rt|||S rI   )r;   r   r   )r!   rD   rL   utilityr   r   r	   
getUtility  s   
zComponents.getUtilityc                 c   s    | j d|E d H  d S rI   )r;   	lookupAllr!   	interfacer   r   r	   getUtilitiesFor%  s   zComponents.getUtilitiesForc                 C   s   | j d|S rI   )r;   subscriptionsr   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 NrU   )	_getAdapterProvided_getAdapterRequiredr   rp   ro   rJ   r   r   AdapterRegistrationr!   rN   requiredrD   rL   rM   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)
rC   r   r   rp   rv   ro   rQ   r   r   r   )r!   rN   r   rD   rL   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   rD   rL   r&   rM   r   r   r	   registeredAdaptersX  s   
zComponents.registeredAdaptersc                 C      | j ||||S r   )ro   queryAdapter)r!   objectr   rL   r   r   r   r	   r   ^  rb   zComponents.queryAdapterc                 C   (   | j |||}|d u rt||||S r   )ro   r   r   )r!   r   r   rL   adapterr   r   r	   
getAdaptera     zComponents.getAdapterc                 C   r   r   )ro   queryMultiAdapter)r!   objectsr   rL   r   r   r   r	   r   g  s   zComponents.queryMultiAdapterc                 C   r   r   )ro   r   r   )r!   r   r   rL   r   r   r   r	   getMultiAdapterl  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   rD   rL   rN   r   r   r   r	   getAdaptersr  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)
rC   r   r   rq   r*   ro   rK   r   r   SubscriptionRegistrationr   r   r   r	   registerSubscriptionAdapterz  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+   )rD   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   rN   rD   r   r   r	   r     s    FrU   T)
rC   r   r   rq   lenro   rS   r   r   r   )r!   rN   r   rD   rL   newr   r   r	   unregisterSubscriptionAdapter  s4   

z(Components.unregisterSubscriptionAdapterc                 C   s   | j ||S r   ro   subscribers)r!   r   rD   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)	rC   r   rr   r*   ro   rK   r   r   HandlerRegistration)r!   rN   r   rL   rM   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   rN   r   r   r	   r     s    
FrU   T)	rC   r   rr   r   ro   rS   r   r   r   )r!   rN   r   rL   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   )r@   r   r   r	   r~     r
   zAComponents.rebuildUtilityRegistryFromLocalCache.<locals>.<lambda>c                  W   r   r   r   )argsr   r   r	   r~   !  r
   r   rB   N)needed_registereddid_not_registerneeded_subscribeddid_not_subscribe)
dictr9   r;   ru   r   rJ   rK   r   
registeredrO   )r!   rebuildregsutilsr   r   r   r   rJ   rK   rD   rL   value_info_factoryr   r   r	   $rebuildUtilityRegistryFromLocalCache  sH   




z/Components.rebuildUtilityRegistryFromLocalCache)rU   r   )NNrU   rU   TN)NNrU   N)rU   N)rU   )NNrU   rU   T)NNNrU   )NrU   rU   T)NNrU   )F)'r0   r1   r2   r\   r#   ra   rj   rX   rY   propertyrt   rw   r}   r[   rP   rT   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)NrB   r   zWThe utility doesn't provide a single interface and no provided interface was specified.)rk   r   r   rC   )r&   rD   r   r   r	   r   B     r   c                 C   r   )NrB   r   zaThe adapter factory doesn't implement a single interface and no provided interface was specified.)rk   r   r   rC   )rN   rD   r   r   r	   r   J  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   rC   r   r   r   rV   typer   r*   rZ   )rN   r   resultr   r   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 )r   Nc                 C   ,   ||||||f\| _ | _| _| _| _| _d S r   )registryrD   rL   r&   rM   rN   )r!   r   rD   rL   r&   docrN   r   r   r	   r#   q  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})r0   )r^   r_   r0   r   getattrrD   rL   r&   reprrN   rM   r`   r   r   r	   ra   v  s   zUtilityRegistration.__repr__c                 C      t | S r   idr`   r   r   r	   __hash__  r5   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   r0   r1   r2   r#   ra   r   r   r   r   r   r   r   r   r   r   r	   r   n  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   rD   rL   rN   rM   )r!   r   r   rD   rL   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   r0   r   r   r   r   r	   r         z0AdapterRegistration.__repr__.<locals>.<listcomp>]r0   )r^   r_   r0   r   joinr   r   rD   rL   rN   r   rM   r`   r   r   r	   ra     s   zAdapterRegistration.__repr__c                 C   r   r   r   r`   r   r   r	   r     r5   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)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   rL   handlerrM   )r!   r   r   rL   r   r   r   r   r	   r#     s   zHandlerRegistration.__init__c                 C   s   | j S r   )r   r`   r   r   r	   rN     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   r0   )r^   r_   r0   r   r   r   rL   r   rN   r   rM   r`   r   r   r	   ra     s   zHandlerRegistration.__repr__)r0   r1   r2   r#   r   rN   rD   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   __all__r   r4   r6   r   r   r   r   r   r   r   r   r   r   r   r   r	   <module>   sX   C   .%%