B
    0`e                 @   s  d Z ddlmZ yddlmZ W n ek
r<   dd ZY nX 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   s   d S )N )argkwr   r   F/home/dcms/DCMS/lib/python3.7/site-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)selfZ	otherdictr   r   r   __init__4   s    z$_UnhashableComponentCounter.__init__c             C   s$   x| j D ]\}}||kr|S qW dS )Nr   )r    )r!   key	componentcountr   r   r   __getitem__8   s    z'_UnhashableComponentCounter.__getitem__c             C   sJ   x4t | jD ]&\}}|d |kr||f| j|< d S qW | j||f d S )Nr   )	enumerater    append)r!   r$   r%   idatar   r   r   __setitem__>   s
    z'_UnhashableComponentCounter.__setitem__c             C   s<   x.t | jD ] \}}|d |kr| j|= d S qW t|d S )Nr   )r'   r    KeyError)r!   r$   r)   r*   r   r   r   __delitem__E   s
    z'_UnhashableComponentCounter.__delitem__N)__name__
__module____qualname__r"   r&   r+   r-   r   r   r   r   r   1   s   r   c               C   s   t tS )N)r   intr   r   r   r   _defaultdict_intL   s    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 )N)r   r2   _cache
_utilities_utility_registrations%_UtilityRegistrations__populate_cache)r!   	utilitiesZutility_registrationsr   r   r   r"   Q   s    
z_UtilityRegistrations.__init__c             C   s8   x2t | j D ] \\}}}|d }| || qW d S )Nr   )iterr6   r   $_UtilityRegistrations__cache_utility)r!   p_r*   r$   r   r   r   Z__populate_cacheY   s    z&_UtilityRegistrations.__populate_cachec             C   s\   y| j | |  d7  < W n< tk
rV   t| j |  }| j |< ||  d7  < Y nX d S )N   )r4   	TypeErrorr   )r!   providedr$   Zprovr   r   r   Z__cache_utility^   s
    z%_UtilityRegistrations.__cache_utilityc             C   s:   | j | }|| }|d8 }|dkr*||= n|||< |dkS )Nr=   r   )r4   )r!   r?   r$   r%   r   r   r   Z__uncache_utilityg   s    
z'_UtilityRegistrations.__uncache_utilityc             C   s,   y| j | | dkS  tk
r&   dS X d S )Nr   F)r4   r>   )r!   r?   r$   r   r   r   _is_utility_subscribedv   s    z,_UtilityRegistrations._is_utility_subscribedc             C   sV   |  ||}|||f| j||f< | jd||| |sF| jd|| | || d S )Nr   )r@   r6   r5   register	subscriber:   )r!   r?   namer$   infofactory
subscribedr   r   r   registerUtility}   s    z%_UtilityRegistrations.registerUtilityc             C   s@   | j ||f= | jd|| | ||}|s<| jd|| d S )Nr   )r6   r5   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r!   r?   rC   r$   rF   r   r   r   unregisterUtility   s
    z'_UtilityRegistrations.unregisterUtilityN)
r.   r/   r0   r"   r7   r:   rI   r@   rG   rK   r   r   r   r   r3   O   s   	r3   c                   sN  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dPd,d-ZdQd.d/ZdRd0d1ZdSd2d3Zd4d5 ZdTd6d7Zd8d9 ZdUd:d;Zd<d= Z dVd>d?Z!d@dA Z"dWdBdCZ#dDdE Z$dXdGdHZ%  Z&S )Yr   N r   c             C   s8   t |tst|| _|   |   t|| _d | _d S )N)	
isinstancer   AssertionErrorr.   _init_registries_init_registrationstuple	__bases___v_utility_registrations_cache)r!   rC   basesr   r   r   r"      s    
zComponents.__init__c             C   s   d| j j| jf S )Nz<%s %s>)	__class__r.   )r!   r   r   r   __repr__   s    zComponents.__repr__c                s<   t 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>   )superr   
__reduce__r   listrQ   )r!   Z	reductionZfiltered_state)rU   r   r   r]      s
    zComponents.__reduce__c             C   s   t  | _t  | _d S )N)r   adaptersr8   )r!   r   r   r   rO      s    zComponents._init_registriesc             C   s   i | _ i | _g | _g | _d S )N)r6   _adapter_registrations_subscription_registrations_handler_registrations)r!   r   r   r   rP      s    zComponents._init_registrationsc             C   s>   | j }|d ks&|j| jk	s&|j| jk	r:t| j| j }| _ |S )N)rS   r5   r8   r6   r3   )r!   cacher   r   r   _utility_registrations_cache   s    z'Components._utility_registrations_cachec             C   s   | j ddS )NrR   r   )__dict__get)r!   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   s   g | ]
}|j qS r   )r_   )r   baser   r   r   r      s    z(Components._setBases.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )r8   )r   rh   r   r   r   r      s    rR   )rQ   r_   rR   r8   re   )r!   rT   r   r   r   	_setBases   s
    zComponents._setBasesc             C   s   |   S )N)rg   )r!   r   r   r   <lambda>   r   zComponents.<lambda>c             C   s
   |  |S )N)ri   )r!   rT   r   r   r   rj      r   Tc          	   C   s   |r|rt d| }|d kr&t|}|dkr6t|}| j||f}|d k	rx|d d ||fkrfd S | |d || | j||||| |rtt	t
| ||||| d S )Nz$Can't specify factory and component.rL   r[   r   )r>   _getUtilityProvided_getNamer6   rf   rK   rd   rG   r   r   UtilityRegistration)r!   r$   r?   rC   rD   eventrE   regr   r   r   rG      s&    zComponents.registerUtilityc             C   s   |r|rt d| }|d kr6|d kr.t dt|}| j||f}|d ksb|d k	rf||d krfdS |d krv|d }| j||| ttt| |||f|dd    dS )Nz$Can't specify factory and component.z3Must specify one of component, factory and providedr   Fr=   T)	r>   rk   r6   rf   rd   rK   r   r   rm   )r!   r$   r?   rC   rE   oldr   r   r   rK      s&    
 zComponents.unregisterUtilityc             c   s8   x2t | j D ] \\}}}t| ||f| V  qW d S )N)r9   r6   r   rm   )r!   r?   rC   r*   r   r   r   registeredUtilities  s    zComponents.registeredUtilitiesc             C   s   | j d|||S )Nr   )r8   lookup)r!   r?   rC   defaultr   r   r   queryUtility  s    zComponents.queryUtilityc             C   s&   | j d||}|d kr"t|||S )Nr   )r8   rr   r
   )r!   r?   rC   utilityr   r   r   
getUtility!  s    
zComponents.getUtilityc             c   s*   x$| j d|D ]\}}||fV  qW d S )Nr   )r8   	lookupAll)r!   	interfacerC   ru   r   r   r   getUtilitiesFor'  s    zComponents.getUtilitiesForc             C   s   | j d|S )Nr   )r8   Zsubscriptions)r!   rx   r   r   r   getAllUtilitiesRegisteredFor+  s    z'Components.getAllUtilitiesRegisteredForc          	   C   sr   |d krt |}t||}|dkr*t|}||f| j|||f< | j|||| |rnttt| ||||| d S )NrL   )	_getAdapterProvided_getAdapterRequiredrl   r`   r_   rA   r   r   AdapterRegistration)r!   rE   requiredr?   rC   rD   rn   r   r   r   registerAdapter.  s    

