etdZdZddlZddlmZddlmZddlmZddlZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZgZiZd ZGddZGdde ZGddeZGdde eZdZdZedZdZdZdZd7dZdZGddZGd d!Z Gd"d#eZ!e!Z"ejFZ$d$Z!d%e!_%d&Z&d'Z'd(Z(eGd)d*e Z)Gd+d,ee)Z*d-Z+Gd.d/Z,d0Z-d1Z.ed2Z/ed3Z0eGd4d5Z1d8d6Z2eZ3e1Z4y)9aImplementation 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) Interface)InterfaceClass)SpecificationBase) Specification)NameAndModuleComparisonMixin) _use_c_implc||j}|j}|j}||j|dz}|S)N)__self_class__ __thisclass____mro__index)ob self_classclass_that_invoked_super complete_mro next_classs =/usr/lib/python3/dist-packages/zope/interface/declarations.py_next_super_classr5sG""J!//%%Ll001IJQNOJ ceZdZdZdZy)namedc||_yN)name)selfrs r__init__znamed.__init__As  rc(|j|_|Sr)r__component_name__r rs r__call__znamed.__call__Ds $  rN)__name__ __module__ __qualname__r!r%rrrr?s rrc\eZdZdZdZdZdZdZdZdZ dZ e Z e d Z e d Zy ) DeclarationzInterface declarationsr)cBtj|t|yr)r r!_normalizeargs)r basess rr!zDeclaration.__init__Nst^E%:;rcL|j|xr||jvS)z:Test whether an interface is in the specification )extends interfaces)r interfaces r __contains__zDeclaration.__contains__Qs$||I&I98I+IIrc"|jS)zCReturn an iterator for the interfaces in the specification r1r s r__iter__zDeclaration.__iter__Ws  rc,t|jS)zCReturn an iterator of all included and extended interfaces )iter__iro__r6s r flattenedzDeclaration.flattened\sDLL!!rc t|jDcgc]4}|jDcgc]}|j|dr|c}s|6c}}Scc}wcc}}w)z/Remove interfaces from a specification r)r+r1r0)r otherijs r__sub__zDeclaration.__sub__asf( ))+99Q?     sA A A A c*g}t|j}t|}|jD]O|vr|jt fd|Dr|j ?|j Qt ||zS)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. c3@K|]}j|ywr)r0).0xr>s r z&Declaration.__add__..s0A199Q<0s)listr1setaddanyappendr+)r r=beforeresultseenr>s @r__add__zDeclaration.__add__msdoo'(6{!!# !ADy HHQK000 a  a  !Vf_..rct|}t|Dcgc]}|j|s|c}}||fzScc}wr) implementedBytuple isOrExtends)r1clsimplemented_by_clsifaces r_add_interfaces_to_clsz"Declaration._add_interfaces_to_clssS +3/# %11%8   /111  s<cg}t}|D]}t}t|tr|j}t }nYt|t r|j}t}n6t|tr|js|j|vrzt|}||v}|j||r||}|j|dj|S)N, )rGrepr isinstancerr&strtype_implements_name ImplementsdeclaredinheritrHrJjoin)r1 ordered_namesnamesrUduplicate_transform this_name already_seens r_argument_names_for_reprz$Declaration._argument_names_for_reprs   ,E"& %0"NN &)#E4("NN &6#UJ/..mmz1 K $-L IIi /6   +7 ,8yy''rN)r&r'r(__doc__ __slots__r!r3r7r;r@rN__radd__ staticmethodrVrgr)rrr+r+IsV I<J ! "  /<H 2 2"("(rr+ceZdZdZdZdZdZedZejdZedZ dxZ xZ Z d Zdd Zdd Zdd Zed ZejdZy)_ImmutableDeclarationr)Nc~tjtj|t_tjSr)rm_ImmutableDeclaration__instanceobject__new__)rSs rrqz_ImmutableDeclaration.__new__s, + + 3/5~~c/B ! ,$///rcy)N_emptyr)r6s r __reduce__z _ImmutableDeclaration.__reduce__srcyNr)r)r6s r __bases__z_ImmutableDeclaration.__bases__src$|dk7r tdy)Nr)z7Cannot set non-empty bases on shared empty Declaration.) TypeError)r new_basess rrwz_ImmutableDeclaration.__bases__s ?UV V rciSrr)r6s r dependentsz _ImmutableDeclaration.dependentss rcyrr))r _ignoreds rz_ImmutableDeclaration.srctdSrv)r9r6s rr1z _ImmutableDeclaration.interfacess Bxrc||juSr)_ROOT)r r2stricts rr0z_ImmutableDeclaration.extendssDJJ&&rc|Srr))r rdefaults rgetz_ImmutableDeclaration.getsrctSr)rm)r callbacks rweakrefz_ImmutableDeclaration.weakrefs %$rciSrr)r6s r_v_attrsz_ImmutableDeclaration._v_attrss  rcyrr))r new_attrss rrz_ImmutableDeclaration._v_attrss r)Tr)r&r'r(rirorqrtpropertyrwsetterr|changed subscribe unsubscriber1r0rrrr)rrrmrmsIJ0 WW)DCGCi+'%__  rrmcJeZdZdZdZdZdZedZfdZdZ dZ xZ S)r^Nr)?cT|j|}||_|j||Sr)rqr&r!)rSrr.insts rrzImplements.named9s,{{3  u rcF |`t| |S#t$rYwxYwr) _super_cacheAttributeErrorsuperr)r originally_changed __class__s rrzImplements.changedEs4 !w122   s   c|jr/t|jddxst|j}n |j}|j |j }|rd|z}dj ||S)Nr&rXzclassImplements({}{}))r`getattrr]r&rgr_format)r rdeclared_namess r__repr__zImplements.__repr__Lsg <<4<<T:\>Nt||>\D==D66t}}E !N2N&--dNCCrc(t|jffSr)rPr`r6s rrtzImplements.__reduce__Vst||...r) r&r'r(r`r_r classmethodrrrrt __classcell__)rs@rr^r^s?2GH LH  3D/rr^cNt|ddxsddzt|ddxsdzS)Nr'r.r&)r)rs rr]r]Zs8 B c * 1c  r:s+2s 44rc2t|j}|j}|tjx}|_|j } ||S#t $rYnwxYwt|}t|}|jj}|j|}||d}|D cgc] } t| ncc} w} } tj|jdz|jzg| } |j| _ |j| _| ||<| S)N:)rPrrrWeakKeyDictionaryrKeyErrorrrrr^rr&r`r_) supimplemented_by_selfcachekeynext_clsimplemented_by_nextmro ix_next_clsclasses_to_keepcrznews r_implementedBy_superrgs%((:(:;  , ,E }3:3L3L3NN#0  C Sz    !%H (1   $ $C))H%K+,'O+:;aq!;;I;   $$s*-@-I-II   C&--CK&//CLE#J Js A AA&B9c   t|tr t|S|jj d}t|tr|S|tj |}||St|}|,|f}tj|gt|}d|_|`nA |j"}tj|g|Dcgc] }t)|c}}||_ ||_t+|ds t,|_t|t0r3d|jvr%t3|t |dt1||_|S#t $r^t |dd}|#tj |}||cYStcYS|jtk(r|cYStt|fcYSwxYw#t $rt%|s t'd|d}Y'wxYwcc}w#t&$r)t|t0s t'd||t|<Y|SwxYw) zReturn the interfaces implemented for a class' instances The value returned is an `~zope.interface.interfaces.IDeclaration`. __implemented__Nz$ImplementedBy called for non-factoryr)__providedBy__ __provides__rz!ImplementedBy called for non-type)rZrr__dict__rrr#BuiltinImplementationSpecificationsrsrr^r+r-r]rr`rrwcallableryrPhasattrobjectSpecificationDescriptorrr\ ClassProvidesr)rSspec spec_namer.rs rrPrPs #6 c5 !(, ,|| 12>$ #  |266s;  K!%I x AN4,@A   MME  Nu,M!]1-=,MN 8"s,-!>C  c4 ^3<<%G,[$s)4 C  KU 6s-t4 <6::3?D M >>Z 'K ND845596b C= FLLE  -N 8#t$?E E37+C0 K 8sSEE F-G A!G1F*6F*>F*F*)F*-"GG.H  H c^t|}d|_d|_d|_t ||dy)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. r)N)rPr_r`rw_classImplements_ordered)rSr1rs rclassImplementsOnlyrs1  D DMDLDNT:r2rc"t|}tt|}g}g}|D]H}|jD]&}|j |s|j |7|j |Jt |t|t|y)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_r0rJr)rSr1rrKafterrUbs rclassImplementsrs2  D~j12J F E   A}}Q e$ LL   T5=%,?rc6t|}t||fdy)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 r)N)rPr)rSrUrs rclassImplementsFirstr3s  DTE8R0rcz|Dcgc])}|j|r|tur|js|+}}|Dcgc])}|j|r|tur|js|+}}g}t}||j|fD]0}|D])}||vs|j ||j |+2t ||_|}|jM|jjD]4} t| }||vs|j ||j |6t ||_ycc}wcc}wr) rRrr_rGrJrHrQr`rwrP) rrKrrD new_declaredrMlrr.rs rrrAsB "qI~dmm F  "qI~dmm  E L 5D T]]E ) A}##A&   ,'DM E ||'' Aa A}  Q  5\DNA  s .D3.D8c@|jd\}}|`||g||S)N__implements_advice_data__)rr)rSr1do_classImplementss r_implements_advicerjs/%(\\2N%O"J" &s(Z( Jrc eZdZdZdZdZdZy) 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. r5c||_yrr5r r1s rr!zimplementer.__init__ $rct|trt|g|j|St |}t j |g|j} ||_|S#t$r td|wxYw)NzCan't declare implements) rZr\rr1r]r^rrrry)r rrrs rr%zimplementer.__call__sx b$  B 1 1I$R(  eZdZdZdZdZdZdZejZ y)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. )__argsc||_t||_||f|z|_t j |g|j ||yr)rrPr_ClassProvides__argsr+r!rV)r rSmetaclsr1s rr!zClassProvides.__init__sG (-G&3 TUD$?$? G$TUrc|jf|jddz}|j|}dj|S)NzdirectlyProvides({}))rrrgr)r r1rbs rrzClassProvides.__repr__s?ii]T[[_4 55jA %,,];;rc2|j|jfSr)rrr6s rrtzClassProvides.__reduce__s~~t{{**rN) r&r'r(rhrir!rrtrrr)rrrrys+IV <+ ''Grrc|t|dd}|t|trtSt |j ddS)zReturn the interfaces directly provided by the given object The value returned is an `~zope.interface.interfaces.IDeclaration`. rN)rrZr^rsr+rw)rprs rrrsC v~t4H  (J/  x))#2. //rceZdZdZdZdZy)provideraDeclare 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:: @provider(I1) class C: pass is equivalent to calling:: directlyProvides(C, I1) after the class has been created. c||_yrr5rs rr!zprovider.__init__rrc2t|g|j|Sr)rr1r$s rr%zprovider.__call__s.doo. rNrr)rrr r s<%rr c tjd}|j}||jusd|vr t dd|vr t dt t gt|x}|d<|xj|dfz c_y)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.rzr,s# .362A. ')#t(-t(nJ KJ fD/-D/N 4,^ V Vr3.)@X 1&"R22h%%X>-{>-@ 3w224"$( ;D FJ -)- -.%(K!2%(P0&$$N1/x!  $ 9  9 x && &>(   = ?r