3 [e{@sdZdZddlZddlmZddlmZddlmZddlZddlm Z ddl m Z dd l m Z dd l m Z dd lmZdd lmZiZd ZdZGdddeZGddde ZGdddeZddZddZeZddZddZddZGdd d eZGd!d"d"eZ d#d$Z!d%d&Z"d'd(Z#Gd)d*d*eZ$e$Z%ej&Z'd+d*Z$d,e$_(d-d.Z)d/d0Z*d1d2Z+Gd3d4d4eZ,e,Z-e,Z.y ddl/Z0Wne1k rYnXdd5l/m.Z.Gd6d7d7ee.Z2d8d9Z3d:d;Z4dd?d?eZ6d@dAZ7dBdCZ8dDdEZ9e9Z:dFdGZ;e;ZdPdJdKZ?eZ@y ddl/Z0Wne1k rVYn2XddLl/mZddMl/mZ>e>ZAdS)QaImplementation 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. ZrestructuredtextN) FunctionType) MethodType) ModuleType)addClassAdvisor)InterfaceClass)SpecificationBase) Specification) CLASS_TYPES)PYTHON3zIClass 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.c@seZdZddZddZdS)namedcCs ||_dS)N)name)selfr r$/usr/lib64/python3.6/declarations.py__init__5sznamed.__init__cCs |j|_|S)N)r Z__component_name__)r obrrr__call__8sznamed.__call__N)__name__ __module__ __qualname__rrrrrrr 3sr c@sLeZdZdZddZddZddZdd Zd d Zd d Z ddZ e Z dS) DeclarationzInterface declarationscGstj|t|dS)N)rr_normalizeargs)r interfacesrrrr?szDeclaration.__init__c Cs.tj||y|`Wntk r(YnXdS)N)rchangedZ_v_attrsAttributeError)r Zoriginally_changedrrrrBs  zDeclaration.changedcCs|j|o||jkS)z:Test whether an interface is in the specification )extendsr)r interfacerrr __contains__IszDeclaration.__contains__cCs|jS)zCReturn an iterator for the interfaces in the specification )r)r rrr__iter__OszDeclaration.__iter__cCs t|jS)zCReturn an iterator of all included and extended interfaces )iterZ__iro__)r rrr flattenedTszDeclaration.flattenedcstfdd|jDS)z/Remove interfaces from a specification cs&g|]fddjDsqS)csg|]}j|dr|qS)r)r).0j)irr ^sz2Declaration.__sub__...)r)r!)other)r#rr$]sz'Declaration.__sub__..)rr)r r%r)r%r__sub__YszDeclaration.__sub__cCs`i}g}x"|jD]}d||<|j|qWx*|jD]}||kr6d||<|j|q6Wt|S)zCAdd two specifications or a specification and an interface )rappendr)r r%seenresultr#rrr__add__cszDeclaration.__add__N) rrr__doc__rrrrr r&r+__radd__rrrrr<s rc@sdeZdZdZfZdZeddZddZddZd d Z d d Z d dZ ddZ ddZ ddZdS) ImplementsN?cGs|j|}||_|j||S)N)__new__rr)clsr rinstrrrr s  zImplements.namedcCs d|jS)Nz)r)r rrr__repr__szImplements.__repr__cCs t|jffS)N) implementedByinherit)r rrr __reduce__szImplements.__reduce__cCs@|dkr dS|j|jf}t|ddt|ddf}||k||kS)Nr'rr)rrgetattr)r r%Zn1Zn2rrrZ__cmps  zImplements.__cmpcCs tj|S)N)r__hash__)r rrrr:szImplements.__hash__cCs|j|}|dkS)Nr)_Implements__cmp)r r%crrr__lt__s zImplements.__lt__cCs|j|}|dkS)Nr)r;)r r%r<rrr__le__s zImplements.__le__cCs|j|}|dkS)Nr)r;)r r%r<rrr__gt__s zImplements.__gt__cCs|j|}|dkS)Nr)r;)r r%r<rrr__ge__s zImplements.__ge__)rrrr5declared classmethodr r3r6r;r:r=r>r?r@rrrrr.{s  r.cCs$t|ddpddt|ddp dS)Nrr/.r)r9)rrrr_implements_names rDcCsy|jjd}WnZtk rjt|dd}|dkrNtj|}|dk rJ|StS|jtkr\|Stt |fSXt |trz|S|dkrtj|}|dk r|St |}|dk r|f}tj |ft |}d|_ |`nZy |j}Wn.tk rt|std|f}YnXtj |fdd|D}||_ yL||_t|dsBt|_t |trrd|jkrrt|t|dt||_Wn4tk rt |tstd ||t|<YnX|S) zReturn the interfaces implemented for a class' instances The value returned is an `~zope.interface.interfaces.IDeclaration`. __implemented__Nz$ImplementedBy called for non-factorycSsg|] }t|qSr)r4)r!r<rrrr$sz)implementedByFallback..__providedBy__ __provides__ __class__z!ImplementedBy called for non-type)__dict__getrr9#BuiltinImplementationSpecifications_emptyrHr.rr isinstancerDr r5rE __bases__callable TypeErrorhasattrobjectSpecificationDescriptorrFDescriptorAwareMetaClasses ClassProvidestyperG)r1spec spec_namebasesrrrimplementedByFallbacks\              rYcGs&t|}f|_d|_t|f|dS)aQDeclare 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. N)r4rAr5classImplements)r1rrVrrrclassImplementsOnly-s r[cGst|}|jtt|7_g}i}x(|jD]}||kr.d||<|j|q.W|jdk rx2|jjD]&}t|}||krdd||<|j|qdWt||_dS)abDeclare 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. r'N)r4rAtuplerr(r5rN)r1rrVrXr)br<rrrrZ;s   rZcCs$|jd\}}|`||f||S)N__implements_advice_data__)rIr^)r1rrZrrr_implements_adviceYsr_c@s eZdZdZddZddZdS) 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. 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. cGs ||_dS)N)r)r rrrrr}szimplementer.__init__c Csft|trt|f|j|St|}tj|f|j}y ||_Wntk r`t d|YnX|S)NzCan't declare implements) rMrSrZrrDr.r rErrP)r rrWrVrrrrs  zimplementer.__call__N)rrrr,rrrrrrr``sr`c@s 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. cGs ||_dS)N)r)r rrrrrszimplementer_only.__init__cCs0t|ttfrtdnt|f|j|SdS)NzIThe implementer_only decorator is not supported for methods or functions.)rMrr ValueErrorr[r)r rrrrrs zimplementer_only.__call__N)rrrr,rrrrrrrasracCs^tjd}|j}||jks"d|kr.t|dd|krBt|d||f|d<ttdddS)Nrz* can be used only from a class definition.r^z- can be used only once in a class definition.)depth)sys _getframef_locals f_globalsrPrr_)r rrZframelocalsrrr _implementss    rlcGs trttdtd|tdS)aDeclare 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). 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 rP _ADVICE_ERRORrlrZ)rrrrrms rmcGs trttdtd|tdS)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. raimplementsOnlyN)r rPrnrlr[)rrrrros roc@s,eZdZdZddZddZdZddZd S) ProvideszImplement ``__provides__``, the instance-specific specification When an object is pickled, we pickle the interfaces that it implements. cGs0|f||_||_tj|f|t|fdS)N)_Provides__args_clsrrr4)r r1rrrrr s zProvides.__init__cCs t|jfS)N)rprq)r rrrr6szProvides.__reduce__zzope.interfacecCs"|dkr||jkr|StddS)zHMake sure that a class __provides__ doesn't leak to an instance NrG)rrr)r r2r1rrr__get__szProvides.__get__N)rrrr,rr6rsrrrrrps rpcGs&tj|}|dkr"t|}|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)InstanceDeclarationsrJ ProvidesClass)rrVrrrrp's  TcGst|dd}|dk r6t|dd|kr6t|ts6tdt|}|dkrNt|}d}xtD]}t||rXd}PqXW|rt||f||_nt |f||_dS)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. rHNzHAttempt to make an interface declaration on a non-descriptor-aware classFT) r9rMrSrPrrU issubclassrTrGrp)objectrr1ZissubZdamcrrrdirectlyProvides7s   rxcGst|t|f|dS)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)rxdirectlyProvidedBy)rwrrrr alsoProvides[s rzcCs(t|t|||j|r$tddS)z; Removes a directly provided interface from an object. z-Can only remove directly provided interfaces.N)rxry providedByrb)rwrrrrnoLongerProvidesfs r|c@seZdZddZdS)ClassProvidesBaseFallbackcCs(||jkr|dkr|S|jStddS)NrG)rrrlr)r r2r1rrrrsos  z!ClassProvidesBaseFallback.__get__N)rrrrsrrrrr}msr})ClassProvidesBasec@s&eZdZdZddZddZejZdS)rTzSpecial 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. cGs<||_t||_||f||_tj|f|t|fdS)N)rrr4rl_ClassProvides__argsrr)r r1metaclsrrrrrs zClassProvides.__init__cCs |j|jfS)N)rHr)r rrrr6szClassProvides.__reduce__N)rrrr,rr6r~rsrrrrrTsrTcCs4t|dd}|dkst|tr"tSt|jddS)zReturn the interfaces directly provided by the given object The value returned is an `~zope.interface.interfaces.IDeclaration`. rGNr'r8)r9rMr.rLrrN)rwprovidesrrrrys   rycGsftrttdtjd}|j}||jks2d|kr:tdd|krJtdt||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. providerr'rz7classProvides can be used only from a class definition.rGz:classProvides can only be used once in a class definition.rc)reN) r rPrnrfrgrhrirr_classProvides_advice)rrjrkrrr classProvidess   rcCs |jd}|`t|f||S)NrG)rIrGrx)r1rrrrrs rc@s eZdZdZddZddZdS)rz(Class decorator version of classProvidescGs ||_dS)N)r)r rrrrrszprovider.__init__cCst|f|j|S)N)rxr)r rrrrrszprovider.__call__N)rrrr,rrrrrrrsrcGsTtjd}|j}||jk s"d|kr*tdd|kr:tdttft||d<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:: moduleImplements(I1) is equivalent to:: directlyProvides(sys.modules[__name__], I1) r'rz9moduleProvides can only be used from a module definition.rGzs          ?O U-'   $   2 ,1