Package zope :: Package interface :: Module declarations
[show private | hide private]
[frames | no frames]

Module zope.interface.declarations

Implementation of interface declarations

There are three flavors of declarations:

$Id: declarations.py,v 1.2 2005/06/24 16:52:37 nyergler Exp $


Classes
ClassProvides Special descriptor for class __provides__
ClassProvidesBase  
ClassProvidesBasePy  
Declaration Interface declarations
implementer  
Implements  
ObjectSpecificationDescriptor Implement the __providedBy__ attribute
ObjectSpecificationDescriptorPy Implement the __providedBy__ attribute

Function Summary
  _classProvides_advice(cls)
  _implements(name, interfaces, classImplements)
  _implements_advice(cls)
  _normalizeargs(sequence, output)
Normalize declaration arguments
  alsoProvides(object, *interfaces)
Declare interfaces declared directly for an object
  classImplements(cls, *interfaces)
Declare additional interfaces implemented for instances of a class
  classImplementsOnly(cls, *interfaces)
Declare the only interfaces implemented by instances of a class
  classProvides(*interfaces)
Declare interfaces provided directly by a class
  directlyProvidedBy(object)
Return the interfaces directly provided by the given object
  directlyProvides(object, *interfaces)
Declare interfaces declared directly for an object
  getObjectSpecification(ob)
  implementedBy(cls)
Return the interfaces implemented for a class' instances
  implementedByFallback(cls)
Return the interfaces implemented for a class' instances
  implements(*interfaces)
Declare interfaces implemented by instances of a class
  implementsOnly(*interfaces)
Declare the only interfaces implemented by instances of a class
  moduleProvides(*interfaces)
Declare interfaces provided by a module
  ObjectSpecification(direct, cls)
Provide object specifications
  providedBy(ob)
  Provides(*interfaces)
Cache instance declarations

