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 ComponentsNcCs*||_|j|jt||_d|_dS)N)r,_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__rlistrN)rZ reductionZfiltered_state)rRrrrZs zComponents.__reduce__cCst|_t|_dS)N)radaptersr6)rrrrrLszComponents._init_registriescCsi|_i|_g|_g|_dS)N)r4_adapter_registrations_subscription_registrations_handler_registrations)rrrrrMszComponents._init_registrationscCs>|j}|dks&|j|jk s&|j|jk r:t|j|j}|_|S)N)rPr3r6r4r1)rcacherrr_utility_registrations_caches  z'Components._utility_registrations_cachecCs|jjdfS)NrO)__dict__get)rrrr _getBasesszComponents._getBasescCs>tdd|D|j_tdd|D|j_t||jd<dS)NcSsg|] }|jqSr)r\)rbaserrrrsz(Components._setBases..cSsg|] }|jqSr)r6)rrerrrrsrO)rNr\rOr6rb)rrQrrr _setBasess zComponents._setBasescCs|jS)N)rd)rrrrszComponents.cCs |j|S)N)rf)rrQrrrrgsTc 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.rKrXr) r<_getUtilityProvided_getNamer4rcrIrarErrUtilityRegistration)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<rhr4rcrarIrrrj)rr"r=rArColdrrrrIs&   zComponents.unregisterUtilityccs8x2t|jjD] \\}}}t|||f|VqWdS)N)r7r4rrj)rr=rAr(rrrregisteredUtilitiesszComponents.registeredUtilitiescCs|jjf|||S)N)r6lookup)rr=rAdefaultrrr queryUtilityszComponents.queryUtilitycCs&|jjf||}|dkr"t|||S)N)r6rnr )rr=rAutilityrrr getUtilitys zComponents.getUtilityccs*x$|jjf|D]\}}||fVqWdS)N)r6 lookupAll)r interfacerArqrrrgetUtilitiesFor!szComponents.getUtilitiesForcCs|jjf|S)N)r6Z subscriptions)rrtrrrgetAllUtilitiesRegisteredFor%sz'Components.getAllUtilitiesRegisteredForc Csr|dkrt|}t||}|dkr*t|}||f|j|||f<|jj|||||rnttt||||||dS)NrK) _getAdapterProvided_getAdapterRequiredrir]r\r?rrAdapterRegistration)rrCrequiredr=rArBrkrrrregisterAdapter(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<rwrxr]rcr\rFrrry)rrCrzr=rArlrrrunregisterAdapter:s"    zComponents.unregisterAdapterccs>x8t|jjD]&\\}}}\}}t||||||VqWdS)N)r7r]rry)rrzr=rAr"rBrrrregisteredAdaptersUs  zComponents.registeredAdapterscCs|jj||||S)N)r\ queryAdapter)robjectrtrArorrrr~[szComponents.queryAdaptercCs(|jj|||}|dkr$t||||S)N)r\r~r )rrrtrAadapterrrr getAdapter^s zComponents.getAdaptercCs|jj||||S)N)r\queryMultiAdapter)robjectsrtrArorrrrdszComponents.queryMultiAdaptercCs(|jj|||}|dkr$t||||S)N)r\rr )rrrtrArrrrgetMultiAdapteris zComponents.getMultiAdapterccsDx>|jjttt||D]"\}}||}|dk r||fVqWdS)N)r\rsr[mapr)rrr=rArCrrrr getAdaptersos  zComponents.getAdaptersc Csn|r td|dkrt|}t||}|jj|||||f|jj||||rjttt ||||||dS)Nz'Named subscribers are not yet supported) r<rwrxr^r&r\r@rrSubscriptionRegistration)rrCrzr=rArBrkrrrregisterSubscriptionAdapterws  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=rzrrrs z.cs<g|]4\}}}}}|ko(|ko(|ks|||||fqSrr)rrr9rrr')rCr=rzrrrs FrKT) r<rwrxr^lenr\rHrrr)rrCrzr=rAnewr)rCr=rzrunregisterSubscriptionAdapters,      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<rxr_r&r\r@rrHandlerRegistration)rrCrzrArBrkrrrregisterHandlers zComponents.registerHandlerccs$x|jD]}t|f|VqWdS)N)r_r)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')rzrrrs z0Components.unregisterHandler..cs0g|](\}}}}|ko|ks||||fqSrr)rrrrr')rCrzrrrs FrKT) r<rxr_rr\rHrrr)rrCrzrArr)rCrzrunregisterHandlers"     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.rPr rSrZrLrMpropertyrardrfrOrErIrmrprrrurvr{r|r}r~rrrrrrrrrrrr __classcell__rr)rRrrJsZ                 )  "rJc Cs y|jStk rdSXdS)NrK)Z__component_name__AttributeError)r"rrrrisricCs,tt|}t|dkr |dStddS)Nr;rzWThe utility doesn't provide a single interface and no provided interface was specified.)r[rrr<)r"r=rrrrhs   rhcCs,tt|}t|dkr |dStddS)Nr;rzaThe adapter factory doesn't implement a single interface and no provided interface was specified.)r[rrr<)rCr=rrrrws   rwc 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<rrr isinstancerrr&rN)rCrzresultrrrrrx s$      rxc@sVeZdZdddZddZddZdd Zd d Zd d ZddZ ddZ ddZ dS)rjNcCs,||||||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,) rRr,rgetattrr=rAr"reprrCrB)rrrrrS1s 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 rSrrrrrrrrrrrrj)s  rjc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)rycCs,||||||f\|_|_|_|_|_|_dS)N)rrzr=rArCrB)rrrzr=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,) rRr,rjoinrzrr=rArCrrB)rrrrrSWs 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 rSrrrrrrrrrrrryOs ryc@s eZdZdS)rN)r,r-r.rrrrrusrc@s,eZdZddZeddZdZddZdS)rcCs&|||||f\|_|_|_|_|_dS)N)rrzrAhandlerrB)rrrzrArrrrrr }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,) rRr,rrrzrArrCrrB)rrrrrSs zHandlerRegistration.__repr__)r,r-r.r rrCr=rSrrrrrzs rN))__doc__ collectionsrZ zope.eventr ImportErrorZzope.interface.interfacesrr r r r r rrrZzope.interface.interfacerZzope.interface.declarationsrrrrZzope.interface.adapterrZzope.interface._compatrrrrr0r1rJrirhrwrxrjryrrrrrrsR                  Cl%%