o $ x`e@sdZddlmZzddlmZWn eyddZYnwddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZdgZGdddeZddZGdddeZ ee GdddeZ!dd Z"d!d"Z#d#d$Z$d%d&Z%ee Gd'd(d(eZ&ee Gd)d*d*eZ'ee Gd+d,d,e'Z(ee Gd-d.d.e'Z)d/S)0zBasic components support ) defaultdict)notifycOdSN)argkwrr9/usr/lib/python3/dist-packages/zope/interface/registry.pyrr)ISpecification)ComponentLookupError)IAdapterRegistration) IComponents)IHandlerRegistration) ISubscriptionAdapterRegistration)IUtilityRegistration) Registered) Unregistered) Interface) implementedBy) implementer)implementer_only) providedBy)AdapterRegistry) CLASS_TYPES) STRING_TYPES Componentsc@s,eZdZddZddZddZddZd S) _UnhashableComponentCountercCsdd|D|_dS)NcSsg|]}|qSrr).0itemrrr 6sz8_UnhashableComponentCounter.__init__..)items_data)self otherdictrrr __init__4sz$_UnhashableComponentCounter.__init__cCs$|jD] \}}||kr|SqdSNr)r")r#key componentcountrrr __getitem__8s z'_UnhashableComponentCounter.__getitem__cCsHt|jD]\}}|d|kr||f|j|<dSq|j||fdSr&) enumerater"append)r#r(r)idatarrr __setitem__>s  z'_UnhashableComponentCounter.__setitem__cCs6t|jD]\}}|d|kr|j|=dSqt|r&)r+r"KeyError)r#r(r-r.rrr __delitem__Es  z'_UnhashableComponentCounter.__delitem__N)__name__ __module__ __qualname__r%r*r/r1rrrr r1s  rcCsttSr)rintrrrr _defaultdict_intLr6c@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)_UtilityRegistrationscCs"tt|_||_||_|dSr)rr6_cache _utilities_utility_registrations%_UtilityRegistrations__populate_cache)r# utilitiesutility_registrationsrrr r%Qs  z_UtilityRegistrations.__init__cCs4t|jD]\\}}}|d}|||qdSr&)iterr;r!$_UtilityRegistrations__cache_utility)r#p_r.r(rrr __populate_cacheYsz&_UtilityRegistrations.__populate_cachecCsZz|j||d7<WdSty,t|j|}|j|<||d7<YdSw)N)r9 TypeErrorr)r#providedr(provrrr __cache_utility^s  z%_UtilityRegistrations.__cache_utilitycCs@|j|}||}|d8}|dkr||=|dkS|||<|dkS)NrDr)r9)r#rFr(r)rrr __uncache_utilitygs z'_UtilityRegistrations.__uncache_utilitycCs*z |j||dkWStyYdSw)NrF)r9rE)r#rFr(rrr _is_utility_subscribedvs  z,_UtilityRegistrations._is_utility_subscribedcCsV|||}|||f|j||f<|jd||||s#|jd|||||dSNr)rJr;r:register subscriber@)r#rFnamer(infofactory subscribedrrr registerUtility}s z%_UtilityRegistrations.registerUtilitycCsD|j||f=|jd|||||}|s |jd||dSdSrK)r;r: unregister&_UtilityRegistrations__uncache_utility unsubscribe)r#rFrNr(rQrrr unregisterUtilitys  z'_UtilityRegistrations.unregisterUtilityN) r2r3r4r%r<r@rTrJrRrVrrrr r8Os  r8csreZdZdZdIddZddZfdd Zd d Zd d Ze ddZ ddZ ddZ e ddddZ  dJddZ  dKddZddZdLddZdMd d!Zd"d#Zd$d%Z  dNd&d'Z  dOd(d)Zd*d+ZdLd,d-ZdMd.d/Z  dLd0d1ZdMd2d3Zd4d5Z   dNd6d7Zd8d9Z  dOd:d;Zdd?Z!d@dAZ"dQdBdCZ#dDdEZ$dRdGdHZ%Z&S)SrNrcCs8t|tsJ||_||t||_d|_dSr) isinstancerr2_init_registries_init_registrationstuple __bases___v_utility_registrations_cache)r#rNbasesrrr r%s   zComponents.__init__cCsd|jj|jfS)Nz<%s %s>) __class__r2r#rrr __repr__zComponents.__repr__cs<tt|}dd|dD}t|}||d<t|S)NcSs i|] \}}|ds||qS)_v_) startswith)rkvrrr s  z)Components.__reduce__..)superr __reduce__r!listr[)r# reductionfiltered_stater_rr rjs zComponents.__reduce__cCst|_t|_dSr)radaptersr=r`rrr rYs zComponents._init_registriescCsi|_i|_g|_g|_dSr)r;_adapter_registrations_subscription_registrations_handler_registrationsr`rrr rZs zComponents._init_registrationscCs>|j}|dus|j|jus|j|jurt|j|j}|_|Sr)r]r:r=r;r8)r#cacherrr _utility_registrations_caches   z'Components._utility_registrations_cachecCs|jddS)Nr\r)__dict__getr`rrr _getBasesszComponents._getBasescCs>tdd|D|j_tdd|D|j_t||jd<dS)NcSg|]}|jqSr)rorbaserrr r z(Components._setBases..cSrxr)r=ryrrr r r{r\)r[ror\r=rur#r^rrr _setBasess  zComponents._setBasescCs|Sr)rwr`rrr szComponents.cCs ||Sr)r}r|rrr r~s Tc Cs|r |rtd|}|durt|}|dkrt|}|j||f}|dur<|dd||fkr3dS||d|||j||||||rWtt t ||||||dSdS)N$Can't specify factory and component.rWrhr) rE_getUtilityProvided_getNamer;rvrVrtrRrrUtilityRegistration)r#r(rFrNrOeventrPregrrr rRs.   zComponents.registerUtilitycCs|r |rtd|}|dur|durtdt|}|j||f}|dus1|dur3||dkr3dS|dur;|d}|j|||ttt||||g|ddRdS)Nrz3Must specify one of component, factory and providedrFrDT) rErr;rvrtrVrrr)r#r(rFrNrPoldrrr rVs* zComponents.unregisterUtilityccs8t|jD]\\}}}t|||g|RVqdSr)r?r;r!r)r#rFrNr.rrr registeredUtilitiess  zComponents.registeredUtilitiescCs|jd|||SrK)r=lookup)r#rFrNdefaultrrr queryUtilityrbzComponents.queryUtilitycCs&|jd||}|durt|||SrK)r=rr )r#rFrNutilityrrr getUtility!s zComponents.getUtilityccs(|jd|D] \}}||fVqdSrK)r= lookupAll)r# interfacerNrrrr getUtilitiesFor's zComponents.getUtilitiesForcCs|jd|SrK)r= subscriptions)r#rrrr getAllUtilitiesRegisteredFor+z'Components.getAllUtilitiesRegisteredForc Csv|durt|}t||}|dkrt|}||f|j|||f<|j|||||r9ttt||||||dSdSNrW) _getAdapterProvided_getAdapterRequiredrrprorLrrAdapterRegistrationr#rPrequiredrFrNrOrrrr registerAdapter.s   zComponents.registerAdaptercCs|dur|dur tdt|}|dur|durtdt||}|j|||f}|dus8|dur:||dkr:dS|j|||f=|j|||ttt ||||g|RdS)N(Must specify one of factory and provided(Must specify one of factory and requiredrFT) rErrrprvrorSrrr)r#rPrrFrNrrrr unregisterAdapter@s&   zComponents.unregisterAdapterccs<t|jD]\\}}}\}}t||||||VqdSr)r?rpr!r)r#rrFrNr(rOrrr registeredAdapters[s  zComponents.registeredAdapterscC|j||||Sr)ro queryAdapter)r#objectrrNrrrr rarbzComponents.queryAdaptercC(|j|||}|durt||||Sr)rorr )r#rrrNadapterrrr getAdapterd zComponents.getAdaptercCrr)roqueryMultiAdapter)r#objectsrrNrrrr rjszComponents.queryMultiAdaptercCrr)rorr )r#rrrNrrrr getMultiAdapterorzComponents.getMultiAdapterccsB|jttt||D]\}}||}|dur||fVq dSr)rorrkmapr)r#rrFrNrPrrrr getAdaptersus   zComponents.getAdaptersc Csr|rtd|durt|}t||}|j|||||f|j||||r7ttt ||||||dSdS)N'Named subscribers are not yet supported) rErrrqr,rorMrrSubscriptionRegistrationrrrr registerSubscriptionAdapter}s     z&Components.registerSubscriptionAdaptercc$|jD] }t|g|RVqdSr)rqrr#r.rrr registeredSubscriptionAdapters z)Components.registeredSubscriptionAdaptersc s|rtddurdurtdtdur"dur"tdtdur7fdd|jD}n fdd|jD}t|t|jkrNdS||jdd<|jttt ||dd S) Nrrrcs4g|]\}}}}}|kr|ks|||||fqSrrrrrAnfr-)rFrrr r s   z.cs<g|]\}}}}}|kr|kr|ks|||||fqSrrrrPrFrrr r s   FrWT) rErrrqlenrorUrrr)r#rPrrFrNnewrrr unregisterSubscriptionAdapters4   z(Components.unregisterSubscriptionAdaptercCs|j||Srro subscribers)r#rrFrrr rrzComponents.subscriberscCs^|rtdt||}|j||||f|j|d||r-ttt|||||dSdS)Nz$Named handlers are not yet supported) rErrrr,rorMrrHandlerRegistration)r#rPrrNrOrrrr registerHandlers   zComponents.registerHandlerccrr)rrrrrrr registeredHandlersrzComponents.registeredHandlerscs|rtddurdurtdtdur&fdd|jD}n fdd|jD}t|t|jkr.cs0g|]\}}}}|kr|ks||||fqSrrrrPrrr r s   FrWT) rErrrrrorUrrr)r#rPrrNrrrr unregisterHandlers(   zComponents.unregisterHandlercGs|j|ddSrr)r#rrrr handlerbzComponents.handleFc Cs"t|j}|j}d}d}d}d}d|jvsJdd|_|r%|j}|j} ndd}} zN|D]:\\} } \} } }|d| | | krN|d| | | |d7}n|d7}| d| | durf|d7}| d| | q0|d7}q0W|`|ry|st|ry||n|`|r|s|r||www||||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 rchangedcSrrr)rBrrr r~r zAComponents.rebuildUtilityRegistryFromLocalCache..cWrrr)argsrrr r~$r rrDN)needed_registereddid_not_registerneeded_subscribeddid_not_subscribe) dictr;r=rurrLrMr! registeredrQ)r#rebuildregsutilsrrrrrLrMrFrNvalue_info_factoryrrr $rebuildUtilityRegistryFromLocalCachesH         z/Components.rebuildUtilityRegistryFromLocalCache)rWr)NNrWrWTN)NNrWN)rWN)rW)NNrWrWT)NNNrW)NrWrWT)NNrW)F)'r2r3r4r]r%rarjrYrZpropertyrtrwr}r\rRrVrrrrrrrrrrrrrrrrrrrrrr __classcell__rrrnr rsr                *  "cCsz|jWStyYdSwr)__component_name__AttributeError)r(rrr r?s  rcC(tt|}t|dkr|dStd)NrDrzWThe utility doesn't provide a single interface and no provided interface was specified.)rkrrrE)r(rFrrr rE  rcCr)NrDrzaThe adapter factory doesn't implement a single interface and no provided interface was specified.)rkrrrE)rPrFrrr rMrrcCs|durz|j}Wntytdwt|rtdg}|D]%}|dur+t}nt|sBt|tr:t|}ntdt || |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__rrEr rrrXrrtyper,r[)rPrresultrrrr rUs,       rc@sVeZdZdddZddZddZdd Zd d Zd d ZddZ ddZ ddZ dS)rNcC,||||||f\|_|_|_|_|_|_dSr)registryrFrNr(rOrP)r#rrFrNr(docrPrrr r%tszUtilityRegistration.__init__c Cs<d|jj|jt|jdd|jt|jdt|j|j|j fS)Nz%s(%r, %s, %r, %s, %r, %r)r2) r_r2rgetattrrFrNr(reprrPrOr`rrr rayszUtilityRegistration.__repr__cCt|Sridr`rrr __hash__r7zUtilityRegistration.__hash__cCt|t|kSrrr#otherrrr __eq__zUtilityRegistration.__eq__cCt|t|kSrrrrrr __ne__rzUtilityRegistration.__ne__cCt|t|kSrrrrrr __lt__rzUtilityRegistration.__lt__cCt|t|kSrrrrrr __le__rzUtilityRegistration.__le__cCt|t|kSrrrrrr __gt__rzUtilityRegistration.__gt__cCt|t|kSrrrrrr __ge__rzUtilityRegistration.__ge__r r2r3r4r%rarrrrrrrrrrr rqs   rc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)rcCrr)rrrFrNrPrO)r#rrrFrNr(rrrr r%s zAdapterRegistration.__init__c CsTd|jj|jdddd|jDdt|jdd|jt|jdt |j|j fS)Nz%s(%r, %s, %s, %r, %s, %r)[, cSrxrr2rrrrr r z0AdapterRegistration.__repr__..]r2) r_r2rjoinrrrFrNrPrrOr`rrr raszAdapterRegistration.__repr__cCrrrr`rrr rr7zAdapterRegistration.__hash__cCrrrrrrr rrzAdapterRegistration.__eq__cCrrrrrrr rrzAdapterRegistration.__ne__cCrrrrrrr rrzAdapterRegistration.__lt__cCrrrrrrr rrzAdapterRegistration.__le__cCrrrrrrr rrzAdapterRegistration.__gt__cCrrrrrrr rrzAdapterRegistration.__ge__Nrrrrr rs  rc@s eZdZdS)rN)r2r3r4rrrr rsrc@s,eZdZddZeddZdZddZdS)rcCs&|||||f\|_|_|_|_|_dSr)rrrNhandlerrO)r#rrrNrrrrr r%s zHandlerRegistration.__init__cCs|jSr)rr`rrr rPszHandlerRegistration.factoryNc CsHd|jj|jdddd|jDd|jt|jdt|j|j fS)Nz%s(%r, %s, %r, %s, %r)rrcSrxrrrrrr r rz0HandlerRegistration.__repr__..rr2) r_r2rrrrNrrPrrOr`rrr raszHandlerRegistration.__repr__)r2r3r4r%rrPrFrarrrr rs   rN)*__doc__ collectionsr zope.eventr ImportErrorzope.interface.interfacesr r r rrrrrrzope.interface.interfacerzope.interface.declarationsrrrrzope.interface.adapterrzope.interface._compatrr__all__rrr6r8rrrrrrrrrrrrr s\                    C/%%