Variable Summary
Declaration _empty = <zope.interface.declarations.Declaration object...
dict BuiltinImplementationSpecifications = {<type 'object'>: ...
tuple DescriptorAwareMetaClasses = (<type 'classobj'>, <type '...
WeakValueDictionary InstanceDeclarations = <WeakValueDictionary at -12099291...
ObjectSpecificationDescriptorPy objectSpecificationDescriptor = <zope.interface.declarat...

Function Details

_normalizeargs(sequence, output=None)

Normalize declaration arguments

Normalization arguments might contain Declarions, tuples, or single interfaces.

Anything but individial interfaces or implements specs will be expanded.

alsoProvides(object, *interfaces)

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) are added to the interfaces previously declared for the object.

Consider the following example:

>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> class A(object): implements(IA1, IA2)
...
>>> class B(object): implements(IB)
...

>>> class C(A, B):
...    implements(IC)

>>> ob = C()
>>> directlyProvides(ob, I1)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
0
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1

>>> alsoProvides(ob, I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
1
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1

The object, ob provides I1, I2, and whatever interfaces instances have been declared for instances of C. Notice that the alsoProvides just extends the provided interfaces.

classImplements(cls, *interfaces)

Declare 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.

Consider the following example:

for example:

>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I4(Interface): pass
...
>>> class I5(Interface): pass
...
>>> class A(object):
...   implements(I3)
>>> class B(object):
...   implements(I4)
>>> class C(A, B):
...   pass
>>> classImplements(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'I3', 'I4']
>>> classImplements(C, I5)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'I5', 'I3', 'I4']

Instances of C provide I1, I2, I5, and whatever interfaces instances of A and B provide.

classImplementsOnly(cls, *interfaces)

Declare 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.

Consider the following example:

>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I4(Interface): pass
...
>>> class A(object):
...   implements(I3)
>>> class B(object):
...   implements(I4)
>>> class C(A, B):
...   pass
>>> classImplementsOnly(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2']

Instances of C provide only I1, I2, and regardless of whatever interfaces instances of A and B implement.

classProvides(*interfaces)

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 (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 directlyProvidedByProvides for a class. For example:

classProvides(I1)

is equivalent to calling:

directlyProvides(theclass, I1)

after the class has been created.

For example:

>>> from zope.interface import Interface
>>> class IFoo(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> class C(object):
...   implements(IFoo)
...   classProvides(IFooFactory)
>>> [i.getName() for i in C.__providedBy__]
['IFooFactory']
>>> [i.getName() for i in C().__providedBy__]
['IFoo']

if equivalent to:

>>> from zope.interface import Interface
>>> class IFoo(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> class C(object):
...   implements(IFoo)
>>> directlyProvides(C, IFooFactory)
>>> [i.getName() for i in C.__providedBy__]
['IFooFactory']
>>> [i.getName() for i in C().__providedBy__]
['IFoo']

directlyProvidedBy(object)

Return the interfaces directly provided by the given object

The value returned is an IDeclaration.

directlyProvides(object, *interfaces)

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.

Consider the following example:

>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> class A(object): implements(IA1, IA2)
...
>>> class B(object): implements(IB)
...

>>> class C(A, B):
...    implements(IC)

>>> ob = C()
>>> directlyProvides(ob, I1, I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
1
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1

The object, ob provides I1, I2, and whatever interfaces instances have been declared for instances of C.

To remove directly provided interfaces, use directlyProvidedBy and subtract the unwanted interfaces. For example:

>>> directlyProvides(ob, directlyProvidedBy(ob)-I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
0

removes I2 from the interfaces directly provided by ob. The object, ob no longer directly provides I2, although it might still provide I2 if it's class implements I2.

To add directly provided interfaces, use directlyProvidedBy and include additional interfaces. For example:

>>> int(I2 in providedBy(ob))
0
>>> directlyProvides(ob, directlyProvidedBy(ob), I2)

adds I2 to the interfaces directly provided by ob:

>>> int(I2 in providedBy(ob))
1

implementedBy(cls)

Return the interfaces implemented for a class' instances

The value returned is an IDeclaration.

for example:

>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> class C1(object):
...   implements(I2)
>>> class C2(C1):
...   implements(I3)
>>> [i.getName() for i in implementedBy(C2)]
['I3', 'I2']

implementedByFallback(cls)

Return the interfaces implemented for a class' instances

The value returned is an IDeclaration.

for example:

>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> class C1(object):
...   implements(I2)
>>> class C2(C1):
...   implements(I3)
>>> [i.getName() for i in implementedBy(C2)]
['I3', 'I2']

implements(*interfaces)

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 (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.

Consider the following example:

>>> from zope.interface import Interface
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> class A(object): implements(IA1, IA2)
...
>>> class B(object): implements(IB)
...

>>> class C(A, B):
...    implements(IC)

>>> ob = C()
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1

Instances of C implement I1, I2, and whatever interfaces instances of A and B implement.

implementsOnly(*interfaces)

Declare 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.

Consider the following example:

>>> from zope.interface import Interface
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> class A(object): implements(IA1, IA2)
...
>>> class B(object): implements(IB)
...

>>> class C(A, B):
...    implementsOnly(IC)

>>> ob = C()
>>> int(IA1 in providedBy(ob))
0
>>> int(IA2 in providedBy(ob))
0
>>> int(IB in providedBy(ob))
0
>>> int(IC in providedBy(ob))
1

Instances of C implement IC, regardless of what instances of A and B implement.

moduleProvides(*interfaces)

Declare 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)

ObjectSpecification(direct, cls)

Provide object specifications

These combine information for the object and for it's classes.

For example:

>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I31(I3): pass
...
>>> class I4(Interface): pass
...
>>> class I5(Interface): pass
...
>>> class A(object): implements(I1)
...
>>> class B(object): __implemented__ = I2
...
>>> class C(A, B): implements(I31)
...
>>> c = C()
>>> directlyProvides(c, I4)
>>> [i.getName() for i in providedBy(c)]
['I4', 'I31', 'I1', 'I2']
>>> [i.getName() for i in providedBy(c).flattened()]
['I4', 'I31', 'I3', 'I1', 'I2', 'Interface']
>>> int(I1 in providedBy(c))
1
>>> int(I3 in providedBy(c))
0
>>> int(providedBy(c).extends(I3))
1
>>> int(providedBy(c).extends(I31))
1
>>> int(providedBy(c).extends(I5))
0
>>> class COnly(A, B): implementsOnly(I31)
...
>>> class D(COnly): implements(I5)
...
>>> c = D()
>>> directlyProvides(c, I4)
>>> [i.getName() for i in providedBy(c)]
['I4', 'I5', 'I31']
>>> [i.getName() for i in providedBy(c).flattened()]
['I4', 'I5', 'I31', 'I3', 'Interface']
>>> int(I1 in providedBy(c))
0
>>> int(I3 in providedBy(c))
0
>>> int(providedBy(c).extends(I3))
1
>>> int(providedBy(c).extends(I1))
0
>>> int(providedBy(c).extends(I31))
1
>>> int(providedBy(c).extends(I5))
1


nonzero:

>>> from zope.interface import Interface
>>> class I1(Interface):
...     pass
>>> class I2(Interface):
...     pass
>>> class C(object):
...     implements(I1)
>>> c = C()
>>> int(bool(providedBy(c)))
1
>>> directlyProvides(c, I2)
>>> int(bool(providedBy(c)))
1
>>> class C(object):
...     pass
>>> c = C()
>>> int(bool(providedBy(c)))
0
>>> directlyProvides(c, I2)
>>> int(bool(providedBy(c)))
1

Provides(*interfaces)

Cache instance declarations

Instance declarations are shared among instances that have the same declaration. The declarations are cached in a weak value dictionary.

(Note that, in the examples below, we are going to make

assertions about the size of the weakvalue dictionary. For the assertions to be meaningful, we need to force garbage collection to make sure garbage objects are, indeed, removed from the system. Depending on how Python is run, we may need to make multiple calls to be sure. We provide a collect function to help with this:

>>> import gc
>>> def collect():
...     for i in range(4):
...         gc.collect()

)

>>> collect()
>>> before = len(InstanceDeclarations)
>>> class C(object):
...    pass
>>> from zope.interface import Interface
>>> class I(Interface):
...    pass
>>> c1 = C()
>>> c2 = C()
>>> len(InstanceDeclarations) == before
1
>>> directlyProvides(c1, I)
>>> len(InstanceDeclarations) == before + 1
1
>>> directlyProvides(c2, I)
>>> len(InstanceDeclarations) == before + 1
1
>>> del c1
>>> collect()
>>> len(InstanceDeclarations) == before + 1
1
>>> del c2
>>> collect()
>>> len(InstanceDeclarations) == before
1

Variable Details

_empty

Type:
Declaration
Value:
<zope.interface.declarations.Declaration object at 0xb7e1f42c>         

BuiltinImplementationSpecifications

Type:
dict
Value:
{<type 'classobj'>: <implementedBy __builtin__.classobj>,
 <type 'int'>: <implementedBy __builtin__.int>,
 <type 'dict'>: <implementedBy __builtin__.dict>,
 <type 'NoneType'>: <implementedBy __builtin__.NoneType>,
 <type 'basestring'>: <implementedBy __builtin__.basestring>,
 <type 'str'>: <implementedBy __builtin__.str>,
 <type 'tuple'>: <implementedBy __builtin__.tuple>,
 <type 'type'>: <implementedBy __builtin__.type>,
...                                                                    

DescriptorAwareMetaClasses

Type:
tuple
Value:
(<type 'classobj'>, <type 'type'>)                                     

InstanceDeclarations

Type:
WeakValueDictionary
Value:
<WeakValueDictionary at -1209929108>                                   

objectSpecificationDescriptor

Type:
ObjectSpecificationDescriptorPy
Value:
<zope.interface.declarations.ObjectSpecificationDescriptorPy object at\
 0xb7e1f34c>                                                           

Generated by Epydoc 2.1 on Fri Jun 24 12:01:21 2005 http://epydoc.sf.net