
|WY                 @   sq  d  Z  d d l m Z d d l Z y d d l m Z Wn e k
 rV d d   Z Yn Xd 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 Gd d   d e  Z Gd d   d e  Z e e  Gd d   d e   Z  d d   Z! d  d!   Z" d" d#   Z# d$ d%   Z$ e e  Gd& d'   d' e   Z% e e
  Gd( d)   d) e   Z& e e  Gd* d+   d+ e&   Z' e e  Gd, d-   d- e&   Z( d S).zBasic components support
    )defaultdictN)notifyc              O   s   d  S)N )argkwr   r   9/usr/lib/python3/dist-packages/zope/interface/registry.pyr      s    r   )ISpecification)ComponentLookupError)IAdapterRegistration)IComponents)IHandlerRegistration) ISubscriptionAdapterRegistration)IUtilityRegistration)
Registered)Unregistered)	Interface)implementedBy)implementer)implementer_only)
providedBy)AdapterRegistry)CLASS_TYPES)STRING_TYPESc               @   s@   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 S)
_UnhashableComponentCounterc             C   s    d d   | j    D |  _ d  S)Nc             S   s   g  |  ] } |  q Sr   r   ).0itemr   r   r   
<listcomp>1   s   	 z8_UnhashableComponentCounter.__init__.<locals>.<listcomp>)items_data)selfZ	otherdictr   r   r   __init__/   s    z$_UnhashableComponentCounter.__init__c             C   s.   x' |  j  D] \ } } | | k r
 | Sq
 Wd S)Nr   )r   )r   key	componentcountr   r   r   __getitem__3   s    z'_UnhashableComponentCounter.__getitem__c             C   sa   xD t  |  j  D]3 \ } } | d | k r | | f |  j | <d  Sq W|  j j | | f  d  S)Nr   )	enumerater   append)r   r"   r#   idatar   r   r   __setitem__9   s
    z'_UnhashableComponentCounter.__setitem__c             C   sN   x; t  |  j  D]* \ } } | d | k r |  j | =d  Sq Wt |   d  S)Nr   )r%   r   KeyError)r   r"   r'   r(   r   r   r   __delitem__@   s
    
z'_UnhashableComponentCounter.__delitem__N)__name__
__module____qualname__r    r$   r)   r+   r   r   r   r   r   ,   s   r   c               @   s   e  Z d  Z i  Z i  Z e d d    Z e 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)_UtilityRegistrationsc                s   t  |   y   j  } Wn t k
 r7 d  } Yn+ X| j | j k	 s\ | j | j k	 rb d  } | d  k r   | j | j  } |   j  <   j k r    f d d   } t j | |    j  <| S)Nc                s$     j  j     j j   d  S)N)_weakrefs_for_componentspop_regs_for_components)r)clsr!   r   r   _cleanupd   s    z6_UtilityRegistrations.for_components.<locals>._cleanup)	idr2   r*   
_utilities	utilities_utility_registrationsr0   weakrefref)r4   Z
componentsregsr5   r   )r4   r!   r   for_componentsM   s    z$_UtilityRegistrations.for_componentsc             C   s   |  j  j   d  S)N)r2   clear)r4   r   r   r   clear_cachel   s    z!_UtilityRegistrations.clear_cachec             C   s5   t  d d    |  _ | |  _ | |  _ |  j   d  S)Nc               S   s
   t  t  S)N)r   intr   r   r   r   <lambda>r   s    z0_UtilityRegistrations.__init__.<locals>.<lambda>)r   _cacher7   r9   %_UtilityRegistrations__populate_cache)r   r8   Zutility_registrationsr   r   r   r    p   s    		z_UtilityRegistrations.__init__c             C   sJ   xC t  |  j j    D], \ \ } } } | d } |  j | |  q Wd  S)Nr   )iterr9   r   $_UtilityRegistrations__cache_utility)r   p_r(   r"   r   r   r   Z__populate_cachex   s    (
z&_UtilityRegistrations.__populate_cachec             C   s^   y |  j  | | d 7<Wn> t k
 rY t |  j  |  } |  j  | <| | d 7<Yn Xd  S)N   )rB   	TypeErrorr   )r   providedr"   Zprovr   r   r   Z__cache_utility}   s
    z%_UtilityRegistrations.__cache_utilityc             C   sK   |  j  | } | | } | d 8} | d k r7 | | =n
 | | | <| d k S)NrH   r   )rB   )r   rJ   r"   r#   r   r   r   Z__uncache_utility   s    



