o $ x`1@sfdZdZddlZddlmZddlmZddlmZddlZddlm Z ddl m Z dd l m Z dd l m Z dd l mZdd l mZdd lmZddlmZddlmZgZiZdZdZddZGdddeZGdddeZGdddeZGdddeeZddZddZ ed d!Z!d"d#Z"d$d%Z#d&d'Z$dYd)d*Z%d+d,Z&Gd-d.d.eZ'Gd/d0d0eZ(d1d2Z)d3d4Z*d5d6Z+Gd7d8d8eZ,e,Z-e.Z/d9d8Z,d:e,_0d;d<Z1d=d>Z2d?d@Z3eGdAdBdBe Z4GdCdDdDee4Z5dEdFZ6dGdHZ7dIdJZ8GdKdLdLeZ9dMdNZ:dOdPZ;edQdRZdZdWdXZ?eZ@e>ZAdS)[aImplementation of interface declarations There are three flavors of declarations: - Declarations are used to simply name declared interfaces. - ImplementsDeclarations are used to express the interfaces that a class implements (that instances of the class provides). Implements specifications support inheriting interfaces. - ProvidesDeclarations are used to express interfaces directly provided by objects. restructuredtextN) FunctionType) MethodType) ModuleType)addClassAdvisor) Interface)InterfaceClass)SpecificationBase) Specification)NameAndModuleComparisonMixin) CLASS_TYPES)PYTHON3) _use_c_implzIClass advice impossible in Python3. Use the @%s class decorator instead.z\The %s API is deprecated, and will not work in Python3 Use the @%s class decorator instead.cCs(|j}|j}|j}|||d}|S)N)__self_class__ __thisclass____mro__index)ob self_classclass_that_invoked_super complete_mro next_classr=/usr/lib/python3/dist-packages/zope/interface/declarations.py_next_super_class=s rc@seZdZddZddZdS)namedcC ||_dSN)name)selfrrrr__init__I znamed.__init__cCs |j|_|Sr)r__component_name__r rrrr__call__Lsznamed.__call__N)__name__ __module__ __qualname__r!r%rrrrrGs rc@s`eZdZdZdZddZddZddZd d Zd d Z d dZ e Z e ddZ e ddZdS) DeclarationzInterface declarationsrcGst|t|dSr)r r!_normalizeargs)r basesrrrr!VszDeclaration.__init__cCs||o ||vS)z:Test whether an interface is in the specification )extends interfaces)r interfacerrr __contains__YszDeclaration.__contains__cCs|S)zCReturn an iterator for the interfaces in the specification r-r rrr__iter___szDeclaration.__iter__cCs t|jS)zCReturn an iterator of all included and extended interfaces )iter__iro__r1rrr flattenedd zDeclaration.flattenedcstfdd|DS)z/Remove interfaces from a specification cs&g|]fddDsqS)csg|] }|dr|qS)rr,).0jirr ns  z2Declaration.__sub__...r0)r8otherr:rr<ls z'Declaration.__sub__..)r)r-)r r>rr=r__sub__is zDeclaration.__sub__csrg}t|}t|}|D]"|vrq|tfdd|Dr-|q|qt||S)a Add two specifications or a specification and an interface and produce a new declaration. .. versionchanged:: 5.4.0 Now tries to preserve a consistent resolution order. Interfaces being added to this object are added to the front of the resulting resolution order if they already extend an interface in this object. Previously, they were always added to the end of the order, which easily resulted in invalid orders. c3s|]}|VqdSrr7r8xr:rr sz&Declaration.__add__..)listr-setaddanyappendr))r r>beforeresultseenrr:r__add__us      zDeclaration.__add__cs(t|tfdd|D}|fS)Ncsg|] }|s|qSr) isOrExtends)r8ifaceimplemented_by_clsrrr<s z6Declaration._add_interfaces_to_cls..) implementedBytuple)r-clsrrNr_add_interfaces_to_clss   z"Declaration._add_interfaces_to_clscCsg}t}|D]@}t}t|tr|j}t}nt|tr!|j}t}nt|tr/|j s/|j |vr/qt|}||v}| ||rB||}| |qd |S)N, )rDrepr isinstancerr&strtype_implements_name ImplementsdeclaredinheritrErGjoin)r- ordered_namesnamesrMduplicate_transform this_name already_seenrrr_argument_names_for_reprs,       z$Declaration._argument_names_for_reprN)r&r'r(__doc__ __slots__r!r/r2r5r?rK__radd__ staticmethodrSrcrrrrr)Qs   r)c@seZdZdZdZddZddZeddZej d dZed d Z d d Z Z Z ddZdddZdddZdddZeddZej ddZdS)_ImmutableDeclarationrNcCstjdur t|t_tjSr)rh_ImmutableDeclaration__instanceobject__new__)rRrrrrks  z_ImmutableDeclaration.__new__cCdS)N_emptyrr1rrr __reduce__z _ImmutableDeclaration.__reduce__cCrlNrrr1rrr __bases__z_ImmutableDeclaration.__bases__cCs|dkrtddS)Nrz7Cannot set non-empty bases on shared empty Declaration.) TypeError)r new_basesrrrrqscCiSrrr1rrr dependentsrrz _ImmutableDeclaration.dependentscCdSrr)r _ignoredrrrsz_ImmutableDeclaration.cCstdSrp)r3r1rrrr-sz _ImmutableDeclaration.interfacesTcCs ||juSr)_ROOT)r r.strictrrrr,r"z_ImmutableDeclaration.extendscCs|Srr)r rdefaultrrrgetroz_ImmutableDeclaration.getcCstSr)rh)r callbackrrrweakrefsz_ImmutableDeclaration.weakrefcCrurrr1rrr_v_attrssz_ImmutableDeclaration._v_attrscCrwrr)r new_attrsrrrrrr)Tr)r&r'r(rerirkrnpropertyrqsetterrvchanged subscribe unsubscriber-r,r}rrrrrrrhs(       rhcsHeZdZdZdZdZdZeddZfddZdd Z d d Z Z S) rZNr?cGs||}||_|j||Sr)rkr&r!)rRrr+instrrrrAs  zImplements.namedcs,z|`Wn ty Ynwtt||Sr) _super_cacheAttributeErrorsuperrZr)r originally_changed __class__rrrMs  zImplements.changedcCsJ|jrt|jddpt|j}n|j}||j}|rd|}d||fS)Nr&rTzclassImplements(%s%s))r\getattrrYr&rcr[)r rdeclared_namesrrr__repr__Ts  zImplements.__repr__cCs t|jffSr)rPr\r1rrrrn^ zImplements.__reduce__) r&r'r(r\r[r classmethodrrrrn __classcell__rrrrrZs   rZcCs$t|ddpddt|ddpdS)Nr'r.r&)r)rrrrrYbs  rYc Cst|j}|j}|durt}|_|j}z||WSty$Ynwt|}t|}|jj}| |}||d}dd|D} t j |j d|j g| R} |j | _ |j| _| ||<| S)NcSg|]}t|qSrrPr8crrrr<z(_implementedBy_super..:)rPrrrWeakKeyDictionaryrKeyErrorrrrrZrr&r\r[) supimplemented_by_selfcachekeynext_clsimplemented_by_nextmro ix_next_clsclasses_to_keeprtnewrrr_implementedBy_superos2     rcCszt|tr t|WS|jd}Wn4tyFt|dd}|dur4t|}|dur0|YStYS|j t kr=|YSt t |fYSwt|t rN|S|dur]t|}|dur]|St |}|dury|f}t j|gt |R}d|_|`n+z|j}Wntyt|std|d}Ynwt j|gdd|DR}||_z*||_t|dst|_t|trd|jvrt|t|d t||_W|SW|SW|Styt|tstd ||t|<Y|Sw) zReturn the interfaces implemented for a class' instances The value returned is an `~zope.interface.interfaces.IDeclaration`. __implemented__Nz$ImplementedBy called for non-factoryrcSrrrrrrrr<rz!implementedBy..__providedBy__ __provides__rz!ImplementedBy called for non-type)rVrr__dict__r}rr#BuiltinImplementationSpecificationsrmrrZr)r*rYrr\rrqcallablershasattrobjectSpecificationDescriptorrDescriptorAwareMetaClasses ClassProvidesrXr)rRspec spec_namer+rrrrPsr                   rPcGs*t|}d|_d|_d|_t||ddS)a Declare the only interfaces implemented by instances of a class The arguments after the class are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The interfaces given (including the interfaces in the specifications) replace any previous declarations, *including* inherited definitions. If you wish to preserve inherited declarations, you can pass ``implementedBy(cls)`` in *interfaces*. This can be used to alter the interface resolution order. rN)rPr[r\rq_classImplements_ordered)rRr-rrrrclassImplementsOnlys  rcGslt|}tt|}g}g}|D]}|jD]}||r#||nq||qt|t|t|dS)aN Declare additional interfaces implemented for instances of a class The arguments after the class are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared. An effort is made to keep a consistent C3 resolution order, but this cannot be guaranteed. .. versionchanged:: 5.0.0 Each individual interface in *interfaces* may be added to either the beginning or end of the list of interfaces declared for *cls*, based on inheritance, in order to try to maintain a consistent resolution order. Previously, all interfaces were added to the end. .. versionchanged:: 5.1.0 If *cls* is already declared to implement an interface (or derived interface) in *interfaces* through inheritance, the interface is ignored. Previously, it would redundantly be made direct base of *cls*, which often produced inconsistent interface resolution orders. Now, the order will be consistent, but may change. Also, if the ``__bases__`` of the *cls* are later changed, the *cls* will no longer be considered to implement such an interface (changing the ``__bases__`` of *cls* has never been supported). N)rPrQr*r[r,rGr)rRr-rrHafterrMbrrrclassImplementss     rcCst|}t||fddS)a- Declare that instances of *cls* additionally provide *iface*. The second argument is an interface or interface specification. It is added as the highest priority (first in the IRO) interface; no attempt is made to keep a consistent resolution order. .. versionadded:: 5.0.0 rN)rPr)rRrMrrrrclassImplementsFirst<s rrc sfdd|D}fdd|D}g}t}|j|fD]}|D]}||vr1||||q!qt|_|}jdurXjjD]}t|}||vrW||||qCt|_dS)Nc(g|]}|r|turjs|qSrrLrr[r@rrrr<Pz,_classImplements_ordered..crrrr@rrrr<Ur)rDr[rGrErQr\rqrP) rrHr new_declaredrJlrr+rrrrrJs2         rcCs&|jd\}}|`||g|R|S)N__implements_advice_data__)rr)rRr-do_classImplementsrrr_implements_advicessrc@s$eZdZdZdZddZddZdS) implementera Declare the interfaces implemented by instances of a class. This function is called as a class decorator. The arguments are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared, unless the interface is already implemented. Previous declarations include declarations for base classes unless implementsOnly was used. This function is provided for convenience. It provides a more convenient way to call `classImplements`. For example:: @implementer(I1) class C(object): pass is equivalent to calling:: classImplements(C, I1) after the class has been created. .. seealso:: `classImplements` The change history provided there applies to this function too. r0cGrrr0r r-rrrr!r"zimplementer.__init__cCsbt|trt|g|jR|St|}tj|g|jR}z||_W|Sty0t d|w)NzCan't declare implements) rVrrr-rYrZrrrrs)r rrrrrrr%s  zimplementer.__call__N)r&r'r(rdrer!r%rrrrrzs   rc@ eZdZdZddZddZdS)implementer_onlyaDeclare the only interfaces implemented by instances of a class This function is called as a class decorator. The arguments are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). Previous declarations including declarations for base classes are overridden. This function is provided for convenience. It provides a more convenient way to call `classImplementsOnly`. For example:: @implementer_only(I1) class C(object): pass is equivalent to calling:: classImplementsOnly(I1) after the class has been created. cGrrr0rrrrr!r"zimplementer_only.__init__cCs,t|ttfr tdt|g|jR|S)NzIThe implementer_only decorator is not supported for methods or functions.)rVrr ValueErrorrr-r$rrrr%szimplementer_only.__call__Nr&r'r(rdr!r%rrrrrs rcCs^td}|j}||jusd|vrt|dd|vr!t|d||f|d<ttdddS)Nr'z* can be used only from a class definition.rz- can be used only once in a class definition.depth)sys _getframef_locals f_globalsrsrr)rr-rframelocalsrrr _implementss    rcG trttdtd|tdS)ac Declare interfaces implemented by instances of a class. .. deprecated:: 5.0 This only works for Python 2. The `implementer` decorator is preferred for all versions. This function is called in a class definition. The arguments are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared. Previous declarations include declarations for base classes unless `implementsOnly` was used. This function is provided for convenience. It provides a more convenient way to call `classImplements`. For example:: implements(I1) is equivalent to calling:: classImplements(C, I1) after the class has been created. r implementsN)r rs _ADVICE_ERRORrrr0rrrrs! rcGr)akDeclare the only interfaces implemented by instances of a class This function is called in a class definition. The arguments are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). Previous declarations including declarations for base classes are overridden. This function is provided for convenience. It provides a more convenient way to call `classImplementsOnly`. For example:: implementsOnly(I1) is equivalent to calling:: classImplementsOnly(I1) after the class has been created. rimplementsOnlyN)r rsrrrr0rrrr s rc@s8eZdZdZddZdZddZddZd Zd d Zd S) ProvideszImplement ``__provides__``, the instance-specific specification When an object is pickled, we pickle the interfaces that it implements. cGs0|f||_||_tj|g|||RdSr)_Provides__args_clsr)r!rS)r rRr-rrrr!3s zProvides.__init__rcCsd}|jtur|jrd}|jdd}n d}|jf|j}||}|r:|j}t|dkr3d|d}d|f|}d||fS) NdirectlyProvidesTrFzsys.modules[%r]rz%s, z%s(%s))rr_v_module_namesrrqrclen)r function_nameproviding_on_moduler-r^ mod_namesrrrr;s$    zProvides.__repr__cCs t|jfSr)rrr1rrrrn[r6zProvides.__reduce__zzope.interfacecCs|dur ||jur |Std)zHMake sure that a class __provides__ doesn't leak to an instance Nr)rrr rrRrrr__get__bszProvides.__get__N) r&r'r(rdr!rrrnrrrrrr-s  rcGs&t|}|durt|}|t|<|S)zCache instance declarations Instance declarations are shared among instances that have the same declaration. The declarations are cached in a weak value dictionary. N)InstanceDeclarationsr} ProvidesClass)r-rrrrrss TcGst|dd}|durt|dd|urt|tstdt|}|dur't|}d}tD] }t||r6d}nq+|rEt||g|R|_dSt |g|R}|_t|t ret |drg|j |j f7_ dSdSdS)aYDeclare interfaces declared directly for an object The arguments after the object are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The interfaces given (including the interfaces in the specifications) replace interfaces previously declared for the object. rNzHAttempt to make an interface declaration on a non-descriptor-aware classFTr&)rrVrrsr*rX issubclassrrrrrrr&)rjr-rRissubdamcprovidesrrrrs*  rcGst|t|g|RdS)aZDeclare interfaces declared directly for an object The arguments after the object are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The interfaces given (including the interfaces in the specifications) are added to the interfaces previously declared for the object. N)rdirectlyProvidedBy)rjr-rrr alsoProvidess rcCs(t|t||||rtddS)z; Removes a directly provided interface from an object. z-Can only remove directly provided interfaces.N)rr providedByr)rjr.rrrnoLongerProvidess rc@eZdZdZddZdS)ClassProvidesBase)rrcCs$||jur|dur |S|jStdNr)rrrrrrrrs zClassProvidesBase.__get__N)r&r'r(rerrrrrrs rc@s2eZdZdZdZddZddZddZej Z d S) rzSpecial descriptor for class ``__provides__`` The descriptor caches the implementedBy info, so that we can get declarations for objects without instance-specific interfaces a bit quicker. )__argscGs<||_t||_||f||_tj|g|||RdSr)rrPr_ClassProvides__argsr)r!rS)r rRmetaclsr-rrrr!s zClassProvides.__init__cCs*|jf|jdd}||}d|fS)NrzdirectlyProvides(%s))rrrc)r r-r^rrrrs  zClassProvides.__repr__cCs |j|jfSr)rrr1rrrrnrzClassProvides.__reduce__N) r&r'r(rdrer!rrnrrrrrrrs rcCs4t|dd}|dust|trtSt|jddS)zReturn the interfaces directly provided by the given object The value returned is an `~zope.interface.interfaces.IDeclaration`. rN)rrVrZrmr)rq)rjrrrrrs rcGsftrttdtd}|j}||jusd|vrtdd|vr%tdt||d<tt ddd S) aDeclare interfaces provided directly by a class This function is called in a class definition. The arguments are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The given interfaces (including the interfaces in the specifications) are used to create the class's direct-object interface specification. An error will be raised if the module class has an direct interface specification. In other words, it is an error to call this function more than once in a class definition. Note that the given interfaces have nothing to do with the interfaces implemented by instances of the class. This function is provided for convenience. It provides a more convenient way to call `directlyProvides` for a class. For example:: classProvides(I1) is equivalent to calling:: directlyProvides(theclass, I1) after the class has been created. providerrr'z7classProvides can be used only from a class definition.rz:classProvides can only be used once in a class definition.rrN) r rsrrrrrr*r_classProvides_advice)r-rrrrr classProvidess   rcCs"|jd}|`t|g|R|Sr)rrr)rRr-rrrrDs rc@r)rz(Class decorator version of classProvidescGrrr0rrrrr!Pr"zprovider.__init__cCst|g|jR|Sr)rr-r$rrrr%Sszprovider.__call__NrrrrrrMs rcGsntd}|j}||jusd|vrtdd|vrtdttgt|R}|d<|j|df7_dS)aDeclare interfaces provided by a module This function is used in a module definition. The arguments are one or more interfaces or interface specifications (`~zope.interface.interfaces.IDeclaration` objects). The given interfaces (including the interfaces in the specifications) are used to create the module's direct-object interface specification. An error will be raised if the module already has an interface specification. In other words, it is an error to call this function more than once in a module definition. This function is provided for convenience. It provides a more convenient way to call directlyProvides. For example:: moduleProvides(I1) is equivalent to:: directlyProvides(sys.modules[__name__], I1) rr&z9moduleProvides can only be used from a module definition.rzs               w SG / Z, )5'% @ ( (2  <  <