o
    ed                     @   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   GD:\Projects\ConvertPro\env\Lib\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)selfZ	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      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"   O   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	   Z__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   )r6   	TypeErrorr   )r!   providedr%   Zprovr   r   r	   Z__cache_utility\   s   z%_UtilityRegistrations.__cache_utilityc                 C   s@   | j | }|| }|d8 }|dkr||= |dkS |||< |dkS )Nr?   r   )r6   )r!   rA   r%   r&   r   r   r	   Z__uncache_utilitye   s   
z'_UtilityRegistrations.__uncache_utilityc                 C   s*   z
| j | | dkW S  ty   Y dS w )Nr   F)r6   r@   )r!   rA   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   )rB   r8   r7   register	subscriber<   )r!   rA   namer%   infofactory
subscribedr   r   r	   registerUtility{   s   z%_UtilityRegistrations.registerUtilityc                 C   sD   | j ||f= | jd|| | ||}|s | jd|| d S d S rC   )r8   r7   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r!   rA   rF   r%   rI   r   r   r	   unregisterUtility   s   z'_UtilityRegistrations.unregisterUtilityN)
r/   r0   r1   r"   r9   r<   rL   rB   rJ   rN   r   r   r   r	   r5   M   s    	r5   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strr/   _init_registries_init_registrationstuple	__bases___v_utility_registrations_cache)r!   rF   basesr   r   r	   r"      s   

zComponents.__init__c                 C   s   d | jj| jS )Nz<{} {}>)format	__class__r/   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 )Z_v_)
startswith)r   kvr   r   r	   
<dictcomp>   s    z)Components.__reduce__.<locals>.<dictcomp>   )super
__reduce__r   listrT   )r!   Z	reductionZfiltered_staterY   r   r	   rc      s
   
zComponents.__reduce__c                 C   s   t  | _t  | _d S r   )r   adaptersr:   rZ   r   r   r	   rR      s   zComponents._init_registriesc                 C   s   i | _ i | _g | _g | _d S r   )r8   _adapter_registrations_subscription_registrations_handler_registrationsrZ   r   r   r	   rS      s   
zComponents._init_registrationsc                 C   s>   | j }|d u s|j| jus|j| jurt| j| j }| _ |S r   )rV   r7   r:   r8   r5   )r!   cacher   r   r	   _utility_registrations_cache   s   
z'Components._utility_registrations_cachec                 C   s   | j ddS )NrU   r   )__dict__getrZ   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   )r:   rp   r   r   r	   r      rr   rU   )rT   rf   rU   r:   rl   r!   rW   r   r   r	   	_setBases   s   zComponents._setBasesc                 C   s   |   S r   )rn   rZ   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.rO   ra   r   )r@   _getUtilityProvided_getNamer8   rm   rN   rk   rJ   r   r   UtilityRegistration)r!   r%   rA   rF   rG   eventrH   regr   r   r	   rJ      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   r8   rm   rk   rN   r   r   ry   )r!   r%   rA   rF   rH   oldr   r   r	   rN      s*   zComponents.unregisterUtilityc                 c   s8    t | j D ]\\}}}t| ||g|R  V  qd S r   )r;   r8   r   ry   )r!   rA   rF   r+   r   r   r	   registeredUtilities  s
   zComponents.registeredUtilitiesc                 C   s   | j d|||S rC   )r:   lookup)r!   rA   rF   defaultr   r   r	   queryUtility  r\   zComponents.queryUtilityc                 C   s&   | j d||}|d u rt|||S rC   )r:   r~   r   )r!   rA   rF   Zutilityr   r   r	   