z'_UtilityRegistrations.__uncache_utilityc             C   s6   y |  j  | | d k SWn t k
 r1 d SYn Xd  S)Nr   F)rB   rI   )r   rJ   r"   r   r   r   _is_utility_subscribed   s    z,_UtilityRegistrations._is_utility_subscribedc             C   sw   |  j  | |  } | | | f |  j | | f <|  j j f  | | |  | sc |  j j f  | |  |  j | |  d  S)N)rK   r9   r7   register	subscriberE   )r   rJ   namer"   infofactory
subscribedr   r   r   registerUtility   s    z%_UtilityRegistrations.registerUtilityc             C   sX   |  j  | | f =|  j j f  | |  |  j | |  } | sT |  j j f  | |  d  S)N)r9   r7   
unregister&_UtilityRegistrations__uncache_utilityunsubscribe)r   rJ   rN   r"   rQ   r   r   r   unregisterUtility   s
    z'_UtilityRegistrations.unregisterUtilityN)r,   r-   r.   r2   r0   classmethodr=   r?   r    rC   rE   rT   rK   rR   rV   r   r   r   r   r/   H   s   	r/   c               @   s  e  Z d  Z d f  d d  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z e	 d d   d d    Z
 d d d d d d d d  Z d d d d d d  Z d d   Z d d d d  Z d d d  Z d d   Z d d    Z d d d d d d! d"  Z d d d d d# d$  Z d% d&   Z d d d' d(  Z d d) d*  Z d d d+ d,  Z d d- d.  Z d/ d0   Z d d d d d d1 d2  Z d3 d4   Z d d d d d5 d6  Z d7 d8   Z d d d d d9 d:  Z d; d<   Z d d d d= d>  Z  d? d@   Z! d S)A
Components c             C   sE   t  | t  s t  | |  _ |  j   |  j   t |  |  _ d  S)N)
isinstancer   AssertionErrorr,   _init_registries_init_registrationstuple	__bases__)r   rN   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             C   s   t    |  _ t    |  _ d  S)N)r   adaptersr8   )r   r   r   r   r\      s    zComponents._init_registriesc             C   s(   i  |  _  i  |  _ g  |  _ g  |  _ d  S)N)r9   _adapter_registrations_subscription_registrations_handler_registrations)r   r   r   r   r]      s    			zComponents._init_registrationsc             C   s   |  j  j d f   S)Nr_   )__dict__get)r   r   r   r   	_getBases   s    zComponents._getBasesc             C   sU   t  d d   | D  |  j _ t  d d   | D  |  j _ t  |  |  j d <d  S)Nc             S   s   g  |  ] } | j   q Sr   )rc   )r   baser   r   r   r      s   	 z(Components._setBases.<locals>.<listcomp>c             S   s   g  |  ] } | j   q Sr   )r8   )r   rj   r   r   r   r      s   	 r_   )r^   rc   r_   r8   rg   )r   r`   r   r   r   	_setBases   s
    zComponents._setBasesc             C   s
   |  j    S)N)ri   )r   r   r   r   rA      s    zComponents.<lambda>c             C   s   |  j  |  S)N)rk   )r   r`   r   r   r   rA      s    NTc          	   C   s   | r! | r t  d   |   } | d  k r9 t |  } | d k rQ t |  } |  j j | | f  } | d  k	 r | d  d  | | f k r d  S|  j | d | |  t j |   j | | | | |  | r t	 t
 t |  | | | | |    d  S)Nz$Can't specify factory and component.rY      r   )rI   _getUtilityProvided_getNamer9   rh   rV   r/   r=   rR   r   r   UtilityRegistration)r   r"   rJ   rN   rO   eventrP   Zregr   r   r   rR      s$    	"zComponents.registerUtilityc          
   C   s   | r! | r t  d   |   } | d  k rQ | d  k rE t  d   t |  } |  j j | | f  } | d  k s | d  k	 r | | d k r d S| d  k r | d } t j |   j | | |  t t t	 |  | | | | d d      d S)Nz$Can't specify factory and component.z3Must specify one of component, factory and providedr   FrH   T)
