o $ x`@sBdZddlZddlZddlmZddlmZddlmZddlmZddlm Z ddl m Z dd l m Z dd l m Z d d gZGd ddeZeZe GdddeZe GdddeZGdddeZGdddeeZee Gdd d eZGdddeeZee Gdd d eZddZddZdd Zd!d"ZdS)#zAdapter management N) implementer providedBy Interface)ro)IAdapterRegistry)_normalize_name) STRING_TYPES) _use_c_implAdapterRegistryVerifyingAdapterRegistryc@seZdZdZdZdZd0ddZddZed d d d Z d d Z e Z e ZeZeZddZddZddZddZddZd1ddZed0ddZddZdd Zd2d"d#Zd$d%Zd&d'Zd(d)Zd2d*d+Z d,d-Z!d.d/Z"d!S)3BaseAdapterRegistrya A basic implementation of the data storage and algorithms required for a :class:`zope.interface.interfaces.IAdapterRegistry`. Subclasses can set the following attributes to control how the data is stored; in particular, these hooks can be helpful for ZODB persistence. They can be class attributes that are the named (or similar) type, or they can be methods that act as a constructor for an object that behaves like the types defined here; this object will not assume that they are type objects, but subclasses are free to do so: _sequenceType = list This is the type used for our two mutable top-level "byorder" sequences. Must support mutation operations like ``append()`` and ``del seq[index]``. These are usually small (< 10). Although at least one of them is accessed when performing lookups or queries on this object, the other is untouched. In many common scenarios, both are only required when mutating registrations and subscriptions (like what :meth:`zope.interface.interfaces.IComponents.registerUtility` does). This use pattern makes it an ideal candidate to be a :class:`~persistent.list.PersistentList`. _leafSequenceType = tuple This is the type used for the leaf sequences of subscribers. It could be set to a ``PersistentList`` to avoid many unnecessary data loads when subscribers aren't being used. Mutation operations are directed through :meth:`_addValueToLeaf` and :meth:`_removeValueFromLeaf`; if you use a mutable type, you'll need to override those. _mappingType = dict This is the mutable mapping type used for the keyed mappings. A :class:`~persistent.mapping.PersistentMapping` could be used to help reduce the number of data loads when the registry is large and parts of it are rarely used. Further reductions in data loads can come from using a :class:`~BTrees.OOBTree.OOBTree`, but care is required to be sure that all required/provided values are fully ordered (e.g., no required or provided values that are classes can be used). _providedType = dict This is the mutable mapping type used for the ``_provided`` mapping. This is separate from the generic mapping type because the values are always integers, so one might choose to use a more optimized data structure such as a :class:`~BTrees.OIBTree.OIBTree`. The same caveats regarding key types apply as for ``_mappingType``. It is possible to also set these on an instance, but because of the need to potentially also override :meth:`_addValueToLeaf` and :meth:`_removeValueFromLeaf`, this may be less useful in a persistent scenario; using a subclass is recommended. .. versionchanged:: 5.3.0 Add support for customizing the way internal data structures are created. .. versionchanged:: 5.3.0 Add methods :meth:`rebuild`, :meth:`allRegistrations` and :meth:`allSubscriptions`. ) lookupqueryMultiAdapterlookup1 queryAdapter adapter_hook lookupAllnames subscriptions subscribersrcCs0||_||_||_|||_dSN) _sequenceType _adapters _subscribers _providedType _provided _createLookup __bases__selfbasesrr8/usr/lib/python3/dist-packages/zope/interface/adapter.py__init__s    zBaseAdapterRegistry.__init__cCs$||jd<t||_||dS)z If subclasses need to track when ``__bases__`` changes, they can override this method. Subclasses must still call this method. r N)__dict__rchangedr!rrr$ _setBasess  zBaseAdapterRegistry._setBasescCs |jdS)Nr )r&r"rrr$ zBaseAdapterRegistry.cCs ||Sr)r(r!rrr$r*r+cCs.|||_|jD] }t|j||j|<q dSr) LookupClass _v_lookup _delegatedgetattrr&)r"namerrr$rs  z!BaseAdapterRegistry._createLookupcCs|dur|fS||fS)a Add the value *new_item* to the *existing_leaf_sequence*, which may be ``None``. Subclasses that redefine `_leafSequenceType` should override this method. :param existing_leaf_sequence: If *existing_leaf_sequence* is not *None*, it will be an instance of `_leafSequenceType`. (Unless the object has been unpickled from an old pickle and the class definition has changed, in which case it may be an instance of a previous definition, commonly a `tuple`.) :return: This method returns the new value to be stored. It may mutate the sequence in place if it was not ``None`` and the type is mutable, but it must also return it. .. versionadded:: 5.3.0 Nr)r"existing_leaf_sequencenew_itemrrr$_addValueToLeafs z#BaseAdapterRegistry._addValueToLeafcstfdd|DS)aa Remove the item *to_remove* from the (non-``None``, non-empty) *existing_leaf_sequence* and return the mutated sequence. If there is more than one item that is equal to *to_remove* they must all be removed. Subclasses that redefine `_leafSequenceType` should override this method. Note that they can call this method to help in their implementation; this implementation will always return a new tuple constructed by iterating across the *existing_leaf_sequence* and omitting items equal to *to_remove*. :param existing_leaf_sequence: As for `_addValueToLeaf`, probably an instance of `_leafSequenceType` but possibly an older type; never `None`. :return: A version of *existing_leaf_sequence* with all items equal to *to_remove* removed. Must not return `None`. However, returning an empty object, even of another type such as the empty tuple, ``()`` is explicitly allowed; such an object will never be stored. .. versionadded:: 5.3.0 cg|]}|kr|qSrr).0v to_removerr$ sz.)tuple)r"r1r8rr7r$_removeValueFromLeafsz(BaseAdapterRegistry._removeValueFromLeafcCs|jd7_|j|dSN) _generationr-r'r"originally_changedrrr$r'szBaseAdapterRegistry.changedc Cst|ts td|dur|||||dStdd|D}t|}t|}|j}t||kr>|| t||ks1||}||f}|D]} | | } | dur\| } | || <| }qI| ||urhdS|||<|j |dd} | |j |<| dkr|j |||dS)Nname is not a stringcSg|]}t|qSr_convert_None_to_Interfacer5rrrr$r9z0BaseAdapterRegistry.register..rr=) isinstancer ValueError unregisterr:r lenrappend _mappingTypegetrr- add_extendorr') r"requiredprovidedr0valueorderbyorder componentskeykdnrrr$registers8       zBaseAdapterRegistry.registerc Csltdd|D}t|}t||krdS||}||f}|D]}||} | dur.dS| }q ||S)NcSrBrrCrErrr$r91rGz2BaseAdapterRegistry._find_leaf..)r:rKrN) r"rTrPrQr0rSrUrVrWrXrrr$ _find_leaf+s    zBaseAdapterRegistry._find_leafcCs||j||t|Sr)r[rr )r"rPrQr0rrr$ registeredAs zBaseAdapterRegistry.registeredc csv|dkr|D] \}}||f|fVq dS|D]\}}||f}|||d|D] \}}||fVq.qdS)Nrr=)items_allKeys) clsrUiparent_krWr6 new_parent_kxyrrr$r_Is  zBaseAdapterRegistry._allKeysc cspt|D]0\}}|||dD]#\}}t||dksJ|d|}|d}|d}||||fVqqdS)Nr=) enumerater_rK) r"rTrarUrVrRrPrQr0rrr$ _all_entriesTs z BaseAdapterRegistry._all_entriesccs||jD]}|VqdS)aT Yields tuples ``(required, provided, name, value)`` for all the registrations that this object holds. These tuples could be passed as the arguments to the :meth:`register` method on another adapter registry to duplicate the registrations this object holds. .. versionadded:: 5.3.0 N)rjr)r"trrr$allRegistrationsfs z$BaseAdapterRegistry.allRegistrationsNcCs8tdd|D}t|}|j}|t|krdS||}||f}g} |D]} || } | dur3dS| || f| }q%||} | durHdS|durR| |urRdS||=|szt| D]\} } | | } | rgn| | =q[|rz|dsz|d=|rz|drq|j|d}|dkr|j|=|j|n||j|<| |dS)NcSrBrrCrErrr$r9urGz2BaseAdapterRegistry.unregister..Frhr=r) r:rKrrNrLreversedrr-remove_extendorr')r"rPrQr0rRrSrTrUrVlookupsrWrXoldcomprYrrr$rJtsF       zBaseAdapterRegistry.unregisterc Cstdd|D}d}t|}|j}t||kr%||t||ks||}||f}|D]} || } | durC|} | || <| }q0||||||<|durm|j|dd} | |j|<| dkrm|j || |dS)NcSrBrrCrErrr$r9rGz1BaseAdapterRegistry.subscribe..r\rr=) r:rKrrLrMrNr3rr-rOr') r"rPrQrRr0rSrTrUrVrWrXrYrrr$ subscribes,      zBaseAdapterRegistry.subscribecCs&||j||dp d}||vr|SdS)Nr\r)r[r)r"rPrQ subscriberrrrr$ subscribedszBaseAdapterRegistry.subscribedccs6||jD]\}}}}|D]}|||fVqqdS)aM Yields tuples ``(required, provided, value)`` for all the subscribers that this object holds. These tuples could be passed as the arguments to the :meth:`subscribe` method on another adapter registry to duplicate the registrations this object holds. .. versionadded:: 5.3.0 N)rjr)r"rPrQ_namerRr6rrr$allSubscriptionss  z$BaseAdapterRegistry.allSubscriptionscCsntdd|D}t|}|j}|t|krdS||}||f}g}|D]} || } | dur3dS||| f| }q%|d} | sFdSt| } |durQd} n|| |} ~ t| | kr`dS| rg| |d<n&|d=t|D]\}} || } | rzn|| =qn|r|ds|d=|r|dr|dur|j|t| | }|dkr|j|=|j |n||j|<| |dS)NcSrBrrCrErrr$r9rGz3BaseAdapterRegistry.unsubscribe..r\rrhr) r:rKrrNrLr;rmrr-rnr')r"rPrQrRrSrTrUrVrorWrXrplen_oldnewrqrYrrr$ unsubscribesT          zBaseAdapterRegistry.unsubscribecCs`|}|}dd}||}||}||j|D]}|j|q|D]}|j|q&dS)aE Rebuild (and replace) all the internal data structures of this object. This is useful, especially for persistent implementations, if you suspect an issue with reference counts keeping interfaces alive even though they are no longer used. It is also useful if you or a subclass change the data types (``_mappingType`` and friends) that are to be used. This method replaces all internal data structures with new objects; it specifically does not re-use any storage. .. versionadded:: 5.3.0 cSs6zt|}Wn tytdYSwt|f|S)Nr)next StopIterationiter itertoolschain)itfirstrrr$buffer2s    z+BaseAdapterRegistry.rebuild..bufferN)rlrvr%r rZrr)r" registrationsrrargsrrr$rebuilds    zBaseAdapterRegistry.rebuildcCsGddd}|S)Nc@seZdZiZdS)z2BaseAdapterRegistry.get..XXXTwistedFakeOutN)__name__ __module__ __qualname__ selfImpliedrrrr$XXXTwistedFakeOutTsrr)r"_rrrr$rNSszBaseAdapterRegistry.getrr\r)#rrr__doc__r.r>r%r(propertyr rlistrr:_leafSequenceTypedictrMrr3r;r'rZr[r] classmethodr_rjrlrJrrrtrvryrrNrrrr$rAs>9 /  #    0 E 7rcsfeZdZddZdddZddZdd d Zdd d Zdd dZdfdd Z ddZ ddZ Z S) LookupBasecCsi|_i|_i|_dSr)_cache_mcache_scacher)rrr$r%^s zLookupBase.__init__NcCs"|j|j|jdSr)rclearrr)r"ignoredrrr$r'cs  zLookupBase.changedcCsL|j|}|duri}||j|<|r$||}|dur"i}|||<|}|Sr)rrN)r"rQr0cachecrrr$ _getcachehs   zLookupBase._getcacher\cCst|ts td|||}t|}t|dkr"||dt}n|t|t}|turH||||}t|dkrB|||d<n||t|<|durN|S|S)NrAr=r) rHr rIrr:rKrN_not_in_mapping_uncached_lookupr"rPrQr0defaultrresultrrr$rus     zLookupBase.lookupcCsTt|ts td|||}||t}|tur"||f|||S|dur(|S|SNrA)rHr rIrrNrrrrrr$rs   zLookupBase.lookup1cCs|||||Sr)r)r"objectrQr0rrrr$rszLookupBase.queryAdapterc szt|ts tdt|}|||}||t}|tur%||f||}|dur;t|tr1|j }||}|dur;|S|Sr) rHr rIrrrNrrsuper__self__) r"rQrr0rrPrfactoryr __class__rr$rs    zLookupBase.adapter_hookcCV|j|}|duri}||j|<t|}||t}|tur)|||}|||<|Sr)rrNr:r_uncached_lookupAllr"rPrQrrrrr$r    zLookupBase.lookupAllcCrr)rrNr:r_uncached_subscriptionsrrrr$rrzLookupBase.subscriptionsrr\N) rrrr%r'rrrrrrr __classcell__rrrr$r[s   rc@s4eZdZddZddZddZddZd d Zd S) VerifyingBasecCs4t|||jjdd|_dd|jD|_dS)Nr=cSg|]}|jqSrr>rErrr$r9z)VerifyingBase.changed..)LookupBaseFallbackr' _registryr _verify_ro_verify_generationsr?rrr$r's zVerifyingBase.changedcCs(dd|jD|jkr|ddSdS)NcSrrrrErrr$r9rz)VerifyingBase._verify..)rrr'r)rrr$_verifys zVerifyingBase._verifycC|t|||Sr)rrr)r"rQr0rrr$rzVerifyingBase._getcachecCrr)rrrr"rPrQrrr$rrzVerifyingBase.lookupAllcCrr)rrrrrrr$rrzVerifyingBase.subscriptionsN)rrrr'rrrrrrrr$rs  rcseZdZfddZdfdd ZddZdd Zd d Zd d ZdddZ dfdd Z ddZ ddZ ddZ ddZZS)AdapterLookupBasecs&||_i|_|tt|dSr)r _requiredinit_extendorsrrr%)r"registryrrr$r%szAdapterLookupBase.__init__NcsFtt|d|jD]}|}|dur||q |jdSr)rrr'rkeysryr)r"rrFrrr$r's zAdapterLookupBase.changedcCs"i|_|jjD]}||qdSr) _extendorsrrrO)r"prrr$rs  z AdapterLookupBase.init_extendorscsP|j}jD]}||d}fdd|Dgfdd|D||<qdS)Nrcsg|] }|r|qSr isOrExtendsr5erQrr$r9z2AdapterLookupBase.add_extendor..csg|] }|s|qSrrrrrr$r9rr__iro__rN)r"rQrra extendorsrrr$rOs  zAdapterLookupBase.add_extendorcs4|j}jD]}fdd||dD||<qdS)Ncr4rrrrrr$r9$sz5AdapterLookupBase.remove_extendor..rr)r"rQrrarrr$rn!s  z!AdapterLookupBase.remove_extendorcGs6|j}|D]}|}||vr||d||<qdSr<)rweakrefrr)r"rP_refsrFrefrrr$ _subscribe(s zAdapterLookupBase._subscriber\c Cs~t|}d}t|}|jjD])}|j}|t|krq|jj|}|s$q||} t| |||d|}|dur7nq|j ||SNr) r:rKrrrr-rrN_lookupr) r"rPrQr0rrSrrTrrUrrr$r0s&    z"AdapterLookupBase._uncached_lookupcsJ|dd|D||}|dur|S|fdd|D}|dur#|S|S)NcSrBrrr5orrr$r9HrGz7AdapterLookupBase.queryMultiAdapter..cs g|] }t|tr |jn|qSr)rHrrrrrr$r9Ls )r)r"objectsrQr0rrrrrr$rGsz#AdapterLookupBase.queryMultiAdapterc Cs~t|}t|}i}t|jjD]#}|j}|t|krq|jj|}|s&q||}t ||||d|q|j |t| Sr) r:rKrmrrrr-rrN _lookupAllrr^) r"rPrQrSrrrTrrUrrr$rRs   z%AdapterLookupBase._uncached_lookupAllcCsdd|||DS)NcSsg|]}|dqS)rr)r5rrrr$r9erGz+AdapterLookupBase.names..)rrrrr$rdszAdapterLookupBase.namesc Cst|}t|}g}t|jjD],}|j}|t|krq|dur$|f}n |jj|}|dur0qt ||||d|d|q|j ||S)Nr\r) r:rKrmrrrr-rrN_subscriptionsr)r"rPrQrSrrrTrrrr$rgs"  z)AdapterLookupBase._uncached_subscriptionscCsd|dd|D|}|durd}|D]}||q|Sg}|D]}||}|dur/||q |S)NcSrBrrrrrr$r9rGz1AdapterLookupBase.subscribers..r)rrL)r"rrQrr subscriptionrsrrr$r~s  zAdapterLookupBase.subscribersrrr)rrrr%r'rrOrnrrrrrrrrrrrr$rs    rc@ eZdZdS) AdapterLookupNrrrrrrr$rrcsNeZdZdZeZd fdd ZddZddZfd d Z fd d Z Z S)r za A full implementation of ``IAdapterRegistry`` that adds support for sub-registries. rcst|_tt||dSr)rWeakKeyDictionary_v_subregistriesrr r%r!rrr$r%s zAdapterRegistry.__init__cCsd|j|<dSr<rr"rFrrr$_addSubregistryszAdapterRegistry._addSubregistrycCs||jvr |j|=dSdSrrrrrr$_removeSubregistrys  z"AdapterRegistry._removeSubregistrycsZ|jdd}|D] }||vr||q |D] }||vr"||qtt||dS)Nr r)r&rNrrrr r()r"r#rprFrrr$r(s  zAdapterRegistry._setBasescs.tt|||jD]}||q dSr)rr r'rr)r"r@subrrr$r's zAdapterRegistry.changedr) rrrrrr,r%rrr(r'rrrrr$r s  c@r)VerifyingAdapterLookupNrrrrr$rrrc@seZdZdZeZdS)r z2 The most commonly-used adapter registry. N)rrrrrr,rrrr$r scCs|durtS|Srr)rdrrr$rDsrDc Cs|j}||kr*||jD]}||}|r't|||||d|} | dur'| Sq dS|D]} || }|rA||} | durA| Sq,dSr<)rN__sro__r) rUspecsrQr0ralcomponents_getspeccompsrFifacerrr$rs$  rc Csp|j}||kr$t||jD]}||}|r!t|||||d|qdSt|D] } || }|r5||q(dSr<)rNrmrrupdate) rUrrQrrarrrrrrrr$rs  rc Cs|j}||kr%t||jD]}||} | r"t| |||||d|qdSt|D]} || } | r=| |} | r=|| q)dSr<)rNrmrrextend) rUrrQr0rrarrrrrrrr$rs    r)rr}rzope.interfacerrrrzope.interface.interfacesrzope.interface._compatr r r __all__rrrrrrrrr rr rDrrrrrrr$sH         $o$(