o $ x`{@sdZddlZddlmZddlmZddlZddlmZddlmZ ddl m Z ddl m Zddlm Z gd Zd Zd Zd Zd ZeZeZddZddZGdddeZeZeGdddeZGdddeZeGdddeeZegdZGdddeZ Gddde!Z"e"dee efd d!iZ#d"d#Z$Gd$dde#Z%e%d%d&d'Z&d(d)e&_'e&(e&e&j)e&fksJe&e _*e&e _*Gd*d+d+e%Z+Gd,d-d-eZ,Gd.d/d/e,Z-d;d0d1Z.d __slots__rKrNrR__call__rrrrrBs   rBc@s8eZdZdZddZddZddZdd Zd d Zd S) NameAndModuleComparisonMixinrcCs\||urdS|dur dS|j|jf}z|j|jf}Wn ty%tYSw||k||kS)a Compare *self* to *other* based on ``__name__`` and ``__module__``. Return 0 if they are equal, return 1 if *self* is greater than *other*, and return -1 if *self* is less than *other*. If *other* does not have ``__name__`` or ``__module__``, then return ``NotImplemented``. .. caution:: This allows comparison to things well outside the type hierarchy, perhaps not symmetrically. For example, ``class Foo(object)`` and ``class Foo(Interface)`` in the same file would compare equal, depending on the order of operands. Writing code like this by hand would be unusual, but it could happen with dynamic creation of types and interfaces. None is treated as a pseudo interface that implies the loosest contact possible, no contract. For that reason, all interfaces sort before None. rN)r%r=AttributeErrorNotImplemented)r(othern1n2rrr_compares  z%NameAndModuleComparisonMixin._comparecCs||}|tur |S|dkSNrr\rXr(rYcrrr__lt__ z#NameAndModuleComparisonMixin.__lt__cCs||}|tur |S|dkSr]r^r_rrr__le__rbz#NameAndModuleComparisonMixin.__le__cCs||}|tur |S|dkSr]r^r_rrr__gt__rbz#NameAndModuleComparisonMixin.__gt__cCs||}|tur |S|dkSr]r^r_rrr__ge__rbz#NameAndModuleComparisonMixin.__ge__N) r%r=r>rSr\rarcrdrerrrrrUs ' rUc@s^eZdZdZdZdddZddZedd Ze fd d Z d d Z ddZ ddZ ddZdS) InterfaceBasez:Base class that wants to be replaced with a C base :) )r% __ibmodule___v_cached_hashNcCs||_||_dSr<r%rg)r(namemodulerrrr) s zInterfaceBase.__init__cCstr<)NotImplementedErrorr(conformrrr _call_conformszInterfaceBase._call_conformcCr*r<)rgr,rrr__module_property__sz!InterfaceBase.__module_property__cCsnz|j}Wn tyd}Ynw|dur ||}|dur |S||}|dur+|S|tur1|Std||)z)Adapt an object to the interface NzCould not adapt) __conform__rWro __adapt___marker TypeError)r(obj alternaternadapterrrrrTs     zInterfaceBase.__call__cCs6||r|StD]}|||}|dur|Sq dS)z(Adapt an object to the receiver N)rKr )r(ruhookrwrrrrr+s  zInterfaceBase.__adapt__cCs2z|jWStyt|j|jf|_Y|jSwr<)rhrWhashr%r=r,rrr__hash__8s  zInterfaceBase.__hash__cCs||}|tur |S|dkSr]r^r_rrr__eq__@rbzInterfaceBase.__eq__cCs*||urdS||}|tur|S|dkS)NFrr^r_rrr__ne__Fs  zInterfaceBase.__ne__NN)r%r=r>r&rSr)ropropertyrprsrTrrrzr{r|rrrrrfs     rfr c@seZdZdZdZdZejZejZdddZ e ddZ dd Z d d Z d d Ze ddeZeZddZddZddZdddZdddZdddZdS) r awSpecifications An interface specification is used to track interface declarations and component registrations. This class is a base class for both interfaces themselves and for interface specifications (declarations). Specifications are mutable. If you reassign their bases, their relations with other specifications are adjusted accordingly. rNcCs2d|_d|_i|_d|_d|_d|_t||_dS)Nr)rDrErCrFrGrHtuple __bases__)r(basesrrrr)hs zSpecification.__init__cCs|jdur t|_|jSr<)rDweakrefWeakKeyDictionaryr,rrr dependentszs  zSpecification.dependentscCs|j|dd|j|<dS)Nrr)rr5rD)r( dependentrrr subscribeszSpecification.subscribecCsXz|j|}Wn tyt|w|d8}|s|j|=dS|dks%J||j|<dS)Nrr)rDrtr2r)r(rnrrr unsubscribes   zSpecification.unsubscribecCs>|jD]}||q||_|D]}||q||dSr<)rrrErchanged)r(rbrrr __setBasess   zSpecification.__setBasescCr*r<)rEr,rrrzSpecification.csV|jdd|jDd}|jdur)|r)|dur)fdd|D}||S)z Calculate and return the resolution order for this object, using its ``__bases__``. Ensures that ``Interface`` is always the last (lowest priority) element. cSsi|]}||jqSr)rH.0rrrr sz0Specification._calculate_sro..) base_mrosNrVcsg|]}|ur|qSrr)rxrootrr s z0Specification._calculate_sro..)_do_calculate_ror_ROOTr)r(srorrr_calculate_sros #   zSpecification._calculate_srocCs~d|_|j}||}t||_tdd|D|_|D]}d||<qt|jr/|jndD]}| |q2d|_dS)z We, or something we depend on, have changed. By the time this is called, the things we depend on, such as our bases, should themselves be stable. NcSsg|] }t|tr|qSr isinstancer)rancestorrrrrs z)Specification.changed..r) rFrCclearrrrHrGrDr8r)r(originally_changedimplied ancestorsrrrrrrs    zSpecification.changedccs:i}|jD]}|D] }||vrd||<|Vq qdS)DReturn an iterator for the interfaces in the specification. rN)r interfaces)r(seenbaserQrrrrs  zSpecification.interfacesTcCs||jvo | p ||kS)zDoes the specification extend the given interface? Test whether an interface in the specification extends the given interface rP)r(rQstrictrrrextends s  zSpecification.extendscCs t||Sr<)rref)r(callbackrrrr zSpecification.weakrefcCsd|j}|dur i}|_||}|dur*|jD]}||}|dur)|||<nq|dur0|S|S)z+Query for an attribute description N)rFr5rGdirect)r(rjr6attrsattrifacerrrr5s    zSpecification.get)r)Tr<)r%r=r>r&rSrrBrRrKr)r~rrr_Specification__setBasesr calculate_rorrrrrrr5rrrrr Rs.      9 r c@s,eZdZdZddZeddZddZdS) _InterfaceMetaClassrcCsPtdjd}tjd}t|trtjd}||d<t||||}||_ |S)Nrr%r=rp) rr f_globalsrf__dict__rstrtype__new___InterfaceMetaClass__module)rOrjrrr= moduledescrkindrrrrts   z_InterfaceMetaClass.__new__cCr*r<)rrOrrrr=r.z_InterfaceMetaClass.__module__cCsd|j|jfS)Nz)rr%rrrr__repr__sz_InterfaceMetaClass.__repr__N)r%r=r>rSrr~rrrrrr)s I  rrrSrcCs&tdj}|ti}|||j<tS)a Convert a method specification to an actual method of the interface. This is a decorator that functions like `staticmethod` et al. The primary use of this decorator is to allow interface definitions to define the ``__adapt__`` method, but other interface methods can be overridden this way too. .. seealso:: `zope.interface.interfaces.IInterfaceDeclaration.interfacemethod` r)rrrrINTERFACE_METHODSr%r)funcrmethodsrrrinterfacemethods  rc@seZdZdZ  d/ddZ  d0ddZdd Zd d Zd d ZddZ d1ddZ ddZ d1ddZ ddZ e ZddZddZd2ddZd2dd Zd2d!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.ZdS)3rz Prototype (scarecrow) Interfaces Implementation. Note that it is not possible to change the ``__name__`` or ``__module__`` after an instance of this object has been constructed. Nrc Cst|tsJ|p i}|td}|rK|d|vr d|dinid|vr+d|d<t|tr4|f}n |turcss&|]}tt|trt|VqdSr<) issubclassr _InterfaceClassWithCustomMethodsrrrr s  z)InterfaceClass.__new__..)rrpoprupdaterrrrPY2len__mro___InterfaceClassBaser) rOrjrrr&r=needs_custom_class cls_basesbases_with_custom_methodsrrrrsD   zInterfaceClass.__new__c Cs(tdd|Ds td|duri}|dur;|d}t|tr%|d=nz tdjd}Wn tt fy:Ynwt ||||d}|durYt|t sY|durV|}|d=|dur_d}t ||||td}|dur|D] \}} ||| qtt |||||_d ||f|_dS) Ncss|]}t|tVqdSr<r)rrrrrrsz*InterfaceClass.__init__..zExpected base interfacesr=rr%r&r"%s.%s)allrtr5rrrrrrWr2rfr) Attributer!rritemsr;r _InterfaceClass__compute_attrs_InterfaceClass__attrs__identifier__) r(rjrrr&r=d tagged_datarvalrrrr)s<       zInterfaceClass.__init__cs"fddfdd|DS)NcsHt|tr|_|js||_|St|trt||d}|Std|)N)rjzConcrete attribute, )rrrQr%r fromFunctionInvalidInterface)anameavalr,rr update_value!s   z4InterfaceClass.__compute_attrs..update_valuecs,i|]\}}|dvr|tur|||qS)) __locals__r>__annotations__)r)rrr)rrrr,s   z2InterfaceClass.__compute_attrs..)r)r(rr)r(rr__compute_attrss  zInterfaceClass.__compute_attrsccs |VdS)rNrr,rrrr:s zInterfaceClass.interfacescCr*r<)rr,rrrgetBases?zInterfaceClass.getBasescCs||kp||S)zSame interface or extends?)r)r(rYrrrisEqualOrExtendedByBsz"InterfaceClass.isEqualOrExtendedByFcCsB|s|jS|j}|jD] }|t||q|S)z4Return the attribute names defined by the interface.)rr8copyrrdictfromkeysnamesr(rrrrrrrFs    zInterfaceClass.namescCst|jddS)NT)r)iterrr,rrr__iter__RszInterfaceClass.__iter__cCsP|s|jSi}|jdddD] }|t||q||j|S)z=Return attribute names and descriptions defined by interface.NrV)rrrrrnamesAndDescriptionsrrrrrUs  z#InterfaceClass.namesAndDescriptionscCs||}|dur |St|)z4Return the attribute description for the given name.N)r5r2r(rjrrrrgetDescriptionForbs z InterfaceClass.getDescriptionForcCs||duSr<r5r(rjrrr __contains__lzInterfaceClass.__contains__cCs |j|Sr<)rr5rrrrrorzInterfaceClass.directcCs |||Sr<r)r(rjr6rrrqueryDescriptionForrrz"InterfaceClass.queryDescriptionForc Cst|jD].}|ddD]%}z||Wq ty0}z|dur%||nWYd}~q d}~wwq|r8t|dS)z&validate object to defined invariants.rrN)rGr?rr)r(ruerrorsrrerrorrrrvalidateInvariantsus     z!InterfaceClass.validateInvariantscCs,|jD]}||t}|tur|Sq|S)z Queries for the value associated with *tag*, returning it from the nearest interface in the ``__iro__``. If not found, returns *default*. )rGr?rs)r(r3r6rrrrrr7s  zInterfaceClass.queryTaggedValuecCs"|j|td}|turt||S)r1)r6)r7rsr2r:rrrr4szInterfaceClass.getTaggedValuecCs$t}|jD] }||q|S)z Returns a list of all tags. )setrGrrA)r(r8rrrrr9s z!InterfaceClass.getTaggedValueTagscCs>z|jWStyt|}d|jj|f}||_|YSw)Nz<%s %s>)_v_reprrWr __class__r%rrrrrs zInterfaceClass.__repr__cCs |j}|j}|rd||f}|S)Nrri)r(rjmrrr__str__s  zInterfaceClass.__str__cCs4z||WStytdjdurYdSw)N)rtrexc_infotb_nextrmrrrros  zInterfaceClass._call_conformcCr*r<r+r,rrr __reduce__rzInterfaceClass.__reduce__)NrNNN)rNNN)Fr<)r%r=r>r&rr)rrrrrrrr __getitem__rrrrr7r4r9rrrorrrrrrs8  3 3          Interfacezzope.interface)r=cCstfSr<)rrrrrrrrc@seZdZdZdS)rz_ Marker class for interfaces with custom methods that override InterfaceClass methods. N)r%r=r>r&rrrrrsrc@s,eZdZdZdZddZddZddZdS) rzAttribute descriptions NcCsdS)z/Return extra data to put at the end of __str__.r"rr,rrr _get_str_infoszAttribute._get_str_infocCs<d}|jdur|jjd|jjd}||jpd|S)Nr".z )rQr=r%r)r(ofrrrrs zAttribute.__str__cCs dt|jt|jt||fS)Nz<%s.%s object at 0x%x %s>)rr=r%idr,rrrrs zAttribute.__repr__)r%r=r>r&rQrrrrrrrrs  rc@sdeZdZdZdZZdZZZddZ ddZ dd Z e e e e Z d d Zd d ZddZeZdS)MethodzMethod interfaces The idea here is that you have objects that describe methods. This provides an opportunity for rich meta-data. rNcCs|jduriS|jSr< _optionalr,rrr _get_optionals zMethod._get_optionalcCs ||_dSr<r)r(optrrr _set_optional zMethod._set_optionalcCs d|_dSr<rr,rrr _del_optionalr zMethod._del_optionalcOst|j|jr<)BrokenImplementationrQr%)r(argskwrrrrT rzMethod.__call__cCs|j|j|j|j|jdS)N positionalrequiredoptionalvarargskwargsrr,rrrgetSignatureInfos zMethod.getSignatureInfocCsg}|jD]}||||jvr"|ddt|j|7<q|jr.|d|j|jr9|d|jdd|S)NrV=*z**z(%s)z, )rrrr8reprrrjoin)r(sigvrrrgetSignatureStrings  zMethod.getSignatureString)r%r=r>r&rrrrrrr r r~rrTrrrrrrrrs   rcCs.|p|j}t||j}t|ddpd}|j}|j|}|j|d}i} t|} | s0t|dd} || } | dkrA|| d}d} | t t || d||d||_ |d| |_ | |_ |} |jt@rq|| |_| d} nd|_|jt@r|| |_nd|_||_|jD] \} }|| |q|S)N __defaults__r__defaults_count__rr)r%rr&getattr__code__ co_argcount co_varnamesrrrziprrrco_flags CO_VARARGSrCO_VARKEYWORDSrrQrrr;)rrQimlevelrjmethoddefaultscodenarrdefaults_countnrargnorrrrrr&s<         rcCs&t|tr |j}n|}t||d|dS)Nr)r'rj)rr__func__r)methrQrjrrrr fromMethodZs r1cCs~ddlm}ddlm}|t|ddlm}|t|ddlm}|t|ddlm }|t |ddlm }|t |dS)Nr)classImplements)IElement) IAttribute)IMethod)ISpecification) IInterface) zope.interface.declarationsr2zope.interface.interfacesr3r!r4rr5rr6r r7r)r2r3r4r5r6r7rrr_wirecs          r:)rN)rK)r)r )_empty)NrNr})7r&rtypesrrrzope.interface._compatrrrzope.interface.exceptionsrzope.interface.rorrzope.interface__all__r%r&rrobjectrrsrr r!SpecificationBasePyrBrUrfr r rrrrrrrrrHrrrrrr1r:r8rNrKrr r;rrrrsr         =.N OXh    3 4