rI   rm   r9   rh   r/   r=   rV   r   r   ro   )r   r"   rJ   rN   rP   oldr   r   r   rV      s$    	
&zComponents.unregisterUtilityc             c   sD   x= t  |  j j    D]& \ \ } } } t |  | | |  Vq Wd  S)N)rD   r9   r   ro   )r   rJ   rN   r(   r   r   r   registeredUtilities  s    %zComponents.registeredUtilitiesc             C   s   |  j  j f  | | |  S)N)r8   lookup)r   rJ   rN   defaultr   r   r   queryUtility  s    zComponents.queryUtilityc             C   s7   |  j  j f  | |  } | d  k r3 t | |   | S)N)r8   rs   r	   )r   rJ   rN   utilityr   r   r   
getUtility  s    zComponents.getUtilityc             c   s5   x. |  j  j f  |  D] \ } } | | f Vq Wd  S)N)r8   	lookupAll)r   	interfacerN   rv   r   r   r   getUtilitiesFor!  s    "zComponents.getUtilitiesForc             C   s   |  j  j f  |  S)N)r8   Zsubscriptions)r   ry   r   r   r   getAllUtilitiesRegisteredFor%  s    z'Components.getAllUtilitiesRegisteredForc          	   C   s   | d  k r t  |  } t | |  } | d k r? t |  } | | f |  j | | | f <|  j j | | | |  | r t t t |  | | | | |    d  S)NrY   )	_getAdapterProvided_getAdapterRequiredrn   rd   rc   rL   r   r   AdapterRegistration)r   rP   requiredrJ   rN   rO   rp   r   r   r   registerAdapter(  s    zComponents.registerAdapterc             C   s   | d  k r0 | d  k r$ t  d   t |  } | d  k rT | d  k rT t  d   t | |  } |  j j | | | f  } | d  k s | d  k	 r | | d k r d S|  j | | | f =|  j j | | |  t t t	 |  | | | |    d S)Nz(Must specify one of factory and providedz(Must specify one of factory and requiredr   FT)
rI   r|   r}   rd   rh   rc   rS   r   r   r~   )r   rP   r   rJ   rN   rq   r   r   r   unregisterAdapter:  s"    zComponents.unregisterAdapterc             c   sS   xL t  |  j j    D]5 \ \ } } } \ } } t |  | | | | |  Vq Wd  S)N)rD   rd   r   r~   )r   r   rJ   rN   r"   rO   r   r   r   registeredAdaptersU  s    .zComponents.registeredAdaptersc             C   s   |  j  j | | | |  S)N)rc   queryAdapter)r   objectry   rN   rt   r   r   r   r   [  s    zComponents.queryAdapterc             C   s:   |  j  j | | |  } | d  k r6 t | | |   | S)N)rc   r   r	   )r   r   ry   rN   adapterr   r   r   
getAdapter^  s    zComponents.getAdapterc             C   s   |  j  j | | | |  S)N)rc   queryMultiAdapter)r   objectsry   rN   rt   r   r   r   r   d  s    	zComponents.queryMultiAdapterc             C   s:   |  j  j | | |  } | d  k r6 t | | |   | S)N)rc   r   r	   )r   r   ry   rN   r   r   r   r   getMultiAdapteri  s    zComponents.getMultiAdapterc             c   s\   xU |  j  j t t t |   |  D]/ \ } } | |   } | d  k	 r% | | f Vq% Wd  S)N)rc   rx   listmapr   )r   r   rJ   rN   rP   r   r   r   r   getAdapterso  s    zComponents.getAdaptersc          	   C   s   | r t  d   | d  k r* t |  } t | |  } |  j j | | | | | f  |  j j | | |  | r t t t	 |  | | | | |    d  S)Nz'Named subscribers are not yet supported)
rI   r|   r}   re   r&   rc   rM   r   r   SubscriptionRegistration)r   rP   r   rJ   rN   rO   rp   r   r   r   registerSubscriptionAdapterw  s    	z&Components.registerSubscriptionAdapterc             c   s&   x |  j  D] } t |  |  Vq
 Wd  S)N)re   r   )r   r(   r   r   r   registeredSubscriptionAdapters  s    z)Components.registeredSubscriptionAdaptersc          	      s6  | r t  d    d  k rB   d  k r6 t  d   t      d  k rf   d  k rf t  d   t        d  k r   f d d   |  j D } n"     f d d   |  j D } t |  t |  j  k r d S| |  j d  d   <|  j 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                sL   g  |  ]B \ } } } } } |  k o0 |   k s | | | | | f  q Sr   r   )r   r3   rF   nfr'   )rJ   r   r   r   r     s   	z<Components.unregisterSubscriptionAdapter.<locals>.<listcomp>c                sX   g  |  ]N \ } } } } } |  k o< |  k o< |   k s | | | | | f  q Sr   r   )r   r3   rF   r   r   r'   )rP   rJ   r   r   r   r     s   	FrY   T)
