o ]Lbx@sjdZddlmZdZddlZddlmZddlmZddlmZddl Z dd l m Z dd l mZdd l mZdd l mZdd lmZddlmZiZdZdZGdddeZGdddeZGdddeZddZddZeZddZddZ dd Z!Gd!d"d"Z"Gd#d$d$Z#d%d&Z$d'd(Z%d)d*Z&Gd+d,d,eZ'e'Z(e )Z*d-d,Z'd.e'_+d/d0Z,d1d2Z-d3d4Z.Gd5d6d6eZ/e/Z0e/Z1zdd7l2m1Z1Wn e3yYnwGd8d9d9ee1Z4d:d;Z5dd?Z7Gd@dAdAZ8dBdCZ9dDdEZ:dFdGZ;e;ZGdJdKdKeZ?e?Z@dOdLdMZAeZBzddNl2mZ>Wn e3y/Ynwe@ZCdS)PaImplementation 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. )absolute_importrestructuredtextN) FunctionType) MethodType) ModuleType)advice)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)namedcC ||_dSN)name)selfrrR/usr/lib/python3/dist-packages/mercurial/thirdparty/zope/interface/declarations.py__init__7 znamed.__init__cCs |j|_|Sr)r__component_name__robrrr__call__:sznamed.__call__N)__name__ __module__ __qualname__rrrrrrr5s rc@sLeZdZdZddZddZddZdd Zd d Zd d Z ddZ e Z dS) DeclarationzInterface declarationscGst|t|dSr)r r_normalizeargsr interfacesrrrrAszDeclaration.__init__cCs,t||z|`WdStyYdSwr)r changed_v_attrsAttributeError)roriginally_changedrrrr"Ds   zDeclaration.changedcCs||o ||vS)z:Test whether an interface is in the specification )extendsr!)r interfacerrr __contains__KszDeclaration.__contains__cCs|S)zCReturn an iterator for the interfaces in the specification r!rrrr__iter__QszDeclaration.__iter__cCs t|jS)zCReturn an iterator of all included and extended interfaces )iter__iro__r*rrr flattenedVs zDeclaration.flattenedcstfdd|DS)z/Remove interfaces from a specification cs&g|]fddDsqS)csg|] }|dr|qS)r)r&).0jirr `s  z2Declaration.__sub__...r))r/otherr1rr3_s z'Declaration.__sub__..)rr!)rr5rr4r__sub__[szDeclaration.__sub__cCsXi}g}|D] }d||<||q|D]}||vr'd||<||qt|S)zCAdd two specifications or a specification and an interface r)r!appendr)rr5seenresultr2rrr__add__es    zDeclaration.__add__N) rrr__doc__rr"r(r+r.r6r:__radd__rrrrr>s rc@sdeZdZdZdZdZeddZddZdd Zd d Z d d Z ddZ ddZ ddZ ddZdS) ImplementsNr?cGs||}||_|j||Sr)__new__rr)clsrr!instrrrrs  zImplements.namedcCs d|jS)Nz)rr*rrr__repr__rzImplements.__repr__cCs t|jffSr) implementedByinheritr*rrr __reduce__ zImplements.__reduce__cCs@|durdS|j|jf}t|ddt|ddf}||k||kS)Nrr)rrgetattr)rr5n1n2rrr__cmps  zImplements.__cmpcCs t|Sr)r__hash__r*rrrrMrzImplements.__hash__cCs||}|dkSNr_Implements__cmprr5crrr__lt__ zImplements.__lt__cCs||}|dkSrNrOrQrrr__le__rTzImplements.__le__cCs||}|dkSrNrOrQrrr__gt__rTzImplements.__gt__cCs||}|dkSrNrOrQrrr__ge__rTzImplements.__ge__)rrrrDdeclared classmethodrrBrErPrMrSrUrVrWrrrrr=}s    r=cCs$t|ddpddt|ddpdS)Nrr>.r)rI)rrrr_implements_names  r[cCsz|jd}Wn4ty<t|dd}|dur*t|}|dur&|YStYS|jtkr3|YStt |fYSwt |trD|S|durSt|}|durS|St |}|duro|f}tj |gt |R}d|_ |`n+z|j}Wntyt|std|d}Ynwtj |gdd|DR}||_ z*||_t|dst|_t |trd|jvrt|t|d t||_W|SW|SW|Styt |tstd ||t|<Y|Sw) zkReturn the interfaces implemented for a class' instances The value returned is an IDeclaration. __implemented__Nz$ImplementedBy called for non-factoryrcSsg|]}t|qSr)rC)r/rRrrrr3sz)implementedByFallback..__providedBy__ __provides__ __class__z!ImplementedBy called for non-type)__dict__getr$rI#BuiltinImplementationSpecifications_emptyr_r=rr isinstancer[rrDr\ __bases__callable TypeErrorhasattrobjectSpecificationDescriptorr]DescriptorAwareMetaClasses ClassProvidestyper^)r@spec spec_namebasesrrrimplementedByFallbacksn                rpcGs(t|}d|_d|_t|g|RdS)a8Declare the only interfaces implemented by instances of a class The arguments after the class are one or more interfaces or interface specifications (``IDeclaration`` objects). The interfaces given (including the interfaces in the specifications) replace any previous declarations. rN)rCrXrDclassImplements)r@r!rmrrrclassImplementsOnly/s rrcGst|}|jtt|7_g}i}|jD]}||vr%d||<||q|jdurC|jjD]}t|}||vrBd||<||q/t||_dS)aIDeclare additional interfaces implemented for instances of a class The arguments after the class are one or more interfaces or interface specifications (``IDeclaration`` objects). The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared. rN)rCrXtuplerr7rDre)r@r!rmror8brRrrrrq=s"      rqcCs&|jd\}}|`||g|R|S)N__implements_advice_data__)r`ru)r@r!rqrrr_implements_advice[srvc@ eZdZdZddZddZdS) implementeraDeclare 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 (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. cGrrr)r rrrrrzimplementer.__init__cCsbt|trt|g|jR|St|}tj|g|jR}z||_W|Sty0t d|w)NzCan't declare implements) rdrjrqr!r[r=rr\r$rg)rrrnrmrrrrs  zimplementer.__call__Nrrrr;rrrrrrrxbs rxc@rw)implementer_onlyanDeclare 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 (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. cGrrr)r rrrrrzimplementer_only.__init__cCs,t|ttfr tdt|g|jR|S)NzIThe implementer_only decorator is not supported for methods or functions.)rdrr ValueErrorrrr!rrrrrszimplementer_only.__call__Nryrrrrrzs rzcCs`td}|j}||jusd|vrt|dd|vr!t|d||f|d<tjtdddS)Nrz* can be used only from a class definition.ruz- can be used only once in a class definition.depth)sys _getframef_locals f_globalsrg advicemodaddClassAdvisorrv)rr!rqframelocalsrrr _implementss    rcG 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 (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. rx implementsN)r rg _ADVICE_ERRORrrqr)rrrrs rcGr)aLDeclare 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 (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. rzimplementsOnlyN)r rgrrrrr)rrrrs rc@s,eZdZdZddZddZdZddZd S) ProvideszImplement __provides__, the instance-specific specification When an object is pickled, we pickle the interfaces that it implements. cGs2|f||_||_tj|g|t|fRdSr)_Provides__args_clsrrrC)rr@r!rrrrs  zProvides.__init__cCs t|jfSr)rrr*rrrrErzProvides.__reduce__zzope.interfacecCs|dur ||jur |Std)zHMake sure that a class __provides__ doesn't leak to an instance Nr^)rr$rrAr@rrr__get__szProvides.__get__N)rrrr;rrErrrrrrs  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)InstanceDeclarationsra ProvidesClass)r!rmrrrr)s TcGst|dd}|durt|dd|urt|tstdt|}|dur't|}d}tD] }t||r6d}nq+|rEt||g|R|_dSt |g|R|_dS)a@Declare interfaces declared directly for an object The arguments after the object are one or more interfaces or interface specifications (``IDeclaration`` objects). The interfaces given (including the interfaces in the specifications) replace interfaces previously declared for the object. r_NzHAttempt to make an interface declaration on a non-descriptor-aware classFT) rIrdrjrgrrl issubclassrkr^r)objectr!r@issubdamcrrrdirectlyProvides9s$  rcGst|t|g|RdS)aADeclare interfaces declared directly for an object The arguments after the object are one or more interfaces or interface specifications (``IDeclaration`` objects). The interfaces given (including the interfaces in the specifications) are added to the interfaces previously declared for the object. N)rdirectlyProvidedBy)rr!rrr alsoProvides]s rcCs(t|t||||rtddS)z; Removes a directly provided interface from an object. z-Can only remove directly provided interfaces.N)rr providedByr{)rr'rrrnoLongerProvideshs rc@seZdZddZdS)ClassProvidesBaseFallbackcCs$||jur|dur |S|jStdNr^)rrr$rrrrrqs z!ClassProvidesBaseFallback.__get__N)rrrrrrrrros r)ClassProvidesBasec@s&eZdZdZddZddZejZdS)rkzSpecial 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|g|t|fRdSr)rrCr_ClassProvides__argsrr)rr@metaclsr!rrrrs  zClassProvides.__init__cCs |j|jfSr)r_rr*rrrrErFzClassProvides.__reduce__N)rrrr;rrErrrrrrrks  rkcCs4t|dd}|dust|trtSt|jddS)zpReturn the interfaces directly provided by the given object The value returned is an ``IDeclaration``. r^NrG)rIrdr=rcrre)rprovidesrrrrs rcGshtrttdtd}|j}||jusd|vrtdd|vr%tdt||d<tj t 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 (``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. providerrrz7classProvides can be used only from a class definition.r^z:classProvides can only be used once in a class definition.r|r~N) r rgrrrrrrrr_classProvides_advicer!rrrrr classProvidess   rcCs"|jd}|`t|g|R|Sr)r`r^r)r@r!rrrrs rc@rw)rz(Class decorator version of classProvidescGrrr)r rrrrrzprovider.__init__cCst|g|jR|Sr)rr!rrrrrszprovider.__call__Nryrrrrrs rcGsVtd}|j}||jusd|vrtdd|vrtdttgt|R|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 (``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) rrz9moduleProvides can only be used from a module definition.r^zs           ?O U-'   $  2 ,1