Adapters, interfaces, and protocol implementations are usually defined in
Python class
statements. To make it more convenient to make protocol
declarations for these classes, the protocols package supplies the
advise() function. This function can make declarations about a
class, simply by being called from the body of that class. It can also be
called from the body of a module, to make a declaration about the module.
**kw) |
When invoked in the top-level code of a module, this function only accepts
the moduleProvides
keyword argument. When invoked in the body of a
class definition, this function accepts any keyword arguments except
moduleProvides
. The complete list of keyword arguments follows.
Unless otherwise specified, protocols must support the IOpenProtocol
interface.
Note:
When used in a class body, this function works by temporarily replacing
the __metaclass__
of the class. If your class sets an explicit
__metaclass__
, it must do so before advise() is
called, or the protocol declarations will not occur!
Keyword arguments accepted by advise():
declareImplementation(containing class,protocols)
.
declareImplementation(containing
class,instancesDoNotProvide=protocols)
.
instancesProvide
argument (which must also be supplied).
Supplying this argument is equivalent to calling
declareAdapter(containing class, instancesProvide,
forTypes=types)
. (Note that this means the containing class must be an
object that provides IAdapterFactory; i.e., its constructor should
accept being called with a single argument: the object to be adapted.)
instancesProvide
argument (which must also be
supplied). Supplying this argument is equivalent to calling
declareAdapter(containing class, instancesProvide,
forProtocols=types)
. (Note that this means the containing class must be
an object that provides IAdapterFactory; i.e., its constructor should
accept being called with a single argument: the object to be adapted.)
asAdapterForTypes
or asAdapterForProtocols
, you can
also supply a factory method name, using this keyword. The method named must be
a class method, and it will be used in place of the class' normal
constructor. (Note that this means the named method must be able to be called
with a single argument: the object to be adapted.)
protocolExtends
works around this:
In the above example, IAnotherInterface
wants to extend both
ISomeInterface
and ISomeZopeInterface
, but cannot do so directly
because the interfaces are of incompatible types. protocolExtends
informs the newly created interface that it implies ISomeZopeInterface
,
even though it isn't derived from it.
Using this keyword argument is equivalent to calling
declareAdapter(NO_ADAPTER_NEEDED, protocols,
forProtocols=[containing class])
. Note that this means that the
containing class must be an object that supports IOpenProtocol, such
as an Interface subclass.
protocolExtends
, but in the
``opposite direction''. It allows you to declare (in effect) that some other
interface is actually a subclass of (extends, implies) this one. See the
examples in section 1.1.4 for illustration.
Using this keyword argument is equivalent to calling
declareAdapter(NO_ADAPTER_NEEDED, [containing class],
forProtocols=protocols)
.
Using this keyword is equivalent to using both the protocolExtends
and
protocolIsSubsetOf
keywords, with the supplied protocols.
adviseObject(containing class, protocols)
. Note that this
means that the containing class may need to support the IOpenProvider
interface. The protocols package supplies default adapters to
support IOpenProvider for both classic and new-style classes, as long
as they do not have custom __conform__ methods. See section
1.1.6, ``Protocol Declarations for Individual Objects''
for more details.
classProvides
declarations. Supplying this argument is equivalent to
calling adviseObject(containing class,
doesNotProvide=protocols)
, and the IOpenProvider requirements
mentioned above for classProvides
apply here as well.
adviseObject(containing module, protocols)
.