rI   r|   r}   re   lenrc   rU   r   r   r   )r   rP   r   rJ   rN   newr   )rP   rJ   r   r   unregisterSubscriptionAdapter  s,    z(Components.unregisterSubscriptionAdapterc             C   s   |  j  j | |  S)N)rc   subscribers)r   r   rJ   r   r   r   r     s    zComponents.subscribersc             C   s   | r t  d   t | |  } |  j j | | | | f  |  j j | d  |  | r{ t t t |  | | | |    d  S)Nz$Named handlers are not yet supported)	rI   r}   rf   r&   rc   rM   r   r   HandlerRegistration)r   rP   r   rN   rO   rp   r   r   r   registerHandler  s    	zComponents.registerHandlerc             c   s&   x |  j  D] } t |  |  Vq
 Wd  S)N)rf   r   )r   r(   r   r   r   registeredHandlers  s    zComponents.registeredHandlersc                s   | r t  d    d  k r6   d  k r6 t  d   t        d  k rp  f d d   |  j D } n    f d d   |  j D } t |  t |  j  k r d S| |  j d  d   <|  j j  d     t t t |   |   d    d S)	Nz'Named subscribers are not yet supportedz(Must specify one of factory and requiredc                s:   g  |  ]0 \ } } } } |   k r | | | | f  q Sr   r   )r   r3   r   r   r'   )r   r   r   r     s   	z0Components.unregisterHandler.<locals>.<listcomp>c                sF   g  |  ]< \ } } } } |  k o- |   k s | | | | f  q Sr   r   )r   r3   r   r   r'   )rP   r   r   r   r     s   	FrY   T)	rI   r}   rf   r   rc   rU   r   r   r   )r   rP   r   rN   r   r   )rP   r   r   unregisterHandler  s"    zComponents.unregisterHandlerc             G   s   |  j  j | d   d  S)N)rc   r   )r   r   r   r   r   handle  s    zComponents.handle)"r,   r-   r.   r    rb   r\   r]   ri   rk   propertyr_   rR   rV   rr   ru   rw   rz   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rX      sT   				)"rX   c             C   s(   y |  j  SWn t k
 r# d SYn Xd  S)NrY   )Z__component_name__AttributeError)r"   r   r   r   rn     s    rn   c             C   s<   t  t |    } t |  d k r, | d St d   d  S)NrH   r   zWThe utility doesn't provide a single interface and no provided interface was specified.)r   r   r   rI   )r"   rJ   r   r   r   rm     s
    rm   c             C   s<   t  t |    } t |  d k r, | d St d   d  S)NrH   r   zaThe adapter factory doesn't implement a single interface and no provided interface was specified.)r   r   r   rI   )rP   rJ   r   r   r   r|     s
    r|   c             C   s   | d  k r= y |  j  } WqX t k
 r9 t d   YqX Xn t j |  rX t d   g  } xi | D]a } | d  k r t } n9 t j |  s t | t  r t |  } n t d   | j	 |  qe Wt
 |  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 interfacez8Required specification must be a specification or class.)Z__component_adapts__r   rI   r   r   r   rZ   r   r   r&   r^   )rP   r   resultr3   r   r   r   r}     s$    	r}   c               @   s   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)ro   Nc             C   s@   | | | | | | f \ |  _  |  _ |  _ |  _ |  _ |  _ d  S)N)registryrJ   rN   r"   rO   rP   )r   r   rJ   rN   r"   docrP   r   r   r   r    ,  s    zUtilityRegistration.__init__c          
   C   sV   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,   )