zComponents.registerAdapterc             C   s   |d kr |d krt dt|}|d kr8|d kr8t dt||}| j|||f}|d ksp|d k	rt||d krtdS | j|||f= | j||| ttt	| |||f|  dS )Nz(Must specify one of factory and providedz(Must specify one of factory and requiredr   FT)
r>   r{   r|   r`   rf   r_   rH   r   r   r}   )r!   rE   r~   r?   rC   rp   r   r   r   unregisterAdapter@  s"    
zComponents.unregisterAdapterc             c   s>   x8t | j D ]&\\}}}\}}t| |||||V  qW d S )N)r9   r`   r   r}   )r!   r~   r?   rC   r$   rD   r   r   r   registeredAdapters[  s     
zComponents.registeredAdaptersc             C   s   | j ||||S )N)r_   queryAdapter)r!   objectrx   rC   rs   r   r   r   r   a  s    zComponents.queryAdapterc             C   s(   | j |||}|d kr$t||||S )N)r_   r   r
   )r!   r   rx   rC   adapterr   r   r   
getAdapterd  s    zComponents.getAdapterc             C   s   | j ||||S )N)r_   queryMultiAdapter)r!   objectsrx   rC   rs   r   r   r   r   j  s    zComponents.queryMultiAdapterc             C   s(   | j |||}|d kr$t||||S )N)r_   r   r
   )r!   r   rx   rC   r   r   r   r   getMultiAdaptero  s    zComponents.getMultiAdapterc             c   sD   x>| j ttt||D ]"\}}|| }|d k	r||fV  qW d S )N)r_   rw   r^   mapr   )r!   r   r?   rC   rE   r   r   r   r   getAdaptersu  s    zComponents.getAdaptersc          	   C   sn   |rt d|d krt|}t||}| j|||||f | j||| |rjttt	| ||||| d S )Nz'Named subscribers are not yet supported)