getUtility  s   
zComponents.getUtilityc                 c   s    | j d|E d H  d S rC   )r:   	lookupAllr!   	interfacer   r   r	   getUtilitiesFor%  s   zComponents.getUtilitiesForc                 C   s   | j d|S rC   )r:   Z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 NrO   )	_getAdapterProvided_getAdapterRequiredrx   rg   rf   rD   r   r   AdapterRegistrationr!   rH   requiredrA   rF   rG   rz   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)
r@   r   r   rg   rm   rf   rK   r   r   r   )r!   rH   r   rA   rF   r|   r   r   r	   unregisterAdapter=  s&   
zComponents.unregisterAdapterc                 c   s<    t | j D ]\\}}}\}}t| |||||V  qd S r   )r;   rg   r   r   )r!   r   rA   rF   r%   rG   r   r   r	   registeredAdaptersX  s   
zComponents.registeredAdaptersc                 C      | j ||||S r   )rf   queryAdapter)r!   objectr   rF   r   r   r   r	   r   ^  r\   zComponents.queryAdapterc                 C   (   | j |||}|d u rt||||S r   )rf   r   r   )r!   r   r   rF   adapterr   r   r	   
getAdaptera     zComponents.getAdapterc                 C   r   r   )rf   queryMultiAdapter)r!   objectsr   rF   r   r   r   r	   r   g  s   zComponents.queryMultiAdapterc                 C   r   r   )rf   r   r   )r!   r   r   rF   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   )rf   r   rd   mapr   )r!   r   rA   rF   rH   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)
r@   r   r   rh   r)   rf   rE   r   r   SubscriptionRegistrationr   r   r   r	   registerSubscriptionAdapterz  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*   )rA   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   rH   rA   r   r   r	   r     s
    FrO   T)
r@   r   r   rh   lenrf   rM   r   r   r   )r!   rH   r   rA   rF   newr   r   r	   unregisterSubscriptionAdapter  s4   

z(Components.unregisterSubscriptionAdapterc                 C   s   | j ||S r   rf   subscribers)r!   r   rA   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   rE   r   r   HandlerRegistration)r!   rH   r   rF   rG   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   rH   r   r   r	   r     s
    

FrO   T)	r@   r   ri   r   rf   rM   r   r   r   )r!   rH   r   rF   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     r
   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)
dictr8   r:   rl   r   rD   rE   r   
registeredrI   )r!   Zrebuildregsutilsr   r   r   r   rD   rE   rA   rF   value_info_factoryr   r   r	   $rebuildUtilityRegistryFromLocalCache  sH   




z/Components.rebuildUtilityRegistryFromLocalCache)rO   r   )NNrO   rO   TN)NNrO   N)rO   N)rO   )NNrO   rO   T)NNNrO   )NrO   rO   T)NNrO   )F)'r/   r0   r1   rV   r"   r[   rc   rR   rS   propertyrk   rn   rt   rU   rJ   rN   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      sr    












	

*

"c                 C   s   z| j W S  ty   Y dS w r   )Z__component_name__AttributeError)r%   r   r   r	   rx   <  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%   rA   r   r   r	   rw   B     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@   )rH   rA   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)Z__component_adapts__r   r@   r   r   r   rP   typer   r)   rT   )rH   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 )ry   Nc                 C   ,   ||||||f\| _ | _| _| _| _| _d S r   )registryrA   rF   r%   rG   rH   )r!   r   rA   rF   r%   docrH   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})r/   )rX   rY   r/   r   getattrrA   rF   r%   reprrH   rG   rZ   r   r   r	   r[   v  s   zUtilityRegistration.__repr__c                 C      t | S r   idrZ   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"   r[   r   r   r   r   r   r   r   r   r   r   r	   ry   n  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   r   r   )r   r   rA   rF   rH   rG   )r!   r   r   rA   rF   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/   )rX   rY   r/   r   joinr   r   rA   rF   rH   r   rG   rZ   r   r   r	   r[     s   zAdapterRegistration.__repr__c                 C   r   r   r   rZ   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   rF   handlerrG   )r!   r   r   rF   r   r   r   r   r	   r"     s   zHandlerRegistration.__init__c                 C   s   | j S r   )r   rZ   r   r   r	   rH     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/   )rX   rY   r/   r   r   r   rF   r   rH   r   rG   rZ   r   r   r	   r[     s   zHandlerRegistration.__repr__)r/   r0   r1   r"   r   rH   rA   r[   r   r   r   r	   r     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   __all__r   r3   r5   r   rx   rw   r   r   ry   r   r   r   r   r   r   r	   <module>   sX   C   .%%