3 [Z @sdZddlmZyddlmZWnek r<ddZYnXddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZGdddeZddZGdddeZee 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)notifycOsdS)N)argkwrr /usr/lib64/python3.6/registry.pyrsr)ISpecification)ComponentLookupError)IAdapterRegistration) IComponents)IHandlerRegistration) ISubscriptionAdapterRegistration)IUtilityRegistration) Registered) Unregistered) Interface) implementedBy) implementer)implementer_only) providedBy)AdapterRegistry) CLASS_TYPES) STRING_TYPESc@s,eZdZddZddZddZddZd S) _UnhashableComponentCountercCsdd|jD|_dS)NcSsg|]}|qSrr).0itemrrr 0sz8_UnhashableComponentCounter.__init__..)items_data)selfZ otherdictrrr__init__.sz$_UnhashableComponentCounter.__init__cCs$x|jD]\}}||kr|SqWdS)Nr)r)rkey componentcountrrr __getitem__2sz'_UnhashableComponentCounter.__getitem__cCsJx4t|jD]&\}}|d|kr ||f|j|<dSq W|jj||fdS)Nr) enumeraterappend)rr"r#idatarrr __setitem__8s  z'_UnhashableComponentCounter.__setitem__cCs<x.t|jD] \}}|d|kr |j|=dSq Wt|dS)Nr)r%rKeyError)rr"r'r(rrr __delitem__?s  z'_UnhashableComponentCounter.__delitem__N)__name__ __module__ __qualname__r r$r)r+rrrrr+srcCsttS)N)rintrrrr_defaultdict_intFsr0c@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)_UtilityRegistrationscCs"tt|_||_||_|jdS)N)rr0_cache _utilities_utility_registrations%_UtilityRegistrations__populate_cache)r utilitiesZutility_registrationsrrrr Ks z_UtilityRegistrations.__init__cCs8x2t|jjD] \\}}}|d}|j||qWdS)Nr)iterr4r$_UtilityRegistrations__cache_utility)rp_r(r"rrrZ__populate_cacheSsz&_UtilityRegistrations.__populate_cachec Cs\y|j||d7<Wn<tk rVt|j|}|j|<||d7<YnXdS)N)r2 TypeErrorr)rprovidedr"ZprovrrrZ__cache_utilityXs z%_UtilityRegistrations.__cache_utilitycCs:|j|}||}|d8}|dkr*||=n|||<|dkS)Nr;r)r2)rr=r"r#rrrZ__uncache_utilityas z'_UtilityRegistrations.__uncache_utilityc Cs,y|j||dkStk r&dSXdS)NrF)r2r<)rr=r"rrr_is_utility_subscribedpsz,_UtilityRegistrations._is_utility_subscribedcCsV|j||}|||f|j||f<|jjf||||sF|jjf|||j||dS)N)r>r4r3register subscriber8)rr=namer"infofactory subscribedrrrregisterUtilityws  z%_UtilityRegistrations.registerUtilitycCs@|j||f=|jjf|||j||}|s<|jjf||dS)N)r4r3 unregister&_UtilityRegistrations__uncache_utility unsubscribe)rr=rAr"rDrrrunregisterUtilitys   z'_UtilityRegistrations.unregisterUtilityN) r,r-r.r r5r8rGr>rErIrrrrr1Is  r1csHeZdZdZdffddZddZfddZd d Zd d Ze d dZ ddZ ddZ e ddddZ dEddZdFddZddZdGddZdHdd Zd!d"Zd#d$ZdId%d&ZdJd'd(Zd)d*ZdKd+d,ZdLd-d.ZdMd/d0ZdNd1d2Zd3d4ZdOd5d6Zd7d8ZdPd9d:Zd;d<Z dQd=d>Z!d?d@Z"dRdAdBZ#dCdDZ$Z%S)S ComponentsNcCs8t|tst||_|j|jt||_d|_dS)N) isinstancerAssertionErrorr,_init_registries_init_registrationstuple __bases___v_utility_registrations_cache)rrAbasesrrrr s  zComponents.__init__cCsd|jj|jfS)Nz<%s %s>) __class__r,)rrrr__repr__szComponents.__repr__cs<tt|j}dd|djD}t|}||d<t|S)NcSs i|]\}}|jds||qS)Z_v_) startswith)rkvrrr sz)Components.__reduce__..)superrJ __reduce__rlistrP)rZ reductionZfiltered_state)rTrrr\s zComponents.__reduce__cCst|_t|_dS)N)radaptersr6)rrrrrNszComponents._init_registriescCsi|_i|_g|_g|_dS)N)r4_adapter_registrations_subscription_registrations_handler_registrations)rrrrrOszComponents._init_registrationscCs>|j}|dks&|j|jk s&|j|jk r:t|j|j}|_|S)N)rRr3r6r4r1)rcacherrr_utility_registrations_caches  z'Components._utility_registrations_cachecCs|jjdfS)NrQ)__dict__get)rrrr _getBasesszComponents._getBasescCs>tdd|D|j_tdd|D|j_t||jd<dS)NcSsg|] }|jqSr)r^)rbaserrrrsz(Components._setBases..cSsg|] }|jqSr)r6)rrgrrrrsrQ)rPr^rQr6rd)rrSrrr _setBasess zComponents._setBasescCs|jS)N)rf)rrrrszComponents.cCs |j|S)N)rh)rrSrrrrisTc Cs|r|rtd|}|dkr&t|}|dkr6t|}|jj||f}|dk rx|dd||fkrfdS|j|d|||jj||||||rtt t ||||||dS)Nz$Can't specify factory and component.rKrZr) r<_getUtilityProvided_getNamer4rerIrcrErrUtilityRegistration)rr"r=rArBeventrCZregrrrrEs& zComponents.registerUtilitycCs|r|rtd|}|dkr6|dkr.tdt|}|jj||f}|dksb|dk rf||dkrfdS|dkrv|d}|jj|||ttt||||f|dddS)Nz$Can't specify factory and component.z3Must specify one of component, factory and providedrFr;T) r<rjr4rercrIrrrl)rr"r=rArColdrrrrIs&   zComponents.unregisterUtilityccs8x2t|jjD] \\}}}t|||f|VqWdS)N)r7r4rrl)rr=rAr(rrrregisteredUtilitiesszComponents.registeredUtilitiescCs|jjf|||S)N)r6lookup)rr=rAdefaultrrr queryUtilityszComponents.queryUtilitycCs&|jjf||}|dkr"t|||S)N)r6rpr )rr=rAutilityrrr getUtilitys zComponents.getUtilityccs*x$|jjf|D]\}}||fVqWdS)N)r6 lookupAll)r interfacerArsrrrgetUtilitiesFor!szComponents.getUtilitiesForcCs|jjf|S)N)r6Z subscriptions)rrvrrrgetAllUtilitiesRegisteredFor%sz'Components.getAllUtilitiesRegisteredForc Csr|dkrt|}t||}|dkr*t|}||f|j|||f<|jj|||||rnttt||||||dS)NrK) _getAdapterProvided_getAdapterRequiredrkr_r^r?rrAdapterRegistration)rrCrequiredr=rArBrmrrrregisterAdapter(s  zComponents.registerAdaptercCs|dkr |dkrtdt|}|dkr8|dkr8tdt||}|jj|||f}|dksp|dk rt||dkrtdS|j|||f=|jj|||ttt ||||f|dS)Nz(Must specify one of factory and providedz(Must specify one of factory and requiredrFT) r<ryrzr_rer^rFrrr{)rrCr|r=rArnrrrunregisterAdapter:s"    zComponents.unregisterAdapterccs>x8t|jjD]&\\}}}\}}t||||||VqWdS)N)r7r_rr{)rr|r=rAr"rBrrrregisteredAdaptersUs  zComponents.registeredAdapterscCs|jj||||S)N)r^ queryAdapter)robjectrvrArqrrrr[szComponents.queryAdaptercCs(|jj|||}|dkr$t||||S)N)r^rr )rrrvrAadapterrrr getAdapter^s zComponents.getAdaptercCs|jj||||S)N)r^queryMultiAdapter)robjectsrvrArqrrrrdszComponents.queryMultiAdaptercCs(|jj|||}|dkr$t||||S)N)r^rr )rrrvrArrrrgetMultiAdapteris zComponents.getMultiAdapterccsDx>|jjttt||D]"\}}||}|dk r||fVqWdS)N)r^rur]mapr)rrr=rArCrrrr getAdaptersos  zComponents.getAdaptersc Csn|r td|dkrt|}t||}|jj|||||f|jj||||rjttt ||||||dS)Nz'Named subscribers are not yet supported) r<ryrzr`r&r^r@rrSubscriptionRegistration)rrCr|r=rArBrmrrrregisterSubscriptionAdapterws  z&Components.registerSubscriptionAdapterccs$x|jD]}t|f|VqWdS)N)r`r)rr(rrrregisteredSubscriptionAdapterss z)Components.registeredSubscriptionAdaptersc s|r tddkr,dkr$tdtdkrDdkrDtdtdkrnfdd|jD}nfdd|jD}t|t|jkrdS||jdd<|jjttt ||dd S) Nz'Named subscribers are not yet supportedz(Must specify one of factory and providedz(Must specify one of factory and requiredcs4g|],\}}}}}|ko |ks|||||fqSrr)rrr9nfr')r=r|rrrs z.cs<g|]4\}}}}}|ko(|ko(|ks|||||fqSrr)rrr9rrr')rCr=r|rrrs FrKT) r<ryrzr`lenr^rHrrr)rrCr|r=rAnewr)rCr=r|runregisterSubscriptionAdapters,      z(Components.unregisterSubscriptionAdaptercCs|jj||S)N)r^ subscribers)rrr=rrrrszComponents.subscriberscCsZ|r tdt||}|jj||||f|jj|d||rVttt|||||dS)Nz$Named handlers are not yet supported) r<rzrar&r^r@rrHandlerRegistration)rrCr|rArBrmrrrregisterHandlers zComponents.registerHandlerccs$x|jD]}t|f|VqWdS)N)rar)rr(rrrregisteredHandlerss zComponents.registeredHandlerscs|r tddkr$dkr$tdtdkrLfdd|jD}nfdd|jD}t|t|jkrxdS||jdd<|jjdttt||ddS) Nz'Named subscribers are not yet supportedz(Must specify one of factory and requiredcs(g|] \}}}}|kr||||fqSrr)rrrrr')r|rrrs z0Components.unregisterHandler..cs0g|](\}}}}|ko|ks||||fqSrr)rrrrr')rCr|rrrs FrKT) r<rzrarr^rHrrr)rrCr|rArr)rCr|runregisterHandlers"     zComponents.unregisterHandlercGs|jj|ddS)N)r^r)rrrrrhandleszComponents.handle)NNrKrKTN)NNrKN)rKN)rK)NNrKrKT)NNNrK)rKN)rK)rKN)rK)NNrKrKT)NNNrK)NrKrKT)NNrK)&r,r-r.rRr rUr\rNrOpropertyrcrfrhrQrErIrorrrtrwrxr}r~rrrrrrrrrrrrrr __classcell__rr)rTrrJsZ                 )  "rJc Cs y|jStk rdSXdS)NrK)Z__component_name__AttributeError)r"rrrrksrkcCs,tt|}t|dkr |dStddS)Nr;rzWThe utility doesn't provide a single interface and no provided interface was specified.)r]rrr<)r"r=rrrrjs   rjcCs,tt|}t|dkr |dStddS)Nr;rzaThe adapter factory doesn't implement a single interface and no provided interface was specified.)r]rrr<)rCr=rrrrys   ryc Cs|dkr2y |j}WqDtk r.tdYqDXntj|rDtdg}xJ|D]B}|dkr`t}n&tj|st|tr~t|}ntd|j |qNWt |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__rr<rrrrLrrr&rP)rCr|resultrrrrrz s$      rzc@sVeZdZdddZddZddZdd Zd d Zd d ZddZ ddZ ddZ dS)rlNcCs,||||||f\|_|_|_|_|_|_dS)N)registryr=rAr"rBrC)rrr=rAr"docrCrrrr ,szUtilityRegistration.__init__c Cs<d|jj|jt|jdd|jt|jdt|j|j|j fS)Nz%s(%r, %s, %r, %s, %r, %r)r,) rTr,rgetattrr=rAr"reprrCrB)rrrrrU1s zUtilityRegistration.__repr__cCst|S)N)id)rrrr__hash__:szUtilityRegistration.__hash__cCst|t|kS)N)r)rotherrrr__eq__=szUtilityRegistration.__eq__cCst|t|kS)N)r)rrrrr__ne__@szUtilityRegistration.__ne__cCst|t|kS)N)r)rrrrr__lt__CszUtilityRegistration.__lt__cCst|t|kS)N)r)rrrrr__le__FszUtilityRegistration.__le__cCst|t|kS)N)r)rrrrr__gt__IszUtilityRegistration.__gt__cCst|t|kS)N)r)rrrrr__ge__LszUtilityRegistration.__ge__)N) r,r-r.r rUrrrrrrrrrrrrl)s  rlc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)r{cCs,||||||f\|_|_|_|_|_|_dS)N)rr|r=rArCrB)rrr|r=rAr"rrrrr RszAdapterRegistration.__init__c CsTd|jj|jddjdd|jDdt|jdd|jt|jdt |j|j fS)Nz%s(%r, %s, %s, %r, %s, %r)[z, cSsg|] }|jqSr)r,)rrrrrr[sz0AdapterRegistration.__repr__..]r,) rTr,rjoinr|rr=rArCrrB)rrrrrUWs zAdapterRegistration.__repr__cCst|S)N)r)rrrrr`szAdapterRegistration.__hash__cCst|t|kS)N)r)rrrrrrcszAdapterRegistration.__eq__cCst|t|kS)N)r)rrrrrrfszAdapterRegistration.__ne__cCst|t|kS)N)r)rrrrrriszAdapterRegistration.__lt__cCst|t|kS)N)r)rrrrrrlszAdapterRegistration.__le__cCst|t|kS)N)r)rrrrrroszAdapterRegistration.__gt__cCst|t|kS)N)r)rrrrrrrszAdapterRegistration.__ge__N) r,r-r.r rUrrrrrrrrrrrr{Os r{c@s eZdZdS)rN)r,r-r.rrrrrusrc@s,eZdZddZeddZdZddZdS)rcCs&|||||f\|_|_|_|_|_dS)N)rr|rAhandlerrB)rrr|rArrrrrr }szHandlerRegistration.__init__cCs|jS)N)r)rrrrrCszHandlerRegistration.factoryNc CsHd|jj|jddjdd|jDd|jt|jdt|j|j fS)Nz%s(%r, %s, %r, %s, %r)rz, cSsg|] }|jqSr)r,)rrrrrrsz0HandlerRegistration.__repr__..rr,) rTr,rrr|rArrCrrB)rrrrrUs zHandlerRegistration.__repr__)r,r-r.r rrCr=rUrrrrrzs rN))__doc__ collectionsrZ zope.eventr ImportErrorZzope.interface.interfacesrr r r r r rrrZzope.interface.interfacerZzope.interface.declarationsrrrrZzope.interface.adapterrZzope.interface._compatrrrrr0r1rJrkrjryrzrlr{rrrrrrsR                  Cl%%