r>   r{   r|   ra   r(   r_   rB   r   r   SubscriptionRegistration)r!   rE   r~   r?   rC   rD   rn   r   r   r   registerSubscriptionAdapter}  s    

z&Components.registerSubscriptionAdapterc             c   s$   x| j D ]}t| f| V  qW d S )N)ra   r   )r!   r*   r   r   r   registeredSubscriptionAdapters  s    z)Components.registeredSubscriptionAdaptersc          	      s   |rt dd kr, d kr$t dt d krD d krDt dt  d krnfdd| jD }n fdd| jD }t|t| jkrdS || jd d < | j  ttt	| | d d	S )
Nz'Named subscribers are not yet supportedz(Must specify one of factory and providedz(Must specify one of factory and requiredc                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 | ]4\}}}}}|kr*|kr*| ks|||||fqS r   r   )r   r   r;   r   r   r)   )rE   r?   r~   r   r   r     s   FrL   T)
r>   r{   r|   ra   lenr_   rJ   r   r   r   )r!   rE   r~   r?   rC   newr   )rE   r?   r~   r   unregisterSubscriptionAdapter  s,    


z(Components.unregisterSubscriptionAdapterc             C   s   | j ||S )N)r_   subscribers)r!   r   r?   r   r   r   r     s    zComponents.subscribersc             C   sZ   |rt dt||}| j||||f | j|d | |rVttt| |||| d S )Nz$Named handlers are not yet supported)	r>   r|   rb   r(   r_   rB   r   r   HandlerRegistration)r!   rE   r~   rC   rD   rn   r   r   r   registerHandler  s    
zComponents.registerHandlerc             c   s$   x| j D ]}t| f| V  qW d S )N)rb   r   )r!   r*   r   r   r   registeredHandlers  s    zComponents.registeredHandlersc                s   |rt dd kr$ d kr$t dt  d krLfdd| jD }n fdd| jD }t|t| jkrxdS || jd d < | jd   ttt| | d dS )	Nz'Named subscribers are not yet supportedz(Must specify one of factory and requiredc                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   r   r   r   r)   )rE   r~   r   r   r     s   
FrL   T)	r>   r|   rb   r   r_   rJ   r   r   r   )r!   rE   r~   rC   r   r   )rE   r~   r   unregisterHandler  s"    


