Current File : //lib64/python3.6/site-packages/zope/interface/__pycache__/declarations.cpython-36.pyc
3
��[e{�@s�dZdZddlZddlmZddlmZddlmZddlZddlm Z ddl
mZdd l
mZdd
l
m
Z
ddlmZddlmZiZd
ZdZGdd�de�ZGdd�de
�ZGdd�de�Zdd�Zdd�ZeZdd�Zdd�Zdd�ZGdd �d e�ZGd!d"�d"e�Z d#d$�Z!d%d&�Z"d'd(�Z#Gd)d*�d*e�Z$e$Z%ej&�Z'd+d*�Z$d,e$_(d-d.�Z)d/d0�Z*d1d2�Z+Gd3d4�d4e�Z,e,Z-e,Z.yddl/Z0Wne1k
�r�YnXdd5l/m.Z.Gd6d7�d7ee.�Z2d8d9�Z3d:d;�Z4d<d=�Z5Gd>d?�d?e�Z6d@dA�Z7dBdC�Z8dDdE�Z9e9Z:dFdG�Z;e;Z<GdHdI�dIe�Z=e=Z>dPdJdK�Z?e�Z@yddl/Z0Wne1k
�rVYn2XddLl/mZddMl/m<Z<ddNl/m:Z:ddOl/m>Z>e>�ZAdS)Qa�Implementation 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.
Zrestructuredtext�N)�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@seZdZdd�Zdd�ZdS)�namedcCs
||_dS)N)�name)�selfr�r�$/usr/lib64/python3.6/declarations.py�__init__5sznamed.__init__cCs|j|_|S)N)rZ__component_name__)r
�obrrr�__call__8sznamed.__call__N)�__name__�
__module__�__qualname__rrrrrrr3src@sLeZdZdZdd�Zdd�Zdd�Zdd �Zd
d�Zdd
�Z dd�Z
e
ZdS)�DeclarationzInterface declarationscGstj|t|��dS)N)rr�_normalizeargs)r
�
interfacesrrrr?szDeclaration.__init__cCs.tj||�y|`Wntk
r(YnXdS)N)r�changedZ_v_attrs�AttributeError)r
Zoriginally_changedrrrrBs
zDeclaration.changedcCs|j|�o||j�kS)z:Test whether an interface is in the specification
)�extendsr)r
� interfacerrr�__contains__IszDeclaration.__contains__cCs|j�S)zCReturn an iterator for the interfaces in the specification
)r)r
rrr�__iter__OszDeclaration.__iter__cCs
t|j�S)zCReturn an iterator of all included and extended interfaces
)�iterZ__iro__)r
rrr� flattenedTszDeclaration.flattenedcst�fdd�|j�D��S)z/Remove interfaces from a specification
cs&g|]��fdd��j�D�s��qS)csg|]}�j|d�r|�qS)r)r)�.0�j)�irr�
<listcomp>^sz2Declaration.__sub__.<locals>.<listcomp>.<listcomp>)r)r!)�other)r#rr$]sz'Declaration.__sub__.<locals>.<listcomp>)rr)r
r%r)r%r�__sub__YszDeclaration.__sub__cCs`i}g}x"|j�D]}d||<|j|�qWx*|j�D]}||kr6d||<|j|�q6Wt|�S)zCAdd two specifications or a specification and an interface
�)r�appendr)r
r%�seen�resultr#rrr�__add__cszDeclaration.__add__N)rrr�__doc__rrrrr r&r+�__radd__rrrrr<s
rc@sdeZdZdZfZdZedd��Zdd�Zdd�Zd d
�Z dd�Z
d
d�Zdd�Zdd�Z
dd�ZdS)�
ImplementsN�?cGs|j|�}||_|j|�|S)N)�__new__rr)�clsrr�instrrrr�s
zImplements.namedcCs
d|jS)Nz<implementedBy %s>)r)r
rrr�__repr__�szImplements.__repr__cCst|jffS)N)�
implementedBy�inherit)r
rrr�
__reduce__�szImplements.__reduce__cCs@|dkrdS|j|jf}t|dd�t|dd�f}||k||kS)Nr'r�r���)rr�getattr)r
r%Zn1Zn2rrrZ__cmp�s
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__)rrrr5�declared�classmethodrr3r6r;r:r=r>r?r@rrrrr.{s
r.cCs$t|dd�pddt|dd�p dS)Nrr/�.r)r9)rrrr�_implements_name�s
rDcCs�y|jjd�}WnZtk
rjt|dd�}|dkrNtj|�}|dk rJ|StS|jtkr\|Stt |f��SXt
|t�rz|S|dkr�tj|�}|dk r�|St|�}|dk r�|f}tj|ft |���}d|_
|`nZy
|j}Wn.tk
�rt|��s�td|��f}YnXtj|fdd�|D���}||_
yL||_t|d��sBt|_t
|t��rrd|jk�rrt|t|dt|���|_Wn4tk
�r�t
|t��s�td |��|t|<YnX|S)
z�Return 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.<locals>.<listcomp>�__providedBy__�__provides__� __class__z!ImplementedBy called for non-type)�__dict__�getrr9�#BuiltinImplementationSpecifications�_emptyrHr.rr�
isinstancerDrr5rE� __bases__�callable� TypeError�hasattr�objectSpecificationDescriptorrF�DescriptorAwareMetaClasses�
ClassProvides�typerG)r1�spec� spec_name�basesrrr�implementedByFallback�s\
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)r4rAr5�classImplements)r1rrVrrr�classImplementsOnly-s r[cGs�t|�}|jtt|��7_g}i}x(|jD]}||kr.d||<|j|�q.W|jdk r�x2|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)r4rA�tuplerr(r5rN)r1rrVrXr)�br<rrrrZ;s
rZcCs$|jd\}}|`||f|��|S)N�__implements_advice_data__)rIr^)r1rrZrrr�_implements_adviceYsr_c@s eZdZdZdd�Zdd�ZdS)�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__cCsft|t�rt|f|j��|St|�}tj|f|j��}y
||_Wntk
r`t d|��YnX|S)NzCan't declare implements)
rMrSrZrrDr.rrErrP)r
rrWrVrrrr�s
zimplementer.__call__N)rrrr,rrrrrrr``sr`c@s eZdZdZdd�Zdd�ZdS)�implementer_onlya�Declare 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
rrrrr�szimplementer_only.__init__cCs0t|ttf�rtd��nt|f|j��|SdS)NzIThe implementer_only decorator is not supported for methods or functions.)rMrr�
ValueErrorr[r)r
rrrrr�s
zimplementer_only.__call__N)rrrr,rrrrrrra�sracCs^tjd�}|j}||jks"d|kr.t|d��d|krBt|d��||f|d<ttdd�dS)N�rz* can be used only from a class definition.r^z- can be used only once in a class definition.�)�depth)�sys� _getframe�f_locals� f_globalsrPrr_)rrrZ�frame�localsrrr�_implements�s
rlcGs trttd��td|t�dS)a�Declare 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)rrrrrm�srmcGs trttd��td|t�dS)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.
ra�implementsOnlyN)r
rPrnrlr[)rrrrro�sroc@s,eZdZdZdd�Zdd�ZdZdd�Zd S)
�Providesz�Implement ``__provides__``, the instance-specific specification
When an object is pickled, we pickle the interfaces that it implements.
cGs0|f||_||_tj|f|t|�f��dS)N)�_Provides__args�_clsrrr4)r
r1rrrrrszProvides.__init__cCs
t|jfS)N)rprq)r
rrrr6szProvides.__reduce__zzope.interfacecCs"|dkr||jkr|Std��dS)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)z�Cache 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
TcGs�t|dd�}|dk r6t|dd�|kr6t|t�s6td��t|�}|dkrNt|�}d}xtD]}t||�rXd}PqXW|r�t||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)�objectrr1ZissubZdamcrrr�directlyProvides7s
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)rx�directlyProvidedBy)rwrrrr�alsoProvides[s rzcCs(t|t|�|�|j|�r$td��dS)z; Removes a directly provided interface from an object.
z-Can only remove directly provided interfaces.N)rxry�
providedByrb)rwrrrr�noLongerProvidesfs
r|c@seZdZdd�ZdS)�ClassProvidesBaseFallbackcCs(||jkr|dkr|S|jStd��dS)NrG)rrrlr)r
r2r1rrrrsos
z!ClassProvidesBaseFallback.__get__N)rrrrsrrrrr}msr})�ClassProvidesBasec@s&eZdZdZdd�Zdd�ZejZdS)rTz�Special 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|�f��dS)N)rrr4rl�_ClassProvides__argsrr)r
r1�metaclsrrrrr�s
zClassProvides.__init__cCs|j|jfS)N)rHr)r
rrrr6�szClassProvides.__reduce__N)rrrr,rr6r~rsrrrrrT�srTcCs4t|dd�}|dkst|t�r"tSt|jdd��S)z�Return the interfaces directly provided by the given object
The value returned is an `~zope.interface.interfaces.IDeclaration`.
rGNr'r8)r9rMr.rLrrN)rw�providesrrrry�s
rycGsftrttd��tjd�}|j}||jks2d|kr:td��d|krJtd��t|�|d<tt dd�d S)
a�Declare 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�
classProvides�s
r�cCs |jd}|`t|f|��|S)NrG)rIrGrx)r1rrrrr��s
r�c@s eZdZdZdd�Zdd�ZdS)r�z(Class decorator version of classProvidescGs
||_dS)N)r)r
rrrrr�szprovider.__init__cCst|f|j��|S)N)rxr)r
rrrrr�szprovider.__call__N)rrrr,rrrrrrr��sr�cGsTtjd�}|j}||jk s"d|kr*td��d|kr:td��ttft|���|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.rGz<moduleProvides can only be used once in a module definition.N)rfrgrhrirPrprr)rrjrkrrr�moduleProvides�s
r�cCs
t||�S)zfProvide object specifications
These combine information for the object and for it's classes.
)rp)Zdirectr1rrr�ObjectSpecificationsr�cCsJt|dd�}|dk r"t|t�r"|Sy
|j}Wntk
r@tSXt|�S)NrG)r9rMrrHrrLr4)rr�r1rrr�getObjectSpecificationFallback$s
r�c!Cs�y
|j}Wntk
r"t|�SXy
|jWnntk
r�y
|j}Wntk
rbt|j�SXy|jj}Wntk
r�|SX||kr�t|j�SYnX|S)N)rFr�getObjectSpecificationrrGr4rH)r�rZcprrr�providedByFallback5s$
r�c@seZdZdZdd�ZdS)�%ObjectSpecificationDescriptorFallbackz�Implement the `__providedBy__` attribute
The `__providedBy__` attribute computes the interfaces peovided by
an object.
cCs0|dkrt|�St|dd�}|dk r(|St|�S)z2Get an object specification for an object
NrG)r�r9r4)r
r2r1r�rrrrsosz-ObjectSpecificationDescriptorFallback.__get__N)rrrr,rsrrrrr�hsr�cCsN|dkrg}|j}t|jks&t|jkr2|j|�nx|D]}t||�q8W|S)z�Normalize declaration arguments
Normalization arguments might contain Declarions, tuples, or single
interfaces.
Anything but individial interfaces or implements specs will be expanded.
N)rHr�__mro__r.r(r)Zsequence�outputr1�vrrrrs
r)r4)r{)r�)�ObjectSpecificationDescriptor)N)Br,Z
__docformat__rf�typesrrr�weakrefZzope.interface.advicerZzope.interface.interfacerrrZzope.interface._compatr rSr
rKrnZ_ADVICE_WARNINGrwrrr.rDrYr4r[rZr_r`rarlrmrorpru�WeakValueDictionaryrtZ__safe_for_unpickling__rxrzr|r}ZClassProvidesBasePyr~Z-zope.interface._zope_interface_coptimizations�zope�ImportErrorrTryr�r�r�r�r�r�r�r�r{r�r�rrLrRrrrr�<module>s� ?OU-'
$2
,1