ra   r,   r   getattrrJ   rN   r"   reprrP   rO   )r   r   r   r   rb   1  s    	zUtilityRegistration.__repr__c             C   s
   t  |   S)N)r6   )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__C  s    zUtilityRegistration.__lt__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   __le__F  s    zUtilityRegistration.__le__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   __gt__I  s    zUtilityRegistration.__gt__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   __ge__L  s    zUtilityRegistration.__ge__)r,   r-   r.   r    rb   r   r   r   r   r   r   r   r   r   r   r   ro   )  s   	ro   c               @   s|   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   rJ   rN   rP   rO   )r   r   r   rJ   rN   r"   r   r   r   r   r    R  s    zAdapterRegistration.__init__c             C   st   d |  j  j |  j d d j 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 Sr   )r,   )r   r3   r   r   r   r   [  s   	 z0AdapterRegistration.__repr__.<locals>.<listcomp>]r,   )ra   r,   r   joinr   r   rJ   rN   rP   r   rO   )r   r   r   r   rb   W  s    	$zAdapterRegistration.__repr__c             C   s
   t  |   S)N)r6   )r   r   r   r   r   `  s    zAdapterRegistration.__hash__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   r   c  s    zAdapterRegistration.__eq__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   r   f  s    zAdapterRegistration.__ne__c             C   s   t  |   t  |  k  S)N)r   )r   r   r   r   r   r   i  s    zAdapterRegistration.__lt__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   r   l  s    zAdapterRegistration.__le__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   r   o  s    zAdapterRegistration.__gt__c             C   s   t  |   t  |  k S)N)r   )r   r   r   r   r   r   r  s    zAdapterRegistration.__ge__N)r,   r-   r.   r    rb   r   r   r   r   r   r   r   r   r   r   r   r~   O  s   	r~   c               @   s   e  Z d  Z d S)r   N)r,   r-   r.   r   r   r   r   r   u  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   s7   | | | | | f \ |  _  |  _ |  _ |  _ |  _ d  S)N)r   r   rN   handlerrO   )r   r   r   rN   r   r   r   r   r   r    }  s    zHandlerRegistration.__init__c             C   s   |  j  S)N)r   )r   r   r   r   rP     s    zHandlerRegistration.factoryNc          
   C   sb   d |  j  j |  j d d j 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 Sr   )r,   )r   r3   r   r   r   r     s   	 z0HandlerRegistration.__repr__.<locals>.<listcomp>r   r,   )
ra   r,   r   r   r   rN   r   rP   r   rO   )r   r   r   r   rb     s    	$zHandlerRegistration.__repr__)r,   r-   r.   r    r   rP   rJ   rb   r   r   r   r   r   z  s   r   ))__doc__collectionsr   r:   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   r   r   r/   rX   rn   rm   r|   r}   ro   r~   r   r   r   r   r   r   <module>   sP   i F%%