zComponents.unregisterHandlerc             G   s   | j |d  d S )N)r_   r   )r!   r   r   r   r   handle  s    zComponents.handleFc          	   C   s  t | j}| j}d}d}d}d}d|jks.tdd |_|rJ|j}|j}	ndd  }}	zx| D ]t\\}
}\}}}|	d|
||kr|d|
|| |d7 }n|d7 }|
d|
|dkr|d7 }|	d|
| qb|d7 }qbW W d|`|r|s|r|| X ||||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   s   d S )Nr   )r<   r   r   r   rj     r   zAComponents.rebuildUtilityRegistryFromLocalCache.<locals>.<lambda>c              W   s   d S )Nr   )argsr   r   r   rj   $  r   r   r=   N)needed_registereddid_not_registerneeded_subscribeddid_not_subscribe)dictr6   r8   re   rN   r   rA   rB   r   Z
registeredrF   )r!   Zrebuildregsutilsr   r   r   r   rA   rB   r?   rC   value_info_factoryr   r   r   $rebuildUtilityRegistryFromLocalCache  s:    


z/Components.rebuildUtilityRegistryFromLocalCache)rL   r   )NNrL   rL   TN)NNrL   N)rL   N)rL   )NNrL   rL   T)NNNrL   )rL   N)rL   )rL   N)rL   )NNrL   rL   T)NNNrL   )NrL   rL   T)NNrL   )F)'r.   r/   r0   rS   r"   rV   r]   rO   rP   propertyrd   rg   ri   rR   rG   rK   rq   rt   rv   ry   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   )rU   r   r      s\   


 
 


 
 


 

	  
 
)  

"c             C   s    y| j S  tk
r   dS X d S )NrL   )Z__component_name__AttributeError)r$   r   r   r   rl   ?  s    rl   c             C   s,   t t| }t|dkr |d S tdd S )Nr=   r   zWThe utility doesn't provide a single interface and no provided interface was specified.)r^   r   r   r>   )r$   r?   r   r   r   rk   E  s
    rk   c             C   s,   t t| }t|dkr |d S tdd S )Nr=   r   zaThe adapter factory doesn't implement a single interface and no provided interface was specified.)r^   r   r   r>   )rE   r?   r   r   r   r{   M  s
    r{   c             C   s   |d kr2y
| j }W qD tk
r.   tdY qDX nt|rDtdg }xR|D ]J}|d kr`t}n.t|st|tr~t|}ntdt	| |
| qNW 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   rM   r   r   typer(   rQ   )rE   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 )rm   Nc             C   s,   ||||||f\| _ | _| _| _| _| _d S )N)registryr?   rC   r$   rD   rE   )r!   r   r?   rC   r$   docrE   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)r.   )
rU   r.   r   getattrr?   rC   r$   reprrE   rD   )r!   r   r   r   rV   y  s    zUtilityRegistration.__repr__c             C   s   t | S )N)id)r!   r   r   r   __hash__  s    zUtilityRegistration.__hash__c             C   s   t | t |kS )N)r   )r!   otherr   r   r   __eq__  s    zUtilityRegistration.__eq__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   __ne__  s    zUtilityRegistration.__ne__c             C   s   t | t |k S )N)r   )r!   r   r   r   r   __lt__  s    zUtilityRegistration.__lt__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   __le__  s    zUtilityRegistration.__le__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   __gt__  s    zUtilityRegistration.__gt__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   __ge__  s    zUtilityRegistration.__ge__)N)r.   r/   r0   r"   rV   r   r   r   r   r   r   r   r   r   r   r   rm   q  s   
	rm   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 )N)r   r~   r?   rC   rE   rD   )r!   r   r~   r?   rC   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)[z, c             S   s   g | ]
}|j qS r   )r.   )r   r   r   r   r   r     s    z0AdapterRegistration.__repr__.<locals>.<listcomp>]r.   )rU   r.   r   joinr~   r   r?   rC   rE   r   rD   )r!   r   r   r   rV     s    zAdapterRegistration.__repr__c             C   s   t | S )N)r   )r!   r   r   r   r     s    zAdapterRegistration.__hash__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   r     s    zAdapterRegistration.__eq__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   r     s    zAdapterRegistration.__ne__c             C   s   t | t |k S )N)r   )r!   r   r   r   r   r     s    zAdapterRegistration.__lt__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   r     s    zAdapterRegistration.__le__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   r     s    zAdapterRegistration.__gt__c             C   s   t | t |kS )N)r   )r!   r   r   r   r   r     s    zAdapterRegistration.__ge__N)r.   r/   r0   r"   rV   r   r   r   r   r   r   r   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 )N)r   r~   rC   handlerrD   )r!   r   r~   rC   r   r   r   r   r   r"     s    zHandlerRegistration.__init__c             C   s   | j S )N)r   )r!   r   r   r   rE     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   z, c             S   s   g | ]
}|j qS r   )r.   )r   r   r   r   r   r     s    z0HandlerRegistration.__repr__.<locals>.<listcomp>r   r.   )
rU   r.   r   r   r~   rC   r   rE   r   rD   )r!   r   r   r   rV     s    zHandlerRegistration.__repr__)r.   r/   r0   r"   r   rE   r?   rV   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   Zzope.interface._compatr   r   __all__r   r   r2   r3   r   rl   rk   r{   r|   rm   r}   r   r   r   r   r   r   <module>   sV   C   /%%